diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 40aa30f..91b24f7 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -153,24 +153,17 @@ class TransactionBuilder { witnessValue, witnessScript, ) { - const data = getSigningData( - this, - signParams, - keyPair, - redeemScript, - hashType, - witnessValue, - witnessScript, - ); - const { input, ourPubKey, signatureHash } = data; - ({ keyPair, hashType } = data); trySign( - input, - ourPubKey, - keyPair, - signatureHash, - hashType, - this.__USE_LOW_R, + getSigningData( + this, + signParams, + keyPair, + redeemScript, + hashType, + witnessValue, + witnessScript, + this.__USE_LOW_R, + ), ); } __addInputUnsafe(txHash, vout, options) { @@ -934,7 +927,14 @@ function checkSignArgs(txb, signParams) { break; } } -function trySign(input, ourPubKey, keyPair, signatureHash, hashType, useLowR) { +function trySign({ + input, + ourPubKey, + keyPair, + signatureHash, + hashType, + useLowR, +}) { // enforce in order signing of public keys let signed = false; for (const [i, pubKey] of input.pubkeys.entries()) { @@ -960,6 +960,7 @@ function getSigningData( hashType, witnessValue, witnessScript, + useLowR, ) { let vin; if (typeof signParams === 'number') { @@ -1045,5 +1046,6 @@ function getSigningData( keyPair, signatureHash, hashType, + useLowR: !!useLowR, }; } diff --git a/ts_src/transaction_builder.ts b/ts_src/transaction_builder.ts index 880a662..9cb3c8f 100644 --- a/ts_src/transaction_builder.ts +++ b/ts_src/transaction_builder.ts @@ -243,26 +243,17 @@ export class TransactionBuilder { witnessValue?: number, witnessScript?: Buffer, ): void { - const data = getSigningData( - this, - signParams, - keyPair, - redeemScript, - hashType, - witnessValue, - witnessScript, - ); - - const { input, ourPubKey, signatureHash } = data; - ({ keyPair, hashType } = data); - trySign( - input, - ourPubKey, - keyPair, - signatureHash, - hashType, - this.__USE_LOW_R, + getSigningData( + this, + signParams, + keyPair, + redeemScript, + hashType, + witnessValue, + witnessScript, + this.__USE_LOW_R, + ), ); } @@ -1161,14 +1152,14 @@ function checkSignArgs(txb: TransactionBuilder, signParams: TxbSignArg): void { } } -function trySign( - input: TxbInput, - ourPubKey: Buffer, - keyPair: ECPairInterface, - signatureHash: Buffer, - hashType: number, - useLowR: boolean, -): void { +function trySign({ + input, + ourPubKey, + keyPair, + signatureHash, + hashType, + useLowR, +}: SigningData): void { // enforce in order signing of public keys let signed = false; for (const [i, pubKey] of input.pubkeys!.entries()) { @@ -1190,6 +1181,15 @@ function trySign( if (!signed) throw new Error('Key pair cannot sign for this input'); } +interface SigningData { + input: TxbInput; + ourPubKey: Buffer; + keyPair: ECPairInterface; + signatureHash: Buffer; + hashType: number; + useLowR: boolean; +} + function getSigningData( txb: TransactionBuilder, signParams: number | TxbSignArg, @@ -1198,13 +1198,8 @@ function getSigningData( hashType?: number, witnessValue?: number, witnessScript?: Buffer, -): { - input: TxbInput; - ourPubKey: Buffer; - keyPair: ECPairInterface; - signatureHash: Buffer; - hashType: number; -} { + useLowR?: boolean, +): SigningData { let vin: number; if (typeof signParams === 'number') { console.warn( @@ -1302,5 +1297,6 @@ function getSigningData( keyPair, signatureHash, hashType, + useLowR: !!useLowR, }; }