Skip to content

Commit

Permalink
feat: wrap Psbt finalize_mut() method
Browse files Browse the repository at this point in the history
Issue: BTC-1459
  • Loading branch information
OttoAllmendinger committed Aug 27, 2024
1 parent ebe709c commit 4fc72ab
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
12 changes: 12 additions & 0 deletions packages/wasm-miniscript/src/psbt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use miniscript::bitcoin::Psbt;
use miniscript::bitcoin::secp256k1::Secp256k1;
use miniscript::psbt::PsbtExt;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::{JsError};
Expand All @@ -18,6 +19,10 @@ impl WrapPsbt {
self.0.serialize()
}

pub fn clone(&self) -> WrapPsbt {
WrapPsbt(self.0.clone())
}

#[wasm_bindgen(js_name = updateInputWithDescriptor)]
pub fn update_input_with_descriptor(&mut self, input_index: usize, descriptor: WrapDescriptor) -> Result<(), JsError> {
match descriptor.0 {
Expand All @@ -32,4 +37,11 @@ impl WrapPsbt {
}
}
}

#[wasm_bindgen(js_name = finalize)]
pub fn finalize_mut(&mut self) -> Result<(), JsError> {
self.0.finalize_mut(&Secp256k1::verification_only()).map_err(|vec_err| {
JsError::new(&format!("{} errors: {:?}", vec_err.len(), vec_err))
})
}
}
24 changes: 14 additions & 10 deletions packages/wasm-miniscript/test/psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ function toUtxoPsbt(psbt: Psbt | Buffer | Uint8Array) {
throw new Error("Invalid input");
}

function assertEqualBuffer(a: Buffer | Uint8Array, b: Buffer | Uint8Array, message?: string) {
assert.strictEqual(Buffer.from(a).toString("hex"), Buffer.from(b).toString("hex"), message);
}

const fixtures = getPsbtFixtures(rootWalletKeys);

function describeUpdateInputWithDescriptor(
Expand All @@ -52,15 +56,15 @@ function describeUpdateInputWithDescriptor(
const updatedPsbt = toUtxoPsbt(wrappedPsbt);
updatedPsbt.signAllInputsHD(rootWalletKeys.triple[0]);
updatedPsbt.signAllInputsHD(rootWalletKeys.triple[2]);
const wrappedSignedPsbt = toWrappedPsbt(updatedPsbt);
updatedPsbt.finalizeAllInputs();
assert.deepStrictEqual(
fullSignedFixture.psbt
.clone()
.finalizeAllInputs()
.extractTransaction()
.toBuffer()
.toString("hex"),
updatedPsbt.extractTransaction().toBuffer().toString("hex"),
wrappedSignedPsbt.finalize();

assertEqualBuffer(updatedPsbt.toBuffer(), wrappedSignedPsbt.serialize());

assertEqualBuffer(
fullSignedFixture.psbt.clone().finalizeAllInputs().extractTransaction().toBuffer(),
updatedPsbt.extractTransaction().toBuffer(),
);
});
});
Expand All @@ -77,11 +81,11 @@ fixtures.forEach(({ psbt, scriptType, stage }) => {
});

it("should map to same hex", function () {
assert.strictEqual(buf.toString("hex"), Buffer.from(wrappedPsbt.serialize()).toString("hex"));
assertEqualBuffer(buf, wrappedPsbt.serialize());
});

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

if (stage === "bare") {
Expand Down

0 comments on commit 4fc72ab

Please sign in to comment.