Browse Source

Improve code re-use for redeem script checks

psbt
Luke Childs 6 years ago
parent
commit
95b4a2806d
  1. 35
      src/psbt.js
  2. 44
      ts_src/psbt.ts

35
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,31 +62,17 @@ 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
const hash = Buffer.alloc(32); const hash = Buffer.alloc(32);
const partialSig = { const partialSig = {

44
ts_src/psbt.ts

@ -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,34 +70,17 @@ 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`,
); );
} }
} }
}
// TODO: Get hash to sign // TODO: Get hash to sign
const hash = Buffer.alloc(32); const hash = Buffer.alloc(32);

Loading…
Cancel
Save