From: Vihiga Tyonum Date: Thu, 21 May 2026 14:59:56 +0000 (+0100) Subject: ref(persister): collapse wallet subdir to persister X-Git-Url: http://internal-gitweb-vhost/blockdata/script/encode/-script/display/struct.ScriptHash.html?a=commitdiff_plain;h=cf9d13bd0974e2eb73a956915e30edf86660feaa;p=bdk-cli ref(persister): collapse wallet subdir to persister - move wallet subdir into the persister module to simplify the structure as wallet was confusing --- diff --git a/src/persister.rs b/src/persister.rs new file mode 100644 index 0000000..2a17db1 --- /dev/null +++ b/src/persister.rs @@ -0,0 +1,119 @@ +#[cfg(any(feature = "sqlite", feature = "redb"))] +use crate::commands::WalletOpts; +use crate::error::BDKCliError as Error; +#[cfg(any(feature = "sqlite", feature = "redb"))] +use bdk_wallet::{KeychainKind, PersistedWallet, bitcoin::Network}; +use bdk_wallet::{Wallet, WalletPersister}; +use clap::ValueEnum; + +#[derive(Clone, ValueEnum, Debug, Eq, PartialEq)] +pub enum DatabaseType { + /// Sqlite database + #[cfg(feature = "sqlite")] + Sqlite, + /// Redb database + #[cfg(feature = "redb")] + Redb, +} + +// Types of Persistence backends supported by bdk-cli +#[cfg(any(feature = "sqlite", feature = "redb"))] +pub(crate) enum Persister { + #[cfg(feature = "sqlite")] + Connection(bdk_wallet::rusqlite::Connection), + #[cfg(feature = "redb")] + RedbStore(bdk_redb::Store), +} + +impl WalletPersister for Persister { + type Error = Error; + + fn initialize(persister: &mut Self) -> Result { + match persister { + #[cfg(feature = "sqlite")] + Persister::Connection(connection) => { + WalletPersister::initialize(connection).map_err(Error::from) + } + #[cfg(feature = "redb")] + Persister::RedbStore(store) => WalletPersister::initialize(store).map_err(Error::from), + } + } + + fn persist(persister: &mut Self, changeset: &bdk_wallet::ChangeSet) -> Result<(), Self::Error> { + match persister { + #[cfg(feature = "sqlite")] + Persister::Connection(connection) => { + WalletPersister::persist(connection, changeset).map_err(Error::from) + } + #[cfg(feature = "redb")] + Persister::RedbStore(store) => { + WalletPersister::persist(store, changeset).map_err(Error::from) + } + } + } +} + +#[cfg(any(feature = "sqlite", feature = "redb"))] +pub(crate) fn new_persisted_wallet( + network: Network, + persister: &mut P, + wallet_opts: &WalletOpts, +) -> Result, Error> +where + P::Error: std::fmt::Display, +{ + let ext_descriptor = wallet_opts.ext_descriptor.clone(); + let int_descriptor = wallet_opts.int_descriptor.clone(); + + let mut wallet_load_params = Wallet::load(); + wallet_load_params = + wallet_load_params.descriptor(KeychainKind::External, Some(ext_descriptor.clone())); + + if int_descriptor.is_some() { + wallet_load_params = + wallet_load_params.descriptor(KeychainKind::Internal, int_descriptor.clone()); + } + wallet_load_params = wallet_load_params.extract_keys(); + + let wallet_opt = wallet_load_params + .check_network(network) + .load_wallet(persister) + .map_err(|e| Error::Generic(e.to_string()))?; + + let wallet = match wallet_opt { + Some(wallet) => wallet, + None => match int_descriptor { + Some(int_descriptor) => Wallet::create(ext_descriptor, int_descriptor) + .network(network) + .create_wallet(persister) + .map_err(|e| Error::Generic(e.to_string()))?, + None => Wallet::create_single(ext_descriptor) + .network(network) + .create_wallet(persister) + .map_err(|e| Error::Generic(e.to_string()))?, + }, + }; + + Ok(wallet) +} + +#[cfg(not(any(feature = "sqlite", feature = "redb")))] +pub(crate) fn new_wallet(network: Network, wallet_opts: &WalletOpts) -> Result { + let ext_descriptor = wallet_opts.ext_descriptor.clone(); + let int_descriptor = wallet_opts.int_descriptor.clone(); + + match int_descriptor { + Some(int_descriptor) => { + let wallet = Wallet::create(ext_descriptor, int_descriptor) + .network(network) + .create_wallet_no_persist()?; + Ok(wallet) + } + None => { + let wallet = Wallet::create_single(ext_descriptor) + .network(network) + .create_wallet_no_persist()?; + Ok(wallet) + } + } +} diff --git a/src/wallet/mod.rs b/src/wallet/mod.rs deleted file mode 100644 index 7ef2e65..0000000 --- a/src/wallet/mod.rs +++ /dev/null @@ -1,74 +0,0 @@ -use crate::commands::WalletOpts; -use crate::error::BDKCliError as Error; -use bdk_wallet::Wallet; -use bdk_wallet::bitcoin::Network; -#[cfg(any(feature = "sqlite", feature = "redb"))] -use bdk_wallet::{KeychainKind, PersistedWallet, WalletPersister}; - -#[cfg(any(feature = "sqlite", feature = "redb"))] -pub mod persister; - -#[cfg(any(feature = "sqlite", feature = "redb"))] -pub(crate) fn new_persisted_wallet( - network: Network, - persister: &mut P, - wallet_opts: &WalletOpts, -) -> Result, Error> -where - P::Error: std::fmt::Display, -{ - let ext_descriptor = wallet_opts.ext_descriptor.clone(); - let int_descriptor = wallet_opts.int_descriptor.clone(); - - let mut wallet_load_params = Wallet::load(); - wallet_load_params = - wallet_load_params.descriptor(KeychainKind::External, Some(ext_descriptor.clone())); - - if int_descriptor.is_some() { - wallet_load_params = - wallet_load_params.descriptor(KeychainKind::Internal, int_descriptor.clone()); - } - wallet_load_params = wallet_load_params.extract_keys(); - - let wallet_opt = wallet_load_params - .check_network(network) - .load_wallet(persister) - .map_err(|e| Error::Generic(e.to_string()))?; - - let wallet = match wallet_opt { - Some(wallet) => wallet, - None => match int_descriptor { - Some(int_descriptor) => Wallet::create(ext_descriptor, int_descriptor) - .network(network) - .create_wallet(persister) - .map_err(|e| Error::Generic(e.to_string()))?, - None => Wallet::create_single(ext_descriptor) - .network(network) - .create_wallet(persister) - .map_err(|e| Error::Generic(e.to_string()))?, - }, - }; - - Ok(wallet) -} - -#[cfg(not(any(feature = "sqlite", feature = "redb")))] -pub(crate) fn new_wallet(network: Network, wallet_opts: &WalletOpts) -> Result { - let ext_descriptor = wallet_opts.ext_descriptor.clone(); - let int_descriptor = wallet_opts.int_descriptor.clone(); - - match int_descriptor { - Some(int_descriptor) => { - let wallet = Wallet::create(ext_descriptor, int_descriptor) - .network(network) - .create_wallet_no_persist()?; - Ok(wallet) - } - None => { - let wallet = Wallet::create_single(ext_descriptor) - .network(network) - .create_wallet_no_persist()?; - Ok(wallet) - } - } -} diff --git a/src/wallet/persister.rs b/src/wallet/persister.rs deleted file mode 100644 index 1f9a742..0000000 --- a/src/wallet/persister.rs +++ /dev/null @@ -1,40 +0,0 @@ -use crate::error::BDKCliError; -use bdk_wallet::WalletPersister; - -// Types of Persistence backends supported by bdk-cli -pub(crate) enum Persister { - #[cfg(feature = "sqlite")] - Connection(bdk_wallet::rusqlite::Connection), - #[cfg(feature = "redb")] - RedbStore(bdk_redb::Store), -} - -impl WalletPersister for Persister { - type Error = BDKCliError; - - fn initialize(persister: &mut Self) -> Result { - match persister { - #[cfg(feature = "sqlite")] - Persister::Connection(connection) => { - WalletPersister::initialize(connection).map_err(BDKCliError::from) - } - #[cfg(feature = "redb")] - Persister::RedbStore(store) => { - WalletPersister::initialize(store).map_err(BDKCliError::from) - } - } - } - - fn persist(persister: &mut Self, changeset: &bdk_wallet::ChangeSet) -> Result<(), Self::Error> { - match persister { - #[cfg(feature = "sqlite")] - Persister::Connection(connection) => { - WalletPersister::persist(connection, changeset).map_err(BDKCliError::from) - } - #[cfg(feature = "redb")] - Persister::RedbStore(store) => { - WalletPersister::persist(store, changeset).map_err(BDKCliError::from) - } - } - } -}