|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use wasm_bindgen::prelude::*; |
| 4 | + |
| 5 | +use crate::{Error, JsStorage, JsStoreLink, Network, Wollet, WolletDescriptor}; |
| 6 | + |
| 7 | +/// A builder for constructing [`Wollet`] instances. |
| 8 | +#[wasm_bindgen] |
| 9 | +pub struct WolletBuilder { |
| 10 | + inner: lwk_wollet::WolletBuilder, |
| 11 | +} |
| 12 | + |
| 13 | +impl From<lwk_wollet::WolletBuilder> for WolletBuilder { |
| 14 | + fn from(value: lwk_wollet::WolletBuilder) -> Self { |
| 15 | + Self { inner: value } |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +#[wasm_bindgen] |
| 20 | +impl WolletBuilder { |
| 21 | + /// Create a builder for a watch-only wallet. |
| 22 | + #[wasm_bindgen(constructor)] |
| 23 | + pub fn new(network: &Network, descriptor: &WolletDescriptor) -> Self { |
| 24 | + lwk_wollet::WolletBuilder::new(network.into(), descriptor.into()).into() |
| 25 | + } |
| 26 | + |
| 27 | + /// Set the threshold used to merge persisted updates during build. |
| 28 | + /// |
| 29 | + /// `None` disables merging (default behavior). |
| 30 | + #[wasm_bindgen(js_name = withMergeThreshold)] |
| 31 | + pub fn with_merge_threshold(self, merge_threshold: Option<u32>) -> Self { |
| 32 | + self.inner |
| 33 | + .with_merge_threshold(merge_threshold.map(|t| t as usize)) |
| 34 | + .into() |
| 35 | + } |
| 36 | + |
| 37 | + /// Experimental: set the wallet as "utxo only". |
| 38 | + #[wasm_bindgen(js_name = utxoOnly)] |
| 39 | + pub fn utxo_only(self, utxo_only: bool) -> Self { |
| 40 | + self.inner.utxo_only(utxo_only).into() |
| 41 | + } |
| 42 | + |
| 43 | + /// Persist wallet updates in the given JavaScript storage object. |
| 44 | + /// |
| 45 | + /// The JS object must have `get(key)`, `put(key, value)`, and `remove(key)` methods. |
| 46 | + #[wasm_bindgen(js_name = withStore)] |
| 47 | + pub fn with_store(self, storage: JsStorage) -> Self { |
| 48 | + self.inner |
| 49 | + .with_store(Arc::new(JsStoreLink::new(storage))) |
| 50 | + .into() |
| 51 | + } |
| 52 | + |
| 53 | + /// Build the wallet from this builder. |
| 54 | + pub fn build(self) -> Result<Wollet, Error> { |
| 55 | + Ok(self.inner.build()?.into()) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +#[cfg(test)] |
| 60 | +mod tests { |
| 61 | + use super::*; |
| 62 | + |
| 63 | + #[test] |
| 64 | + fn test_builder() { |
| 65 | + let mnemonic = crate::Mnemonic::new( |
| 66 | + "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about", |
| 67 | + ) |
| 68 | + .unwrap(); |
| 69 | + let network = Network::regtest_default(); |
| 70 | + let signer = crate::Signer::new(&mnemonic, &network).unwrap(); |
| 71 | + let descriptor = signer.wpkh_slip77_descriptor().unwrap(); |
| 72 | + |
| 73 | + let wollet = WolletBuilder::new(&network, &descriptor) |
| 74 | + .with_merge_threshold(Some(2)) |
| 75 | + .utxo_only(true) |
| 76 | + .build() |
| 77 | + .unwrap(); |
| 78 | + |
| 79 | + assert_eq!(wollet.address(Some(0)).unwrap().index(), 0); |
| 80 | + } |
| 81 | +} |
0 commit comments