]> Untitled Git - bdk-cli/commitdiff
fix(handlers): take the network of wallet contexts from the wallet
authorMuawiya Abubakar <muawiyaabk@gmail.com>
Fri, 20 Nov 2026 14:22:43 +0000 (15:22 +0100)
committerMuawiya Abubakar <muawiyaabk@gmail.com>
Mon, 23 Nov 2026 02:24:59 +0000 (03:24 +0100)
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

src/handlers/mod.rs
src/handlers/repl.rs
src/main.rs

index 7d3d7f8cb3dda7dbffcc3be2a6d8ead0e95eb8b1..bf8be129a32b99aca0999fc6954950ceacd2b13f 100644 (file)
@@ -70,9 +70,9 @@ impl AppContext<Init> {
 }
 
 impl<'a> AppContext<OfflineOperations<'a>> {
-    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<OfflineOperations<'a>> {
 ))]
 impl<'a> AppContext<OnlineOperations<'a>> {
     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<C> {
 
     async fn execute(&self, ctx: &mut C) -> Result<Self::Output, Error>;
 }
+
+#[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);
+    }
+}
index b93916e9c896ef3717295526e9ee0fd158d18a86..53639403d9594b450dfae42a26ce629e527b607f 100644 (file)
@@ -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,
index 7ed73d466c869e8269e571191cd8fe8518489498..9d8ba3844fa08d5eaf9bd37a1637c819514161fe 100644 (file)
@@ -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?