diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 4511d6d0..1b4226a2 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -983,7 +983,7 @@ script.isPubkeyhashInput = function isPubkeyhashInput(s, key) { if (key) return utils.isEqual(s[1], key); - return true; + return s[1]; }; script.isMultisigInput = function isMultisigInput(s, pubs, tx, i) { @@ -1029,7 +1029,9 @@ script.isScripthashInput = function isScripthashInput(s, redeem) { if (redeem) return utils.isEqual(redeem, r); - return true; + var keys = script.decode(r).slice(1, -2); + + return keys; }; // https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 339251a1..964ff589 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -125,7 +125,7 @@ TX.prototype._inputIndex = function _inputIndex(hash, index) { return -1; }; -TX.prototype.signatureHash = function(i, type) { +TX.prototype.signatureHash = function signatureHash(i, type) { if (typeof i === 'object') i = this.inputs.indexOf(i); @@ -147,7 +147,7 @@ TX.prototype.signatureHash = function(i, type) { return hash; }; -TX.prototype.signature = function(i, key, type) { +TX.prototype.signature = function signature(i, key, type) { if (typeof i === 'object') i = this.inputs.indexOf(i); @@ -170,7 +170,7 @@ TX.prototype.signature = function(i, key, type) { }; // Build the scriptSigs for inputs, excluding the signatures -TX.prototype.scriptInput = function(input, pub) { +TX.prototype.scriptInput = function scriptInput(input, pub) { // Get the previous output's subscript var s = input.out.tx.getSubscript(input.out.index); @@ -230,7 +230,7 @@ TX.prototype.scriptInput = function(input, pub) { }; // Sign the now-built scriptSigs -TX.prototype.signInput = function(input, key, type) { +TX.prototype.signInput = function signInput(input, key, type) { if (!type) type = 'all'; @@ -329,7 +329,12 @@ TX.prototype.signInput = function(input, key, type) { }; // Build the scriptSig and sign it -TX.prototype.scriptSig = function(input, key, pub, type) { +TX.prototype.scriptSig = function scriptSig(input, key, pub, type) { + if (!Array.isArray(pub)) { + type = pub; + pub = key.getPublic(true, 'array'); + } + // Build script for input this.scriptInput(input, pub); @@ -365,7 +370,7 @@ TX.prototype.output = function output(options, value) { // compat TX.prototype.out = TX.prototype.output; -TX.prototype.scriptOutput = function(output, options) { +TX.prototype.scriptOutput = function scriptOutput(output, options) { options = options || output; var script = output.script ? output.script.slice() : []; @@ -805,7 +810,7 @@ TX.prototype.inputAddrs = function inputAddrs() { }); }; -TX.getInputKey = function(input) { +TX.getInputData = function getInputData(input) { if (!input || !input.script) return; var script = input.script; @@ -823,34 +828,38 @@ TX.getInputKey = function(input) { }; } - if (!input.out.tx) - return false; - - var output = input.out.tx.outputs[input.out.index]; - - if (bcoin.script.isScripthash(script)) { + if (bcoin.script.isScripthashInput(script)) { var pub = script[script.length - 1]; var hash = utils.ripesha(pub); var addr = bcoin.wallet.hash2addr(hash, 'scripthash'); var redeem = bcoin.script.decode(pub); - var keys = TX.getOutputKey({ script: redeem }); - keys.pub = pub; - keys.hash = hash; - keys.addr = addr; - keys.p2sh = { - sig: null, + var data = TX.getOutputData({ script: redeem }); + data.pub = pub; + data.hash = hash; + data.addr = addr; + data.scripthash = { redeem: redeem, pub: pub, hash: hash, - addr: addr + addr: addr, + m: data.multisig.m, + n: data.multisig.n, + keys: data.multisig.keys, + hashes: data.multisig.hashes, + addrs: data.multisig.addrs }; - return keys; + return data; } - return TX.getOutputKey(output); + if (!input.out.tx) + return; + + var output = input.out.tx.outputs[input.out.index]; + + return TX.getOutputData(output); }; -TX.getOutputKey = function(output) { +TX.getOutputData = function getOutputData(output) { if (!output || !output.script) return; var script = output.script; @@ -878,27 +887,32 @@ TX.getOutputKey = function(output) { }; } - var pubKeys = bcoin.script.isMultisig(script); - if (pubKeys) { - var keys = pubKeys.map(function(pubKey) { - var hash = utils.ripesha(pubKey); - var addr = bcoin.wallet.hash2addr(hash); - return { - pub: pubKey, - hash: hash, - addr: addr - }; - }); + var pubs = bcoin.script.isMultisig(script); + if (pubs) { + var hash = utils.ripesha(pubs[0]); + var addr = bcoin.wallet.hash2addr(hash); return { sig: null, - pub: keys[0].pub, - hash: keys[0].hash, - addr: keys[0].addr, - multisig: keys + pub: pubs[0], + hash: hash, + addr: addr, + keys: pubs, + multisig: { + m: new bn(script[0]).toNumber(), + n: new bn(script[script.length - 2]).toNumber(), + keys: keys, + hashes: keys.map(function(key) { + return utils.ripesha(key); + }), + addrs: keys.map(function(key) { + var hash = utils.ripesha(key); + return bcoin.wallet.hash2addr(hash); + }) + } }; } - if (bcoin.script.isScripthash(script, scriptHash)) { + if (bcoin.script.isScripthash(script)) { var hash = utils.toHex(s[1]); var addr = bcoin.wallet.hash2addr(hash, 'scripthash'); return { @@ -906,8 +920,7 @@ TX.getOutputKey = function(output) { pub: null, hash: hash, addr: addr, - p2sh: { - sig: null, + scripthash: { redeem: null, pub: null, hash: hash, @@ -915,6 +928,12 @@ TX.getOutputKey = function(output) { } }; } + + if (bcoin.script.isColored(script)) { + return { + data: bcoin.script.colored(script) + }; + } }; TX.prototype.getFee = function getFee() {