diff options
| author | mtg0001 <106211915+mtg0001@users.noreply.github.com> | 2026-08-01 21:43:45 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-08-01 21:43:45 -0300 |
| commit | 5a5478669c487963e99491112abc065c59973966 (patch) | |
| tree | e2e73a4e2283c5dae4fa342ec98cdf8d719017ca /src/walletcmd.cpp | |
| parent | fc071066c9de259795a210e449b364ac126416ad (diff) | |
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.
Diffstat (limited to 'src/walletcmd.cpp')
| -rw-r--r-- | src/walletcmd.cpp | 17 |
1 files changed, 14 insertions, 3 deletions
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, | |||
| 94 | if (!CanScanWalletTransactions(strErrorRet)) | 94 | if (!CanScanWalletTransactions(strErrorRet)) |
| 95 | return false; | 95 | return false; |
| 96 | 96 | ||
| 97 | size_t nWalletStart = 0; | ||
| 98 | CRITICAL_BLOCK(cs_mapWallet) | ||
| 99 | nWalletStart = mapWallet.size(); | ||
| 100 | |||
| 97 | if (!SetHDSeedFromMnemonic(strMnemonic, strErrorRet)) | 101 | if (!SetHDSeedFromMnemonic(strMnemonic, strErrorRet)) |
| 98 | return false; | 102 | return false; |
| 99 | 103 | ||
| 100 | // Derive forward in batches, scanning after each, until a whole batch turns | 104 | // Derive forward in batches, scanning after each, until a whole batch turns |
| 101 | // up nothing. Every derived key is written to the wallet before the scan, | 105 | // up nothing. Every derived key is written to the wallet before the scan, |
| 102 | // because the scan asks the wallet what belongs to it. | 106 | // because the scan asks the wallet what belongs to it. |
| 103 | int nTotalDerived = 0; | 107 | int nTotalDerived = (int)nHDNext; |
| 104 | while (nTotalDerived < RESTORE_MAX) | 108 | while (nTotalDerived < RESTORE_MAX) |
| 105 | { | 109 | { |
| 106 | // Count wallet transactions, not scan hits. | 110 | // Count wallet transactions, not scan hits. |
| @@ -134,7 +138,11 @@ bool RestoreFromPhrase(const std::string& strMnemonic, | |||
| 134 | nWalletAfter = mapWallet.size(); | 138 | nWalletAfter = mapWallet.size(); |
| 135 | 139 | ||
| 136 | if (fnProgress) | 140 | if (fnProgress) |
| 137 | fnProgress(pArg, nTotalDerived, (int)nWalletAfter); | 141 | { |
| 142 | size_t nRecoveredNow = nWalletAfter > nWalletStart ? | ||
| 143 | nWalletAfter - nWalletStart : 0; | ||
| 144 | fnProgress(pArg, nTotalDerived, (int)nRecoveredNow); | ||
| 145 | } | ||
| 138 | 146 | ||
| 139 | // A whole batch with nothing in it means far enough -- unless the | 147 | // A whole batch with nothing in it means far enough -- unless the |
| 140 | // caller asked to look deeper anyway. | 148 | // caller asked to look deeper anyway. |
| @@ -148,8 +156,11 @@ bool RestoreFromPhrase(const std::string& strMnemonic, | |||
| 148 | mapKeyPool.clear(); | 156 | mapKeyPool.clear(); |
| 149 | TopUpKeyPool(); | 157 | TopUpKeyPool(); |
| 150 | 158 | ||
| 159 | size_t nWalletEnd = 0; | ||
| 151 | CRITICAL_BLOCK(cs_mapWallet) | 160 | CRITICAL_BLOCK(cs_mapWallet) |
| 152 | nRecoveredRet = (int)mapWallet.size(); | 161 | nWalletEnd = mapWallet.size(); |
| 162 | nRecoveredRet = (int)(nWalletEnd > nWalletStart ? | ||
| 163 | nWalletEnd - nWalletStart : 0); | ||
| 153 | nDerivedRet = nTotalDerived; | 164 | nDerivedRet = nTotalDerived; |
| 154 | return true; | 165 | return true; |
| 155 | } | 166 | } |