Skip to content

fix(wallet): allow migrating legacy HD wallets with no stored accounts - #7798

Closed
PastaPastaPasta wants to merge 1 commit into
dashpay:developfrom
PastaPastaPasta:fix/allow-migrating-legacy-hd-wallets-with-no-stored
Closed

PastaPastaPasta wants to merge 1 commit into
dashpay:developfrom
PastaPastaPasta:fix/allow-migrating-legacy-hd-wallets-with-no-stored

Conversation

@PastaPastaPasta

Copy link
Copy Markdown
Member

Issue being fixed or feature implemented

migratewallet fails on a legacy HD wallet whose HD chain has no stored accounts. The error is:

MigrateToDescriptor: legacy HD chain has 0 accounts; migration only supports wallets with a single BIP44 account

A legacy wallet can end up with zero accounts through the stock RPCs. sethdseed with an explicit seed writes the chain with AddHDChainSingle() before any account exists. The newHdChain.AddAccount() call that follows changes only the local copy after it has been written. Account 0 is created later, when DeriveNewChildKey() derives the first key. Normally a keypool top-up does that right away. It does not happen when sethdseed is called with newkeypool=false and no key is derived before migration. The clearest case is an encrypted wallet. migratewallet reloads the wallet locked, so the top-up on load cannot derive anything. It then unlocks the wallet with the passphrase and calls MigrateToDescriptor(), which rejects the chain because of the CountAccounts() != 1 guard.

This guard is meant to reject chains with more than one account, which a stock build never creates. A chain with no accounts is already handled elsewhere: CHDChain::GetAccount() treats it as account 0 with zeroed counters ("A chain stored without accounts creates one on first use, with zeroed counters").

Why this is a real problem

The new functional test case runs against the unfixed tree (develop c14104b). It creates an encrypted blank legacy wallet, calls sethdseed false <wif>, locks the wallet, and calls migratewallet with the passphrase:

test/functional/test_runner.py wallet_migration.py, unfixed C++ with only the new test case added
$ test/functional/test_runner.py wallet_migration.py
...
TestFramework (INFO): Test migration of an encrypted HD wallet whose chain has no stored accounts
TestFramework (ERROR): JSONRPC error
Traceback (most recent call last):
  ...
  File ".../test/functional/wallet_migration.py", line 228, in test_hd_seed_no_accounts
    wallet.migratewallet(passphrase="pass")
  ...
test_framework.authproxy.JSONRPCException: MigrateToDescriptor: legacy HD chain has 0 accounts; migration only supports wallets with a single BIP44 account (-1)
...
wallet_migration.py | ✖ Failed  | 11 s

Before migrating, the test checks that getwalletinfo reports hdaccountcount: 0, so the wallet really is in the zero-account state.

Code path at c14104b:

Why it matters

The owner of such a wallet cannot migrate it to a descriptor wallet. Migration fails closed and no keys or funds are lost. The workaround (unlock the wallet and derive a key before migrating) is not obvious from the error, which suggests the wallet has an unsupported account layout. The case is narrow, but legacy wallets are on their way out and migratewallet is the supported path forward.

What was done?

In LegacyScriptPubKeyMan::MigrateToDescriptor(), the guard changes from CountAccounts() != 1 to CountAccounts() > 1, and the comment now explains the zero-account case. A chain with no stored accounts has not derived any key. GetAccount(0) returns zeroed counters for it, so migration builds the combo descriptors with range end 0 and creates the active descriptors from the seed, starting at index 0.

A new test_hd_seed_no_accounts case in test/functional/wallet_migration.py covers the scenario above. After migration it checks that the wallet is a descriptor wallet and that its first getnewaddress is the m/44'/1'/0'/0/0 address derived from the seed.

Why this is the correct minimal fix

The guard exists to stop wallets with several accounts from silently losing the higher accounts' keys. Zero accounts is not that case, and the rest of the migration path already handles it through GetAccount(0). Relaxing the condition is the smallest change that makes migration work for wallets that already have a zero-account chain on disk.

