Skip to content

Commit 713ca46

Browse files
Merge pull request #40 from BitGo/BTC-1459.add-psbt-finalize
feat: wrap finalize_mut()
2 parents ddbeeb8 + 4fc72ab commit 713ca46

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

packages/wasm-miniscript/src/psbt.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use miniscript::bitcoin::Psbt;
2+
use miniscript::bitcoin::secp256k1::Secp256k1;
23
use miniscript::psbt::PsbtExt;
34
use wasm_bindgen::prelude::wasm_bindgen;
45
use wasm_bindgen::{JsError};
@@ -18,6 +19,10 @@ impl WrapPsbt {
1819
self.0.serialize()
1920
}
2021

22+
pub fn clone(&self) -> WrapPsbt {
23+
WrapPsbt(self.0.clone())
24+
}
25+
2126
#[wasm_bindgen(js_name = updateInputWithDescriptor)]
2227
pub fn update_input_with_descriptor(&mut self, input_index: usize, descriptor: WrapDescriptor) -> Result<(), JsError> {
2328
match descriptor.0 {
@@ -32,4 +37,11 @@ impl WrapPsbt {
3237
}
3338
}
3439
}
40+
41+
#[wasm_bindgen(js_name = finalize)]
42+
pub fn finalize_mut(&mut self) -> Result<(), JsError> {
43+
self.0.finalize_mut(&Secp256k1::verification_only()).map_err(|vec_err| {
44+
JsError::new(&format!("{} errors: {:?}", vec_err.len(), vec_err))
45+
})
46+
}
3547
}

packages/wasm-miniscript/test/psbt.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ function toUtxoPsbt(psbt: Psbt | Buffer | Uint8Array) {
2929
throw new Error("Invalid input");
3030
}
3131

32+
function assertEqualBuffer(a: Buffer | Uint8Array, b: Buffer | Uint8Array, message?: string) {
33+
assert.strictEqual(Buffer.from(a).toString("hex"), Buffer.from(b).toString("hex"), message);
34+
}
35+
3236
const fixtures = getPsbtFixtures(rootWalletKeys);
3337

3438
function describeUpdateInputWithDescriptor(
@@ -47,20 +51,20 @@ function describeUpdateInputWithDescriptor(
4751
const descriptorStr = getDescriptorForScriptType(rootWalletKeys, scriptType, "internal");
4852
const index = 0;
4953
const descriptor = Descriptor.fromString(descriptorStr, "derivable");
50-
const wrappedPsbt = toWrappedPsbt(toPsbtWithPrevOutOnly(psbt));
54+
const wrappedPsbt = toWrappedPsbt(psbt);
5155
wrappedPsbt.updateInputWithDescriptor(0, descriptor.atDerivationIndex(index));
5256
const updatedPsbt = toUtxoPsbt(wrappedPsbt);
5357
updatedPsbt.signAllInputsHD(rootWalletKeys.triple[0]);
5458
updatedPsbt.signAllInputsHD(rootWalletKeys.triple[2]);
59+
const wrappedSignedPsbt = toWrappedPsbt(updatedPsbt);
5560
updatedPsbt.finalizeAllInputs();
56-
assert.deepStrictEqual(
57-
fullSignedFixture.psbt
58-
.clone()
59-
.finalizeAllInputs()
60-
.extractTransaction()
61-
.toBuffer()
62-
.toString("hex"),
63-
updatedPsbt.extractTransaction().toBuffer().toString("hex"),
61+
wrappedSignedPsbt.finalize();
62+
63+
assertEqualBuffer(updatedPsbt.toBuffer(), wrappedSignedPsbt.serialize());
64+
65+
assertEqualBuffer(
66+
fullSignedFixture.psbt.clone().finalizeAllInputs().extractTransaction().toBuffer(),
67+
updatedPsbt.extractTransaction().toBuffer(),
6468
);
6569
});
6670
});
@@ -77,11 +81,11 @@ fixtures.forEach(({ psbt, scriptType, stage }) => {
7781
});
7882

7983
it("should map to same hex", function () {
80-
assert.strictEqual(buf.toString("hex"), Buffer.from(wrappedPsbt.serialize()).toString("hex"));
84+
assertEqualBuffer(buf, wrappedPsbt.serialize());
8185
});
8286

8387
it("should round-trip utxolib -> ms -> utxolib", function () {
84-
assert.strictEqual(buf.toString("hex"), toUtxoPsbt(wrappedPsbt).toBuffer().toString("hex"));
88+
assertEqualBuffer(buf, toUtxoPsbt(wrappedPsbt).toBuffer());
8589
});
8690

8791
if (stage === "bare") {

0 commit comments

Comments
 (0)