aboutsummaryrefslogtreecommitdiff
path: root/src/wallet
diff options
context:
space:
mode:
authorAndrew Chow <achow101-github@achow101.com>2022-04-13 15:34:23 -0400
committerAndrew Chow <github@achow101.com>2023-06-19 11:35:15 -0400
commit3ccde4599b5150577400c4fa9029f4146617f751 (patch)
tree59a3b42947725cea56dc50eef5d53f5f94d91586 /src/wallet
parent7be10adff36c0dc49ae56ac571bb033cba7a565b (diff)
walletdb: Refactor crypted key loading to its own function
Diffstat (limited to 'src/wallet')
-rw-r--r--src/wallet/walletdb.cpp67
-rw-r--r--src/wallet/walletdb.h1
2 files changed, 41 insertions, 27 deletions
diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp
index 3958211da8..95df773bca 100644
--- a/src/wallet/walletdb.cpp
+++ b/src/wallet/walletdb.cpp
@@ -386,6 +386,45 @@ bool LoadKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::stri
return true;
}
+bool LoadCryptedKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr)
+{
+ LOCK(pwallet->cs_wallet);
+ try {
+ CPubKey vchPubKey;
+ ssKey >> vchPubKey;
+ if (!vchPubKey.IsValid())
+ {
+ strErr = "Error reading wallet database: CPubKey corrupt";
+ return false;
+ }
+ std::vector<unsigned char> vchPrivKey;
+ ssValue >> vchPrivKey;
+
+ // Get the checksum and check it
+ bool checksum_valid = false;
+ if (!ssValue.eof()) {
+ uint256 checksum;
+ ssValue >> checksum;
+ if (!(checksum_valid = Hash(vchPrivKey) == checksum)) {
+ strErr = "Error reading wallet database: Encrypted key corrupt";
+ return false;
+ }
+ }
+
+ if (!pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadCryptedKey(vchPubKey, vchPrivKey, checksum_valid))
+ {
+ strErr = "Error reading wallet database: LegacyScriptPubKeyMan::LoadCryptedKey failed";
+ return false;
+ }
+ } catch (const std::exception& e) {
+ if (strErr.empty()) {
+ strErr = e.what();
+ }
+ return false;
+ }
+ return true;
+}
+
static bool
ReadKeyValue(CWallet* pwallet, DataStream& ssKey, CDataStream& ssValue,
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,
if (pwallet->nMasterKeyMaxID < nID)
pwallet->nMasterKeyMaxID = nID;
} else if (strType == DBKeys::CRYPTED_KEY) {
- CPubKey vchPubKey;
- ssKey >> vchPubKey;
- if (!vchPubKey.IsValid())
- {
- strErr = "Error reading wallet database: CPubKey corrupt";
- return false;
- }
- std::vector<unsigned char> vchPrivKey;
- ssValue >> vchPrivKey;
-
- // Get the checksum and check it
- bool checksum_valid = false;
- if (!ssValue.eof()) {
- uint256 checksum;
- ssValue >> checksum;
- if (!(checksum_valid = Hash(vchPrivKey) == checksum)) {
- strErr = "Error reading wallet database: Encrypted key corrupt";
- return false;
- }
- }
-
wss.nCKeys++;
-
- if (!pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadCryptedKey(vchPubKey, vchPrivKey, checksum_valid))
- {
- strErr = "Error reading wallet database: LegacyScriptPubKeyMan::LoadCryptedKey failed";
- return false;
- }
+ if (!LoadCryptedKey(pwallet, ssKey, ssValue, strErr)) return false;
wss.fIsEncrypted = true;
} else if (strType == DBKeys::KEYMETA) {
CPubKey vchPubKey;
diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h
index 4228d428fa..86b92e5f35 100644
--- a/src/wallet/walletdb.h
+++ b/src/wallet/walletdb.h
@@ -307,6 +307,7 @@ using KeyFilterFn = std::function<bool(const std::string&)>;
bool ReadKeyValue(CWallet* pwallet, DataStream& ssKey, CDataStream& ssValue, std::string& strType, std::string& strErr, const KeyFilterFn& filter_fn = nullptr);
bool LoadKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
+bool LoadCryptedKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
} // namespace wallet
#endif // BITCOIN_WALLET_WALLETDB_H