Improve code re-use for redeem script checks

This commit is contained in:
Luke Childs 2019-06-28 18:21:32 +07:00
parent 10b3aff4fd
commit 95b4a2806d
2 changed files with 40 additions and 43 deletions

View file

@ -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

View file

@ -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,
);
}
}