Skip to content

Commit

Permalink
feat: add finalizePsbt util
Browse files Browse the repository at this point in the history
Issue: BTC-1451
  • Loading branch information
OttoAllmendinger committed Oct 24, 2024
1 parent 445fd1b commit d8b182b
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions packages/wasm-miniscript/test/psbt.util.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import * as utxolib from "@bitgo/utxo-lib";
import { Descriptor, Psbt } from "../js";

export function toWrappedPsbt(psbt: utxolib.bitgo.UtxoPsbt | Buffer | Uint8Array) {
if (psbt instanceof utxolib.bitgo.UtxoPsbt) {
function toAddress(descriptor: Descriptor, network: utxolib.Network) {
utxolib.address.fromOutputScript(Buffer.from(descriptor.scriptPubkey()), network);
}

export function toWrappedPsbt(psbt: utxolib.bitgo.UtxoPsbt | utxolib.Psbt | Buffer | Uint8Array) {
if (psbt instanceof utxolib.bitgo.UtxoPsbt || psbt instanceof utxolib.Psbt) {
psbt = psbt.toBuffer();
}
if (psbt instanceof Buffer || psbt instanceof Uint8Array) {
Expand All @@ -24,11 +28,20 @@ export function toUtxoPsbt(psbt: Psbt | Buffer | Uint8Array) {
}

export function updateInputWithDescriptor(
psbt: utxolib.bitgo.UtxoPsbt,
psbt: utxolib.Psbt,
inputIndex: number,
descriptor: Descriptor,
) {
const wrappedPsbt = toWrappedPsbt(psbt);
wrappedPsbt.updateInputWithDescriptor(inputIndex, descriptor);
psbt.data.inputs[inputIndex] = toUtxoPsbt(wrappedPsbt).data.inputs[inputIndex];
}

export function finalizePsbt(psbt: utxolib.Psbt) {
const wrappedPsbt = toWrappedPsbt(psbt);
wrappedPsbt.finalize();
const unwrappedPsbt = toUtxoPsbt(wrappedPsbt);
for (let i = 0; i < psbt.data.inputs.length; i++) {
psbt.data.inputs[i] = unwrappedPsbt.data.inputs[i];
}
}

0 comments on commit d8b182b

Please sign in to comment.