What changed, and why it matters
This change fixes a bug in Monero's wallet recovery process. Previously, if wallet recovery from keys failed partway through, the wallet software could leave the existing wallet file in a broken or overwritten state. The fix marks such failures as critical and, based on the added test, ensures a failed recovery does not overwrite or corrupt an existing wallet file. For users, this means attempting to recover a wallet with wrong or mismatched keys is less likely to destroy an existing wallet.
Treat this as a bug-fix commit with security-adjacent data-integrity implications. Review the full recovery path to confirm that setStatusCritical() or associated logic actually prevents file overwrite/rollback, since the diff only changes status severity and adds a test. If the project maintains a security changelog, consider noting this as a wallet-recovery robustness improvement. No immediate emergency response is indicated by the commit alone.
Security signals we found
Change from error to critical status on wallet recovery failure
New regression test: failed key recovery must not overwrite existing wallet file
Potential data-loss/corruption scenario in wallet recovery code path
No explicit CVE or vendor security advisory referenced in commit
Evidence from the diff
The commit modifies WalletImpl::recoverFromKeysWithPassword() in src/wallet/api/wallet.cpp to call setStatusCritical() instead of setStatusError() when an exception occurs during wallet generation from keys. A new regression test verifies that invoking createWalletFromKeys() with the same path, address, view key, and spend key as an existing wallet results in Status_Critical and leaves the original wallet file byte-for-byte unchanged. The test also confirms the original wallet remains openable and retains its address. The change signals a critical failure path and adds test coverage, but the diff alone does not show the actual file-overwrite prevention logic; it only changes the status severity and adds a test.
Changed components
src/wallet/api/wallet.cppWalletImpl::recoverFromKeysWithPassword()wallet_api key-recovery failure handlingInspect captured patch +34 / −1
### src/wallet/api/wallet.cpp
@@ -687,7 +687,7 @@ bool WalletImpl::recoverFromKeysWithPassword(const std::string &path,
}
catch (const std::exception& e) {
- setStatusError(string(tr("failed to generate new wallet: ")) + e.what());
+ setStatusCritical(string(tr("failed to generate new wallet: ")) + e.what());
return false;
}
m_password = password;
### tests/libwallet_api_tests/main.cpp
@@ -46,6 +46,7 @@
#include <iostream>
#include <fstream>
+#include <iterator>
#include <cstring>
#include <vector>
#include <atomic>
@@ -478,6 +479,38 @@ TEST_F(WalletManagerTest, WalletManagerStoresPasswordOfWalletRecoveredFromKeys)
ASSERT_TRUE(wmgr->closeWallet(wallet3));
}
+TEST_F(WalletManagerTest, FailedKeyRecoveryDoesNotOverwriteExistingWallet)
+{
+ Monero::Wallet *wallet = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG, Monero::NetworkType::MAINNET);
+ ASSERT_EQ(wallet->status(), Monero::Wallet::Status_Ok);
+ const std::string address = wallet->mainAddress();
+ const std::string view_key = wallet->secretViewKey();
+ const std::string spend_key = wallet->secretSpendKey();
+ ASSERT_TRUE(wmgr->closeWallet(wallet));
+
+ std::ifstream before_file(WALLET_NAME, std::ios::binary);
+ ASSERT_TRUE(before_file.good());
+ const std::string before((std::istreambuf_iterator<char>(before_file)), std::istreambuf_iterator<char>());
+ before_file.close();
+ ASSERT_FALSE(before.empty());
+
+ wallet = wmgr->createWalletFromKeys(WALLET_NAME, WALLET_PASS, WALLET_LANG,
+ Monero::NetworkType::MAINNET, 0, address, view_key, spend_key);
+ ASSERT_EQ(wallet->status(), Monero::Wallet::Status_Critical);
+ ASSERT_TRUE(wmgr->closeWallet(wallet));
+
+ std::ifstream after_file(WALLET_NAME, std::ios::binary);
+ ASSERT_TRUE(after_file.good());
+ const std::string after((std::istreambuf_iterator<char>(after_file)), std::istreambuf_iterator<char>());
+ after_file.close();
+ EXPECT_TRUE(after == before);
+
+ wallet = wmgr->openWallet(WALLET_NAME, WALLET_PASS, Monero::NetworkType::MAINNET);
+ ASSERT_EQ(wallet->status(), Monero::Wallet::Status_Ok);
+ EXPECT_EQ(wallet->mainAddress(), address);
+ ASSERT_TRUE(wmgr->closeWallet(wallet));
+}
+
TEST_F(WalletManagerTest, WalletManagerStoresWallet1)
{Why this scored 59/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.