Browse Source

Fix TransactionBuilder.addOutput

hk-custom-address
Kirill Fomichev 9 years ago
parent
commit
5de6818fcb
  1. 9
      src/transaction_builder.js
  2. 28
      test/transaction_builder.js

9
src/transaction_builder.js

@ -227,10 +227,13 @@ TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOu
}
TransactionBuilder.prototype.addOutput = function (scriptPubKey, value) {
var valid = this.inputs.every(function (input) {
var tx = this.tx
var valid = this.inputs.every(function (input, index) {
if (input.hashType === undefined) return true
return (input.hashType & 0x1f) === Transaction.SIGHASH_SINGLE
var hashType = input.hashType & 0x1f
return hashType === Transaction.SIGHASH_NONE ||
(hashType === Transaction.SIGHASH_SINGLE && index < tx.outs.length)
})
if (!valid) throw new Error('No, this would invalidate signatures')
@ -240,7 +243,7 @@ TransactionBuilder.prototype.addOutput = function (scriptPubKey, value) {
scriptPubKey = baddress.toOutputScript(scriptPubKey, this.network)
}
return this.tx.addOutput(scriptPubKey, value)
return tx.addOutput(scriptPubKey, value)
}
TransactionBuilder.prototype.build = function () {

28
test/transaction_builder.js

@ -202,6 +202,34 @@ describe('TransactionBuilder', function () {
}, /2NGHjvjw83pcVFgMcA7QvSMh2c246rxLVz9 has no matching Script/)
})
it('add second output after signed first input with SIGHASH_NONE', function () {
txb.addInput(txHash, 0)
txb.addOutput(scripts[0], 2000)
txb.sign(0, keyPair, undefined, Transaction.SIGHASH_NONE)
assert.equal(txb.addOutput(scripts[1], 9000), 1)
})
it('add first output after signed first input with SIGHASH_NONE', function () {
txb.addInput(txHash, 0)
txb.sign(0, keyPair, undefined, Transaction.SIGHASH_NONE)
assert.equal(txb.addOutput(scripts[0], 2000), 0)
})
it('add second output after signed first input with SIGHASH_SINGLE', function () {
txb.addInput(txHash, 0)
txb.addOutput(scripts[0], 2000)
txb.sign(0, keyPair, undefined, Transaction.SIGHASH_SINGLE)
assert.equal(txb.addOutput(scripts[1], 9000), 1)
})
it('add first output after signed first input with SIGHASH_SINGLE', function () {
txb.addInput(txHash, 0)
txb.sign(0, keyPair, undefined, Transaction.SIGHASH_SINGLE)
assert.throws(function () {
txb.addOutput(scripts[0], 2000)
}, /No, this would invalidate signatures/)
})
it('throws if SIGHASH_ALL has been used to sign any existing scriptSigs', function () {
txb.addInput(txHash, 0)
txb.addOutput(scripts[0], 2000)

Loading…
Cancel
Save