Skip to content

Commit 445fd1b

Browse files
feat: add updateInputWithDescriptor util
This util function allows working with native UtxoPsbt Issue: BTC-1451
1 parent 2788a62 commit 445fd1b

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

packages/wasm-miniscript/test/psbt.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getPsbtFixtures } from "./psbtFixtures";
44
import { Descriptor, Psbt } from "../js";
55

66
import { getDescriptorForScriptType } from "./descriptorUtil";
7-
import { toUtxoPsbt, toWrappedPsbt } from "./psbt.util";
7+
import { toUtxoPsbt, toWrappedPsbt, updateInputWithDescriptor } from "./psbt.util";
88

99
const rootWalletKeys = new utxolib.bitgo.RootWalletKeys(utxolib.testutil.getKeyTriple("wasm"));
1010

@@ -25,11 +25,12 @@ function describeUpdateInputWithDescriptor(
2525
throw new Error("Could not find fullsigned fixture");
2626
}
2727

28-
describe("updateInputWithDescriptor", function () {
28+
const descriptorStr = getDescriptorForScriptType(rootWalletKeys, scriptType, "internal");
29+
const index = 0;
30+
const descriptor = Descriptor.fromString(descriptorStr, "derivable");
31+
32+
describe("Wrapped PSBT updateInputWithDescriptor", function () {
2933
it("should update the input with the descriptor", function () {
30-
const descriptorStr = getDescriptorForScriptType(rootWalletKeys, scriptType, "internal");
31-
const index = 0;
32-
const descriptor = Descriptor.fromString(descriptorStr, "derivable");
3334
const wrappedPsbt = toWrappedPsbt(psbt);
3435
wrappedPsbt.updateInputWithDescriptor(0, descriptor.atDerivationIndex(index));
3536
const updatedPsbt = toUtxoPsbt(wrappedPsbt);
@@ -47,6 +48,21 @@ function describeUpdateInputWithDescriptor(
4748
);
4849
});
4950
});
51+
52+
describe("updateInputWithDescriptor util", function () {
53+
it("should update the input with the descriptor", function () {
54+
const cloned = psbt.clone();
55+
updateInputWithDescriptor(cloned, 0, descriptor.atDerivationIndex(index));
56+
cloned.signAllInputsHD(rootWalletKeys.triple[0]);
57+
cloned.signAllInputsHD(rootWalletKeys.triple[2]);
58+
cloned.finalizeAllInputs();
59+
60+
assertEqualBuffer(
61+
fullSignedFixture.psbt.clone().finalizeAllInputs().extractTransaction().toBuffer(),
62+
cloned.extractTransaction().toBuffer(),
63+
);
64+
});
65+
});
5066
}
5167

5268
fixtures.forEach(({ psbt, scriptType, stage }) => {

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

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

44
export function toWrappedPsbt(psbt: utxolib.bitgo.UtxoPsbt | Buffer | Uint8Array) {
55
if (psbt instanceof utxolib.bitgo.UtxoPsbt) {
@@ -21,4 +21,14 @@ export function toUtxoPsbt(psbt: Psbt | Buffer | Uint8Array) {
2121
});
2222
}
2323
throw new Error("Invalid input");
24-
}
24+
}
25+
26+
export function updateInputWithDescriptor(
27+
psbt: utxolib.bitgo.UtxoPsbt,
28+
inputIndex: number,
29+
descriptor: Descriptor,
30+
) {
31+
const wrappedPsbt = toWrappedPsbt(psbt);
32+
wrappedPsbt.updateInputWithDescriptor(inputIndex, descriptor);
33+
psbt.data.inputs[inputIndex] = toUtxoPsbt(wrappedPsbt).data.inputs[inputIndex];
34+
}

0 commit comments

Comments
 (0)