Skip to content

Commit f7a3775

Browse files
authored
feat: sign options (MetaMask#60)
1 parent 9687e15 commit f7a3775

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

src/bitcoin/wallet.rs

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{cell::RefCell, rc::Rc};
22

3-
use bdk_wallet::{SignOptions, Wallet as BdkWallet};
3+
use bdk_wallet::{SignOptions as BdkSignOptions, Wallet as BdkWallet};
44
use js_sys::Date;
55
use wasm_bindgen::{prelude::wasm_bindgen, JsError};
66

@@ -145,8 +145,8 @@ impl Wallet {
145145
self.0.borrow().public_descriptor(keychain.into()).to_string()
146146
}
147147

148-
pub fn sign(&self, psbt: &mut Psbt) -> JsResult<bool> {
149-
let result = self.0.borrow().sign(psbt, SignOptions::default())?;
148+
pub fn sign(&self, psbt: &mut Psbt, options: SignOptions) -> JsResult<bool> {
149+
let result = self.0.borrow().sign(psbt, options.into())?;
150150
Ok(result)
151151
}
152152

@@ -184,3 +184,86 @@ impl Wallet {
184184
.map(|(keychain, index)| SpkIndexed(keychain.into(), index))
185185
}
186186
}
187+
188+
#[wasm_bindgen]
189+
pub struct SignOptions(BdkSignOptions);
190+
191+
#[wasm_bindgen]
192+
impl SignOptions {
193+
#[wasm_bindgen(constructor)]
194+
pub fn new() -> Self {
195+
SignOptions(BdkSignOptions::default())
196+
}
197+
198+
#[wasm_bindgen(getter)]
199+
pub fn trust_witness_utxo(&self) -> bool {
200+
self.0.trust_witness_utxo
201+
}
202+
203+
#[wasm_bindgen(setter)]
204+
pub fn set_trust_witness_utxo(&mut self, value: bool) {
205+
self.0.trust_witness_utxo = value;
206+
}
207+
208+
#[wasm_bindgen(getter)]
209+
pub fn assume_height(&self) -> Option<u32> {
210+
self.0.assume_height
211+
}
212+
213+
#[wasm_bindgen(setter)]
214+
pub fn set_assume_height(&mut self, value: Option<u32>) {
215+
self.0.assume_height = value;
216+
}
217+
218+
#[wasm_bindgen(getter)]
219+
pub fn allow_all_sighashes(&self) -> bool {
220+
self.0.allow_all_sighashes
221+
}
222+
223+
#[wasm_bindgen(setter)]
224+
pub fn set_allow_all_sighashes(&mut self, value: bool) {
225+
self.0.allow_all_sighashes = value;
226+
}
227+
228+
#[wasm_bindgen(getter)]
229+
pub fn try_finalize(&self) -> bool {
230+
self.0.try_finalize
231+
}
232+
233+
#[wasm_bindgen(setter)]
234+
pub fn set_try_finalize(&mut self, value: bool) {
235+
self.0.try_finalize = value;
236+
}
237+
238+
#[wasm_bindgen(getter)]
239+
pub fn sign_with_tap_internal_key(&self) -> bool {
240+
self.0.sign_with_tap_internal_key
241+
}
242+
243+
#[wasm_bindgen(setter)]
244+
pub fn set_sign_with_tap_internal_key(&mut self, value: bool) {
245+
self.0.sign_with_tap_internal_key = value;
246+
}
247+
248+
#[wasm_bindgen(getter)]
249+
pub fn allow_grinding(&self) -> bool {
250+
self.0.allow_grinding
251+
}
252+
253+
#[wasm_bindgen(setter)]
254+
pub fn set_allow_grinding(&mut self, value: bool) {
255+
self.0.allow_grinding = value;
256+
}
257+
}
258+
259+
impl From<SignOptions> for BdkSignOptions {
260+
fn from(options: SignOptions) -> Self {
261+
options.0
262+
}
263+
}
264+
265+
impl Default for SignOptions {
266+
fn default() -> Self {
267+
Self::new()
268+
}
269+
}

tests/node/integration/esplora.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Network,
77
Recipient,
88
Wallet,
9+
SignOptions,
910
} from "../../../pkg/bitcoindevkit";
1011

1112
// Tests are expected to run in order
@@ -71,7 +72,7 @@ describe("Esplora client", () => {
7172

7273
expect(psbt.fee().to_sat()).toBeGreaterThan(100); // We cannot know the exact fees
7374

74-
const finalized = wallet.sign(psbt);
75+
const finalized = wallet.sign(psbt, new SignOptions());
7576
expect(finalized).toBeTruthy();
7677

7778
const tx = psbt.extract_tx();

0 commit comments

Comments
 (0)