@@ -605,19 +605,30 @@ static bool EncryptMasterKey(const SecureString& wallet_passphrase, const CKeyin
605605 return true ;
606606}
607607
608- bool CWallet::Unlock (const SecureString& strWalletPassphrase )
608+ static bool DecryptMasterKey (const SecureString& wallet_passphrase, const CMasterKey& master_key, CKeyingMaterial& plain_master_key )
609609{
610610 CCrypter crypter;
611+ if (!crypter.SetKeyFromPassphrase (wallet_passphrase, master_key.vchSalt , master_key.nDeriveIterations , master_key.nDerivationMethod )) {
612+ return false ;
613+ }
614+ if (!crypter.Decrypt (master_key.vchCryptedKey , plain_master_key)) {
615+ return false ;
616+ }
617+
618+ return true ;
619+ }
620+
621+ bool CWallet::Unlock (const SecureString& strWalletPassphrase)
622+ {
611623 CKeyingMaterial _vMasterKey;
612624
613625 {
614626 LOCK (cs_wallet);
615627 for (const MasterKeyMap::value_type& pMasterKey : mapMasterKeys)
616628 {
617- if (!crypter.SetKeyFromPassphrase (strWalletPassphrase, pMasterKey.second .vchSalt , pMasterKey.second .nDeriveIterations , pMasterKey.second .nDerivationMethod ))
618- return false ;
619- if (!crypter.Decrypt (pMasterKey.second .vchCryptedKey , _vMasterKey))
629+ if (!DecryptMasterKey (strWalletPassphrase, pMasterKey.second , _vMasterKey)) {
620630 continue ; // try another master key
631+ }
621632 if (Unlock (_vMasterKey)) {
622633 // Now that we've unlocked, upgrade the key metadata
623634 UpgradeKeyMetadata ();
@@ -638,14 +649,12 @@ bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase,
638649 LOCK2 (m_relock_mutex, cs_wallet);
639650 Lock ();
640651
641- CCrypter crypter;
642652 CKeyingMaterial _vMasterKey;
643653 for (MasterKeyMap::value_type& pMasterKey : mapMasterKeys)
644654 {
645- if (!crypter.SetKeyFromPassphrase (strOldWalletPassphrase, pMasterKey.second .vchSalt , pMasterKey.second .nDeriveIterations , pMasterKey.second .nDerivationMethod ))
646- return false ;
647- if (!crypter.Decrypt (pMasterKey.second .vchCryptedKey , _vMasterKey))
655+ if (!DecryptMasterKey (strOldWalletPassphrase, pMasterKey.second , _vMasterKey)) {
648656 return false ;
657+ }
649658 if (Unlock (_vMasterKey))
650659 {
651660 if (!EncryptMasterKey (strNewWalletPassphrase, _vMasterKey, pMasterKey.second )) {
0 commit comments