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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ page. See [DEVELOPMENT_CYCLE.md](DEVELOPMENT_CYCLE.md) for more details.
- Removed MSRV and bumped Rust Edition to 2024
- Add `--pretty` top level flag for formatting commands output in a tabular format
- Updated `bdk_wallet ` to `2.1.0`, `bdk_bitcoind_rpc` to `0.21.0`, `bdk_esplora` to `0.22.1`, `bdk_kyoto` to `0.13.1`

- Add wallet subcommand `init` to save wallet configs
- Add an optional flag `--use-config` to use saved configs for a wallet

## [1.0.0]

Expand Down
91 changes: 87 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ serde_json = "1.0"
thiserror = "2.0.11"
tokio = { version = "1", features = ["full"] }
cli-table = "0.5.0"
toml = "0.8.23"
serde= {version = "1.0", features = ["derive"]}

# Optional dependencies
bdk_bitcoind_rpc = { version = "0.21.0", features = ["std"], optional = true }
Expand Down
2 changes: 1 addition & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ descriptors private wallet=default_wallet:
# 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}}
bitcoin-cli -datadir={{default_datadir}} -regtest -rpcwallet={{wallet}} -rpcuser={{rpc_user}} -rpcpassword={{rpc_password}} {{command}}
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,42 @@ You can optionally return outputs of commands in human-readable, tabular format
cargo run --pretty -n signet wallet -w {wallet_name} -d sqlite balance
```
This is available for wallet, key, repl and compile features. When ommitted, outputs default to `JSON`.

## Initializing Wallet Configurations with `init` Subcommand

The `wallet init` sub-command simplifies wallet operations by saving configuration parameters to `config.toml` in the data directory (default `~/.bdk-bitcoin/config.toml`). This allows you to run subsequent `bdk-cli wallet` commands without repeatedly specifying configuration details, easing wallet operations.

To initialize a wallet configuration, use the following command structure:

```shell
cargo run --features <list-of-features> -- -n <network> wallet --wallet <wallet_name> --ext-descriptor <ext_descriptor> --int-descriptor <int_descriptor> --client-type <client_type> --url <server_url> [--database-type <database_type>] [--rpc-user <rpc_user>]
[--rpc-password <rpc_password>] init
```

For example, to initialize a wallet named `my_wallet` with `electrum` as the backend on `signet` network:

```shell
cargo run --features electrum -- -n signet wallet -w my_wallet -e "tr(tprv8Z.../0/*)#dtdqk3dx" -i "tr(tprv8Z.../1/*)#ulgptya7" -d sqlite -c electrum -u "ssl://mempool.space:60602" init
```

To overwrite an existing wallet configuration, use the `--force` flag after the `init` sub-command.

You can omit the following arguments to use their default values:

`network`: Defaults to `testnet`

`database_type`: Defaults to `sqlite`

#### Using Saved Configuration

After a wallet is initialized, you can then run `bdk-cli` wallet commands without specifying the parameters, referencing only the wallet subcommand.

For example, with the wallet `my_wallet` initialized, generate a new address and sync the wallet as follow:

```shell
cargo run wallet -w my_wallet --use-config new_address

cargo run --features electrum wallet -w my_wallet --use-config sync
```

Note that each wallet has its own configuration, allowing multiple wallets with different configurations.
59 changes: 59 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@

#![allow(clippy::large_enum_variant)]

use crate::config::WalletConfig;
use crate::error::BDKCliError as Error;
use bdk_wallet::bitcoin::{
Address, Network, OutPoint, ScriptBuf,
bip32::{DerivationPath, Xpriv},
};
use clap::{Args, Parser, Subcommand, ValueEnum, value_parser};
use std::path::Path;

#[cfg(any(feature = "electrum", feature = "esplora", feature = "rpc"))]
use crate::utils::parse_proxy_auth;
Expand Down Expand Up @@ -112,6 +115,12 @@ pub enum CliSubCommand {
/// Wallet operation subcommands.
#[derive(Debug, Subcommand, Clone, PartialEq)]
pub enum WalletSubCommand {
/// Initialize a wallet configuration and save to `config.toml`.
Init {
/// Overwrite existing wallet configuration if it exists.
#[arg(long = "force", default_value_t = false)]
force: bool,
},
#[cfg(any(
feature = "electrum",
feature = "esplora",
Expand Down Expand Up @@ -214,6 +223,56 @@ pub struct WalletOpts {
pub compactfilter_opts: CompactFilterOpts,
}

impl WalletOpts {
/// Merges optional configuration values from config.toml into the current WalletOpts.
pub fn load_config(&mut self, wallet_name: &str, datadir: &Path) -> Result<(), Error> {
if let Some(config) = WalletConfig::load(datadir)? {
if let Ok(config_opts) = config.get_wallet_opts(wallet_name) {
self.verbose = self.verbose || config_opts.verbose;
#[cfg(feature = "electrum")]
{
self.batch_size = if self.batch_size != 10 {
self.batch_size
} else {
config_opts.batch_size
};
}
#[cfg(feature = "esplora")]
{
self.parallel_requests = if self.parallel_requests != 5 {
self.parallel_requests
} else {
config_opts.parallel_requests
};
}
#[cfg(feature = "rpc")]
{
self.basic_auth = if self.basic_auth != ("user".into(), "password".into()) {
self.basic_auth.clone()
} else {
config_opts.basic_auth
};
self.cookie = self.cookie.take().or(config_opts.cookie);
}
#[cfg(feature = "cbf")]
{
if self.compactfilter_opts.conn_count == 2
&& config_opts.compactfilter_opts.conn_count != 2
{
self.compactfilter_opts.conn_count =
config_opts.compactfilter_opts.conn_count;
}
if self.compactfilter_opts.skip_blocks.is_none() {
self.compactfilter_opts.skip_blocks =
config_opts.compactfilter_opts.skip_blocks;
}
}
}
}
Ok(())
}
}

/// Options to configure a SOCKS5 proxy for a blockchain client connection.
#[cfg(any(feature = "electrum", feature = "esplora"))]
#[derive(Debug, Args, Clone, PartialEq, Eq)]
Expand Down
Loading
Loading