]> Untitled Git - bdk-cli/commitdiff
refactor(runtime): Add address and createtx to db
authorVihiga Tyonum <withtvpeter@gmail.com>
Mon, 22 Jun 2026 15:07:28 +0000 (16:07 +0100)
committerVihiga Tyonum <withtvpeter@gmail.com>
Mon, 22 Jun 2026 21:46:32 +0000 (22:46 +0100)
- Add new address, unused address and createtx
commands to list of commands that require
loading existing db
- adding persisting changes in offline and online
commands

src/handlers/online.rs
src/handlers/repl.rs
src/main.rs
src/utils/common.rs
src/utils/runtime.rs

index adb6268e56582db2b527a15eb0dceab040d53c79..f4fc06277a259e7cb088eaa10902e164c2931149 100644 (file)
@@ -96,14 +96,14 @@ impl AsyncAppCommand<AppContext<OnlineOperations<'_>>> for FullScanCommand {
 
         #[cfg(any(feature = "electrum", feature = "esplora"))]
         let request = wallet.start_full_scan().inspect({
-            let mut stdout = std::io::stdout();
+            let mut stderr = std::io::stderr();
             let mut once = HashSet::<KeychainKind>::new();
             move |k, spk_i, _| {
                 if once.insert(k) {
-                    print!("\nScanning keychain [{k:?}]");
+                    eprint!("\nScanning keychain [{k:?}]");
                 }
-                print!(" {spk_i:<3}");
-                stdout.flush().expect("must flush");
+                eprint!(" {spk_i:<3}");
+                stderr.flush().expect("must flush");
             }
         });
 
index 977e819a9bb02c7dd8d522c2d87372395a5f5d87..73fbc418cb9e864770a48279af554df204af80c5 100644 (file)
@@ -98,7 +98,6 @@ pub(crate) async fn respond(
         }
 
         ReplSubCommand::Exit => None,
