Skip to content

fix(wallet): prevent wallet network changes and clarify mismatch errors - #377

Open
muawiya1 wants to merge 3 commits into
bitcoindevkit:masterfrom
muawiya1:network-mismatch-359
Open

muawiya1 wants to merge 3 commits into
bitcoindevkit:masterfrom
muawiya1:network-mismatch-359

Conversation

@muawiya1

@muawiya1 muawiya1 commented Nov 8, 2026 •

Copy link
Copy Markdown

Description

Wallet commands other than wallet config already use the network saved in config.toml. The mismatch happens when that network differs from the one in the wallet database.

One way to reproduce this is to run wallet config --force without the original --network option. When the default testnet network is used, it overwrites the saved network while the database keeps the original one.

This PR:

  1. Prevents wallet config --force from changing an existing wallet's network, while still allowing updates to other settings.
  2. Clarifies the wallet load error when the network in config.toml doesn't match the wallet database.
  3. Takes the network of wallet contexts from the loaded wallet, so wallet handlers such as create_dns_tx, unspent and wallet HWI always use the wallet's network.

Fixes #359

Notes to the reviewers

  • --network remains a top-level option. Moving it is left for separate work.
  • If wallet config --force uses the default testnet network for a wallet configured on another network, the command now fails instead of overwriting the saved network.
  • The guard checks the saved configuration, so --force cannot repair an existing network mismatch.
  • LoadWallet and its From impl are gated behind either the sqlite or redb feature because only persisted wallets load from a database.
  • Wallet contexts now set their network from wallet.network() in their constructors, so the handlers didn't need to change.
  • Added a CHANGELOG.md entry for the wallet config --force fix.

Checklists

All Submissions:

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

Bugfixes:

  • I've added tests to reproduce the issue which are now passing
  • I'm linking the issue being fixed by this PR

@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 76.66667% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.87%. Comparing base (e2811c0) to head (6e0df2b).
⚠️ Report is 16 commits behind head on master.

Files with missing lines Patch % Lines
src/error.rs 0.00% 6 Missing ⚠️
src/main.rs 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #377      +/-   ##
==========================================
+ Coverage   59.63%   59.87%   +0.23%     
==========================================
  Files          25       25              
  Lines        4544     4558      +14     
