@@ -298,6 +298,7 @@ function prepareInput (input, kpPubKey, redeemScript, witnessValue, witnessScrip
298298
299299 expanded = expandOutput ( witnessScript , undefined , kpPubKey )
300300 if ( ! expanded . pubKeys ) throw new Error ( 'WitnessScript not supported "' + bscript . toASM ( redeemScript ) + '"' )
301+
301302 prevOutType = btemplates . types . P2SH
302303 prevOutScript = btemplates . scriptHash . output . encode ( redeemScriptHash )
303304 p2sh = witness = p2wsh = true
@@ -347,18 +348,13 @@ function prepareInput (input, kpPubKey, redeemScript, witnessValue, witnessScrip
347348 } else {
348349 prevOutScript = btemplates . pubKeyHash . output . encode ( bcrypto . hash160 ( kpPubKey ) )
349350 expanded = expandOutput ( prevOutScript , scriptTypes . P2PKH , kpPubKey )
351+
350352 prevOutType = scriptTypes . P2PKH
351353 witness = false
352354 signType = prevOutType
353355 signScript = prevOutScript
354356 }
355357
356- if ( witnessValue !== undefined || witness ) {
357- typeforce ( types . Satoshi , witnessValue )
358- if ( input . value !== undefined && input . value !== witnessValue ) throw new Error ( 'Input didn\'t match witnessValue' )
359- input . value = witnessValue
360- }
361-
362358 if ( signType === scriptTypes . P2WPKH ) {
363359 signScript = btemplates . pubKeyHash . output . encode ( btemplates . witnessPubKeyHash . output . decode ( signScript ) )
364360 }
@@ -686,7 +682,13 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
686682
687683 var kpPubKey = keyPair . getPublicKeyBuffer ( )
688684 if ( ! canSign ( input ) ) {
689- prepareInput ( input , kpPubKey , redeemScript , witnessValue , witnessScript )
685+ if ( witnessValue !== undefined ) {
686+ if ( input . value !== undefined && input . value !== witnessValue ) throw new Error ( 'Input didn\'t match witnessValue' )
687+ typeforce ( types . Satoshi , witnessValue )
688+ input . value = witnessValue
689+ }
690+
691+ if ( ! canSign ( input ) ) prepareInput ( input , kpPubKey , redeemScript , witnessValue , witnessScript )
690692 if ( ! canSign ( input ) ) throw Error ( input . prevOutType + ' not supported' )
691693 }
692694
0 commit comments