Improve code re-use for redeem script checks
This commit is contained in:
parent
10b3aff4fd
commit
95b4a2806d
2 changed files with 40 additions and 43 deletions
37
src/psbt.js
37
src/psbt.js
|
@ -3,6 +3,17 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
||||||
const bip174_1 = require('bip174');
|
const bip174_1 = require('bip174');
|
||||||
const payments = require('./payments');
|
const payments = require('./payments');
|
||||||
const transaction_1 = require('./transaction');
|
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 {
|
class Psbt extends bip174_1.Psbt {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
@ -51,29 +62,15 @@ class Psbt extends bip174_1.Psbt {
|
||||||
if (input.redeemScript) {
|
if (input.redeemScript) {
|
||||||
const prevoutIndex = unsignedTx.ins[inputIndex].index;
|
const prevoutIndex = unsignedTx.ins[inputIndex].index;
|
||||||
const prevout = nonWitnessUtxoTx.outs[prevoutIndex];
|
const prevout = nonWitnessUtxoTx.outs[prevoutIndex];
|
||||||
const redeemScriptOutput = payments.p2sh({
|
checkRedeemScript(inputIndex, prevout.script, input.redeemScript);
|
||||||
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`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (input.witnessUtxo) {
|
} else if (input.witnessUtxo) {
|
||||||
if (input.redeemScript) {
|
if (input.redeemScript) {
|
||||||
const redeemScriptOutput = payments.p2sh({
|
checkRedeemScript(
|
||||||
redeem: { output: input.redeemScript },
|
inputIndex,
|
||||||
}).output;
|
input.witnessUtxo.script,
|
||||||
// If a redeemScript is provided, the scriptPubKey must be for that redeemScript
|
input.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`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TODO: Get hash to sign
|
// TODO: Get hash to sign
|
||||||
|
|
|
@ -3,6 +3,23 @@ import { Signer } from './ecpair';
|
||||||
import * as payments from './payments';
|
import * as payments from './payments';
|
||||||
import { Transaction } from './transaction';
|
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 {
|
export class Psbt extends PsbtBase {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
@ -53,32 +70,15 @@ export class Psbt extends PsbtBase {
|
||||||
if (input.redeemScript) {
|
if (input.redeemScript) {
|
||||||
const prevoutIndex = unsignedTx.ins[inputIndex].index;
|
const prevoutIndex = unsignedTx.ins[inputIndex].index;
|
||||||
const prevout = nonWitnessUtxoTx.outs[prevoutIndex];
|
const prevout = nonWitnessUtxoTx.outs[prevoutIndex];
|
||||||
|
checkRedeemScript(inputIndex, prevout.script, 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(prevout.script, redeemScriptOutput) !== 0) {
|
|
||||||
throw new Error(
|
|
||||||
`Redeem script for input #${inputIndex} doesn't match the scriptPubKey in the prevout`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (input.witnessUtxo) {
|
} else if (input.witnessUtxo) {
|
||||||
if (input.redeemScript) {
|
if (input.redeemScript) {
|
||||||
const redeemScriptOutput = payments.p2sh({
|
checkRedeemScript(
|
||||||
redeem: { output: input.redeemScript },
|
inputIndex,
|
||||||
}).output as Buffer;
|
input.witnessUtxo.script,
|
||||||
|
input.redeemScript,
|
||||||
// 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`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue