Skip to content

Commit 3ccde45

Browse files
committed
walletdb: Refactor crypted key loading to its own function
1 parent 7be10ad commit 3ccde45

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

src/wallet/walletdb.cpp

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,45 @@ bool LoadKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::stri
386386
return true;
387387
}
388388

389+
bool LoadCryptedKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr)
390+
{
391+
LOCK(pwallet->cs_wallet);
392+
try {
393+
CPubKey vchPubKey;
394+
ssKey >> vchPubKey;
395+
if (!vchPubKey.IsValid())
396+
{
397+
strErr = "Error reading wallet database: CPubKey corrupt";
398+
return false;
399+
}
400+
std::vector<unsigned char> vchPrivKey;
401+
ssValue >> vchPrivKey;
402+
403+
// Get the checksum and check it
404+
bool checksum_valid = false;
405+
if (!ssValue.eof()) {
406+
uint256 checksum;
407+
ssValue >> checksum;
408+
if (!(checksum_valid = Hash(vchPrivKey) == checksum)) {
409+
strErr = "Error reading wallet database: Encrypted key corrupt";
410+
return false;
411+
}
412+
}
413+
414+
if (!pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadCryptedKey(vchPubKey, vchPrivKey, checksum_valid))
415+
{
416+
strErr = "Error reading wallet database: LegacyScriptPubKeyMan::LoadCryptedKey failed";
417+
return false;
418+
}
419+
} catch (const std::exception& e) {
420+
if (strErr.empty()) {
421+
strErr = e.what();
422+
}
423+
return false;
424+
}
425+
return true;
426+
}
427+
389428
static bool
390429
ReadKeyValue(CWallet* pwallet, DataStream& ssKey, CDataStream& ssValue,
391430
CWalletScanState &wss, std::string& strType, std::string& strErr, const KeyFilterFn& filter_fn = nullptr) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
@@ -493,34 +532,8 @@ ReadKeyValue(CWallet* pwallet, DataStream& ssKey, CDataStream& ssValue,
493532
if (pwallet->nMasterKeyMaxID < nID)
494533
pwallet->nMasterKeyMaxID = nID;
495534
} else if (strType == DBKeys::CRYPTED_KEY) {
496-
CPubKey vchPubKey;
497-
ssKey >> vchPubKey;
498-
if (!vchPubKey.IsValid())
499-
{
500-
strErr = "Error reading wallet database: CPubKey corrupt";
501-
return false;
502-
}
503-
std::vector<unsigned char> vchPrivKey;
504-
ssValue >> vchPrivKey;
505-
506-
// Get the checksum and check it
507-
bool checksum_valid = false;
508-
if (!ssValue.eof()) {
509-
uint256 checksum;
510-
ssValue >> checksum;
511-
if (!(checksum_valid = Hash(vchPrivKey) == checksum)) {
512-
strErr = "Error reading wallet database: Encrypted key corrupt";
513-
return false;
514-
}
515-
}
516-
517535
wss.nCKeys++;
518-
519-
if (!pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadCryptedKey(vchPubKey, vchPrivKey, checksum_valid))
520-
{
521-
strErr = "Error reading wallet database: LegacyScriptPubKeyMan::LoadCryptedKey failed";
522-
return false;
523-
}
536+
if (!LoadCryptedKey(pwallet, ssKey, ssValue, strErr)) return false;
524537
wss.fIsEncrypted = true;
525538
} else if (strType == DBKeys::KEYMETA) {
526539
CPubKey vchPubKey;

src/wallet/walletdb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ using KeyFilterFn = std::function<bool(const std::string&)>;
307307
bool ReadKeyValue(CWallet* pwallet, DataStream& ssKey, CDataStream& ssValue, std::string& strType, std::string& strErr, const KeyFilterFn& filter_fn = nullptr);
308308

309309
bool LoadKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
310+
bool LoadCryptedKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
310311
} // namespace wallet
311312

312313
#endif // BITCOIN_WALLET_WALLETDB_H

0 commit comments

Comments
 (0)