Making sethdseed and the -hdseed path call AddAccount() before writing the chain would stop new zero-account chains from appearing. It would not help wallets already in that state, so the guard would still have to change. It would also change wallet-creation behavior, which this PR does not need to do. That ordering is intentionally left unchanged here. Chains with more than one account are still rejected.

How Has This Been Tested?

macOS arm64 build (--enable-debug --enable-werror, depends prefix).

  • test/functional/test_runner.py wallet_migration.py before the fix (new test case only): fails with the error shown above.
  • The same command with the fix: passes.
  • test/functional/test_runner.py wallet_migration.py wallet_hd.py wallet_encryption.py with the fix: all pass, in both --legacy-wallet and --descriptors variants where applicable.
  • test/lint/lint-whitespace.py, test/lint/lint-python.py and contrib/devtools/clang-format-diff.py on the diff: clean.

This should also go to v24.0.x: the guard shipped in v24.0.0-rc.1 together with legacy HD wallet migration, and it is present on that branch.

Breaking Changes

None.

Checklist:

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have assigned this pull request to a milestone (for repository code-owners and collaborators only)

🤖 Generated with Claude Code

`sethdseed` with an explicit seed stores the HD chain before any account
is added, and account 0 is only created when the first key is derived.
A wallet that is migrated while still in that state (for example an
encrypted wallet that was seeded with `newkeypool=false` and is reloaded
locked by `migratewallet`) has a chain with zero accounts, and
`MigrateToDescriptor` rejected it with "legacy HD chain has 0 accounts".

`CHDChain::GetAccount(0)` already returns zeroed counters for a chain
with no stored accounts, so only reject chains with more than one
account, which is what the guard is meant to catch.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 4, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 25 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2457ee43-779c-4607-91cc-41ee86f3e44b
📥 Commits

Reviewing files that changed from the base of the PR and between 8819507 and 739949c.

📒 Files selected for processing (2)
  • src/wallet/scriptpubkeyman.cpp
  • test/functional/wallet_migration.py
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@thepastaclaw

thepastaclaw commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

✅ Final review complete — no blockers (commit 739949c) · triage: low

@thepastaclaw thepastaclaw 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.

Final validation — Phase 1 + Phase 2

The exact-head diff is a minimal, coherent wallet fix: accepting zero stored accounts matches GetAccount(0)'s existing zero-counter behavior, while wallets with multiple accounts remain rejected. The regression test establishes the triggering zero-account state and checks successful descriptor migration and the first seed-derived BIP44 address; no actionable in-scope defects were found. Verification was static only: the supplied CI snapshot shows ClangFormat passing, but build/test validation is not yet established and several checks remain queued.

Review provenance

Source: reviewer 1: glm-5.3-flash (agent: phase1-reviewer, role: general); reviewer 2: gpt-6.1-sol (agent: phase2-reviewer, role: general); reviewer 3: gpt-6.1-sol (agent: phase2-reviewer, role: dash-core-commit-history); final verifier: gpt-6.1-sol (agent: sol-verifier, role: final-verifier)

  • Triage: low by gpt-6.1-sol (effort low) — The diff makes a small, contained migration guard change to accept the existing zero-account fallback and adds a focused functional regression test.
  • Phase 1 reviewers: glm-5.3-flash — general (completed, effort high); agent phase1-reviewer
  • Phase 1 model: glm-5.3-flash — zai quota: 5h 99% left, weekly 69% left; passed over gemini-3.8-flash-high (antigravity below 15% reserve: weekly 15% left, 5h 100% left)
  • Fresh verifier: gpt-6.1-sol — final-verifier; agent sol-verifier
  • Phase 2 reviewers: gpt-6.1-sol — general (completed, effort medium); agent phase2-reviewer, gpt-6.1-sol — dash-core-commit-history (completed, effort medium); agent phase2-reviewer

@thepastaclaw thepastaclaw added the pastaclaw:approved thepastaclaw's latest review approved this PR label Oct 4, 2026
@knst

knst commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

Super-seeded by #7803

@thepastaclaw thepastaclaw removed the pastaclaw:approved thepastaclaw's latest review approved this PR label Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants