diff --git a/Justfile b/Justfile index c1c462ab..013c4076 100644 --- a/Justfile +++ b/Justfile @@ -95,3 +95,8 @@ send n address wallet=default_wallet: [group('rpc')] descriptors private wallet=default_wallet: bitcoin-cli -datadir={{default_datadir}} -regtest -rpcwallet={{wallet}} -rpcuser={{rpc_user}} -rpcpassword={{rpc_password}} listdescriptors {{private}} + +# run any bitcoin-cli rpc command +[group('rpc')] +rpc command wallet=default_wallet: + bitcoin-cli -datadir={{default_datadir}} -regtest -rpcwallet={{wallet}} -rpcuser={{rpc_user}} -rpcpassword={{rpc_password}} {{command}} \ No newline at end of file diff --git a/README.md b/README.md index 1c7d5f70..e5d87743 100644 --- a/README.md +++ b/README.md @@ -122,11 +122,78 @@ The below are some of the commands included: ``` shell just # list all available recipes -just start # start regtest bitcoind in default dir just test # test the project just build # build the project ``` +### Using `Justfile` to run `bitcoind` as a Client + +If you are testing `bdk-cli` in regtest mode and wants to use your `bitcoind` node as a blockchain client, the `Justfile` can help you to quickly do so. Below are the steps to use your `bitcoind` node in *regtest* mode with `bdk-cli`: + +Note: You can modify the `Justfile` to reflect your nodes' configuration values. These values are the default values used in `bdk-cli` + > * default wallet: The set default wallet name is `regtest_default_wallet` + > * default data directory: The set default data directory is `~/.bdk-bitcoin` + > * RPC username: The set RPC username is `user` + > * RPC password: The set RPC password is `password` + +#### Steps + +1. Start bitcoind + ```shell + just start + ``` + +2. Create or load a bitcoind wallet with default wallet name + + ```shell + just create + ``` + or + ```shell + just load + ``` + +3. Generate a bitcoind wallet address to send regtest bitcoins to. + + ```shell + just address + ``` + +4. Mine 101 blocks on regtest to bitcoind wallet address + ```shell + just generate 101 $(just address) + ``` + +5. Check the bitcoind wallet balance + ```shell + just balance + ``` + +6. Setup your `bdk-cli` wallet config and connect it to your regtest node to perform a `sync` + ```shell + export NETWORK=regtest + export EXT_DESCRIPTOR='wpkh(tprv8ZgxMBicQKsPdMzWj9KHvoExKJDqfZFuT5D8o9XVZ3wfyUcnPNPJKncq5df8kpDWnMxoKbGrpS44VawHG17ZSwTkdhEtVRzSYXd14vDYXKw/0/*)' + export INT_DESCRIPTOR='wpkh(tprv8ZgxMBicQKsPdMzWj9KHvoExKJDqfZFuT5D8o9XVZ3wfyUcnPNPJKncq5df8kpDWnMxoKbGrpS44VawHG17ZSwTkdhEtVRzSYXd14vDYXKw/1/*)' + export DATABASE_TYPE=sqlite + cargo run --features rpc -- wallet -u "127.0.0.1:18443" -c rpc -a user:password sync + ``` + +7. Generate an address from your `bdk-cli` wallet and fund it with 10 bitcoins from your bitcoind node's wallet + ```shell + export address=$(cargo run --features rpc -- wallet -u "127.0.0.1:18443" -c rpc -a user:password new_address | jq '.address') + just send 10 $address + ``` + +8. Mine 6 more blocks to the bitcoind wallet + ```shell + just generate 6 $(just address) + ``` + +9. You can `sync` your `bdk-cli` wallet now and the balance should reflect the regtest bitcoin you received + ```shell + cargo run --features rpc -- wallet -u "127.0.0.1:18443" -c rpc -a user:password sync + cargo run --features rpc -- wallet -u "127.0.0.1:18443" -c rpc -a user:password balance + ``` ## Minimum Supported Rust Version (MSRV) diff --git a/src/handlers.rs b/src/handlers.rs index 2d0c4bde..e3f26055 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -554,7 +554,7 @@ pub(crate) async fn handle_online_wallet_subcommand( mut warning_subscriber, update_subscriber: _, node, - } = client; + } = *client; let subscriber = tracing_subscriber::FmtSubscriber::new(); tracing::subscriber::set_global_default(subscriber) diff --git a/src/utils.rs b/src/utils.rs index c170cf8a..c72fd80e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -140,7 +140,7 @@ pub(crate) enum BlockchainClient { }, #[cfg(feature = "cbf")] - KyotoClient { client: LightClient }, + KyotoClient { client: Box }, } #[cfg(any( @@ -206,7 +206,9 @@ pub(crate) fn new_blockchain_client( .data_dir(&_datadir) .build_with_wallet(_wallet, scan_type)?; - BlockchainClient::KyotoClient { client } + BlockchainClient::KyotoClient { + client: Box::new(client), + } } }; Ok(client) @@ -323,7 +325,7 @@ pub async fn trace_logger( // Handle Kyoto Client sync #[cfg(feature = "cbf")] -pub async fn sync_kyoto_client(wallet: &mut Wallet, client: LightClient) -> Result<(), Error> { +pub async fn sync_kyoto_client(wallet: &mut Wallet, client: Box) -> Result<(), Error> { let LightClient { requester, log_subscriber, @@ -331,7 +333,7 @@ pub async fn sync_kyoto_client(wallet: &mut Wallet, client: LightClient) -> Resu warning_subscriber, mut update_subscriber, node, - } = client; + } = *client; let subscriber = tracing_subscriber::FmtSubscriber::new(); tracing::subscriber::set_global_default(subscriber) diff --git a/tests/integration.rs b/tests/integration.rs index fb35abea..31ffb698 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -20,6 +20,7 @@ mod test { use std::process::Command; /// Testing errors for integration tests + #[allow(dead_code)] #[derive(Debug)] enum IntTestError { // IO error @@ -38,11 +39,12 @@ mod test { // Helper function // Runs a system command with given args + #[allow(dead_code)] fn run_cmd_with_args(cmd: &str, args: &[&str]) -> Result { let output = Command::new(cmd).args(args).output().unwrap(); let mut value = output.stdout; let error = output.stderr; - if value.len() == 0 { + if value.is_empty() { return Err(IntTestError::CmdExec(String::from_utf8(error).unwrap())); } value.pop(); // remove `\n` at end @@ -56,6 +58,7 @@ mod test { // Helper Function // Transforms a json value to string + #[allow(dead_code)] fn value_to_string(value: &Value) -> Result { match value { Value::Bool(bool) => match bool { @@ -72,6 +75,7 @@ mod test { // Helper Function // Extracts value from a given json object and key + #[allow(dead_code)] fn get_value(json: &Value, key: &str) -> Result { let map = json .as_object() @@ -86,6 +90,7 @@ mod test { /// The bdk-cli command struct /// Use it to perform all bdk-cli operations + #[allow(dead_code)] #[derive(Debug)] struct BdkCli { target: String, @@ -108,10 +113,10 @@ mod test { let mut feat = "--features=".to_string(); for item in features { feat.push_str(item); - feat.push_str(","); + feat.push(','); } feat.pop(); // remove the last comma - let _build = Command::new("cargo").args(&["build", &feat]).output()?; + let _build = Command::new("cargo").args(["build", &feat]).output()?; let mut bdk_cli = Self { target: "./target/debug/bdk-cli".to_string(), @@ -159,7 +164,7 @@ mod test { wallet_args.push("-d"); wallet_args.push(self.recv_desc.as_ref().unwrap()); wallet_args.push("-c"); - wallet_args.push(&self.chang_desc.as_ref().unwrap()); + wallet_args.push(self.chang_desc.as_ref().unwrap()); for arg in args { wallet_args.push(arg); @@ -195,6 +200,7 @@ mod test { // Run A Basic wallet operation test, with given feature #[cfg(test)] + #[allow(dead_code)] fn basic_wallet_ops(feature: &str) { // Create a temporary directory for testing env let mut test_dir = std::env::current_dir().unwrap();