]> Untitled Git - bdk-cli/commitdiff
ref(persister): collapse wallet subdir to persister
authorVihiga Tyonum <withtvpeter@gmail.com>
Thu, 21 May 2026 14:59:56 +0000 (15:59 +0100)
committerVihiga Tyonum <withtvpeter@gmail.com>
Fri, 5 Jun 2026 16:32:14 +0000 (17:32 +0100)
- move wallet subdir into the persister module
to simplify the structure as wallet was confusing

src/persister.rs [new file with mode: 0644]
src/wallet/mod.rs [deleted file]
src/wallet/persister.rs [deleted file]

diff --git a/src/persister.rs b/src/persister.rs
new file mode 100644 (file)
index 0000000..2a17db1
--- /dev/null
@@ -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<bdk_wallet::ChangeSet, Self::Error> {
+        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<P: WalletPersister>(
+    network: Network,
+    persister: &mut P,
+    wallet_opts: &WalletOpts,
+) -> Result<PersistedWallet<P>, 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<Wallet, Error> {
+    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 (file)
index 7ef2e65..0000000
+++ /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<P: WalletPersister>(
-    network: Network,
-    persister: &mut P,
-    wallet_opts: &WalletOpts,
-) -> Result<PersistedWallet<P>, 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<Wallet, Error> {
-    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 (file)
index 1f9a742..0000000
+++ /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<bdk_wallet::ChangeSet, Self::Error> {
-        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)
-            }
-        }
-    }
-}