From 5a5478669c487963e99491112abc065c59973966 Mon Sep 17 00:00:00 2001 From: mtg0001 <106211915+mtg0001@users.noreply.github.com> Date: Sat, 1 Aug 2026 21:43:45 -0300 Subject: Keep new phrase addresses recoverable (#102) -newphrase installed a seed but left the wallet's visible address as the pre-seed random key and the old random pool intact, so the next address handed out -- a mining payout included -- could be one the twelve words cannot reproduce, while the window said a recovery phrase existed. Installing a phrase now clears the stale pool records (private keys stay in wallet.dat), reserves a derived default receiving key, tolerates already-stored derived keys so repeated installs do not stall the pool, and -restorephrase reports what it recovered rather than the wallet's total transaction count. Self-test covers installing a phrase over a pre-existing random pool. --- src/walletcmd.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src/walletcmd.cpp') diff --git a/src/walletcmd.cpp b/src/walletcmd.cpp index 94e9d72..8eb3592 100644 --- a/src/walletcmd.cpp +++ b/src/walletcmd.cpp @@ -94,13 +94,17 @@ bool RestoreFromPhrase(const std::string& strMnemonic, if (!CanScanWalletTransactions(strErrorRet)) return false; + size_t nWalletStart = 0; + CRITICAL_BLOCK(cs_mapWallet) + nWalletStart = mapWallet.size(); + if (!SetHDSeedFromMnemonic(strMnemonic, strErrorRet)) return false; // Derive forward in batches, scanning after each, until a whole batch turns // up nothing. Every derived key is written to the wallet before the scan, // because the scan asks the wallet what belongs to it. - int nTotalDerived = 0; + int nTotalDerived = (int)nHDNext; while (nTotalDerived < RESTORE_MAX) { // Count wallet transactions, not scan hits. @@ -134,7 +138,11 @@ bool RestoreFromPhrase(const std::string& strMnemonic, nWalletAfter = mapWallet.size(); if (fnProgress) - fnProgress(pArg, nTotalDerived, (int)nWalletAfter); + { + size_t nRecoveredNow = nWalletAfter > nWalletStart ? + nWalletAfter - nWalletStart : 0; + fnProgress(pArg, nTotalDerived, (int)nRecoveredNow); + } // A whole batch with nothing in it means far enough -- unless the // caller asked to look deeper anyway. @@ -148,8 +156,11 @@ bool RestoreFromPhrase(const std::string& strMnemonic, mapKeyPool.clear(); TopUpKeyPool(); + size_t nWalletEnd = 0; CRITICAL_BLOCK(cs_mapWallet) - nRecoveredRet = (int)mapWallet.size(); + nWalletEnd = mapWallet.size(); + nRecoveredRet = (int)(nWalletEnd > nWalletStart ? + nWalletEnd - nWalletStart : 0); nDerivedRet = nTotalDerived; return true; } -- cgit v1.2.3