From 018d534c473be1fae2597c25d2d64e31696ae6e7 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 13 Jan 2016 20:05:25 -0800 Subject: [PATCH] fix anyonecanpay. --- lib/bcoin/tx.js | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 43e3fb08..301a4bd9 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -588,6 +588,7 @@ TX.prototype.getSubscript = function getSubscript(index) { TX.prototype.signatureHash = function signatureHash(index, s, type) { var copy = this.clone(); + var inputIndex = index; var msg, hash; if (typeof index !== 'number') @@ -599,28 +600,34 @@ TX.prototype.signatureHash = function signatureHash(index, s, type) { assert(type != null); assert(index < copy.inputs.length) - // bitcoind used to return 1 as an error code: - // it ended up being treated like a hash. - // if (index >= copy.inputs.length) - // return constants.oneHash.slice(); + if (type & constants.hashType.anyonecanpay) { + copy.inputs = [copy.inputs[inputIndex]]; + inputIndex = 0; + } copy.inputs.forEach(function(input, i) { - input.script = i === index ? s : []; + input.script = i === inputIndex ? s : []; }); if ((type & 0x1f) === constants.hashType.all) { ; } else if ((type & 0x1f) === constants.hashType.none) { - copy.outputs = []; copy.inputs.forEach(function(input, i) { - if (i !== index) + if (i !== inputIndex) input.seq = 0; }); + copy.outputs = []; } else if ((type & 0x1f) === constants.hashType.single) { - // bitcoind sighash_single bug: + // Bitcoind used to return 1 as an error code: + // it ended up being treated like a hash. if (index >= copy.outputs.length) return constants.oneHash.slice(); + copy.inputs.forEach(function(input, i) { + if (i !== inputIndex) + input.seq = 0; + }); + while (copy.outputs.length < index + 1) copy.outputs.push({}); @@ -633,20 +640,10 @@ TX.prototype.signatureHash = function signatureHash(index, s, type) { output.value = new bn('ffffffffffffffff', 'hex'); } }); - - copy.inputs.forEach(function(input, i) { - if (i !== index) - input.seq = 0; - }); } else { assert(false); } - if (type & constants.hashType.anyonecanpay) { - copy.inputs.length = 1; - copy.inputs[0].script = s; - } - msg = copy.render(true); utils.writeU32(msg, type, msg.length);