Skip to content

Commit 9eb3e60

Browse files
feat: expose Wallet::create_single method
1 parent 5ef6134 commit 9eb3e60

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

bdk-ffi/src/wallet.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,48 @@ impl Wallet {
6767
})
6868
}
6969

70+
/// Build a new single descriptor `Wallet`.
71+
///
72+
/// If you have previously created a wallet, use `Wallet::load` instead.
73+
///
74+
/// # Note
75+
///
76+
/// Only use this method when creating a wallet designed to be used with a single
77+
/// descriptor and keychain. Otherwise the recommended way to construct a new wallet is
78+
/// by using `Wallet::create`. It's worth noting that not all features are available
79+
/// with single descriptor wallets, for example setting a `change_policy` on `TxBuilder`
80+
/// and related methods such as `do_not_spend_change`. This is because all payments are
81+
/// received on the external keychain (including change), and without a change keychain
82+
/// BDK lacks enough information to distinguish between change and outside payments.
83+
///
84+
/// Additionally because this wallet has no internal (change) keychain, all methods that
85+
/// require a `KeychainKind` as input, e.g. `reveal_next_address` should only be called
86+
/// using the `External` variant. In most cases passing `Internal` is treated as the
87+
/// equivalent of `External` but this behavior must not be relied on.
88+
#[uniffi::constructor(default(lookahead = 25))]
89+
pub fn create_single(
90+
descriptor: Arc<Descriptor>,
91+
network: Network,
92+
persister: Arc<Persister>,
93+
lookahead: u32,
94+
) -> Result<Self, CreateWithPersistError> {
95+
let descriptor = descriptor.to_string_with_secret();
96+
let mut persist_lock = persister.inner.lock().unwrap();
97+
let deref = persist_lock.deref_mut();
98+
99+
let wallet: PersistedWallet<PersistenceType> = BdkWallet::create_single(descriptor)
100+
.network(network)
101+
.lookahead(lookahead)
102+
.create_wallet(deref)
103+
.map_err(|e| CreateWithPersistError::Persist {
104+
error_message: e.to_string(),
105+
})?;
106+
107+
Ok(Wallet {
108+
inner_mutex: Mutex::new(wallet),
109+
})
110+
}
111+
70112
/// Build Wallet by loading from persistence.
71113
//
72114
// Note that the descriptor secret keys are not persisted to the db.

0 commit comments

Comments
 (0)