diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 91b24f7..252e7c1 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -155,7 +155,10 @@ class TransactionBuilder { ) { trySign( getSigningData( - this, + this.network, + this.__INPUTS, + this.__needsOutputs.bind(this), + this.__TX, signParams, keyPair, redeemScript, @@ -725,7 +728,7 @@ function canSign(input) { function signatureHashType(buffer) { return buffer.readUInt8(buffer.length - 1); } -function checkSignArgs(txb, signParams) { +function checkSignArgs(inputs, signParams) { if (!PREVOUT_TYPES.has(signParams.prevOutScriptType)) { throw new TypeError( `Unknown prevOutScriptType "${signParams.prevOutScriptType}"`, @@ -739,8 +742,7 @@ function checkSignArgs(txb, signParams) { ), [signParams.vin, signParams.hashType, signParams.keyPair], ); - // @ts-ignore - const prevOutType = (txb.__INPUTS[signParams.vin] || []).prevOutType; + const prevOutType = (inputs[signParams.vin] || []).prevOutType; const posType = signParams.prevOutScriptType; switch (posType) { case 'p2pkh': @@ -953,7 +955,10 @@ function trySign({ if (!signed) throw new Error('Key pair cannot sign for this input'); } function getSigningData( - txb, + network, + inputs, + needsOutputs, + tx, signParams, keyPair, redeemScript, @@ -970,7 +975,7 @@ function getSigningData( ); vin = signParams; } else if (typeof signParams === 'object') { - checkSignArgs(txb, signParams); + checkSignArgs(inputs, signParams); ({ vin, keyPair, @@ -988,16 +993,12 @@ function getSigningData( throw new Error('sign requires keypair'); } // TODO: remove keyPair.network matching in 4.0.0 - if (keyPair.network && keyPair.network !== txb.network) + if (keyPair.network && keyPair.network !== network) throw new TypeError('Inconsistent network'); - // @ts-ignore - if (!txb.__INPUTS[vin]) throw new Error('No input at index: ' + vin); + if (!inputs[vin]) throw new Error('No input at index: ' + vin); hashType = hashType || transaction_1.Transaction.SIGHASH_ALL; - // @ts-ignore - if (txb.__needsOutputs(hashType)) - throw new Error('Transaction needs outputs'); - // @ts-ignore - const input = txb.__INPUTS[vin]; + if (needsOutputs(hashType)) throw new Error('Transaction needs outputs'); + const input = inputs[vin]; // if redeemScript was previously provided, enforce consistency if ( input.redeemScript !== undefined && @@ -1029,16 +1030,14 @@ function getSigningData( // ready to sign let signatureHash; if (input.hasWitness) { - // @ts-ignore - signatureHash = txb.__TX.hashForWitnessV0( + signatureHash = tx.hashForWitnessV0( vin, input.signScript, input.value, hashType, ); } else { - // @ts-ignore - signatureHash = txb.__TX.hashForSignature(vin, input.signScript, hashType); + signatureHash = tx.hashForSignature(vin, input.signScript, hashType); } return { input, diff --git a/ts_src/transaction_builder.ts b/ts_src/transaction_builder.ts index 9cb3c8f..fede01b 100644 --- a/ts_src/transaction_builder.ts +++ b/ts_src/transaction_builder.ts @@ -245,7 +245,10 @@ export class TransactionBuilder { ): void { trySign( getSigningData( - this, + this.network, + this.__INPUTS, + this.__needsOutputs.bind(this), + this.__TX, signParams, keyPair, redeemScript, @@ -949,7 +952,7 @@ function signatureHashType(buffer: Buffer): number { return buffer.readUInt8(buffer.length - 1); } -function checkSignArgs(txb: TransactionBuilder, signParams: TxbSignArg): void { +function checkSignArgs(inputs: TxbInput[], signParams: TxbSignArg): void { if (!PREVOUT_TYPES.has(signParams.prevOutScriptType)) { throw new TypeError( `Unknown prevOutScriptType "${signParams.prevOutScriptType}"`, @@ -963,8 +966,7 @@ function checkSignArgs(txb: TransactionBuilder, signParams: TxbSignArg): void { ), [signParams.vin, signParams.hashType, signParams.keyPair], ); - // @ts-ignore - const prevOutType = (txb.__INPUTS[signParams.vin] || []).prevOutType; + const prevOutType = (inputs[signParams.vin] || []).prevOutType; const posType = signParams.prevOutScriptType; switch (posType) { case 'p2pkh': @@ -1190,8 +1192,13 @@ interface SigningData { useLowR: boolean; } +type HashTypeCheck = (hashType: number) => boolean; + function getSigningData( - txb: TransactionBuilder, + network: Network, + inputs: TxbInput[], + needsOutputs: HashTypeCheck, + tx: Transaction, signParams: number | TxbSignArg, keyPair?: ECPairInterface, redeemScript?: Buffer, @@ -1208,7 +1215,7 @@ function getSigningData( ); vin = signParams; } else if (typeof signParams === 'object') { - checkSignArgs(txb, signParams); + checkSignArgs(inputs, signParams); ({ vin, keyPair, @@ -1226,18 +1233,14 @@ function getSigningData( throw new Error('sign requires keypair'); } // TODO: remove keyPair.network matching in 4.0.0 - if (keyPair.network && keyPair.network !== txb.network) + if (keyPair.network && keyPair.network !== network) throw new TypeError('Inconsistent network'); - // @ts-ignore - if (!txb.__INPUTS[vin]) throw new Error('No input at index: ' + vin); + if (!inputs[vin]) throw new Error('No input at index: ' + vin); hashType = hashType || Transaction.SIGHASH_ALL; - // @ts-ignore - if (txb.__needsOutputs(hashType)) - throw new Error('Transaction needs outputs'); + if (needsOutputs(hashType)) throw new Error('Transaction needs outputs'); - // @ts-ignore - const input = txb.__INPUTS[vin]; + const input = inputs[vin]; // if redeemScript was previously provided, enforce consistency if ( @@ -1275,16 +1278,14 @@ function getSigningData( // ready to sign let signatureHash: Buffer; if (input.hasWitness) { - // @ts-ignore - signatureHash = txb.__TX.hashForWitnessV0( + signatureHash = tx.hashForWitnessV0( vin, input.signScript as Buffer, input.value as number, hashType, ); } else { - // @ts-ignore - signatureHash = txb.__TX.hashForSignature( + signatureHash = tx.hashForSignature( vin, input.signScript as Buffer, hashType,