From: Muawiya Abubakar Date: Fri, 20 Nov 2026 14:22:43 +0000 (+0100) Subject: fix(handlers): take the network of wallet contexts from the wallet X-Git-Url: http://internal-gitweb-vhost/-script/src/consensus/encode/scripts/struct.EncoderStringWriter.html?a=commitdiff_plain;h=4b93fe62f57d29c86e2be16b80c2b1e5a4a09ec7;p=bdk-cli fix(handlers): take the network of wallet contexts from the wallet Wallet handlers read the network from a separate argument instead of the wallet. Take the network from `wallet.network()` and remove the argument. Fixes #359 --- diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 7d3d7f8..bf8be12 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -70,9 +70,9 @@ impl AppContext { } impl<'a> AppContext> { - pub fn new_offline_wallet(network: Network, datadir: PathBuf, wallet: &'a mut Wallet) -> Self { + pub fn new_offline_wallet(datadir: PathBuf, wallet: &'a mut Wallet) -> Self { Self { - network, + network: wallet.network(), datadir, state: OfflineOperations { wallet }, } @@ -87,14 +87,13 @@ impl<'a> AppContext> { ))] impl<'a> AppContext> { pub fn new_online_wallet( - network: Network, datadir: PathBuf, wallet: &'a mut Wallet, client: &'a BlockchainClient, wallet_name: String, ) -> Self { Self { - network, + network: wallet.network(), datadir, state: OnlineOperations { wallet, @@ -124,3 +123,22 @@ pub trait AsyncAppCommand { async fn execute(&self, ctx: &mut C) -> Result; } + +#[cfg(test)] +mod tests { + use super::*; + + const DESCRIPTOR: &str = "wpkh(tpubD6NzVbkrYhZ4XgiXtGrdW5XDAPFCL9h7we1vwNCpn8tGbBcgfVYjXyhWo4E1xkh56hjod1RhGjxbaTLV3X4FyWuejifB9jusQ46QzG87VKp/0/*)"; + + #[test] + fn offline_wallet_context_uses_wallet_network() { + let mut wallet = Wallet::create_single(DESCRIPTOR) + .network(Network::Regtest) + .create_wallet_no_persist() + .unwrap(); + + let ctx = AppContext::new_offline_wallet(PathBuf::new(), &mut wallet); + + assert_eq!(ctx.network, Network::Regtest); + } +} diff --git a/src/handlers/repl.rs b/src/handlers/repl.rs index b93916e..5363940 100644 --- a/src/handlers/repl.rs +++ b/src/handlers/repl.rs @@ -54,7 +54,7 @@ pub(crate) async fn respond( let response = match repl_subcommand { ReplSubCommand::Wallet { subcommand } => match subcommand { WalletSubCommand::OfflineWalletSubCommand(cmd) => { - let mut ctx = AppContext::new_offline_wallet(network, datadir, wallet); + let mut ctx = AppContext::new_offline_wallet(datadir, wallet); cmd.execute(&mut ctx).map_err(|e| e.to_string())?; Some(()) } @@ -67,7 +67,6 @@ pub(crate) async fn respond( WalletSubCommand::OnlineWalletSubCommand(cmd) => { let client_ref = client.ok_or("Online commands require a client.".to_string())?; let mut ctx = AppContext::new_online_wallet( - network, datadir, wallet, client_ref, diff --git a/src/main.rs b/src/main.rs index 7ed73d4..9d8ba38 100644 --- a/src/main.rs +++ b/src/main.rs @@ -71,7 +71,6 @@ async fn run(cli_opts: CliOpts) -> Result<(), Error> { let client = runtime.build_client(&wallet)?; { let mut ctx = AppContext::new_online_wallet( - runtime.network, runtime.home_dir.clone(), &mut wallet, &client, @@ -88,11 +87,8 @@ async fn run(cli_opts: CliOpts) -> Result<(), Error> { 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.home_dir.clone(), &mut wallet); match cmd { #[cfg(feature = "dns_payment")] @@ -120,11 +116,7 @@ async fn run(cli_opts: CliOpts) -> Result<(), Error> { let runtime = WalletRuntime::load(&home_dir, &wallet_name)?; let mut wallet = runtime.build_wallet(false)?; - let mut ctx = AppContext::new_offline_wallet( - runtime.network, - runtime.home_dir.clone(), - &mut wallet, - ); + let mut ctx = AppContext::new_offline_wallet(runtime.home_dir.clone(), &mut wallet); cmd.run(&mut ctx, &wallet_name) .await?