Skip to content

Commit cc4ba36

Browse files
committed
Add fallible Wallet try_get_address(), try_get_internal_address functions when a PersistBackend is used
1 parent d99e4a5 commit cc4ba36

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

crates/bdk/src/wallet/mod.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,24 @@ impl Wallet {
160160
NewError::Persist(_) => unreachable!("no persistence so it can't fail"),
161161
})
162162
}
163+
164+
/// Infallibly return a derived address using the external descriptor, see [`AddressIndex`] for
165+
/// available address index selection strategies. If none of the keys in the descriptor are derivable
166+
/// (i.e. does not end with /*) then the same address will always be returned for any [`AddressIndex`].
167+
pub fn get_address(&mut self, address_index: AddressIndex) -> AddressInfo {
168+
self.try_get_address(address_index).unwrap()
169+
}
170+
171+
/// Infallibly return a derived address using the internal (change) descriptor.
172+
///
173+
/// If the wallet doesn't have an internal descriptor it will use the external descriptor.
174+
///
175+
/// see [`AddressIndex`] for available address index selection strategies. If none of the keys
176+
/// in the descriptor are derivable (i.e. does not end with /*) then the same address will always
177+
/// be returned for any [`AddressIndex`].
178+
pub fn get_internal_address(&mut self, address_index: AddressIndex) -> AddressInfo {
179+
self.try_get_internal_address(address_index).unwrap()
180+
}
163181
}
164182

165183
#[derive(Debug)]
@@ -330,27 +348,37 @@ impl<D> Wallet<D> {
330348
/// Return a derived address using the external descriptor, see [`AddressIndex`] for
331349
/// available address index selection strategies. If none of the keys in the descriptor are derivable
332350
/// (i.e. does not end with /*) then the same address will always be returned for any [`AddressIndex`].
333-
pub fn get_address(&mut self, address_index: AddressIndex) -> AddressInfo
351+
///
352+
/// A `PersistBackend<ChangeSet>::WriteError` will result if unable to persist the new address
353+
/// to the `PersistBackend`.
354+
pub fn try_get_address(
355+
&mut self,
356+
address_index: AddressIndex,
357+
) -> Result<AddressInfo, D::WriteError>
334358
where
335359
D: PersistBackend<ChangeSet>,
336360
{
337361
self._get_address(KeychainKind::External, address_index)
338-
.expect("persistence backend must not fail")
339362
}
340363

341364
/// Return a derived address using the internal (change) descriptor.
342365
///
343366
/// If the wallet doesn't have an internal descriptor it will use the external descriptor.
344367
///
368+
/// A `PersistBackend<ChangeSet>::WriteError` will result if unable to persist the new address
369+
/// to the `PersistBackend`.
370+
///
345371
/// see [`AddressIndex`] for available address index selection strategies. If none of the keys
346372
/// in the descriptor are derivable (i.e. does not end with /*) then the same address will always
347373
/// be returned for any [`AddressIndex`].
348-
pub fn get_internal_address(&mut self, address_index: AddressIndex) -> AddressInfo
374+
pub fn try_get_internal_address(
375+
&mut self,
376+
address_index: AddressIndex,
377+
) -> Result<AddressInfo, D::WriteError>
349378
where
350379
D: PersistBackend<ChangeSet>,
351380
{
352381
self._get_address(KeychainKind::Internal, address_index)
353-
.expect("persistence backend must not fail")
354382
}
355383

356384
/// Return a derived address using the specified `keychain` (external/internal).

example-crates/wallet_electrum/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2626
Network::Testnet,
2727
)?;
2828

29-
let address = wallet.get_address(bdk::wallet::AddressIndex::New);
29+
let address = wallet
30+
.try_get_address(bdk::wallet::AddressIndex::New)
31+
.expect("new_address");
3032
println!("Generated Address: {}", address);
3133

3234
let balance = wallet.get_balance();

example-crates/wallet_esplora/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2626
Network::Testnet,
2727
)?;
2828

29-
let address = wallet.get_address(AddressIndex::New);
29+
let address = wallet
30+
.try_get_address(AddressIndex::New)
31+
.expect("new address");
3032
println!("Generated Address: {}", address);
3133

3234
let balance = wallet.get_balance();

example-crates/wallet_esplora_async/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2727
Network::Testnet,
2828
)?;
2929

30-
let address = wallet.get_address(AddressIndex::New);
30+
let address = wallet
31+
.try_get_address(AddressIndex::New)
32+
.expect("new address");
3133
println!("Generated Address: {}", address);
3234

3335
let balance = wallet.get_balance();

0 commit comments

Comments
 (0)