Skip to content

Commit 0b1c3bf

Browse files
Thomas Kerindcousens
authored andcommitted
Should be able to deal with incomplete P2SH/P2WSH inputs when allowIncomplete is set
1 parent bc3aef5 commit 0b1c3bf

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

src/transaction_builder.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ function buildInput (input, allowIncomplete) {
406406
if (scriptType === bscript.types.P2SH) {
407407
// We can remove this error later when we have a guarantee prepareInput
408408
// rejects unsignable scripts - it MUST be signable at this point.
409-
if (P2SH.indexOf(input.redeemScriptType) === -1) {
409+
if (P2SH.indexOf(input.redeemScriptType) === -1 && !allowIncomplete) {
410410
throw new Error('Impossible to sign this type')
411411
}
412412
p2sh = true
@@ -422,16 +422,13 @@ function buildInput (input, allowIncomplete) {
422422
witness = buildStack(bscript.types.P2PKH, input.signatures, input.pubKeys, allowIncomplete)
423423
} else if (scriptType === bscript.types.P2WSH) {
424424
// We can remove this check later
425-
if (SIGNABLE.indexOf(input.witnessScriptType) !== -1) {
425+
if (SIGNABLE.indexOf(input.witnessScriptType) === -1 && !allowIncomplete) {
426+
throw new Error('Impossible to sign this type')
427+
} else if (SIGNABLE.indexOf(input.witnessScriptType) !== -1) {
426428
witness = buildStack(input.witnessScriptType, input.signatures, input.pubKeys, allowIncomplete)
427429
witness.push(input.witnessScript)
428-
} else {
429-
// We can remove this error later when we have a guarantee prepareInput
430-
// rejects unsignble scripts - it MUST be signable at this point.
431-
throw new Error()
430+
scriptType = input.witnessScriptType
432431
}
433-
434-
scriptType = input.witnessScriptType
435432
}
436433

437434
// append redeemScript if necessary
@@ -578,7 +575,6 @@ TransactionBuilder.prototype.__addInputUnsafe = function (txHash, vout, options)
578575
var vin = this.tx.addInput(txHash, vout, options.sequence, options.scriptSig)
579576
this.inputs[vin] = input
580577
this.prevTxMap[prevTxOut] = vin
581-
582578
return vin
583579
}
584580

test/transaction_builder.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,38 @@ describe('TransactionBuilder', function () {
366366
tx = tx.buildIncomplete()
367367
assert(tx)
368368
})
369+
370+
it('for incomplete P2SH with 0 signatures', function () {
371+
var inp = Buffer.from('010000000173120703f67318aef51f7251272a6816d3f7523bb25e34b136d80be959391c100000000000ffffffff0100c817a80400000017a91471a8ec07ff69c6c4fee489184c462a9b1b9237488700000000', 'hex') // arbitrary P2SH input
372+
var inpTx = Transaction.fromBuffer(inp)
373+
374+
var txb = new TransactionBuilder(NETWORKS.testnet)
375+
txb.addInput(inpTx, 0)
376+
txb.addOutput(baddress.toOutputScript('2NAkqp5xffoomp5RLBcakuGpZ12GU4twdz4', NETWORKS.testnet), 1e8) // arbitrary output
377+
378+
txb.buildIncomplete()
379+
})
380+
381+
it('for incomplete P2WPKH with 0 signatures', function () {
382+
var inp = Buffer.from('010000000173120703f67318aef51f7251272a6816d3f7523bb25e34b136d80be959391c100000000000ffffffff0100c817a8040000001600141a15805e1f4040c9f68ccc887fca2e63547d794b00000000', 'hex')
383+
var inpTx = Transaction.fromBuffer(inp)
384+
385+
var txb = new TransactionBuilder(NETWORKS.testnet)
386+
txb.addInput(inpTx, 0)
387+
txb.addOutput(baddress.toOutputScript('2NAkqp5xffoomp5RLBcakuGpZ12GU4twdz4', NETWORKS.testnet), 1e8) // arbitrary output
388+
389+
txb.buildIncomplete()
390+
})
391+
392+
it('for incomplete P2WSH with 0 signatures', function () {
393+
var inpTx = Transaction.fromBuffer(Buffer.from('010000000173120703f67318aef51f7251272a6816d3f7523bb25e34b136d80be959391c100000000000ffffffff0100c817a80400000022002072df76fcc0b231b94bdf7d8c25d7eef4716597818d211e19ade7813bff7a250200000000', 'hex'))
394+
395+
var txb = new TransactionBuilder(NETWORKS.testnet)
396+
txb.addInput(inpTx, 0)
397+
txb.addOutput(baddress.toOutputScript('2NAkqp5xffoomp5RLBcakuGpZ12GU4twdz4', NETWORKS.testnet), 1e8) // arbitrary output
398+
399+
txb.buildIncomplete()
400+
})
369401
})
370402

371403
describe('multisig', function () {

0 commit comments

Comments
 (0)