bdk-cli icon indicating copy to clipboard operation
bdk-cli copied to clipboard

Add saving wallet config with `bdk-cli wallet config`

Open tvpeter opened this issue 6 months ago • 12 comments

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

  • [x] 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>
  • [x] 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...
  • [x] Repl uses the same wallet configs: repl [-w <name>]
  • [x] command to list all saved wallet configs: wallets
  • [x] throw warnings if using mainnet and saving a private descriptor in a config.

All Submissions:

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

New Features:

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

tvpeter avatar Jun 18 '25 20:06 tvpeter

Pull Request Test Coverage Report for Build 19264362308

Details

  • 3 of 331 (0.91%) changed or added relevant lines in 3 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-1.9%) to 10.267%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/utils.rs 0 43 0.0%
src/config.rs 0 85 0.0%
src/handlers.rs 3 203 1.48%
<!-- Total: 3 331
Files with Coverage Reduction New Missed Lines %
src/handlers.rs 1 13.32%
<!-- Total: 1
Totals Coverage Status
Change from base Build 19253249526: -1.9%
Covered Lines: 173
Relevant Lines: 1685

💛 - Coveralls

coveralls avatar Jun 18 '25 20:06 coveralls

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 avatar Jun 24 '25 19:06 notmandatory

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 avatar Jun 24 '25 20:06 tvpeter

Alright, I will update.

@notmandatory I have updated the PR

tvpeter avatar Jun 27 '25 11:06 tvpeter

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.

thunderbiscuit avatar Sep 03 '25 15:09 thunderbiscuit

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 avatar Sep 03 '25 15:09 tvpeter

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.

notmandatory avatar Sep 06 '25 04:09 notmandatory

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.

tvpeter avatar Sep 06 '25 04:09 tvpeter

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.

notmandatory avatar Sep 06 '25 20:09 notmandatory

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 avatar Oct 04 '25 05:10 tvpeter

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.

notmandatory avatar Oct 04 '25 12:10 notmandatory

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.

tvpeter avatar Oct 27 '25 20:10 tvpeter