Skip to content

Commit

Permalink
feat(wallet)!: introduce WalletPersister
Browse files Browse the repository at this point in the history
This replaces `bdk_chain::PersistWith` which wanted to persist anything
(not only `Wallet`), hence, requiring a whole bunch of generic
parameters.

Having `WalletPersister` dedicated to persisting `Wallet` simplifies the
trait by a lot.

In addition, `AsyncWalletPersister` has proper lifetime bounds whereas
`bdk_chain::PersistAsyncWith` did not.
  • Loading branch information
evanlinjin committed Aug 15, 2024
1 parent e0822d7 commit 039622f
Show file tree
Hide file tree
Showing 3 changed files with 316 additions and 142 deletions.
45 changes: 23 additions & 22 deletions crates/wallet/src/wallet/params.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use alloc::boxed::Box;
use bdk_chain::{keychain_txout::DEFAULT_LOOKAHEAD, PersistAsyncWith, PersistWith};
use bdk_chain::keychain_txout::DEFAULT_LOOKAHEAD;
use bitcoin::{BlockHash, Network};
use miniscript::descriptor::KeyMap;

use crate::{
descriptor::{DescriptorError, ExtendedDescriptor, IntoWalletDescriptor},
utils::SecpCtx,
KeychainKind, Wallet,
AsyncWalletPersister, CreateWithPersistError, KeychainKind, LoadWithPersistError, Wallet,
WalletPersister,
};

use super::{ChangeSet, LoadError, PersistedWallet};
Expand Down Expand Up @@ -109,25 +110,25 @@ impl CreateParams {
}

/// Create [`PersistedWallet`] with the given `Db`.
pub fn create_wallet<Db>(
pub fn create_wallet<P>(
self,
db: &mut Db,
) -> Result<PersistedWallet, <Wallet as PersistWith<Db>>::CreateError>
persister: &mut P,
) -> Result<PersistedWallet, CreateWithPersistError<P::Error>>
where
Wallet: PersistWith<Db, CreateParams = Self>,
P: WalletPersister,
{
PersistedWallet::create(db, self)
PersistedWallet::create(persister, self)
}

/// Create [`PersistedWallet`] with the given async `Db`.
pub async fn create_wallet_async<Db>(
pub async fn create_wallet_async<P>(
self,
db: &mut Db,
) -> Result<PersistedWallet, <Wallet as PersistAsyncWith<Db>>::CreateError>
persister: &mut P,
) -> Result<PersistedWallet, CreateWithPersistError<P::Error>>
where
Wallet: PersistAsyncWith<Db, CreateParams = Self>,
P: AsyncWalletPersister,
{
PersistedWallet::create_async(db, self).await
PersistedWallet::create_async(persister, self).await
}

/// Create [`Wallet`] without persistence.
Expand Down Expand Up @@ -220,25 +221,25 @@ impl LoadParams {
}

/// Load [`PersistedWallet`] with the given `Db`.
pub fn load_wallet<Db>(
pub fn load_wallet<P>(
self,
db: &mut Db,
) -> Result<Option<PersistedWallet>, <Wallet as PersistWith<Db>>::LoadError>
persister: &mut P,
) -> Result<Option<PersistedWallet>, LoadWithPersistError<P::Error>>
where
Wallet: PersistWith<Db, LoadParams = Self>,
P: WalletPersister,
{
PersistedWallet::load(db, self)
PersistedWallet::load(persister, self)
}

/// Load [`PersistedWallet`] with the given async `Db`.
pub async fn load_wallet_async<Db>(
pub async fn load_wallet_async<P>(
self,
db: &mut Db,
) -> Result<Option<PersistedWallet>, <Wallet as PersistAsyncWith<Db>>::LoadError>
persister: &mut P,
) -> Result<Option<PersistedWallet>, LoadWithPersistError<P::Error>>
where
Wallet: PersistAsyncWith<Db, LoadParams = Self>,
P: AsyncWalletPersister,
{
PersistedWallet::load_async(db, self).await
PersistedWallet::load_async(persister, self).await
}

/// Load [`Wallet`] without persistence.
Expand Down
Loading

0 comments on commit 039622f

Please sign in to comment.