Check Non-witness UTXO hash when signing PSBT input

This commit is contained in:
Luke Childs 2019-06-26 17:55:02 +07:00
parent ff3caa02fe
commit 98dff9a47e
2 changed files with 38 additions and 0 deletions

View file

@ -1,6 +1,7 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const bip174_1 = require('bip174');
const transaction_1 = require('./transaction');
class Psbt extends bip174_1.Psbt {
constructor() {
super();
@ -29,6 +30,24 @@ class Psbt extends bip174_1.Psbt {
// sign_witness(witnessScript)
// else:
// assert False
const input = this.inputs[inputIndex];
if (input === undefined) throw new Error(`No input #${inputIndex}`);
// If a non-witness UTXO is provided, its hash must match the hash specified in the prevout
if (input.nonWitnessUtxo) {
const unsignedTx = transaction_1.Transaction.fromBuffer(
this.globalMap.unsignedTx,
);
const nonWitnessUtxoTx = transaction_1.Transaction.fromBuffer(
input.nonWitnessUtxo,
);
const inputHash = unsignedTx.ins[inputIndex].hash;
const utxoHash = nonWitnessUtxoTx.getHash();
if (Buffer.compare(inputHash, utxoHash) !== 0) {
throw new Error(
`Non-witness UTXO hash for input #${inputIndex} doesn't match the hash specified in the prevout`,
);
}
}
// TODO: Get hash to sign
const hash = Buffer.alloc(32);
const partialSig = {

View file

@ -1,5 +1,6 @@
import { Psbt as PsbtBase } from 'bip174';
import { Signer } from './ecpair';
import { Transaction } from './transaction';
export class Psbt extends PsbtBase {
constructor() {
@ -31,6 +32,24 @@ export class Psbt extends PsbtBase {
// else:
// assert False
const input = this.inputs[inputIndex];
if (input === undefined) throw new Error(`No input #${inputIndex}`);
// If a non-witness UTXO is provided, its hash must match the hash specified in the prevout
if (input.nonWitnessUtxo) {
const unsignedTx = Transaction.fromBuffer(this.globalMap.unsignedTx!);
const nonWitnessUtxoTx = Transaction.fromBuffer(input.nonWitnessUtxo);
const inputHash = unsignedTx.ins[inputIndex].hash;
const utxoHash = nonWitnessUtxoTx.getHash();
if (Buffer.compare(inputHash, utxoHash) !== 0) {
throw new Error(
`Non-witness UTXO hash for input #${inputIndex} doesn't match the hash specified in the prevout`,
);
}
}
// TODO: Get hash to sign
const hash = Buffer.alloc(32);