diff --git a/src/psbt.js b/src/psbt.js index 5bdb176..5d40ab4 100644 --- a/src/psbt.js +++ b/src/psbt.js @@ -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 = { diff --git a/ts_src/psbt.ts b/ts_src/psbt.ts index 73308a2..9b0e9ce 100644 --- a/ts_src/psbt.ts +++ b/ts_src/psbt.ts @@ -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);