@@ -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
0 commit comments