@@ -218,14 +218,16 @@ function prepareInput (input, kpPubKey, redeemScript, hashType) {
218218
219219function fixMultisigOrder ( input , transaction , vin ) {
220220 var hashScriptType = input . redeemScriptType || input . prevOutType
221- if ( hashScriptType !== 'multisig' ) throw new TypeError ( 'Expected multisig input' )
221+ if ( hashScriptType !== 'multisig' ) return
222222
223- var hashType = input . hashType || Transaction . SIGHASH_ALL
224223 var hashScript = input . redeemScript || input . prevOutScript
224+ if ( ! hashScript ) return
225+ if ( ! input . pubKeys ) return
226+ if ( input . pubKeys . length === input . signatures . length ) return
225227
226- // maintain a local copy of unmatched signatures
227228 var unmatched = input . signatures . concat ( )
228- var signatureHash = transaction . hashForSignature ( vin , hashScript , hashType )
229+ var hashType = input . hashType || Transaction . SIGHASH_ALL
230+ var hash = transaction . hashForSignature ( vin , hashScript , hashType )
229231
230232 input . signatures = input . pubKeys . map ( function ( pubKey , y ) {
231233 var keyPair = ECPair . fromPublicKeyBuffer ( pubKey )
@@ -237,7 +239,7 @@ function fixMultisigOrder (input, transaction, vin) {
237239 if ( ! signature ) return false
238240
239241 // skip if signature does not match pubKey
240- if ( ! keyPair . verify ( signatureHash , signature ) ) return false
242+ if ( ! keyPair . verify ( hash , signature ) ) return false
241243
242244 // remove matched signature from unmatched
243245 unmatched [ i ] = undefined
@@ -246,7 +248,7 @@ function fixMultisigOrder (input, transaction, vin) {
246248 return true
247249 } )
248250
249- return match || undefined
251+ return match
250252 } )
251253}
252254
@@ -299,14 +301,7 @@ TransactionBuilder.fromTransaction = function (transaction, network) {
299301
300302 // fix some things not possible through the public API
301303 txb . inputs . forEach ( function ( input , i ) {
302- // attempt to fix any multisig inputs if they exist
303- if ( ( input . redeemScriptType || input . prevOutType ) === 'multisig' ) {
304- // pubKeys will only exist for 'multisig' if a redeemScript was found
305- if ( ! input . pubKeys || ! input . signatures ) return
306- if ( input . pubKeys . length === input . signatures . length ) return
307-
308- fixMultisigOrder ( input , transaction , i )
309- }
304+ fixMultisigOrder ( input , transaction , i )
310305 } )
311306
312307 return txb
0 commit comments