diff --git a/src/handlers/offline.rs b/src/handlers/offline.rs index 9aa15fcb..1c5bb5a0 100644 --- a/src/handlers/offline.rs +++ b/src/handlers/offline.rs @@ -1,5 +1,7 @@ use crate::commands::OfflineWalletSubCommand; use crate::error::BDKCliError as Error; +#[cfg(feature = "dns_payment")] +use crate::handlers::AsyncAppCommand; use crate::handlers::{AppCommand, AppContext, OfflineOperations}; use crate::utils::output::{FormatOutput, ListResult}; use crate::utils::types::{ @@ -38,7 +40,7 @@ use { }; impl OfflineWalletSubCommand { - pub fn execute(&self, ctx: &mut AppContext>) -> Result<(), Error> { + pub async fn execute(&self, ctx: &mut AppContext>) -> Result<(), Error> { match self { Self::NewAddress(new_address) => new_address.execute(ctx)?.write_out(std::io::stdout()), Self::Balance(balance) => balance.execute(ctx)?.write_out(std::io::stdout()), @@ -89,9 +91,10 @@ impl OfflineWalletSubCommand { locked_utxos.execute(ctx)?.write_out(std::io::stdout()) } #[cfg(feature = "dns_payment")] - Self::CreateDnsTx(_) => Err(Error::Generic( - "CreateDnsTx is dispatched asynchronously through main".to_string(), - )), + Self::CreateDnsTx(create_dns_tx) => create_dns_tx + .execute(ctx) + .await? + .write_out(std::io::stdout()), } } } diff --git a/src/handlers/repl.rs b/src/handlers/repl.rs index 53639403..f2819afe 100644 --- a/src/handlers/repl.rs +++ b/src/handlers/repl.rs @@ -55,7 +55,7 @@ pub(crate) async fn respond( ReplSubCommand::Wallet { subcommand } => match subcommand { WalletSubCommand::OfflineWalletSubCommand(cmd) => { let mut ctx = AppContext::new_offline_wallet(datadir, wallet); - cmd.execute(&mut ctx).map_err(|e| e.to_string())?; + cmd.execute(&mut ctx).await.map_err(|e| e.to_string())?; Some(()) } #[cfg(any( diff --git a/src/main.rs b/src/main.rs index 9d8ba384..31385ef9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -90,16 +90,7 @@ async fn run(cli_opts: CliOpts) -> Result<(), Error> { let mut ctx = AppContext::new_offline_wallet(runtime.home_dir.clone(), &mut wallet); - match cmd { - #[cfg(feature = "dns_payment")] - commands::OfflineWalletSubCommand::CreateDnsTx(dns_cmd) => { - dns_cmd - .execute(&mut ctx) - .await? - .write_out(std::io::stdout())?; - } - other => other.execute(&mut ctx)?, - } + cmd.execute(&mut ctx).await?; } wallet.persist()?; } diff --git a/tests/integration/offline.rs b/tests/integration/offline.rs index 772ff53f..c3d60287 100644 --- a/tests/integration/offline.rs +++ b/tests/integration/offline.rs @@ -468,6 +468,80 @@ mod repl_tests { } } +#[cfg(all(feature = "repl", feature = "dns_payment", feature = "sqlite"))] +mod repl_dns_tests { + use crate::common::BdkCli; + use serde_json::Value; + use tempfile::TempDir; + + const WALLET_NAME: &str = "repl_dns_test_wallet"; + /// A `--to` argument, the wallet is unfunded, so nothing is spent. + const RECIPIENT: &str = "bcrt1qw508d6qejxtdg4y5r3zarvary0c5xw7kygt080:1000"; + + /// `wallet config` requires `--client-type` (and `--url`, except for cbf) + /// when a backend feature is enabled. The server is never contacted. + fn backend_args() -> &'static [&'static str] { + if cfg!(feature = "electrum") { + &["--client-type", "electrum", "--url", "127.0.0.1:1"] + } else if cfg!(feature = "esplora") { + &["--client-type", "esplora", "--url", "http://127.0.0.1:1"] + } else if cfg!(feature = "rpc") { + &["--client-type", "rpc", "--url", "127.0.0.1:1"] + } else if cfg!(feature = "cbf") { + &["--client-type", "cbf"] + } else { + &[] + } + } + + #[test] + fn test_repl_create_dns_tx_is_dispatched_async() { + let temp = TempDir::new().unwrap(); + let cli = BdkCli::new("regtest", Some(temp.path().to_path_buf())); + + let desc = cli.cmd("descriptor", &["--type", "tr"]).output().unwrap(); + let v: Value = serde_json::from_slice(&desc.stdout).unwrap(); + let ext = v["private_descriptors"]["external"].as_str().unwrap(); + let int = v["private_descriptors"]["internal"].as_str().unwrap(); + + cli.build_base_cmd() + .args(["wallet", "--wallet", WALLET_NAME, "config"]) + .args(["--ext-descriptor", ext, "--int-descriptor", int]) + .args(["--database-type", "sqlite"]) + .args(backend_args()) + .assert() + .success(); + + let output = cli + .build_base_cmd() + .args(["repl", "--wallet", WALLET_NAME]) + .write_stdin(format!("wallet create_dns_tx --to {RECIPIENT}\nexit\n")) + .output() + .expect("failed to run repl"); + + let combined = format!( + "{}{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + + assert!( + combined.contains("Entering REPL mode"), + "REPL did not start:\n{combined}" + ); + // The command must reach the async handler, not the sync stub. + assert!( + !combined.contains("dispatched asynchronously"), + "create_dns_tx hit the sync stub in REPL mode:\n{combined}" + ); + // The wallet is unfunded, so the async handler returns some other error. + assert!( + combined.contains("Error:"), + "create_dns_tx did not return an error from the handler:\n{combined}" + ); + } +} + #[cfg(all( feature = "sqlite", not(any(