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
ts_src

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);