diff --git a/lib/bcoin/protocol/constants.js b/lib/bcoin/protocol/constants.js index f4b7e031..825cec0e 100644 --- a/lib/bcoin/protocol/constants.js +++ b/lib/bcoin/protocol/constants.js @@ -150,3 +150,8 @@ exports.block = { }; exports.locktimeThreshold = 500000000; // Tue Nov 5 00:53:20 1985 UTC + +exports.oneHash = utils.toArray( + '0000000000000000000000000000000000000000000000000000000000000001', + 'hex' +); diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index ef40b2fa..bfae68a7 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -414,6 +414,11 @@ TX.prototype.subscriptHash = function subscriptHash(index, s, type) { if (typeof type === 'string') type = constants.hashType[type]; + // 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; + copy.inputs.forEach(function(input, i) { input.script = index === i ? s : []; }); @@ -427,6 +432,9 @@ TX.prototype.subscriptHash = function subscriptHash(index, s, type) { input.seq = 0; }); } else if ((type & 0x1f) === constants.hashType.single) { + // bitcoind sighash_single bug: + if (index >= copy.outputs.length) + return constants.oneHash; while (copy.outputs.length < index + 1) copy.outputs.push({}); while (copy.outputs.length > index + 1) @@ -434,8 +442,7 @@ TX.prototype.subscriptHash = function subscriptHash(index, s, type) { copy.outputs.forEach(function(output, i) { if (i !== index) { output.script = []; - // output.value = new bn('ffffffffffffffff', 'hex'); - output.value = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]; + output.value = utils.toArray('ffffffffffffffff', 'hex'); output.value.toArray = function() { return this; }; } });