Created
November 27, 2018 12:55
-
-
Save fjccoin/18620bc7496efc0e8c969e9add2dcd76 to your computer and use it in GitHub Desktop.
block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import _ from 'lodash' | |
import { Block } from 'bitcoinjs-lib' | |
export default Block | |
Block.prototype.vanilla = function(){ | |
return { | |
hash: this.getId() | |
, tx: this.transactions.length | |
, size: this.byteLength() | |
, ts: this.timestamp | |
} | |
} | |
Block.prototype.findOutputs = function(options){ | |
return this.find('outputs', options) | |
} | |
Block.prototype.findInputs = function(options){ | |
return this.find('inputs', options) | |
} | |
Block.prototype.find = function(type, options){ | |
const blockhash = this.getId() | |
const ts = this.timestamp | |
const proxy = { | |
outputs: 'findOutputs' | |
, inputs: 'findInputs' | |
} | |
let items = _.flatten(this.transactions.map((tx, i) => { | |
return tx[proxy[type]](options) | |
})) | |
// appen blockhash & ts | |
items = _.map(items, el => { | |
if(options.hydrate){ | |
el.blockhash = blockhash | |
el.ts = ts | |
} | |
return el | |
}) | |
return items | |
} | |
// return total sats of outputs | |
Block.prototype.getOutputsTotal = function() { | |
return this.transactions.reduce((acc, tx) => acc + tx.getOutputsTotal(), 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment