From 95b4a2806d6d433bd0d766d0be7c0183d28541cd Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Fri, 28 Jun 2019 18:21:32 +0700 Subject: [PATCH] Improve code re-use for redeem script checks --- src/psbt.js | 37 +++++++++++++++++-------------------- ts_src/psbt.ts | 46 +++++++++++++++++++++++----------------------- 2 files changed, 40 insertions(+), 43 deletions(-) diff --git a/src/psbt.js b/src/psbt.js index 4dc72ff..c68afb0 100644 --- a/src/psbt.js +++ b/src/psbt.js @@ -3,6 +3,17 @@ Object.defineProperty(exports, '__esModule', { value: true }); const bip174_1 = require('bip174'); const payments = require('./payments'); const transaction_1 = require('./transaction'); +const checkRedeemScript = (inputIndex, scriptPubKey, redeemScript) => { + const redeemScriptOutput = payments.p2sh({ + redeem: { output: redeemScript }, + }).output; + // If a redeemScript is provided, the scriptPubKey must be for that redeemScript + if (Buffer.compare(scriptPubKey, redeemScriptOutput) !== 0) { + throw new Error( + `Redeem script for input #${inputIndex} doesn't match the scriptPubKey in the prevout`, + ); + } +}; class Psbt extends bip174_1.Psbt { constructor() { super(); @@ -51,29 +62,15 @@ class Psbt extends bip174_1.Psbt { if (input.redeemScript) { const prevoutIndex = unsignedTx.ins[inputIndex].index; const prevout = nonWitnessUtxoTx.outs[prevoutIndex]; - const redeemScriptOutput = payments.p2sh({ - redeem: { output: input.redeemScript }, - }).output; - // If a redeemScript is provided, the scriptPubKey must be for that redeemScript - if (Buffer.compare(prevout.script, redeemScriptOutput) !== 0) { - throw new Error( - `Redeem script for input #${inputIndex} doesn't match the scriptPubKey in the prevout`, - ); - } + checkRedeemScript(inputIndex, prevout.script, input.redeemScript); } } else if (input.witnessUtxo) { if (input.redeemScript) { - const redeemScriptOutput = payments.p2sh({ - redeem: { output: input.redeemScript }, - }).output; - // If a redeemScript is provided, the scriptPubKey must be for that redeemScript - if ( - Buffer.compare(input.witnessUtxo.script, redeemScriptOutput) !== 0 - ) { - throw new Error( - `Redeem script for input #${inputIndex} doesn't match the scriptPubKey in the prevout`, - ); - } + checkRedeemScript( + inputIndex, + input.witnessUtxo.script, + input.redeemScript, + ); } } // TODO: Get hash to sign diff --git a/ts_src/psbt.ts b/ts_src/psbt.ts index ab1fd7b..70e04c9 100644 --- a/ts_src/psbt.ts +++ b/ts_src/psbt.ts @@ -3,6 +3,23 @@ import { Signer } from './ecpair'; import * as payments from './payments'; import { Transaction } from './transaction'; +const checkRedeemScript = ( + inputIndex: number, + scriptPubKey: Buffer, + redeemScript: Buffer, +): void => { + const redeemScriptOutput = payments.p2sh({ + redeem: { output: redeemScript }, + }).output as Buffer; + + // If a redeemScript is provided, the scriptPubKey must be for that redeemScript + if (Buffer.compare(scriptPubKey, redeemScriptOutput) !== 0) { + throw new Error( + `Redeem script for input #${inputIndex} doesn't match the scriptPubKey in the prevout`, + ); + } +}; + export class Psbt extends PsbtBase { constructor() { super(); @@ -53,32 +70,15 @@ export class Psbt extends PsbtBase { if (input.redeemScript) { const prevoutIndex = unsignedTx.ins[inputIndex].index; const prevout = nonWitnessUtxoTx.outs[prevoutIndex]; - - const redeemScriptOutput = payments.p2sh({ - redeem: { output: input.redeemScript }, - }).output as Buffer; - - // If a redeemScript is provided, the scriptPubKey must be for that redeemScript - if (Buffer.compare(prevout.script, redeemScriptOutput) !== 0) { - throw new Error( - `Redeem script for input #${inputIndex} doesn't match the scriptPubKey in the prevout`, - ); - } + checkRedeemScript(inputIndex, prevout.script, input.redeemScript); } } else if (input.witnessUtxo) { if (input.redeemScript) { - const redeemScriptOutput = payments.p2sh({ - redeem: { output: input.redeemScript }, - }).output as Buffer; - - // If a redeemScript is provided, the scriptPubKey must be for that redeemScript - if ( - Buffer.compare(input.witnessUtxo.script, redeemScriptOutput) !== 0 - ) { - throw new Error( - `Redeem script for input #${inputIndex} doesn't match the scriptPubKey in the prevout`, - ); - } + checkRedeemScript( + inputIndex, + input.witnessUtxo.script, + input.redeemScript, + ); } }