-        // _ => todo!(), // Handled specifically depending on your ReplSubCommand definitions
     };
 
     if response.is_some() {
index 527a3d965421966c5976eed09308afd58c517c01..3f399dc7394d1324edf8da798bc2ad764f06a4e4 100644 (file)
@@ -72,32 +72,35 @@ async fn run(cli_opts: CliOpts) -> Result<(), Error> {
             ))]
             WalletSubCommand::OnlineWalletSubCommand(cmd) => {
                 let runtime = WalletRuntime::load(&home_dir, &wallet_name)?;
-
                 let mut wallet = runtime.build_wallet(true)?;
                 let client = runtime.build_client(&wallet)?;
-
-                let mut ctx = AppContext::new_online_wallet(
-                    runtime.network,
-                    runtime.home_dir.clone(),
-                    &mut wallet,
-                    &client,
-                );
-
-                cmd.execute(&mut ctx).await?;
+                {
+                    let mut ctx = AppContext::new_online_wallet(
+                        runtime.network,
+                        runtime.home_dir.clone(),
+                        &mut wallet,
+                        &client,
+                    );
+
+                    cmd.execute(&mut ctx).await?;
+                }
+                wallet.persist()?;
             }
 
             WalletSubCommand::OfflineWalletSubCommand(cmd) => {
                 let runtime = WalletRuntime::load(&home_dir, &wallet_name)?;
-
                 let mut wallet = runtime.build_wallet(command_requires_db(&cmd))?;
 
-                let mut ctx = AppContext::new_offline_wallet(
-                    runtime.network,
-                    runtime.home_dir.clone(),
-                    &mut wallet,
-                );
+                {
+                    let mut ctx = AppContext::new_offline_wallet(
+                        runtime.network,
+                        runtime.home_dir.clone(),
+                        &mut wallet,
+                    );
 
-                cmd.execute(&mut ctx)?;
+                    cmd.execute(&mut ctx)?;
+                }
+                wallet.persist()?;
             }
 
             WalletSubCommand::Config(mut config_cmd) => {
index d696210b937b1f658320c88114dbb07d243f5a80..21be3ad487450fe2b352f66c4488086f26fd0c10 100644 (file)
@@ -224,12 +224,12 @@ pub fn command_requires_db(command: &OfflineWalletSubCommand) -> bool {
         OfflineWalletSubCommand::Balance(_)
         | OfflineWalletSubCommand::Unspent(_)
         | OfflineWalletSubCommand::Transactions(_)
-        | OfflineWalletSubCommand::BumpFee(_) => true,
-
-        OfflineWalletSubCommand::NewAddress(_)
+        | OfflineWalletSubCommand::BumpFee(_)
+        | OfflineWalletSubCommand::NewAddress(_)
         | OfflineWalletSubCommand::UnusedAddress(_)
-        | OfflineWalletSubCommand::CreateTx(_)
-        | OfflineWalletSubCommand::Policies(_)
+        | OfflineWalletSubCommand::CreateTx(_) => true,
+
+        OfflineWalletSubCommand::Policies(_)
         | OfflineWalletSubCommand::PublicDescriptor(_)
         | OfflineWalletSubCommand::Sign(_)
         | OfflineWalletSubCommand::ExtractPsbt(_)
index 2bf6ec198a8b2f96f2dac90365bb8160b62e0fcc..29dac4d97f27ff0772148ec6b490c8f9a8faaef2 100644 (file)
@@ -26,9 +26,9 @@ use {
 use crate::client::{BlockchainClient, new_blockchain_client};
 
 pub enum RuntimeWallet {
-    Standard(Wallet),
+    Standard(Box<Wallet>),
     #[cfg(any(feature = "sqlite", feature = "redb"))]
-    Persisted(PersistedWallet<Persister>),
+    Persisted(Box<PersistedWallet<Persister>>, Box<Persister>),
 }
 
 impl Deref for RuntimeWallet {
@@ -37,7 +37,7 @@ impl Deref for RuntimeWallet {
         match self {
             Self::Standard(wallet) => wallet,
             #[cfg(any(feature = "sqlite", feature = "redb"))]
-            Self::Persisted(wallet) => wallet,
+            Self::Persisted(wallet, _) => wallet,
         }
     }
 }
@@ -47,7 +47,20 @@ impl DerefMut for RuntimeWallet {
         match self {
             Self::Standard(wallet) => wallet,
             #[cfg(any(feature = "sqlite", feature = "redb"))]
-            Self::Persisted(wallet) => wallet,
+            Self::Persisted(wallet, _) => wallet,
+        }
+    }
+}
+
+impl RuntimeWallet {
+    pub fn persist(&mut self) -> Result<(), Error> {
+        match self {
+            Self::Standard(_) => Ok(()),
+            #[cfg(any(feature = "sqlite", feature = "redb"))]
+            Self::Persisted(wallet, persister) => {
+                wallet.persist(persister)?;
+                Ok(())
+            }
         }
     }
 }
@@ -78,25 +91,28 @@ impl WalletRuntime {
 
     pub fn build_wallet(&self, require_db: bool) -> Result<RuntimeWallet, Error> {
         if !require_db {
-            return Ok(RuntimeWallet::Standard(new_wallet(
+            return Ok(RuntimeWallet::Standard(Box::new(new_wallet(
                 self.network,
                 &self.wallet_opts,
-            )?));
+            )?)));
         }
 
         #[cfg(any(feature = "sqlite", feature = "redb"))]
         {
             let mut persister = self.create_persister()?;
             let wallet = new_persisted_wallet(self.network, &mut persister, &self.wallet_opts)?;
-            Ok(RuntimeWallet::Persisted(wallet))
+            Ok(RuntimeWallet::Persisted(
+                Box::new(wallet),
+                Box::new(persister),
+            ))
         }
 
         #[cfg(not(any(feature = "sqlite", feature = "redb")))]
         {
-            Ok(RuntimeWallet::Standard(new_wallet(
+            Ok(RuntimeWallet::Standard(Box::new(new_wallet(
                 self.network,
                 &self.wallet_opts,
-            )?))
+            )?)))
         }
     }