Skip to content
Merged
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
5 changes: 5 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
69 changes: 68 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub(crate) enum BlockchainClient {
},

#[cfg(feature = "cbf")]
KyotoClient { client: LightClient },
KyotoClient { client: Box<LightClient> },
}

#[cfg(any(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -323,15 +325,15 @@ 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<LightClient>) -> Result<(), Error> {
let LightClient {
requester,
log_subscriber,
info_subscriber,
warning_subscriber,
mut update_subscriber,
node,
} = client;
} = *client;

let subscriber = tracing_subscriber::FmtSubscriber::new();
tracing::subscriber::set_global_default(subscriber)
Expand Down
14 changes: 10 additions & 4 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod test {
use std::process::Command;

/// Testing errors for integration tests
#[allow(dead_code)]
#[derive(Debug)]
enum IntTestError {
// IO error
Expand All @@ -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<serde_json::Value, IntTestError> {
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
Expand All @@ -56,6 +58,7 @@ mod test {

// Helper Function
// Transforms a json value to string
#[allow(dead_code)]
fn value_to_string(value: &Value) -> Result<String, IntTestError> {
match value {
Value::Bool(bool) => match bool {
Expand All @@ -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<String, IntTestError> {
let map = json
.as_object()
Expand All @@ -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,
Expand All @@ -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(),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
Loading