Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/walletdb.cpp
Show First 20 Lines • Show All 125 Lines • ▼ Show 20 Lines | |||||
bool WalletBatch::WriteCryptedKey(const CPubKey &vchPubKey, | bool WalletBatch::WriteCryptedKey(const CPubKey &vchPubKey, | ||||
const std::vector<uint8_t> &vchCryptedSecret, | const std::vector<uint8_t> &vchCryptedSecret, | ||||
const CKeyMetadata &keyMeta) { | const CKeyMetadata &keyMeta) { | ||||
if (!WriteKeyMetadata(keyMeta, vchPubKey, true)) { | if (!WriteKeyMetadata(keyMeta, vchPubKey, true)) { | ||||
return false; | return false; | ||||
} | } | ||||
if (!WriteIC(std::make_pair(DBKeys::CRYPTED_KEY, vchPubKey), | // Compute a checksum of the encrypted key | ||||
vchCryptedSecret, false)) { | uint256 checksum = Hash(vchCryptedSecret.begin(), vchCryptedSecret.end()); | ||||
const auto key = std::make_pair(DBKeys::CRYPTED_KEY, vchPubKey); | |||||
if (!WriteIC(key, std::make_pair(vchCryptedSecret, checksum), false)) { | |||||
// It may already exist, so try writing just the checksum | |||||
std::vector<uint8_t> val; | |||||
if (!m_batch.Read(key, val)) { | |||||
return false; | |||||
} | |||||
if (!WriteIC(key, std::make_pair(val, checksum), true)) { | |||||
return false; | return false; | ||||
} | } | ||||
} | |||||
EraseIC(std::make_pair(DBKeys::KEY, vchPubKey)); | EraseIC(std::make_pair(DBKeys::KEY, vchPubKey)); | ||||
return true; | return true; | ||||
} | } | ||||
bool WalletBatch::WriteMasterKey(unsigned int nID, | bool WalletBatch::WriteMasterKey(unsigned int nID, | ||||
const CMasterKey &kMasterKey) { | const CMasterKey &kMasterKey) { | ||||
return WriteIC(std::make_pair(DBKeys::MASTER_KEY, nID), kMasterKey, true); | return WriteIC(std::make_pair(DBKeys::MASTER_KEY, nID), kMasterKey, true); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 287 Lines • ▼ Show 20 Lines | try { | ||||
CPubKey vchPubKey; | CPubKey vchPubKey; | ||||
ssKey >> vchPubKey; | ssKey >> vchPubKey; | ||||
if (!vchPubKey.IsValid()) { | if (!vchPubKey.IsValid()) { | ||||
strErr = "Error reading wallet database: CPubKey corrupt"; | strErr = "Error reading wallet database: CPubKey corrupt"; | ||||
return false; | return false; | ||||
} | } | ||||
std::vector<uint8_t> vchPrivKey; | std::vector<uint8_t> vchPrivKey; | ||||
ssValue >> 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.begin(), | |||||
vchPrivKey.end()) != checksum)) { | |||||
strErr = | |||||
"Error reading wallet database: Crypted key corrupt"; | |||||
return false; | |||||
} | |||||
} | |||||
wss.nCKeys++; | wss.nCKeys++; | ||||
if (!pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadCryptedKey( | if (!pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadCryptedKey( | ||||
vchPubKey, vchPrivKey)) { | vchPubKey, vchPrivKey, checksum_valid)) { | ||||
strErr = "Error reading wallet database: " | strErr = "Error reading wallet database: " | ||||
"LegacyScriptPubKeyMan::LoadCryptedKey failed"; | "LegacyScriptPubKeyMan::LoadCryptedKey failed"; | ||||
return false; | return false; | ||||
} | } | ||||
wss.fIsEncrypted = true; | wss.fIsEncrypted = true; | ||||
} else if (strType == DBKeys::KEYMETA) { | } else if (strType == DBKeys::KEYMETA) { | ||||
CPubKey vchPubKey; | CPubKey vchPubKey; | ||||
ssKey >> vchPubKey; | ssKey >> vchPubKey; | ||||
▲ Show 20 Lines • Show All 558 Lines • Show Last 20 Lines |