mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-09 19:37:27 -03:00
wallet: refactor: dedup master key decryption
This commit is contained in:
parent
de9b54780b
commit
ba9f582332
1 changed files with 17 additions and 8 deletions
|
@ -599,19 +599,30 @@ static bool EncryptMasterKey(const SecureString& wallet_passphrase, const CKeyin
|
|||
return true;
|
||||
}
|
||||
|
||||
bool CWallet::Unlock(const SecureString& strWalletPassphrase)
|
||||
static bool DecryptMasterKey(const SecureString& wallet_passphrase, const CMasterKey& master_key, CKeyingMaterial& plain_master_key)
|
||||
{
|
||||
CCrypter crypter;
|
||||
if (!crypter.SetKeyFromPassphrase(wallet_passphrase, master_key.vchSalt, master_key.nDeriveIterations, master_key.nDerivationMethod)) {
|
||||
return false;
|
||||
}
|
||||
if (!crypter.Decrypt(master_key.vchCryptedKey, plain_master_key)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CWallet::Unlock(const SecureString& strWalletPassphrase)
|
||||
{
|
||||
CKeyingMaterial _vMasterKey;
|
||||
|
||||
{
|
||||
LOCK(cs_wallet);
|
||||
for (const MasterKeyMap::value_type& pMasterKey : mapMasterKeys)
|
||||
{
|
||||
if(!crypter.SetKeyFromPassphrase(strWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
|
||||
return false;
|
||||
if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, _vMasterKey))
|
||||
if (!DecryptMasterKey(strWalletPassphrase, pMasterKey.second, _vMasterKey)) {
|
||||
continue; // try another master key
|
||||
}
|
||||
if (Unlock(_vMasterKey)) {
|
||||
// Now that we've unlocked, upgrade the key metadata
|
||||
UpgradeKeyMetadata();
|
||||
|
@ -632,14 +643,12 @@ bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase,
|
|||
LOCK2(m_relock_mutex, cs_wallet);
|
||||
Lock();
|
||||
|
||||
CCrypter crypter;
|
||||
CKeyingMaterial _vMasterKey;
|
||||
for (MasterKeyMap::value_type& pMasterKey : mapMasterKeys)
|
||||
{
|
||||
if(!crypter.SetKeyFromPassphrase(strOldWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
|
||||
return false;
|
||||
if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, _vMasterKey))
|
||||
if (!DecryptMasterKey(strOldWalletPassphrase, pMasterKey.second, _vMasterKey)) {
|
||||
return false;
|
||||
}
|
||||
if (Unlock(_vMasterKey))
|
||||
{
|
||||
if (!EncryptMasterKey(strNewWalletPassphrase, _vMasterKey, pMasterKey.second)) {
|
||||
|
|
Loading…
Reference in a new issue