Skip to content

Commit 4fc72ab

Browse files
feat: wrap Psbt finalize_mut() method
Issue: BTC-1459
1 parent ebe709c commit 4fc72ab

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
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: 14 additions & 10 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(
@@ -52,15 +56,15 @@ function describeUpdateInputWithDescriptor(
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)