@@ -667,7 +667,8 @@ function canSign (input) {
667667}
668668
669669TransactionBuilder . prototype . sign = function ( vin , keyPair , redeemScript , hashType , witnessValue , witnessScript ) {
670- if ( keyPair . network !== this . network ) throw new Error ( 'Inconsistent network' )
670+ // TODO: remove keyPair.network matching in 4.0.0
671+ if ( keyPair . network && keyPair . network !== this . network ) throw new TypeError ( 'Inconsistent network' )
671672 if ( ! this . inputs [ vin ] ) throw new Error ( 'No input at index: ' + vin )
672673 hashType = hashType || Transaction . SIGHASH_ALL
673674
@@ -680,7 +681,7 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
680681 throw new Error ( 'Inconsistent redeemScript' )
681682 }
682683
683- var kpPubKey = keyPair . getPublicKeyBuffer ( )
684+ var kpPubKey = keyPair . publicKey || keyPair . getPublicKeyBuffer ( )
684685 if ( ! canSign ( input ) ) {
685686 if ( witnessValue !== undefined ) {
686687 if ( input . value !== undefined && input . value !== witnessValue ) throw new Error ( 'Input didn\'t match witnessValue' )
@@ -699,14 +700,18 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
699700 } else {
700701 signatureHash = this . tx . hashForSignature ( vin , input . signScript , hashType )
701702 }
703+
702704 // enforce in order signing of public keys
703705 var signed = input . pubKeys . some ( function ( pubKey , i ) {
704706 if ( ! kpPubKey . equals ( pubKey ) ) return false
705707 if ( input . signatures [ i ] ) throw new Error ( 'Signature already exists' )
706- if ( ! keyPair . compressed &&
708+ if ( kpPubKey . length !== 33 &&
707709 input . signType === scriptTypes . P2WPKH ) throw new Error ( 'BIP143 rejects uncompressed public keys in P2WPKH or P2WSH' )
708710
709- input . signatures [ i ] = keyPair . sign ( signatureHash ) . toScriptSignature ( hashType )
711+ var signature = keyPair . sign ( signatureHash )
712+ if ( Buffer . isBuffer ( signature ) ) signature = ECSignature . fromRSBuffer ( signature )
713+
714+ input . signatures [ i ] = signature . toScriptSignature ( hashType )
710715 return true
711716 } )
712717
0 commit comments