Skip to content

Commit d67055e

Browse files
committed
Upgrade or rewrite encrypted key checksums
If fDecryptionThoroughlyChecked is false, after a key has been checked, write (or rewrite) its checksum. This serves to upgrade wallets and correct those which have the checksum corrupted but not the key.
1 parent c9a9ddb commit d67055e

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ bool LegacyScriptPubKeyMan::CheckDecryptionKey(const CKeyingMaterial& master_key
210210
bool keyPass = mapCryptedKeys.empty(); // Always pass when there are no encrypted keys
211211
bool keyFail = false;
212212
CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
213+
WalletBatch batch(m_storage.GetDatabase());
213214
for (; mi != mapCryptedKeys.end(); ++mi)
214215
{
215216
const CPubKey &vchPubKey = (*mi).second.first;
@@ -223,6 +224,10 @@ bool LegacyScriptPubKeyMan::CheckDecryptionKey(const CKeyingMaterial& master_key
223224
keyPass = true;
224225
if (fDecryptionThoroughlyChecked)
225226
break;
227+
else {
228+
// Rewrite these encrypted keys with checksums
229+
batch.WriteCryptedKey(vchPubKey, vchCryptedSecret, mapKeyMetadata[vchPubKey.GetID()]);
230+
}
226231
}
227232
if (keyPass && keyFail)
228233
{

src/wallet/walletdb.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,14 @@ bool WalletBatch::WriteCryptedKey(const CPubKey& vchPubKey,
114114

115115
const auto key = std::make_pair(DBKeys::CRYPTED_KEY, vchPubKey);
116116
if (!WriteIC(key, std::make_pair(vchCryptedSecret, checksum), false)) {
117-
return false;
117+
// It may already exist, so try writing just the checksum
118+
std::vector<unsigned char> val;
119+
if (!m_batch.Read(key, val)) {
120+
return false;
121+
}
122+
if (!WriteIC(key, std::make_pair(val, checksum), true)) {
123+
return false;
124+
}
118125
}
119126
EraseIC(std::make_pair(DBKeys::KEY, vchPubKey));
120127
return true;

0 commit comments

Comments
 (0)