Merge pull request #504 from fanatid/txb/addOutput
Fix TransactionBuilder.addOutput
This commit is contained in:
commit
ce9670eab4
2 changed files with 34 additions and 3 deletions
|
@ -227,10 +227,13 @@ TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOu
|
||||||
}
|
}
|
||||||
|
|
||||||
TransactionBuilder.prototype.addOutput = function (scriptPubKey, value) {
|
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
|
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')
|
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)
|
scriptPubKey = baddress.toOutputScript(scriptPubKey, this.network)
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.tx.addOutput(scriptPubKey, value)
|
return tx.addOutput(scriptPubKey, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
TransactionBuilder.prototype.build = function () {
|
TransactionBuilder.prototype.build = function () {
|
||||||
|
|
|
@ -202,6 +202,34 @@ describe('TransactionBuilder', function () {
|
||||||
}, /2NGHjvjw83pcVFgMcA7QvSMh2c246rxLVz9 has no matching Script/)
|
}, /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 () {
|
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], 2000)
|
txb.addOutput(scripts[0], 2000)
|
||||||
|
|
Loading…
Add table
Reference in a new issue