Skip to content

Conversation

@tvpeter
Copy link
Collaborator

@tvpeter tvpeter commented Jun 18, 2025

Description

This PR adds bdk-cli wallet config command to save wallet configuration information to config.toml file in the data directory.

Fixes #192

Notes to the reviewers

  • Reusing the exported serde crate from bdk_wallet did not offer the derive feature

Changelog notice

  • Add wallet subcommand config to save wallet configs
  • Add top-level wallets command to show all saved wallet configs

Checklists

Features

  • command to save a wallet config, give wallet name or a default name is used, clap enforces required options; optional -f to override existing config: wallet [-f] [-w <name>] config <wallet opts>
  • All other wallet commands require that a config exists, use wallet name or the default is used. Give error if config is missing: wallet [-w <name>] sync | balance | new_address | etc...
  • Repl uses the same wallet configs: repl [-w <name>]
  • command to list all saved wallet configs: wallets
  • throw warnings if using mainnet and saving a private descriptor in a config.

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing

New Features:

  • I've added tests for the new feature
  • I've added docs for the new feature
  • I've updated CHANGELOG.md

@tvpeter tvpeter force-pushed the feat/init-wallet-support branch from 9e56084 to b3966e3 Compare June 18, 2025 20:41
@coveralls
Copy link

coveralls commented Jun 18, 2025

Pull Request Test Coverage Report for Build 18853801376

Details

  • 0 of 310 (0.0%) changed or added relevant lines in 3 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-1.5%) to 7.277%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/utils.rs 0 43 0.0%
src/config.rs 0 67 0.0%
src/handlers.rs 0 200 0.0%
Files with Coverage Reduction New Missed Lines %
src/handlers.rs 1 9.26%
Totals Coverage Status
Change from base Build 18520893551: -1.5%
Covered Lines: 105
Relevant Lines: 1443

💛 - Coveralls

@tvpeter tvpeter force-pushed the feat/init-wallet-support branch from b3966e3 to bf14df8 Compare June 19, 2025 04:32
@tvpeter tvpeter requested a review from notmandatory June 19, 2025 04:33
@notmandatory notmandatory moved this to Ready to Review in BDK-CLI Jun 24, 2025
@notmandatory notmandatory added this to the CLI 1.1.0 milestone Jun 24, 2025
@notmandatory
Copy link
Member

notmandatory commented Jun 24, 2025

I like the approach for loading the config file parameters if the file exists, but is there a reason you didn't add a CLI bdk-cli wallet init ... command in rust to create the config file rather than using just ?

Creating the config file in rust should be easier to maintain/keep in sync than your just approach and shouldn't take too much more than adding the Serialize trait to WalletConfigInner. You should be able to reuse the WalletOpts to capture the values with clap that you need to put in the config file too.

@notmandatory notmandatory added the enhancement New feature or request label Jun 24, 2025
@tvpeter
Copy link
Collaborator Author

tvpeter commented Jun 24, 2025

I like the approach for loading the config file parameters if the file exists, but is there a reason you didn't add a CLI bdk-cli wallet init ... command in rust to create the config file rather than using just ?

Creating the config file in rust should be easier to maintain/keep in sync than your just approach and shouldn't take too much more than adding the Serialize trait to WalletConfigInner. You should be able to reuse the WalletOpts to capture the values with clap that you need to put in the config file too.

Alright, I will update.

@tvpeter tvpeter force-pushed the feat/init-wallet-support branch from bf14df8 to 36ee973 Compare June 27, 2025 04:04
@tvpeter tvpeter changed the title Add initializing wallet configuration with Justfile Add initializing wallet configuration with bdk-cli wallet init Jun 27, 2025
@tvpeter tvpeter force-pushed the feat/init-wallet-support branch 4 times, most recently from 5125cf8 to fcf7ce8 Compare June 27, 2025 10:03
@tvpeter
Copy link
Collaborator Author

tvpeter commented Jun 27, 2025

Alright, I will update.

@notmandatory I have updated the PR

@tvpeter tvpeter force-pushed the feat/init-wallet-support branch 2 times, most recently from f77b1da to 70a5390 Compare August 25, 2025 19:26
@tvpeter tvpeter force-pushed the feat/init-wallet-support branch 2 times, most recently from 9461e85 to 5d9feb0 Compare September 1, 2025 04:20
@thunderbiscuit
Copy link
Member

I need to start keeping track of all these awesome new features I honestly see the PRs come in and can't keep up. This is super cool.

@tvpeter
Copy link
Collaborator Author

tvpeter commented Sep 3, 2025

I need to start keeping track of all these awesome new features I honestly see the PRs come in and can't keep up. This is super cool.

Thank you @thunderbiscuit

@tvpeter tvpeter force-pushed the feat/init-wallet-support branch 2 times, most recently from 9549e09 to 0f6b320 Compare September 3, 2025 15:45
@notmandatory
Copy link
Member

I spent some time today reviewing and even though this looks like a workable way to do it and is based on my suggestion I'm afraid it's going to be a hassle to maintain. I've been experimenting with somehow using the different clap parsing functions (https://docs.rs/clap/latest/clap/trait.Parser.html) but so far haven't figured out a better way. I'd like to keep thinking about it. We might have to simplify the problem somehow such as by not trying to merge loaded and CLI args.

@tvpeter
Copy link
Collaborator Author

tvpeter commented Sep 6, 2025

