Skip to content

Commit

Permalink
scripted-diff: wallet: rename plain and encrypted master key variables
Browse files Browse the repository at this point in the history
-BEGIN VERIFY SCRIPT-
sed -i s/_vMasterKey/plain_master_key/g ./src/wallet/wallet.cpp
sed -i s/kMasterKey/master_key/g ./src/wallet/wallet.cpp
sed -i "s/const MasterKeyMap::value_type& pMasterKey/const auto\& \[_, master_key\]/g" ./src/wallet/wallet.cpp
sed -i s/pMasterKey\.second/master_key/g ./src/wallet/wallet.cpp
sed -i "s/MasterKeyMap::value_type& pMasterKey/auto\& \[master_key_id, master_key\]/g" ./src/wallet/wallet.cpp
sed -i s/pMasterKey\.first/master_key_id/g ./src/wallet/wallet.cpp
-END VERIFY SCRIPT-
  • Loading branch information
theStack committed Dec 1, 2024
1 parent ba9f582 commit bda3a48
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,16 +614,16 @@ static bool DecryptMasterKey(const SecureString& wallet_passphrase, const CMaste

bool CWallet::Unlock(const SecureString& strWalletPassphrase)
{
CKeyingMaterial _vMasterKey;
CKeyingMaterial plain_master_key;

{
LOCK(cs_wallet);
for (const MasterKeyMap::value_type& pMasterKey : mapMasterKeys)
for (const auto& [_, master_key] : mapMasterKeys)
{
if (!DecryptMasterKey(strWalletPassphrase, pMasterKey.second, _vMasterKey)) {
if (!DecryptMasterKey(strWalletPassphrase, master_key, plain_master_key)) {
continue; // try another master key
}
if (Unlock(_vMasterKey)) {
if (Unlock(plain_master_key)) {
// Now that we've unlocked, upgrade the key metadata
UpgradeKeyMetadata();
// Now that we've unlocked, upgrade the descriptor cache
Expand All @@ -643,20 +643,20 @@ bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase,
LOCK2(m_relock_mutex, cs_wallet);
Lock();

CKeyingMaterial _vMasterKey;
for (MasterKeyMap::value_type& pMasterKey : mapMasterKeys)
CKeyingMaterial plain_master_key;
for (auto& [master_key_id, master_key] : mapMasterKeys)
{
if (!DecryptMasterKey(strOldWalletPassphrase, pMasterKey.second, _vMasterKey)) {
if (!DecryptMasterKey(strOldWalletPassphrase, master_key, plain_master_key)) {
return false;
}
if (Unlock(_vMasterKey))
if (Unlock(plain_master_key))
{
if (!EncryptMasterKey(strNewWalletPassphrase, _vMasterKey, pMasterKey.second)) {
if (!EncryptMasterKey(strNewWalletPassphrase, plain_master_key, master_key)) {
return false;
}
WalletLogPrintf("Wallet passphrase changed to an nDeriveIterations of %i\n", pMasterKey.second.nDeriveIterations);
WalletLogPrintf("Wallet passphrase changed to an nDeriveIterations of %i\n", master_key.nDeriveIterations);

WalletBatch(GetDatabase()).WriteMasterKey(pMasterKey.first, pMasterKey.second);
WalletBatch(GetDatabase()).WriteMasterKey(master_key_id, master_key);
if (fWasLocked)
Lock();
return true;
Expand Down Expand Up @@ -831,35 +831,35 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
if (IsCrypted())
return false;

CKeyingMaterial _vMasterKey;
CKeyingMaterial plain_master_key;

_vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE);
GetStrongRandBytes(_vMasterKey);
plain_master_key.resize(WALLET_CRYPTO_KEY_SIZE);
GetStrongRandBytes(plain_master_key);

CMasterKey kMasterKey;
CMasterKey master_key;

kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE);
GetStrongRandBytes(kMasterKey.vchSalt);
master_key.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE);
GetStrongRandBytes(master_key.vchSalt);

if (!EncryptMasterKey(strWalletPassphrase, _vMasterKey, kMasterKey)) {
if (!EncryptMasterKey(strWalletPassphrase, plain_master_key, master_key)) {
return false;
}
WalletLogPrintf("Encrypting Wallet with an nDeriveIterations of %i\n", kMasterKey.nDeriveIterations);
WalletLogPrintf("Encrypting Wallet with an nDeriveIterations of %i\n", master_key.nDeriveIterations);

{
LOCK2(m_relock_mutex, cs_wallet);
mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
mapMasterKeys[++nMasterKeyMaxID] = master_key;
WalletBatch* encrypted_batch = new WalletBatch(GetDatabase());
if (!encrypted_batch->TxnBegin()) {
delete encrypted_batch;
encrypted_batch = nullptr;
return false;
}
encrypted_batch->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
encrypted_batch->WriteMasterKey(nMasterKeyMaxID, master_key);

for (const auto& spk_man_pair : m_spk_managers) {
auto spk_man = spk_man_pair.second.get();
if (!spk_man->Encrypt(_vMasterKey, encrypted_batch)) {
if (!spk_man->Encrypt(plain_master_key, encrypted_batch)) {
encrypted_batch->TxnAbort();
delete encrypted_batch;
encrypted_batch = nullptr;
Expand Down

0 comments on commit bda3a48

Please sign in to comment.