Compare commits

...

2 Commits

Author SHA1 Message Date
Daniel Cousens
ba315dd275 txb: enforce outputs exist when signing 2018-07-27 15:55:17 +10:00
Daniel Cousens
a6231b391f tests/txb: remove getAddress 2018-07-27 15:47:28 +10:00
3 changed files with 35 additions and 9 deletions

View File

@ -633,6 +633,8 @@ 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
const input = this.__inputs[vin]
@ -694,8 +696,7 @@ function signatureHashType (buffer) {
TransactionBuilder.prototype.__canModifyInputs = function () {
return this.__inputs.every(function (input) {
// any signatures?
if (input.signatures === undefined) return true
if (!input.signatures) return true
return input.signatures.every(function (signature) {
if (!signature) return true
@ -708,6 +709,20 @@ TransactionBuilder.prototype.__canModifyInputs = function () {
})
}
TransactionBuilder.prototype.__needsOutputs = function () {
// if any signature is not SIGHASH_NONE,
// we need outputs
return (this.__tx.outs.length === 0) && this.__inputs.some(function (input) {
if (!input.signatures) return false
return input.signatures.some(function (signature) {
if (!signature) return false
const hashType = signatureHashType(signature)
return ~(hashType & Transaction.SIGHASH_NONE)
})
})
}
TransactionBuilder.prototype.__canModifyOutputs = function () {
const nInputs = this.__tx.ins.length
const nOutputs = this.__tx.outs.length

View File

@ -2336,6 +2336,22 @@
"value": 1000
}
]
},
{
"description": "Transaction w/ no outputs",
"exception": "Transaction needs outputs",
"inputs": [
{
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"vout": 0,
"signs": [
{
"keyPair": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"
}
]
}
],
"outputs": []
}
],
"fromTransaction": [

View File

@ -2,7 +2,6 @@
const assert = require('assert')
const baddress = require('../src/address')
const bcrypto = require('../src/crypto')
const bscript = require('../src/script')
const ops = require('bitcoin-ops')
const payments = require('../src/payments')
@ -14,11 +13,6 @@ const NETWORKS = require('../src/networks')
const fixtures = require('./fixtures/transaction_builder')
// TODO: remove
function getAddress (node) {
return baddress.toBase58Check(bcrypto.hash160(node.publicKey), NETWORKS.bitcoin.pubKeyHash)
}
function construct (f, dontSign) {
const network = NETWORKS[f.network]
let txb = new TransactionBuilder(network)
@ -228,7 +222,7 @@ describe('TransactionBuilder', function () {
})
it('accepts an address string and value', function () {
const address = getAddress(keyPair)
const { address } = payments.p2pkh({ pubkey: keyPair.publicKey })
const vout = txb.addOutput(address, 1000)
assert.strictEqual(vout, 0)
@ -295,6 +289,7 @@ describe('TransactionBuilder', function () {
it('throws if if there exist any scriptSigs', function () {
const txb = new TransactionBuilder()
txb.addInput(txHash, 0)
txb.addOutput(scripts[0], 100)
txb.sign(0, keyPair)
assert.throws(function () {