I'm afraid it's going to be a hassle to maintain. I've been experimenting with somehow using the different clap parsing functions (https://docs.rs/clap/latest/clap/trait.Parser.html) but so far haven't figured out a better way. I'd like to keep thinking about it. We might have to simplify the problem somehow such as by not trying to merge loaded and CLI args.

Thank you @notmandatory.
Yes, I agree that it will be challenging to maintain, as it involves two steps (pre clap parsing and after parsing) and even more difficult as they are in different locations in the codebase. I thought about moving everything to pre clap parsing based on the wallet sub-command the user has entered but that will clog the entry point as there will be lots of if...else checks.

Also, for the Parser trait, I read that it does not allow interacting with an external resource such as reading a file as I would want it.

I will also be trying out other approaches in case there is a cleaner way to handle it.

@notmandatory
Copy link
Member

notmandatory commented Sep 6, 2025

How about something like:

  1. One command to save a wallet config, give wallet name or a default name is used, clap enforces required options; optional -f to override existing config:
    wallet [-f] [-w <name>] config <wallet opts>
  2. All other wallet commands require that a config exists, use wallet name or the default is used. Give error if config is missing:
    wallet [-w <name>] sync | balance | new_address | etc...
  3. Repl uses the same wallet configs:
    repl [-w <name>]
  4. Add a command to list all available config names with summary of configured options:
    wallet list
  5. Should throw warnings if using mainnet and saving a private descriptor in a config.

@tvpeter
Copy link
Collaborator Author

tvpeter commented Oct 4, 2025

How about something like:

  1. One command to save a wallet config, give wallet name or a default name is used, clap enforces required options; optional -f to override existing config:
    wallet [-f] [-w <name>] config <wallet opts>
  2. All other wallet commands require that a config exists, use wallet name or the default is used. Give error if config is missing:
    wallet [-w <name>] sync | balance | new_address | etc...
  3. Repl uses the same wallet configs:
    repl [-w <name>]
  4. Add a command to list all available config names with summary of configured options:
    wallet list
  5. Should throw warnings if using mainnet and saving a private descriptor in a config.

Hi @notmandatory, there is an issues that we did not envisage and discuss on this PR that came up during implementation:
The wallet [-w <name>] config <wallet opts> command introduced a wallet name parameter that has to be supplied whenever the wallet command is used. This means that the list subcommand that should not require a wallet name has to accept the wallet name. If we make the wallet name optional above, we will have to do manual validation for the name.

@tvpeter tvpeter force-pushed the feat/init-wallet-support branch 2 times, most recently from 512b4b5 to ea0315c Compare October 4, 2025 09:18
@notmandatory
Copy link
Member

Good point. Yes I agree a new top level command makes sense to list the wallets. Either "wallets" as you suggest or "list" makes sense to me.

@tvpeter tvpeter changed the title Add initializing wallet configuration with bdk-cli wallet init Add initializing wallet configuration with bdk-cli wallet config Oct 4, 2025
@tvpeter tvpeter changed the title Add initializing wallet configuration with bdk-cli wallet config Add saving wallet config with bdk-cli wallet config Oct 4, 2025
@tvpeter tvpeter force-pushed the feat/init-wallet-support branch 3 times, most recently from 7cd062b to 28796ca Compare October 5, 2025 03:50
- add config.rs to store and retrieve
values
- add toml and serde crates for desearilizing and
reading values
- update utils, commands and handlers files
to use values from config.toml
-refactor prepare_wallet_db fn
- fix clippy issues

[Issue: bitcoindevkit#192]
- rename init to config and move walletopts as
config options
@tvpeter tvpeter force-pushed the feat/init-wallet-support branch 2 times, most recently from 77864b4 to c5c4fdb Compare October 15, 2025 13:17
Copy link
Member

@notmandatory notmandatory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found an error when testing with testnet4, and a couple small nits should be fixed too. Otherwise this looks good.

In the future if everyone prefers the config approach it should probably become the only way to use the wallet features.

README.md Outdated


```shell
cargo run --features electrum wallet -w my_wallet new_address
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛏️ nit, has extra space:

Suggested change
cargo run --features electrum wallet -w my_wallet new_address
cargo run --features electrum wallet -w my_wallet new_address

src/commands.rs Outdated
#[arg(env = "WALLET_NAME", short = 'w', long = "wallet")]
#[arg(skip)]
pub wallet: Option<String>,
// #[arg(env = "WALLET_NAME", short = 'w', long = "wallet", required = true)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented out line should be removed.

Suggested change
// #[arg(env = "WALLET_NAME", short = 'w', long = "wallet", required = true)]

"bitcoin" => Ok(Network::Bitcoin),
"testnet" => Ok(Network::Testnet),
"regtest" => Ok(Network::Regtest),
"signet" => Ok(Network::Signet),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔨 need to add Testnet4:

Suggested change
"signet" => Ok(Network::Signet),
"signet" => Ok(Network::Signet),
"testnet4" => Ok(Network::Testnet4),

- add wallets command
- add warning for using priv descriptors
- update readme
- add loading network from config
- fix review comments
@tvpeter tvpeter force-pushed the feat/init-wallet-support branch from c5c4fdb to 7be9a30 Compare October 27, 2025 19:41
@tvpeter
Copy link
Collaborator Author

tvpeter commented Oct 27, 2025

I found an error when testing with testnet4, and a couple small nits should be fixed too. Otherwise this looks good.

Thank you for catching all these.
I have updated.

In the future if everyone prefers the config approach it should probably become the only way to use the wallet features.

Yes, I agree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: Ready to Review

Development

Successfully merging this pull request may close these issues.

Add "init" wallet support

4 participants