Skip to content

Commit 8ce36f5

Browse files
committed
feat(wallet): Add fallible Wallet try_get_address(), try_get_internal_address functions when a PersistBackend is used
1 parent c482e70 commit 8ce36f5

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
@@ -256,6 +256,24 @@ impl Wallet {
256256
NewError::Write(_) => unreachable!("mock-write must always succeed"),
257257
})
258258
}
259+
260+
/// Infallibly return a derived address using the external descriptor, see [`AddressIndex`] for
261+
/// available address index selection strategies. If none of the keys in the descriptor are derivable
262+
/// (i.e. does not end with /*) then the same address will always be returned for any [`AddressIndex`].
263+
pub fn get_address(&mut self, address_index: AddressIndex) -> AddressInfo {
264+
self.try_get_address(address_index).unwrap()
265+
}
266+
267+
/// Infallibly return a derived address using the internal (change) descriptor.
268+
///
269+
/// If the wallet doesn't have an internal descriptor it will use the external descriptor.
270+
///
271+
/// see [`AddressIndex`] for available address index selection strategies. If none of the keys
272+
/// in the descriptor are derivable (i.e. does not end with /*) then the same address will always
273+
/// be returned for any [`AddressIndex`].
274+
pub fn get_internal_address(&mut self, address_index: AddressIndex) -> AddressInfo {
275+
self.try_get_internal_address(address_index).unwrap()
276+
}
259277
}
260278

261279
/// The error type when constructing a fresh [`Wallet`].
@@ -668,27 +686,37 @@ impl<D> Wallet<D> {
668686
/// Return a derived address using the external descriptor, see [`AddressIndex`] for
669687
/// available address index selection strategies. If none of the keys in the descriptor are derivable
670688
/// (i.e. does not end with /*) then the same address will always be returned for any [`AddressIndex`].
671-
pub fn get_address(&mut self, address_index: AddressIndex) -> AddressInfo
689+
///
690+
/// A `PersistBackend<ChangeSet>::WriteError` will result if unable to persist the new address
691+
/// to the `PersistBackend`.
692+
pub fn try_get_address(
693+
&mut self,
694+
address_index: AddressIndex,
695+
) -> Result<AddressInfo, D::WriteError>
672696
where
673697
D: PersistBackend<ChangeSet>,
674698
{
675699
self._get_address(KeychainKind::External, address_index)
676-
.expect("persistence backend must not fail")
677700
}
678701

679702
/// Return a derived address using the internal (change) descriptor.
680703
///
681704
/// If the wallet doesn't have an internal descriptor it will use the external descriptor.
682705
///
706+
/// A `PersistBackend<ChangeSet>::WriteError` will result if unable to persist the new address
707+
/// to the `PersistBackend`.
708+
///
683709
/// see [`AddressIndex`] for available address index selection strategies. If none of the keys
684710
/// in the descriptor are derivable (i.e. does not end with /*) then the same address will always
685711
/// be returned for any [`AddressIndex`].
686-
pub fn get_internal_address(&mut self, address_index: AddressIndex) -> AddressInfo
712+
pub fn try_get_internal_address(
713+
&mut self,
714+
address_index: AddressIndex,
715+
) -> Result<AddressInfo, D::WriteError>
687716
where
688717
D: PersistBackend<ChangeSet>,
689718
{
690719
self._get_address(KeychainKind::Internal, address_index)
691-
.expect("persistence backend must not fail")
692720
}
693721

694722
/// 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
@@ -29,7 +29,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2929
Network::Testnet,
3030
)?;
3131

32-
let address = wallet.get_address(bdk::wallet::AddressIndex::New);
32+
let address = wallet
33+
.try_get_address(bdk::wallet::AddressIndex::New)
34+
.expect("new_address");
3335
println!("Generated Address: {}", address);
3436

3537
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();

example-crates/wallet_esplora_blocking/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();

0 commit comments

Comments
 (0)