Browse Source

Check Non-witness UTXO hash when signing PSBT input

psbt
Luke Childs 6 years ago
parent
commit
98dff9a47e
  1. 19
      src/psbt.js
  2. 19
      ts_src/psbt.ts

19
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 = {

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

Loading…
Cancel
Save