Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/walletdb.cpp
Show First 20 Lines • Show All 113 Lines • ▼ Show 20 Lines | bool WalletBatch::WriteKey(const CPubKey &vchPubKey, const CPrivKey &vchPrivKey, | ||||
} | } | ||||
// hash pubkey/privkey to accelerate wallet load | // hash pubkey/privkey to accelerate wallet load | ||||
std::vector<uint8_t> vchKey; | std::vector<uint8_t> vchKey; | ||||
vchKey.reserve(vchPubKey.size() + vchPrivKey.size()); | vchKey.reserve(vchPubKey.size() + vchPrivKey.size()); | ||||
vchKey.insert(vchKey.end(), vchPubKey.begin(), vchPubKey.end()); | vchKey.insert(vchKey.end(), vchPubKey.begin(), vchPubKey.end()); | ||||
vchKey.insert(vchKey.end(), vchPrivKey.begin(), vchPrivKey.end()); | vchKey.insert(vchKey.end(), vchPrivKey.begin(), vchPrivKey.end()); | ||||
return WriteIC( | return WriteIC(std::make_pair(DBKeys::KEY, vchPubKey), | ||||
std::make_pair(DBKeys::KEY, vchPubKey), | std::make_pair(vchPrivKey, Hash(vchKey)), false); | ||||
std::make_pair(vchPrivKey, Hash(vchKey.begin(), vchKey.end())), false); | |||||
} | } | ||||
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; | ||||
} | } | ||||
// Compute a checksum of the encrypted key | // Compute a checksum of the encrypted key | ||||
uint256 checksum = Hash(vchCryptedSecret.begin(), vchCryptedSecret.end()); | uint256 checksum = Hash(vchCryptedSecret); | ||||
const auto key = std::make_pair(DBKeys::CRYPTED_KEY, vchPubKey); | const auto key = std::make_pair(DBKeys::CRYPTED_KEY, vchPubKey); | ||||
if (!WriteIC(key, std::make_pair(vchCryptedSecret, checksum), false)) { | if (!WriteIC(key, std::make_pair(vchCryptedSecret, checksum), false)) { | ||||
// It may already exist, so try writing just the checksum | // It may already exist, so try writing just the checksum | ||||
std::vector<uint8_t> val; | std::vector<uint8_t> val; | ||||
if (!m_batch.Read(key, val)) { | if (!m_batch.Read(key, val)) { | ||||
return false; | return false; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 77 Lines • ▼ Show 20 Lines | bool WalletBatch::WriteDescriptorKey(const uint256 &desc_id, | ||||
// hash pubkey/privkey to accelerate wallet load | // hash pubkey/privkey to accelerate wallet load | ||||
std::vector<uint8_t> key; | std::vector<uint8_t> key; | ||||
key.reserve(pubkey.size() + privkey.size()); | key.reserve(pubkey.size() + privkey.size()); | ||||
key.insert(key.end(), pubkey.begin(), pubkey.end()); | key.insert(key.end(), pubkey.begin(), pubkey.end()); | ||||
key.insert(key.end(), privkey.begin(), privkey.end()); | key.insert(key.end(), privkey.begin(), privkey.end()); | ||||
return WriteIC(std::make_pair(DBKeys::WALLETDESCRIPTORKEY, | return WriteIC(std::make_pair(DBKeys::WALLETDESCRIPTORKEY, | ||||
std::make_pair(desc_id, pubkey)), | std::make_pair(desc_id, pubkey)), | ||||
std::make_pair(privkey, Hash(key.begin(), key.end())), | std::make_pair(privkey, Hash(key)), false); | ||||
false); | |||||
} | } | ||||
bool WalletBatch::WriteCryptedDescriptorKey( | bool WalletBatch::WriteCryptedDescriptorKey( | ||||
const uint256 &desc_id, const CPubKey &pubkey, | const uint256 &desc_id, const CPubKey &pubkey, | ||||
const std::vector<uint8_t> &secret) { | const std::vector<uint8_t> &secret) { | ||||
if (!WriteIC(std::make_pair(DBKeys::WALLETDESCRIPTORCKEY, | if (!WriteIC(std::make_pair(DBKeys::WALLETDESCRIPTORCKEY, | ||||
std::make_pair(desc_id, pubkey)), | std::make_pair(desc_id, pubkey)), | ||||
secret, false)) { | secret, false)) { | ||||
▲ Show 20 Lines • Show All 159 Lines • ▼ Show 20 Lines | try { | ||||
if (!hash.IsNull()) { | if (!hash.IsNull()) { | ||||
// hash pubkey/privkey to accelerate wallet load | // hash pubkey/privkey to accelerate wallet load | ||||
std::vector<uint8_t> vchKey; | std::vector<uint8_t> vchKey; | ||||
vchKey.reserve(vchPubKey.size() + pkey.size()); | vchKey.reserve(vchPubKey.size() + pkey.size()); | ||||
vchKey.insert(vchKey.end(), vchPubKey.begin(), vchPubKey.end()); | vchKey.insert(vchKey.end(), vchPubKey.begin(), vchPubKey.end()); | ||||
vchKey.insert(vchKey.end(), pkey.begin(), pkey.end()); | vchKey.insert(vchKey.end(), pkey.begin(), pkey.end()); | ||||
if (Hash(vchKey.begin(), vchKey.end()) != hash) { | if (Hash(vchKey) != hash) { | ||||
strErr = "Error reading wallet database: CPubKey/CPrivKey " | strErr = "Error reading wallet database: CPubKey/CPrivKey " | ||||
"corrupt"; | "corrupt"; | ||||
return false; | return false; | ||||
} | } | ||||
fSkipCheck = true; | fSkipCheck = true; | ||||
} | } | ||||
Show All 34 Lines | try { | ||||
std::vector<uint8_t> vchPrivKey; | std::vector<uint8_t> vchPrivKey; | ||||
ssValue >> vchPrivKey; | ssValue >> vchPrivKey; | ||||
// Get the checksum and check it | // Get the checksum and check it | ||||
bool checksum_valid = false; | bool checksum_valid = false; | ||||
if (!ssValue.eof()) { | if (!ssValue.eof()) { | ||||
uint256 checksum; | uint256 checksum; | ||||
ssValue >> checksum; | ssValue >> checksum; | ||||
if ((checksum_valid = Hash(vchPrivKey.begin(), | if ((checksum_valid = Hash(vchPrivKey) != checksum)) { | ||||
vchPrivKey.end()) != checksum)) { | |||||
strErr = | strErr = | ||||
"Error reading wallet database: Crypted key corrupt"; | "Error reading wallet database: Crypted key corrupt"; | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
wss.nCKeys++; | wss.nCKeys++; | ||||
▲ Show 20 Lines • Show All 148 Lines • ▼ Show 20 Lines | try { | ||||
ssValue >> hash; | ssValue >> hash; | ||||
// hash pubkey/privkey to accelerate wallet load | // hash pubkey/privkey to accelerate wallet load | ||||
std::vector<uint8_t> to_hash; | std::vector<uint8_t> to_hash; | ||||
to_hash.reserve(pubkey.size() + pkey.size()); | to_hash.reserve(pubkey.size() + pkey.size()); | ||||
to_hash.insert(to_hash.end(), pubkey.begin(), pubkey.end()); | to_hash.insert(to_hash.end(), pubkey.begin(), pubkey.end()); | ||||
to_hash.insert(to_hash.end(), pkey.begin(), pkey.end()); | to_hash.insert(to_hash.end(), pkey.begin(), pkey.end()); | ||||
if (Hash(to_hash.begin(), to_hash.end()) != hash) { | if (Hash(to_hash) != hash) { | ||||
strErr = | strErr = | ||||
"Error reading wallet database: CPubKey/CPrivKey corrupt"; | "Error reading wallet database: CPubKey/CPrivKey corrupt"; | ||||
return false; | return false; | ||||
} | } | ||||
if (!key.Load(pkey, pubkey, true)) { | if (!key.Load(pkey, pubkey, true)) { | ||||
strErr = "Error reading wallet database: CPrivKey corrupt"; | strErr = "Error reading wallet database: CPrivKey corrupt"; | ||||
return false; | return false; | ||||
▲ Show 20 Lines • Show All 403 Lines • Show Last 20 Lines |