Skip to content

Instantly share code, notes, and snippets.

@fjccoin
Created November 27, 2018 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fjccoin/18620bc7496efc0e8c969e9add2dcd76 to your computer and use it in GitHub Desktop.
Save fjccoin/18620bc7496efc0e8c969e9add2dcd76 to your computer and use it in GitHub Desktop.
block
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