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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changelog info is also documented on the [GitHub releases](https://github.com/bi
page. See [DEVELOPMENT_CYCLE.md](DEVELOPMENT_CYCLE.md) for more details.

## [Unreleased]
- Add wallet subcommand `config` to save wallet configs
- Add `wallets` command to list all wallets saved configs

## [2.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}}
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,44 @@ 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`.

## Saving and using wallet configurations

The `wallet config` sub-command allows you to save wallet settings to a `config.toml` file in the default directory (`~/.bdk-bitcoin/`) or custom directory specified with the `--datadir` flag. This eliminate the need to repeatedly specify descriptors, client types, and other parameters for each command. Once configured, you can use any wallet command by simply specifying the wallet name. All other parameters are automatically loaded from the saved configuration.

To save a wallet settings:

```shell
cargo run --features <list-of-features> -- -n <network> wallet --wallet <wallet_name> config [ -f ] --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>]
```

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 config -e "tr(tprv8Z.../0/*)#dtdqk3dx" -i "tr(tprv8Z.../1/*)#ulgptya7" -d sqlite -c electrum -u "ssl://mempool.space:60602"
```

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

#### Using a Configured Wallet

Once configured, use any wallet command with just the wallet name:


```shell
cargo run --features electrum wallet -w my_wallet new_address

cargo run --features electrum wallet -w my_wallet full_scan
```

Note that each wallet has its own configuration, allowing multiple wallets with different configurations.

#### View all saved Wallet Configs

To view all saved wallet configurations:

```shell
cargo run wallets`
```
You can also use the `--pretty` flag for a formatted output.
29 changes: 21 additions & 8 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
//! All subcommands are defined in the below enums.

#![allow(clippy::large_enum_variant)]

use bdk_wallet::bitcoin::{
Address, Network, OutPoint, ScriptBuf,
bip32::{DerivationPath, Xpriv},
Expand Down Expand Up @@ -70,8 +69,10 @@ pub enum CliSubCommand {
/// needs backend like `sync` and `broadcast`, compile the binary with specific backend feature
/// and use the configuration options below to configure for that backend.
Wallet {
#[command(flatten)]
wallet_opts: WalletOpts,
/// Selects the wallet to use.
#[arg(env = "WALLET_NAME", short = 'w', long = "wallet", required = true)]
wallet: String,

#[command(subcommand)]
subcommand: WalletSubCommand,
},
Expand Down Expand Up @@ -104,14 +105,26 @@ pub enum CliSubCommand {
/// REPL command loop can be used to make recurring callbacks to an already loaded wallet.
/// This mode is useful for hands on live testing of wallet operations.
Repl {
#[command(flatten)]
wallet_opts: WalletOpts,
/// Wallet name for this REPL session
#[arg(env = "WALLET_NAME", short = 'w', long = "wallet", required = true)]
wallet: String,
},
/// List all saved wallet configurations.
Wallets,
}

/// Wallet operation subcommands.
#[derive(Debug, Subcommand, Clone, PartialEq)]
pub enum WalletSubCommand {
/// Save wallet configuration to `config.toml`.
Config {
/// Overwrite existing wallet configuration if it exists.
#[arg(short = 'f', long = "force", default_value_t = false)]
force: bool,

#[command(flatten)]
wallet_opts: WalletOpts,
},
#[cfg(any(
feature = "electrum",
feature = "esplora",
Expand Down Expand Up @@ -156,14 +169,14 @@ pub enum ClientType {
#[derive(Debug, Args, Clone, PartialEq, Eq)]
pub struct WalletOpts {
/// Selects the wallet to use.
#[arg(env = "WALLET_NAME", short = 'w', long = "wallet")]
#[arg(skip)]
pub wallet: Option<String>,
/// Adds verbosity, returns PSBT in JSON format alongside serialized, displays expanded objects.
#[arg(env = "VERBOSE", short = 'v', long = "verbose")]
pub verbose: bool,
/// Sets the descriptor to use for the external addresses.
#[arg(env = "EXT_DESCRIPTOR", short = 'e', long)]
pub ext_descriptor: Option<String>,
#[arg(env = "EXT_DESCRIPTOR", short = 'e', long, required = true)]
pub ext_descriptor: String,
/// Sets the descriptor to use for internal/change addresses.
#[arg(env = "INT_DESCRIPTOR", short = 'i', long)]
pub int_descriptor: Option<String>,
Expand Down
Loading
Loading