Skip to content

Instantly share code, notes, and snippets.

@eordano
Created January 26, 2016 23:09
Show Gist options
  • Save eordano/f0d0e5e342904defb294 to your computer and use it in GitHub Desktop.
Save eordano/f0d0e5e342904defb294 to your computer and use it in GitHub Desktop.
const bitcore = require('bitcore')
const semiHash = hash => hash.substr(0, 4) + '...' + hash.substr(60, 4)
bitcore.util.semiHash = semiHash
const visualCoinbaseScript = function(coinbase, opts) {
return `\t${coinbase._scriptBuffer.toString('hex')}
\t${coinbase._scriptBuffer.toString()}`
}
bitcore.Transaction.Input.prototype.prevHash = function() {
return this.prevTxId.toString('hex')
}
bitcore.Transaction.prototype.visual = function(opts) {
if (this.isCoinbase()) {
return `Coinbase Transaction ${this.hash} (nLockTime: ${this.nLockTime}):
${visualCoinbaseScript(this.inputs[0])}
\t${this.outputs.length} outputs: \n${this.outputs.map(o => o.visual(opts))}
`
}
return `Transaction ${this.hash} (nLockTime: ${this.nLockTime}):
\t${this.inputs.length} inputs: \n${this.inputs.map(i => i.visual(opts)).join('')}\t${this.outputs.length} outputs: \n${this.outputs.map(o => o.visual(opts)).join('')}
`
}
bitcore.Script.prototype.looksLikePayToScriptHash = function() {
try {
if (!this.script || !this.script.chunks) {
return false
}
new bitcore.Script(this.script.chunks[this.script.chunks.length - 1].buf)
return true
} catch (e) {
if (e instanceof bitcore.errors.Script.InvalidBuffer) {
return false
} else {
throw e
}
}
}
bitcore.Transaction.classifyOutPoint = outpoint => {
const input = outpoint.script.isPublicKeyIn() ? 'P2PK'
: outpoint.script.isPublicKeyHashIn() ? 'P2PKH'
: outpoint.script.looksLikePayToScriptHash() ? 'P2SH'
: 'unknown'
const output = outpoint.script.isPublicKeyOut() ? 'P2PK'
: outpoint.script.isPublicKeyHashOut() ? 'P2PKH'
: outpoint.script.isScriptHashOut() ? 'P2SH'
: outpoint.script.isMultisigOut() ? 'Multisig Output'
: outpoint.script.isDataOut() ? 'Data Output'
: 'unknown'
if (input === 'unknown') {
return output
}
return input
}
bitcore.Transaction.Input.prototype.visual = function() {
this.script._isInput = true
const type = this.script.isPublicKeyIn() ? 'P2PK'
: this.script.isPublicKeyHashIn() ? 'P2PKH'
: this.script.looksLikePayToScriptHash() ? 'P2SH'
: 'unknown'
const prev = `\t\t${semiHash(this.prevHash())}:${this.outputIndex} (${type} ${this.script.toAddress()})`
var ret = prev
if (this.sequenceNumber !== (2 ** 32) - 1) {
ret += ` (sequenceNumber: ${this.sequenceNumber})\n`
} else {
ret += '\n'
}
if (this.outputIndex > 200) {
ret += `\t\tPrevious output: ${this.prevHash()}\n`
}
if (this.output) {
ret += `\t\t(${this.output.satoshis} with script ${this.output.script})`
}
return ret
}
bitcore.Transaction.Output.prototype.visual = function() {
const type = this.script.isPublicKeyOut() ? 'P2PK'
: this.script.isPublicKeyHashOut() ? 'P2PKH'
: this.script.isScriptHashOut() ? 'P2SH'
: this.script.isMultisigOut() ? 'Multisig Output'
: this.script.isDataOut() ? 'Data Output'
: 'unknown'
var extra = ''
if (type !== 'unknown') {
extra = `paying to ${this.script.toAddress()}`
}
return `\t\t${type} - ${this.satoshis} satoshis ${extra}\n`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment