From: Vihiga Tyonum Date: Mon, 22 Jun 2026 15:07:28 +0000 (+0100) Subject: refactor(runtime): Add address and createtx to db X-Git-Url: http://internal-gitweb-vhost/blockdata/script/encode/consensus/enum.GetKeyError.html?a=commitdiff_plain;h=188e68d147f7c548041c802dab2f7c425c3732b8;p=bdk-cli refactor(runtime): Add address and createtx to db - 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 --- diff --git a/src/handlers/online.rs b/src/handlers/online.rs index adb6268..f4fc062 100644 --- a/src/handlers/online.rs +++ b/src/handlers/online.rs @@ -96,14 +96,14 @@ impl AsyncAppCommand>> 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::::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"); } }); diff --git a/src/handlers/repl.rs b/src/handlers/repl.rs index 977e819..73fbc41 100644 --- a/src/handlers/repl.rs +++ b/src/handlers/repl.rs @@ -98,7 +98,6 @@ pub(crate) async fn respond( } ReplSubCommand::Exit => None, - // _ => todo!(), // Handled specifically depending on your ReplSubCommand definitions }; if response.is_some() { diff --git a/src/main.rs b/src/main.rs index 527a3d9..3f399dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) => { diff --git a/src/utils/common.rs b/src/utils/common.rs index d696210..21be3ad 100644 --- a/src/utils/common.rs +++ b/src/utils/common.rs @@ -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(_) diff --git a/src/utils/runtime.rs b/src/utils/runtime.rs index 2bf6ec1..29dac4d 100644 --- a/src/utils/runtime.rs +++ b/src/utils/runtime.rs @@ -26,9 +26,9 @@ use { use crate::client::{BlockchainClient, new_blockchain_client}; pub enum RuntimeWallet { - Standard(Wallet), + Standard(Box), #[cfg(any(feature = "sqlite", feature = "redb"))] - Persisted(PersistedWallet), + Persisted(Box>, Box), } 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 { 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, - )?)) + )?))) } }