Skip to content

Commit c9a9ddb

Browse files
committed
Set fDecryptionThoroughlyChecked based on whether crypted key checksums are valid
Change fDecryptionThoroughlyChecked to default to true so that it can latch to false when an invalid checksum is seen. Checksums may be invalid if the wallet does not have checksums or if the wallet became corrupted. It is safe to default fDecryptionThoroughlyChecked to true because any existing wallet without a checksum will set it to false. Any new or blank wallet where encrypted keys are added will then set this to true when the first encrypted key is generated by virtue of CheckDecryptionKey doing that during the initial Unlock prior to keys being added.
1 parent a8334f7 commit c9a9ddb

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,13 @@ bool LegacyScriptPubKeyMan::AddKeyPubKeyInner(const CKey& key, const CPubKey &pu
643643
return true;
644644
}
645645

646-
bool LegacyScriptPubKeyMan::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
646+
bool LegacyScriptPubKeyMan::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret, bool checksum_valid)
647647
{
648+
// Set fDecryptionThoroughlyChecked to false when the checksum is invalid
649+
if (!checksum_valid) {
650+
fDecryptionThoroughlyChecked = false;
651+
}
652+
648653
return AddCryptedKeyInner(vchPubKey, vchCryptedSecret);
649654
}
650655

src/wallet/scriptpubkeyman.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
229229
{
230230
private:
231231
//! keeps track of whether Unlock has run a thorough check before
232-
bool fDecryptionThoroughlyChecked = false;
232+
bool fDecryptionThoroughlyChecked = true;
233233

234234
using WatchOnlySet = std::set<CScript>;
235235
using WatchKeyMap = std::map<CKeyID, CPubKey>;
@@ -365,7 +365,7 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
365365
//! Adds an encrypted key to the store, and saves it to disk.
366366
bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
367367
//! Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
368-
bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
368+
bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret, bool checksum_valid);
369369
void UpdateTimeFirstKey(int64_t nCreateTime) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore);
370370
//! Adds a CScript to the store
371371
bool LoadCScript(const CScript& redeemScript);

src/wallet/walletdb.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,18 +338,19 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
338338
ssValue >> vchPrivKey;
339339

340340
// Get the checksum and check it
341+
bool checksum_valid = false;
341342
if (!ssValue.eof()) {
342343
uint256 checksum;
343344
ssValue >> checksum;
344-
if (Hash(vchPrivKey.begin(), vchPrivKey.end()) != checksum) {
345+
if ((checksum_valid = Hash(vchPrivKey.begin(), vchPrivKey.end()) != checksum)) {
345346
strErr = "Error reading wallet database: Crypted key corrupt";
346347
return false;
347348
}
348349
}
349350

350351
wss.nCKeys++;
351352

352-
if (!pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadCryptedKey(vchPubKey, vchPrivKey))
353+
if (!pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadCryptedKey(vchPubKey, vchPrivKey, checksum_valid))
353354
{
354355
strErr = "Error reading wallet database: LegacyScriptPubKeyMan::LoadCryptedKey failed";
355356
return false;

0 commit comments

Comments
 (0)