Browse Source

txb/tests: add solo SIGHASH_ALL fixes

v4
Daniel Cousens 7 years ago
parent
commit
a58c5b4f5b
  1. 8
      src/transaction_builder.js
  2. 22
      test/fixtures/transaction_builder.json
  3. 1
      test/transaction_builder.js

8
src/transaction_builder.js

@ -633,9 +633,9 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
// TODO: remove keyPair.network matching in 4.0.0 // TODO: remove keyPair.network matching in 4.0.0
if (keyPair.network && keyPair.network !== this.network) throw new TypeError('Inconsistent network') if (keyPair.network && keyPair.network !== this.network) throw new TypeError('Inconsistent network')
if (!this.__inputs[vin]) throw new Error('No input at index: ' + vin) if (!this.__inputs[vin]) throw new Error('No input at index: ' + vin)
if (this.__needsOutputs()) throw new Error('Transaction needs outputs')
hashType = hashType || Transaction.SIGHASH_ALL hashType = hashType || Transaction.SIGHASH_ALL
if (this.__needsOutputs(hashType)) throw new Error('Transaction needs outputs')
const input = this.__inputs[vin] const input = this.__inputs[vin]
@ -709,7 +709,11 @@ TransactionBuilder.prototype.__canModifyInputs = function () {
}) })
} }
TransactionBuilder.prototype.__needsOutputs = function () { TransactionBuilder.prototype.__needsOutputs = function (signingHashType) {
if (signingHashType === Transaction.SIGHASH_ALL) {
return this.__tx.outs.length === 0
}
// if inputs are being signed with SIGHASH_NONE, we don't strictly need outputs // if inputs are being signed with SIGHASH_NONE, we don't strictly need outputs
// .build() will fail, but .buildIncomplete() is OK // .build() will fail, but .buildIncomplete() is OK
return (this.__tx.outs.length === 0) && this.__inputs.some((input) => { return (this.__tx.outs.length === 0) && this.__inputs.some((input) => {

22
test/fixtures/transaction_builder.json

@ -2359,7 +2359,7 @@
] ]
}, },
{ {
"description": "Transaction w/ no outputs (but 1 SIGHASH_ALL)", "description": "Transaction w/ no outputs (but 1 SIGHASH_NONE)",
"exception": "Transaction needs outputs", "exception": "Transaction needs outputs",
"inputs": [ "inputs": [
{ {
@ -2367,7 +2367,8 @@
"vout": 0, "vout": 0,
"signs": [ "signs": [
{ {
"keyPair": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" "keyPair": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn",
"hashType": 2
} }
] ]
}, },
@ -2383,6 +2384,23 @@
} }
], ],
"outputs": [] "outputs": []
},
{
"description": "Transaction w/ no outputs",
"exception": "Transaction needs outputs",
"inputs": [
{
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"vout": 0,
"signs": [
{
"keyPair": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn",
"throws": true
}
]
}
],
"outputs": []
} }
], ],
"fromTransaction": [ "fromTransaction": [

1
test/transaction_builder.js

@ -231,6 +231,7 @@ describe('TransactionBuilder', function () {
it('throws if SIGHASH_ALL has been used to sign any existing scriptSigs', function () { it('throws if SIGHASH_ALL has been used to sign any existing scriptSigs', function () {
txb.addInput(txHash, 0) txb.addInput(txHash, 0)
txb.addOutput(scripts[0], 1000)
txb.sign(0, keyPair) txb.sign(0, keyPair)
assert.throws(function () { assert.throws(function () {

Loading…
Cancel
Save