Skip to content

Commit

Permalink
Add generic bitcoin-cli commands
Browse files Browse the repository at this point in the history
This commit adds a special `external_subcommand` that can call any valid
bitcoin-cli rpc call on the node. Even if we don't have it covered in our
node command lists.
  • Loading branch information
rajarshimaitra committed Jul 25, 2022
1 parent c1921a7 commit 831f339
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ pub enum NodeSubCommand {
GetBalance,
/// Send to an external wallet address
SendToAddress { address: String, amount: u64 },
/// Execute any bitcoin-cli commands
#[structopt(external_subcommand)]
BitcoinCli(Vec<String>),
}

#[derive(Debug, StructOpt, Clone, PartialEq)]
Expand Down
5 changes: 4 additions & 1 deletion src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,10 @@ pub(crate) fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
#[cfg(feature = "regtest-node")]
ReplSubCommand::Node { subcommand } => {
let backend = new_backend(&home_dir)?;
backend.exec_cmd(subcommand)
match backend.exec_cmd(subcommand) {
Ok(result) => Ok(result),
Err(e) => Ok(serde_json::Value::String(e.to_string())),
}
}
#[cfg(any(
feature = "electrum",
Expand Down
12 changes: 12 additions & 0 deletions src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use {
Error,
},
electrsd::bitcoind::bitcoincore_rpc::{Client, RpcApi},
serde_json::Value,
std::str::FromStr,
};

Expand Down Expand Up @@ -95,6 +96,17 @@ impl Nodes {
.map_err(|e| Error::Generic(e.to_string()))?;
Ok(serde_json::to_value(&txid)?)
}

NodeSubCommand::BitcoinCli(args) => {
let cmd = &args[0];
let args = args[1..]
.iter()
.map(|arg| serde_json::Value::from_str(arg))
.collect::<Result<Vec<Value>, _>>()?;
client
.call::<Value>(cmd, &args)
.map_err(|e| Error::Generic(e.to_string()))
}
}
}

Expand Down

0 comments on commit 831f339

Please sign in to comment.