Skip to content

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

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

muawiya1 wants to merge 4 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 follows the approach agreed in the issue:

  1. Clarifies that wallet commands other than wallet config use the network saved in the configuration.
  2. Prevents wallet config --force from changing an existing wallet’s network, while still allowing updates to other settings.
  3. Adds a clearer mismatch error showing the wallet name, both networks and how to correct the configuration.

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. The new error explains which network value to restore in config.toml.
  • NetworkMismatch is gated behind either the sqlite or redb feature because only persisted wallets can hit it.
  • Updated CHANGELOG.md to cover these changes.

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

Wallet commands load the network saved in the wallet configuration,
so --network only applies to `wallet config` and to commands that do
not load a wallet, such as `key` and `descriptor`. Say so in the
--network help text.
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 network mismatch error.

- Reject a --force that would change an existing wallet network, and
  say which --network to pass instead
- Still allow --force to update other settings on the same network
- Add tests for both cases
A network mismatch between config.toml and the wallet database was
wrapped in a generic error that named neither the wallet nor the file
to fix.

- Add a `NetworkMismatch` error naming the wallet, the network in its
  database and the network in config.toml, and how to fix it
- Pass the wallet name to `new_persisted_wallet`
- Add a test for the new error

Fixes bitcoindevkit#359
@codecov

codecov Bot commented Nov 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.65217% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 59.69%. Comparing base (3dec0e0) to head (ee925f5).
⚠️ Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
src/persister.rs 88.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #377      +/-   ##
==========================================
- Coverage   60.00%   59.69%   -0.32%     
==========================================
  Files          25       25              
  Lines        4736     4573     -163     
==========================================
- Hits         2842     2730     -112     
+ Misses       1894     1843      -51     
Flag Coverage Δ
rust 59.69% <95.65%> (-0.32%) ⬇️

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.

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

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

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

2 participants