Skip to content

Commit d8b182b

Browse files
feat: add finalizePsbt util
Issue: BTC-1451
1 parent 445fd1b commit d8b182b

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

packages/wasm-miniscript/test/psbt.util.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import * as utxolib from "@bitgo/utxo-lib";
22
import { Descriptor, Psbt } from "../js";
33

4-
export function toWrappedPsbt(psbt: utxolib.bitgo.UtxoPsbt | Buffer | Uint8Array) {
5-
if (psbt instanceof utxolib.bitgo.UtxoPsbt) {
4+
function toAddress(descriptor: Descriptor, network: utxolib.Network) {
5+
utxolib.address.fromOutputScript(Buffer.from(descriptor.scriptPubkey()), network);
6+
}
7+
8+
export function toWrappedPsbt(psbt: utxolib.bitgo.UtxoPsbt | utxolib.Psbt | Buffer | Uint8Array) {
9+
if (psbt instanceof utxolib.bitgo.UtxoPsbt || psbt instanceof utxolib.Psbt) {
610
psbt = psbt.toBuffer();
711
}
812
if (psbt instanceof Buffer || psbt instanceof Uint8Array) {
@@ -24,11 +28,20 @@ export function toUtxoPsbt(psbt: Psbt | Buffer | Uint8Array) {
2428
}
2529

2630
export function updateInputWithDescriptor(
27-
psbt: utxolib.bitgo.UtxoPsbt,
31+
psbt: utxolib.Psbt,
2832
inputIndex: number,
2933
descriptor: Descriptor,
3034
) {
3135
const wrappedPsbt = toWrappedPsbt(psbt);
3236
wrappedPsbt.updateInputWithDescriptor(inputIndex, descriptor);
3337
psbt.data.inputs[inputIndex] = toUtxoPsbt(wrappedPsbt).data.inputs[inputIndex];
3438
}
39+
40+
export function finalizePsbt(psbt: utxolib.Psbt) {
41+
const wrappedPsbt = toWrappedPsbt(psbt);
42+
wrappedPsbt.finalize();
43+
const unwrappedPsbt = toUtxoPsbt(wrappedPsbt);
44+
for (let i = 0; i < psbt.data.inputs.length; i++) {
45+
psbt.data.inputs[i] = unwrappedPsbt.data.inputs[i];
46+
}
47+
}

0 commit comments

Comments
 (0)