Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/wallet.cpp
Show First 20 Lines • Show All 377 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
bool CWallet::LoadWatchOnly(const CScript &dest) { | bool CWallet::LoadWatchOnly(const CScript &dest) { | ||||
return CCryptoKeyStore::AddWatchOnly(dest); | return CCryptoKeyStore::AddWatchOnly(dest); | ||||
} | } | ||||
bool CWallet::Unlock(const SecureString &strWalletPassphrase) { | bool CWallet::Unlock(const SecureString &strWalletPassphrase) { | ||||
CCrypter crypter; | CCrypter crypter; | ||||
CKeyingMaterial vMasterKey; | CKeyingMaterial _vMasterKey; | ||||
LOCK(cs_wallet); | LOCK(cs_wallet); | ||||
for (const MasterKeyMap::value_type &pMasterKey : mapMasterKeys) { | for (const MasterKeyMap::value_type &pMasterKey : mapMasterKeys) { | ||||
if (!crypter.SetKeyFromPassphrase( | if (!crypter.SetKeyFromPassphrase( | ||||
strWalletPassphrase, pMasterKey.second.vchSalt, | strWalletPassphrase, pMasterKey.second.vchSalt, | ||||
pMasterKey.second.nDeriveIterations, | pMasterKey.second.nDeriveIterations, | ||||
pMasterKey.second.nDerivationMethod)) { | pMasterKey.second.nDerivationMethod)) { | ||||
return false; | return false; | ||||
} | } | ||||
if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey)) { | if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, _vMasterKey)) { | ||||
// try another master key | // try another master key | ||||
continue; | continue; | ||||
} | } | ||||
if (CCryptoKeyStore::Unlock(vMasterKey)) { | if (CCryptoKeyStore::Unlock(_vMasterKey)) { | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
bool CWallet::ChangeWalletPassphrase( | bool CWallet::ChangeWalletPassphrase( | ||||
const SecureString &strOldWalletPassphrase, | const SecureString &strOldWalletPassphrase, | ||||
const SecureString &strNewWalletPassphrase) { | const SecureString &strNewWalletPassphrase) { | ||||
bool fWasLocked = IsLocked(); | bool fWasLocked = IsLocked(); | ||||
LOCK(cs_wallet); | LOCK(cs_wallet); | ||||
Lock(); | Lock(); | ||||
CCrypter crypter; | CCrypter crypter; | ||||
CKeyingMaterial vMasterKey; | CKeyingMaterial _vMasterKey; | ||||
for (MasterKeyMap::value_type &pMasterKey : mapMasterKeys) { | for (MasterKeyMap::value_type &pMasterKey : mapMasterKeys) { | ||||
if (!crypter.SetKeyFromPassphrase( | if (!crypter.SetKeyFromPassphrase( | ||||
strOldWalletPassphrase, pMasterKey.second.vchSalt, | strOldWalletPassphrase, pMasterKey.second.vchSalt, | ||||
pMasterKey.second.nDeriveIterations, | pMasterKey.second.nDeriveIterations, | ||||
pMasterKey.second.nDerivationMethod)) { | pMasterKey.second.nDerivationMethod)) { | ||||
return false; | return false; | ||||
} | } | ||||
if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey)) { | if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, _vMasterKey)) { | ||||
return false; | return false; | ||||
} | } | ||||
if (CCryptoKeyStore::Unlock(vMasterKey)) { | if (CCryptoKeyStore::Unlock(_vMasterKey)) { | ||||
int64_t nStartTime = GetTimeMillis(); | int64_t nStartTime = GetTimeMillis(); | ||||
crypter.SetKeyFromPassphrase(strNewWalletPassphrase, | crypter.SetKeyFromPassphrase(strNewWalletPassphrase, | ||||
pMasterKey.second.vchSalt, | pMasterKey.second.vchSalt, | ||||
pMasterKey.second.nDeriveIterations, | pMasterKey.second.nDeriveIterations, | ||||
pMasterKey.second.nDerivationMethod); | pMasterKey.second.nDerivationMethod); | ||||
pMasterKey.second.nDeriveIterations = | pMasterKey.second.nDeriveIterations = | ||||
pMasterKey.second.nDeriveIterations * | pMasterKey.second.nDeriveIterations * | ||||
(100 / ((double)(GetTimeMillis() - nStartTime))); | (100 / ((double)(GetTimeMillis() - nStartTime))); | ||||
Show All 19 Lines | for (MasterKeyMap::value_type &pMasterKey : mapMasterKeys) { | ||||
if (!crypter.SetKeyFromPassphrase( | if (!crypter.SetKeyFromPassphrase( | ||||
strNewWalletPassphrase, pMasterKey.second.vchSalt, | strNewWalletPassphrase, pMasterKey.second.vchSalt, | ||||
pMasterKey.second.nDeriveIterations, | pMasterKey.second.nDeriveIterations, | ||||
pMasterKey.second.nDerivationMethod)) { | pMasterKey.second.nDerivationMethod)) { | ||||
return false; | return false; | ||||
} | } | ||||
if (!crypter.Encrypt(vMasterKey, pMasterKey.second.vchCryptedKey)) { | if (!crypter.Encrypt(_vMasterKey, | ||||
pMasterKey.second.vchCryptedKey)) { | |||||
return false; | return false; | ||||
} | } | ||||
CWalletDB(*dbw).WriteMasterKey(pMasterKey.first, pMasterKey.second); | CWalletDB(*dbw).WriteMasterKey(pMasterKey.first, pMasterKey.second); | ||||
if (fWasLocked) { | if (fWasLocked) { | ||||
Lock(); | Lock(); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 178 Lines • ▼ Show 20 Lines | void CWallet::AddToSpends(const TxId &wtxid) { | ||||
} | } | ||||
} | } | ||||
bool CWallet::EncryptWallet(const SecureString &strWalletPassphrase) { | bool CWallet::EncryptWallet(const SecureString &strWalletPassphrase) { | ||||
if (IsCrypted()) { | if (IsCrypted()) { | ||||
return false; | return false; | ||||
} | } | ||||
CKeyingMaterial vMasterKey; | CKeyingMaterial _vMasterKey; | ||||
vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE); | _vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE); | ||||
GetStrongRandBytes(&vMasterKey[0], WALLET_CRYPTO_KEY_SIZE); | GetStrongRandBytes(&_vMasterKey[0], WALLET_CRYPTO_KEY_SIZE); | ||||
CMasterKey kMasterKey; | CMasterKey kMasterKey; | ||||
kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE); | kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE); | ||||
GetStrongRandBytes(&kMasterKey.vchSalt[0], WALLET_CRYPTO_SALT_SIZE); | GetStrongRandBytes(&kMasterKey.vchSalt[0], WALLET_CRYPTO_SALT_SIZE); | ||||
CCrypter crypter; | CCrypter crypter; | ||||
int64_t nStartTime = GetTimeMillis(); | int64_t nStartTime = GetTimeMillis(); | ||||
Show All 20 Lines | LogPrintf("Encrypting Wallet with an nDeriveIterations of %i\n", | ||||
kMasterKey.nDeriveIterations); | kMasterKey.nDeriveIterations); | ||||
if (!crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, | if (!crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, | ||||
kMasterKey.nDeriveIterations, | kMasterKey.nDeriveIterations, | ||||
kMasterKey.nDerivationMethod)) { | kMasterKey.nDerivationMethod)) { | ||||
return false; | return false; | ||||
} | } | ||||
if (!crypter.Encrypt(vMasterKey, kMasterKey.vchCryptedKey)) { | if (!crypter.Encrypt(_vMasterKey, kMasterKey.vchCryptedKey)) { | ||||
return false; | return false; | ||||
} | } | ||||
{ | { | ||||
LOCK(cs_wallet); | LOCK(cs_wallet); | ||||
mapMasterKeys[++nMasterKeyMaxID] = kMasterKey; | mapMasterKeys[++nMasterKeyMaxID] = kMasterKey; | ||||
assert(!pwalletdbEncryption); | assert(!pwalletdbEncryption); | ||||
pwalletdbEncryption = new CWalletDB(*dbw); | pwalletdbEncryption = new CWalletDB(*dbw); | ||||
if (!pwalletdbEncryption->TxnBegin()) { | if (!pwalletdbEncryption->TxnBegin()) { | ||||
delete pwalletdbEncryption; | delete pwalletdbEncryption; | ||||
pwalletdbEncryption = nullptr; | pwalletdbEncryption = nullptr; | ||||
return false; | return false; | ||||
} | } | ||||
pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey); | pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey); | ||||
if (!EncryptKeys(vMasterKey)) { | if (!EncryptKeys(_vMasterKey)) { | ||||
pwalletdbEncryption->TxnAbort(); | pwalletdbEncryption->TxnAbort(); | ||||
delete pwalletdbEncryption; | delete pwalletdbEncryption; | ||||
// We now probably have half of our keys encrypted in memory, and | // We now probably have half of our keys encrypted in memory, and | ||||
// half not... die and let the user reload the unencrypted wallet. | // half not... die and let the user reload the unencrypted wallet. | ||||
assert(false); | assert(false); | ||||
} | } | ||||
// Encryption was introduced in version 0.4.0 | // Encryption was introduced in version 0.4.0 | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | DBErrors CWallet::ReorderTransactions() { | ||||
LOCK(cs_wallet); | LOCK(cs_wallet); | ||||
CWalletDB walletdb(*dbw); | CWalletDB walletdb(*dbw); | ||||
// Old wallets didn't have any defined order for transactions. Probably a | // Old wallets didn't have any defined order for transactions. Probably a | ||||
// bad idea to change the output of this. | // bad idea to change the output of this. | ||||
// First: get all CWalletTx and CAccountingEntry into a sorted-by-time | // First: get all CWalletTx and CAccountingEntry into a sorted-by-time | ||||
// multimap. | // multimap. | ||||
typedef std::pair<CWalletTx *, CAccountingEntry *> TxPair; | |||||
typedef std::multimap<int64_t, TxPair> TxItems; | |||||
TxItems txByTime; | TxItems txByTime; | ||||
for (std::map<TxId, CWalletTx>::iterator it = mapWallet.begin(); | for (std::map<TxId, CWalletTx>::iterator it = mapWallet.begin(); | ||||
it != mapWallet.end(); ++it) { | it != mapWallet.end(); ++it) { | ||||
CWalletTx *wtx = &((*it).second); | CWalletTx *wtx = &((*it).second); | ||||
txByTime.insert( | txByTime.insert( | ||||
std::make_pair(wtx->nTimeReceived, TxPair(wtx, nullptr))); | std::make_pair(wtx->nTimeReceived, TxPair(wtx, nullptr))); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 3,436 Lines • Show Last 20 Lines |