Skip to content

Commit b3966e3

Browse files
committed
feat: add wallet init with Justfile
- update README by adding how to use the `just init` command [Issue: 192]
1 parent 0885c16 commit b3966e3

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

Justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ rpc command wallet=default_wallet:
102102
bitcoin-cli -datadir={{default_datadir}} -regtest -rpcwallet={{wallet}} -rpcuser={{rpc_user}} -rpcpassword={{rpc_password}} {{command}}
103103

104104
[group('wallet')]
105-
init wallet_name ext_descriptor int_descriptor client_type url database_type='sqlite' rpc_user='user' rpc_password='pass' force='false':
105+
init wallet_name ext_descriptor int_descriptor client_type url database_type='sqlite' rpc_user='user' rpc_password='password' force='false':
106106
mkdir -p {{default_datadir}}
107107
# Check if wallet configuration exists
108108
if [ "{{force}}" = "false" ] && grep -Fx "[wallets.{{wallet_name}}]" {{default_datadir}}/config.toml > /dev/null; then \

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,69 @@ Note: You can modify the `Justfile` to reflect your nodes' configuration values.
195195
cargo run --features rpc -- wallet -u "127.0.0.1:18443" -c rpc -a user:password balance
196196
```
197197

198+
### Initializing a Wallet with `just`
199+
200+
When using `bdk-cli`, repeatedly specifying parameter values for each command can be tedious. The `just` command allows you to initialize a wallet with configuration values once, saving them for reuse in subsequent `bdk-cli` commands.
201+
This eliminate the need to provide repetitive arguments. To set up a wallet with persistent configuration values, use the following just command:
202+
203+
```shell
204+
just init <wallet_name> <ext_descriptor> <int_descriptor> <database_type> <client_type> <url> <rpc_user> <rpc_password> [--force]
205+
```
206+
The arguments must be provided in the order shown above. Replace each placeholder with the corresponding value:
207+
208+
> * `wallet_name`: The unique name for your wallet (e.g., my_wallet)
209+
> * `ext_descriptor`: The external descriptor for generating receiving addresses (e.g., tr(tprv.../0/*)#checksum)
210+
> * `int_descriptor`: The internal descriptor for generating change addresses (e.g., tr(tprv.../1/*)#checksum)
211+
> * `database_type`: The database type for wallet persistence (e.g., sqlite). Defaults to `sqlite` if omitted
212+
> * `client_type`: The blockchain backend (e.g., electrum, esplora, rpc, cbf)
213+
> * `url`: The server URL for the blockchain backend (e.g., ssl://mempool.space:60602 for Electrum).
214+
> * `rpc_user`: The RPC username for rpc client type (e.g., user). Defaults to user if omitted.
215+
> * `rpc_password`: The RPC password for rpc client type (e.g., password). Defaults to password if omitted.
216+
> * `--force`: Optional. Overwrites existing configuration for the specified <wallet_name> if set. By default, `just init` fails if the wallet config values already exists.
217+
218+
#### Example
219+
220+
To initialize a wallet named `my_wallet` with `electrum` as the backend:
221+
222+
```shell
223+
just init my_wallet "tr(tprv8Z.../0/*)#dtdqk3dx" "tr(tprv8Z.../1/*)#ulgptya7" sqlite electrum "ssl://mempool.space:60602" user password
224+
```
225+
226+
To overwrite an existing wallet configuration:
227+
228+
```shell
229+
just init-wallet my_wallet "tr(tprv8Z.../0/*)#dtdqk3dx" "tr(tprv8Z.../1/*)#ulgptya7" sqlite electrum "ssl://mempool.space:60602" user password --force
230+
```
231+
232+
You can omit the following arguments to use their default values:
233+
234+
`database_type`: Defaults to sqlite.
235+
`rpc_user`: Defaults to user.
236+
`rpc_password`: Defaults to password.
237+
238+
For example, to initialize a wallet with default database_type, rpc_user, and rpc_password:
239+
240+
```shell
241+
just init-wallet my_wallet "tr(tprv8Z.../0/*)#dtdqk3dx" "tr(tprv8Z.../1/*)#ulgptya7" electrum "ssl://mempool.space:60602"
242+
```
243+
244+
#### Using Saved Configuration
245+
246+
After initializing a wallet with `just init`, the configuration is saved in `~/.bdk-bitcoin/config.toml`. You can then run `bdk-cli` wallet commands without specifying the parameters, referencing only the wallet name and network.
247+
248+
With the wallet `my_wallet` initialized, generate a new address and sync the wallet as follow:
249+
250+
```shell
251+
cargo run --features electrum,sqlite -- -n signet wallet -w my_wallet new_address
252+
253+
cargo run --features electrum,sqlite -- -n signet wallet -w my_wallet sync
254+
```
255+
256+
#### Notes:
257+
258+
* Each wallet has its own configuration, allowing multiple wallets with different settings (e.g., different descriptors or backends).
259+
* You can override saved configuration values for a single command by specifying them explicitly (e.g., `--client-type esplora` or `--url https://mempool.space/signet/api`).
260+
198261
## Minimum Supported Rust Version (MSRV)
199262

200263
This library should always compile with any valid combination of features on Rust **1.75.0**.

src/commands.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
1515
#![allow(clippy::large_enum_variant)]
1616

17-
use std::path::Path;
1817
use crate::config::WalletConfig;
1918
use crate::error::BDKCliError as Error;
2019
use bdk_wallet::bitcoin::{
2120
bip32::{DerivationPath, Xpriv},
2221
Address, Network, OutPoint, ScriptBuf,
2322
};
2423
use clap::{value_parser, Args, Parser, Subcommand, ValueEnum};
24+
use std::path::Path;
2525

2626
#[cfg(any(feature = "electrum", feature = "esplora", feature = "rpc"))]
2727
use crate::utils::parse_proxy_auth;
@@ -173,7 +173,13 @@ pub struct WalletOpts {
173173
#[arg(env = "CLIENT_TYPE", short = 'c', long, value_enum)]
174174
pub client_type: Option<ClientType>,
175175
#[cfg(feature = "sqlite")]
176-
#[arg(env = "DATABASE_TYPE", short = 'd', long, value_enum, default_value="sqlite")]
176+
#[arg(
177+
env = "DATABASE_TYPE",
178+
short = 'd',
179+
long,
180+
value_enum,
181+
default_value = "sqlite"
182+
)]
177183
pub database_type: Option<DatabaseType>,
178184
/// Sets the server url.
179185
#[cfg(any(feature = "electrum", feature = "esplora", feature = "rpc"))]

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub enum BDKCliError {
3535

3636
#[error("Hex conversion error: {0}")]
3737
HexToArrayError(#[from] bdk_wallet::bitcoin::hashes::hex::HexToArrayError),
38-
38+
3939
#[error("Key error: {0}")]
4040
KeyError(#[from] bdk_wallet::keys::KeyError),
4141

0 commit comments

Comments
 (0)