==========================================
+ Hits         2710     2729      +19     
+ Misses       1834     1829       -5     
Flag Coverage Δ
rust 59.87% <76.66%> (+0.23%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@emma31-dev emma31-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for this!

The config-level fix is solid. Blocking wallet config --force from changing an existing wallet's network and surfacing a mismatch error that names the wallet and both networks is a real improvement, and the tests cover it well.

One thing though: my issue extends beyond the config/database layer. For wallet-scoped commands, the network should come from the loaded wallet (ctx.state.wallet.network()), but in some handlers it is still read from the top-level --network via ctx.network. For example create_dns_tx in src/handlers/dns/mod.rs, unspent in src/handlers/offline.rs, and wallet-scoped HWI in src/handlers/hwi.rs. If the top-level value is configured differently from the wallet's network, those commands can fail or use the wrong network even when config and the database agree. It currently works only because main.rs/repl.rs happen to build AppContext with runtime.network; that invariant is implicit and easy to break again.

So this PR fixes the config-side reproduction, but the ctx.network vs wallet.network() issue is still open. A good follow-up would be to use the wallet's network in those handlers (or remove the duplicated network from wallet contexts) and add a regression test that runs a wallet command with a different top-level --network. But personally i think that will need a separate PR.

Thanks for working on this part of it.

@muawiya1

Copy link
Copy Markdown
Author

Thank you @emma31-dev for taking your time to take a look at my work, I hadn’t looked at that part because this PR was focused on the mismatch between config.toml and the wallet database, this was really a good catch. After checking the code, I believe you're right, because the wallet scope handlers read ctx.network instead of wallet.network(), and they match because main.rs and REPL passes the same runtime.network to both.

And I agree that this should be handled in a separate PR, I would be happy to work on it.

@emma31-dev

Copy link
Copy Markdown
Contributor

@muawiya1 Update PR description to say Related to #359 rather than fixes it. Then do an empty commit to rerun CI jobs. Thank you.

@muawiya1
muawiya1 force-pushed the network-mismatch-359 branch 2 times, most recently from 1ec6700 to 340a74d Compare November 16, 2026 22:46
@muawiya1

Copy link
Copy Markdown
Author

Thanks @emma31-dev, I’ve changed “Fixes #359” to “Related to #359” in both the PR description and the commit message, so merging won’t close the issue. I also rebased on master to resolve the CHANGELOG conflict and force-pushed, so CI should rerun from that push.

@emma31-dev emma31-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the update. The config-level fix itself looks good: wallet config --force can no longer change an existing wallet’s network, and the mismatch error now names the wallet, both networks, and how to fix config.toml.

I ran the config tests on the current head (340a74d):

cargo test --features default --test cli test_config

test result: ok. 8 passed; 0 failed

A few things to tidy up before this merges.

Could you rebase this down to one commit? The four commits (help text, force guard, mismatch error, changelog) are one logical change and read better squashed.

Thank you.

Comment thread tests/integration/init.rs Resolved
Comment thread CHANGELOG.md Resolved
@tvpeter tvpeter moved this from Todo to Ready to Review in BDK-CLI Nov 18, 2026
@muawiya1
muawiya1 force-pushed the network-mismatch-359 branch from 340a74d to 4f0f845 Compare November 18, 2026 20:39
@muawiya1

Copy link
Copy Markdown
Author

Thank you @emma31-dev for the review, I have made the requested changes.

@tvpeter tvpeter added the good first issue Good for newcomers label Nov 18, 2026
@tvpeter

tvpeter commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

So this PR fixes the config-side reproduction, but the ctx.network vs wallet.network() issue is still open. A good follow-up would be to use the wallet's network in those handlers (or remove the duplicated network from wallet contexts) and add a regression test that runs a wallet command with a different top-level --network. But personally i think that will need a separate PR.

@muawiya1 please include this fix as a commit. It is not large enough to be a separate PR.

@tvpeter tvpeter left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you @muawiya1 for working on this.

I have left some comments.

Comment thread src/commands.rs Resolved
Comment thread src/error.rs Resolved
Comment thread src/persister.rs Resolved
Comment thread tests/integration/init.rs Resolved

@tvpeter tvpeter left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

hi @muawiya1 ,

Thank you so much for continuing to work on this.

So for the comments on the error variant, since this is a binary app, we don't want to reinvent the wheel by defining new error types. We import them from the library and wrap it in the error enum and reuse it. So changing the error from Error: Generic error: Network mismatch: loaded regtest, expected signet to Wallet 'network_wallet' is configured for network 'regtest', and --force cannot change it to 'signet' improves users experience but the cost of doing that is not worth it (importing errors, matching the returned error and all).

Apologies for the multiple reviews.
Thank you.

Comment thread src/handlers/config.rs Resolved
Comment thread src/error.rs Resolved
Comment thread src/persister.rs Resolved
Comment thread tests/integration/init.rs Resolved
Running `wallet config --force` without the original `--network` saved
the default network (testnet) over the existing one. The config then no
longer matched the wallet database, and the wallet failed to load with
a generic error.

- Reject a `--force` that would change an existing wallet's network
- Clarify the wallet load error for a network mismatch

Fixes bitcoindevkit#359
Wallet handlers read the network from a separate argument instead of
the wallet. Take the network from `wallet.network()` and remove the
argument.

Fixes bitcoindevkit#359
@muawiya1
muawiya1 force-pushed the network-mismatch-359 branch from b823d0c to 5010229 Compare November 22, 2026 14:24
@muawiya1

Copy link
Copy Markdown
Author

Thank you @tvpeter for taking the time to review this and for the detailed feedback. I have made the changes.

@muawiya1
muawiya1 requested a review from tvpeter November 22, 2026 14:44

@tvpeter tvpeter left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you for continuing to work on this PR @muawiya1 .

I have left a suggestion for implementing LoadWallet error enum and fixing the resulting warning in new_persisted_wallet fn.

Comment thread src/error.rs Resolved
Comment thread src/persister.rs Resolved
Comment thread src/persister.rs Resolved
Co-authored-by: Peter Tyonum <28492640+tvpeter@users.noreply.github.com>
@tvpeter

tvpeter commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Please squash into two commits.

This branch has not been deployed

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

Labels

good first issue Good for newcomers

Projects

Status: Ready to Review

Development

Successfully merging this pull request may close these issues.

Make wallet network immutable after creation, or document and validate it explicitly

3 participants