Skip to content

Commit 72c2a54

Browse files
committed
walletdb: Refactor encryption key loading to its own function
1 parent 3ccde45 commit 72c2a54

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/wallet/walletdb.cpp

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,33 @@ bool LoadCryptedKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, st
425425
return true;
426426
}
427427

428+
bool LoadEncryptionKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr)
429+
{
430+
LOCK(pwallet->cs_wallet);
431+
try {
432+
// Master encryption key is loaded into only the wallet and not any of the ScriptPubKeyMans.
433+
unsigned int nID;
434+
ssKey >> nID;
435+
CMasterKey kMasterKey;
436+
ssValue >> kMasterKey;
437+
if(pwallet->mapMasterKeys.count(nID) != 0)
438+
{
439+
strErr = strprintf("Error reading wallet database: duplicate CMasterKey id %u", nID);
440+
return false;
441+
}
442+
pwallet->mapMasterKeys[nID] = kMasterKey;
443+
if (pwallet->nMasterKeyMaxID < nID)
444+
pwallet->nMasterKeyMaxID = nID;
445+
446+
} catch (const std::exception& e) {
447+
if (strErr.empty()) {
448+
strErr = e.what();
449+
}
450+
return false;
451+
}
452+
return true;
453+
}
454+
428455
static bool
429456
ReadKeyValue(CWallet* pwallet, DataStream& ssKey, CDataStream& ssValue,
430457
CWalletScanState &wss, std::string& strType, std::string& strErr, const KeyFilterFn& filter_fn = nullptr) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
@@ -518,19 +545,7 @@ ReadKeyValue(CWallet* pwallet, DataStream& ssKey, CDataStream& ssValue,
518545
wss.nKeys++;
519546
if (!LoadKey(pwallet, ssKey, ssValue, strErr)) return false;
520547
} else if (strType == DBKeys::MASTER_KEY) {
521-
// Master encryption key is loaded into only the wallet and not any of the ScriptPubKeyMans.
522-
unsigned int nID;
523-
ssKey >> nID;
524-
CMasterKey kMasterKey;
525-
ssValue >> kMasterKey;
526-
if(pwallet->mapMasterKeys.count(nID) != 0)
527-
{
528-
strErr = strprintf("Error reading wallet database: duplicate CMasterKey id %u", nID);
529-
return false;
530-
}
531-
pwallet->mapMasterKeys[nID] = kMasterKey;
532-
if (pwallet->nMasterKeyMaxID < nID)
533-
pwallet->nMasterKeyMaxID = nID;
548+
if (!LoadEncryptionKey(pwallet, ssKey, ssValue, strErr)) return false;
534549
} else if (strType == DBKeys::CRYPTED_KEY) {
535550
wss.nCKeys++;
536551
if (!LoadCryptedKey(pwallet, ssKey, ssValue, strErr)) return false;

src/wallet/walletdb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ bool ReadKeyValue(CWallet* pwallet, DataStream& ssKey, CDataStream& ssValue, std
308308

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

313314
#endif // BITCOIN_WALLET_WALLETDB_H

0 commit comments

Comments
 (0)