Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/handlers/offline.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -38,7 +40,7 @@ use {
};

impl OfflineWalletSubCommand {
pub fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<(), Error> {
pub async fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> 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()),
Expand Down Expand Up @@ -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()),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 1 addition & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
}
Expand Down
74 changes: 74 additions & 0 deletions tests/integration/offline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down