Browse Source

txb: enforce outputs exist when signing

v4
Daniel Cousens 7 years ago
parent
commit
ed1c1a5661
  1. 10
      src/transaction_builder.js
  2. 17
      test/fixtures/transaction_builder.json
  3. 1
      test/transaction_builder.js

10
src/transaction_builder.js

@ -633,6 +633,8 @@ 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
const input = this.__inputs[vin] const input = this.__inputs[vin]
@ -694,8 +696,7 @@ function signatureHashType (buffer) {
TransactionBuilder.prototype.__canModifyInputs = function () { TransactionBuilder.prototype.__canModifyInputs = function () {
return this.__inputs.every(function (input) { return this.__inputs.every(function (input) {
// any signatures? if (!input.signatures) return true
if (input.signatures === undefined) return true
return input.signatures.every(function (signature) { return input.signatures.every(function (signature) {
if (!signature) return true if (!signature) return true
@ -708,6 +709,11 @@ TransactionBuilder.prototype.__canModifyInputs = function () {
}) })
} }
// TODO: handle incomplete SIGHASH_NONE flow
TransactionBuilder.prototype.__needsOutputs = function () {
return this.__tx.outs.length === 0
}
TransactionBuilder.prototype.__canModifyOutputs = function () { TransactionBuilder.prototype.__canModifyOutputs = function () {
const nInputs = this.__tx.ins.length const nInputs = this.__tx.ins.length
const nOutputs = this.__tx.outs.length const nOutputs = this.__tx.outs.length

17
test/fixtures/transaction_builder.json

@ -2357,6 +2357,23 @@
"value": 1000 "value": 1000
} }
] ]
},
{
"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

@ -313,6 +313,7 @@ describe('TransactionBuilder', function () {
it('throws if if there exist any scriptSigs', function () { it('throws if if there exist any scriptSigs', function () {
const txb = new TransactionBuilder() const txb = new TransactionBuilder()
txb.addInput(txHash, 0) txb.addInput(txHash, 0)
txb.addOutput(scripts[0], 100)
txb.sign(0, keyPair) txb.sign(0, keyPair)
assert.throws(function () { assert.throws(function () {

Loading…
Cancel
Save