Skip to content

Commit 6dd641d

Browse files
Melvillianbpdavenport
authored andcommitted
Merge in necessary bug fixes for segwit upgrade (#1)
* Should be able to deal with incomplete P2SH/P2WSH inputs when allowIncomplete is set * add additional fixes to afk11's pull request
1 parent e7eaf77 commit 6dd641d

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

src/transaction_builder.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -406,36 +406,35 @@ 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
413413
if (SIGNABLE.indexOf(input.redeemScriptType) !== -1) {
414414
sig = buildStack(input.redeemScriptType, input.signatures, input.pubKeys, allowIncomplete)
415415
}
416416
// If it wasn't SIGNABLE, it's witness, defer to that
417-
scriptType = input.redeemScriptType
417+
if (input.redeemScriptType !== undefined) {
418+
scriptType = input.redeemScriptType
419+
}
418420
}
419421

420422
if (scriptType === bscript.types.P2WPKH) {
421423
// P2WPKH is a special case of P2PKH
422424
witness = buildStack(bscript.types.P2PKH, input.signatures, input.pubKeys, allowIncomplete)
423425
} else if (scriptType === bscript.types.P2WSH) {
424426
// We can remove this check later
425-
if (SIGNABLE.indexOf(input.witnessScriptType) !== -1) {
427+
if (SIGNABLE.indexOf(input.witnessScriptType) === -1 && !allowIncomplete) {
428+
throw new Error('Impossible to sign this type')
429+
} else if (SIGNABLE.indexOf(input.witnessScriptType) !== -1) {
426430
witness = buildStack(input.witnessScriptType, input.signatures, input.pubKeys, allowIncomplete)
427431
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()
432+
scriptType = input.witnessScriptType
432433
}
433-
434-
scriptType = input.witnessScriptType
435434
}
436435

437436
// append redeemScript if necessary
438-
if (p2sh) {
437+
if (p2sh && input.redeemScript) {
439438
sig.push(input.redeemScript)
440439
}
441440

@@ -578,7 +577,6 @@ TransactionBuilder.prototype.__addInputUnsafe = function (txHash, vout, options)
578577
var vin = this.tx.addInput(txHash, vout, options.sequence, options.scriptSig)
579578
this.inputs[vin] = input
580579
this.prevTxMap[prevTxOut] = vin
581-
582580
return vin
583581
}
584582

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)