txb/tests: add solo SIGHASH_ALL fixes

This commit is contained in:
Daniel Cousens 2018-09-26 14:47:14 +10:00
parent 2223e6170e
commit a58c5b4f5b
3 changed files with 27 additions and 4 deletions

View file

@ -633,9 +633,9 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
// TODO: remove keyPair.network matching in 4.0.0
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.__needsOutputs()) throw new Error('Transaction needs outputs')
hashType = hashType || Transaction.SIGHASH_ALL
if (this.__needsOutputs(hashType)) throw new Error('Transaction needs outputs')
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
// .build() will fail, but .buildIncomplete() is OK
return (this.__tx.outs.length === 0) && this.__inputs.some((input) => {

View file

@ -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",
"inputs": [
{
@ -2367,7 +2367,8 @@
"vout": 0,
"signs": [
{
"keyPair": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"
"keyPair": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn",
"hashType": 2
}
]
},
@ -2383,6 +2384,23 @@
}
],
"outputs": []
},
{
"description": "Transaction w/ no outputs",
"exception": "Transaction needs outputs",
"inputs": [
{
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"vout": 0,
"signs": [
{
"keyPair": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn",
"throws": true
}
]
}
],
"outputs": []
}
],
"fromTransaction": [

View file

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