Repository navigation
fix(wallet): allow migrating legacy HD wallets with no stored accounts - #7798
PastaPastaPasta wants to merge 1 commit into
Conversation
`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>
|
Warning Review limit reachedYou'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. View limit detailsLimit details: You’ve used all 4 included reviews currently available. Review configuration: ⚙️ Run configuration
📒 Files selected for processing (2)
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. Comment |
|
✅ Final review complete — no blockers (commit 739949c) · triage: low |
thepastaclaw
left a comment
There was a problem hiding this comment.
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:
lowbygpt-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); agentphase1-reviewer - Phase 1 model:
glm-5.3-flash— zai quota: 5h 99% left, weekly 69% left; passed overgemini-3.8-flash-high(antigravity below 15% reserve: weekly 15% left, 5h 100% left) - Fresh verifier:
gpt-6.1-sol— final-verifier; agentsol-verifier - Phase 2 reviewers:
gpt-6.1-sol— general (completed, effort medium); agentphase2-reviewer,gpt-6.1-sol— dash-core-commit-history (completed, effort medium); agentphase2-reviewer
|
Super-seeded by #7803 |
Issue being fixed or feature implemented
migratewalletfails on a legacy HD wallet whose HD chain has no stored accounts. The error is:A legacy wallet can end up with zero accounts through the stock RPCs.
sethdseedwith an explicit seed writes the chain withAddHDChainSingle()before any account exists. ThenewHdChain.AddAccount()call that follows changes only the local copy after it has been written. Account 0 is created later, whenDeriveNewChildKey()derives the first key. Normally a keypool top-up does that right away. It does not happen whensethdseedis called withnewkeypool=falseand no key is derived before migration. The clearest case is an encrypted wallet.migratewalletreloads the wallet locked, so the top-up on load cannot derive anything. It then unlocks the wallet with the passphrase and callsMigrateToDescriptor(), which rejects the chain because of theCountAccounts() != 1guard.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 callsmigratewalletwith the passphrase:test/functional/test_runner.py wallet_migration.py, unfixed C++ with only the new test case added
Before migrating, the test checks that
getwalletinforeportshdaccountcount: 0, so the wallet really is in the zero-account state.Code path at c14104b:
src/wallet/rpc/wallet.cpp#L925-L932:sethdseedpersists the chain, then callsAddAccount()on the local copy only.src/wallet/scriptpubkeyman.cpp#L2000-L2004: theCountAccounts() != 1guard.src/wallet/hdchain.cpp#L187-L195:GetAccount()accepts an empty account map and returns zeroed counters.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
migratewalletis the supported path forward.What was done?
In
LegacyScriptPubKeyMan::MigrateToDescriptor(), the guard changes fromCountAccounts() != 1toCountAccounts() > 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_accountscase intest/functional/wallet_migration.pycovers the scenario above. After migration it checks that the wallet is a descriptor wallet and that its firstgetnewaddressis them/44'/1'/0'/0/0address 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
sethdseedand the-hdseedpath callAddAccount()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.pybefore the fix (new test case only): fails with the error shown above.test/functional/test_runner.py wallet_migration.py wallet_hd.py wallet_encryption.pywith the fix: all pass, in both--legacy-walletand--descriptorsvariants where applicable.test/lint/lint-whitespace.py,test/lint/lint-python.pyandcontrib/devtools/clang-format-diff.pyon 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:
🤖 Generated with Claude Code