Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/wallet.cpp
Show First 20 Lines • Show All 3,176 Lines • ▼ Show 20 Lines | if (nLoadWalletRet == DB_NEED_REWRITE) { | ||||
setExternalKeyPool.clear(); | setExternalKeyPool.clear(); | ||||
m_pool_key_to_index.clear(); | m_pool_key_to_index.clear(); | ||||
// Note: can't top-up keypool here, because wallet is locked. | // Note: can't top-up keypool here, because wallet is locked. | ||||
// User will be prompted to unlock wallet the next operation | // User will be prompted to unlock wallet the next operation | ||||
// that requires a new key. | // that requires a new key. | ||||
} | } | ||||
} | } | ||||
// This wallet is in its first run if all of these are empty | |||||
fFirstRunRet = mapKeys.empty() && mapCryptedKeys.empty() && | |||||
mapWatchKeys.empty() && setWatchOnly.empty() && | |||||
mapScripts.empty(); | |||||
if (nLoadWalletRet != DB_LOAD_OK) { | if (nLoadWalletRet != DB_LOAD_OK) { | ||||
return nLoadWalletRet; | return nLoadWalletRet; | ||||
} | } | ||||
fFirstRunRet = !vchDefaultKey.IsValid(); | |||||
uiInterface.LoadWallet(this); | uiInterface.LoadWallet(this); | ||||
return DB_LOAD_OK; | return DB_LOAD_OK; | ||||
} | } | ||||
DBErrors CWallet::ZapSelectTx(std::vector<uint256> &vHashIn, | DBErrors CWallet::ZapSelectTx(std::vector<uint256> &vHashIn, | ||||
std::vector<uint256> &vHashOut) { | std::vector<uint256> &vHashOut) { | ||||
AssertLockHeld(cs_wallet); // mapWallet | AssertLockHeld(cs_wallet); // mapWallet | ||||
vchDefaultKey = CPubKey(); | |||||
DBErrors nZapSelectTxRet = | DBErrors nZapSelectTxRet = | ||||
CWalletDB(*dbw, "cr+").ZapSelectTx(vHashIn, vHashOut); | CWalletDB(*dbw, "cr+").ZapSelectTx(vHashIn, vHashOut); | ||||
for (uint256 hash : vHashOut) { | for (uint256 hash : vHashOut) { | ||||
mapWallet.erase(hash); | mapWallet.erase(hash); | ||||
} | } | ||||
if (nZapSelectTxRet == DB_NEED_REWRITE) { | if (nZapSelectTxRet == DB_NEED_REWRITE) { | ||||
if (dbw->Rewrite("\x04pool")) { | if (dbw->Rewrite("\x04pool")) { | ||||
Show All 11 Lines | DBErrors CWallet::ZapSelectTx(std::vector<uint256> &vHashIn, | ||||
} | } | ||||
MarkDirty(); | MarkDirty(); | ||||
return DB_LOAD_OK; | return DB_LOAD_OK; | ||||
} | } | ||||
DBErrors CWallet::ZapWalletTx(std::vector<CWalletTx> &vWtx) { | DBErrors CWallet::ZapWalletTx(std::vector<CWalletTx> &vWtx) { | ||||
vchDefaultKey = CPubKey(); | |||||
DBErrors nZapWalletTxRet = CWalletDB(*dbw, "cr+").ZapWalletTx(vWtx); | DBErrors nZapWalletTxRet = CWalletDB(*dbw, "cr+").ZapWalletTx(vWtx); | ||||
if (nZapWalletTxRet == DB_NEED_REWRITE) { | if (nZapWalletTxRet == DB_NEED_REWRITE) { | ||||
if (dbw->Rewrite("\x04pool")) { | if (dbw->Rewrite("\x04pool")) { | ||||
LOCK(cs_wallet); | LOCK(cs_wallet); | ||||
setInternalKeyPool.clear(); | setInternalKeyPool.clear(); | ||||
setExternalKeyPool.clear(); | setExternalKeyPool.clear(); | ||||
m_pool_key_to_index.clear(); | m_pool_key_to_index.clear(); | ||||
// Note: can't top-up keypool here, because wallet is locked. | // Note: can't top-up keypool here, because wallet is locked. | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | bool CWallet::DelAddressBook(const CTxDestination &address) { | ||||
NotifyAddressBookChanged(this, address, "", | NotifyAddressBookChanged(this, address, "", | ||||
::IsMine(*this, address) != ISMINE_NO, "", | ::IsMine(*this, address) != ISMINE_NO, "", | ||||
CT_DELETED); | CT_DELETED); | ||||
CWalletDB(*dbw).ErasePurpose(address); | CWalletDB(*dbw).ErasePurpose(address); | ||||
return CWalletDB(*dbw).EraseName(address); | return CWalletDB(*dbw).EraseName(address); | ||||
} | } | ||||
bool CWallet::SetDefaultKey(const CPubKey &vchPubKey) { | |||||
if (!CWalletDB(*dbw).WriteDefaultKey(vchPubKey)) { | |||||
return false; | |||||
} | |||||
vchDefaultKey = vchPubKey; | |||||
return true; | |||||
} | |||||
/** | /** | ||||
* Mark old keypool keys as used, and generate all new keys. | * Mark old keypool keys as used, and generate all new keys. | ||||
*/ | */ | ||||
bool CWallet::NewKeyPool() { | bool CWallet::NewKeyPool() { | ||||
LOCK(cs_wallet); | LOCK(cs_wallet); | ||||
CWalletDB walletdb(*dbw); | CWalletDB walletdb(*dbw); | ||||
for (int64_t nIndex : setInternalKeyPool) { | for (int64_t nIndex : setInternalKeyPool) { | ||||
▲ Show 20 Lines • Show All 903 Lines • ▼ Show 20 Lines | if (fFirstRun) { | ||||
// Generate a new master key. | // Generate a new master key. | ||||
CPubKey masterPubKey = walletInstance->GenerateNewHDMasterKey(); | CPubKey masterPubKey = walletInstance->GenerateNewHDMasterKey(); | ||||
if (!walletInstance->SetHDMasterKey(masterPubKey)) { | if (!walletInstance->SetHDMasterKey(masterPubKey)) { | ||||
throw std::runtime_error(std::string(__func__) + | throw std::runtime_error(std::string(__func__) + | ||||
": Storing master key failed"); | ": Storing master key failed"); | ||||
} | } | ||||
} | } | ||||
CPubKey newDefaultKey; | // Top up the keypool | ||||
if (walletInstance->GetKeyFromPool(newDefaultKey, false)) { | if (!walletInstance->TopUpKeyPool()) { | ||||
walletInstance->SetDefaultKey(newDefaultKey); | InitError(_("Unable to generate initial keys") += "\n"); | ||||
if (!walletInstance->SetAddressBook( | |||||
walletInstance->vchDefaultKey.GetID(), "", "receive")) { | |||||
InitError(_("Cannot write default address") += "\n"); | |||||
return nullptr; | return nullptr; | ||||
} | } | ||||
} | |||||
walletInstance->SetBestChain(chainActive.GetLocator()); | walletInstance->SetBestChain(chainActive.GetLocator()); | ||||
} else if (gArgs.IsArgSet("-usehd")) { | } else if (gArgs.IsArgSet("-usehd")) { | ||||
bool useHD = gArgs.GetBoolArg("-usehd", DEFAULT_USE_HD_WALLET); | bool useHD = gArgs.GetBoolArg("-usehd", DEFAULT_USE_HD_WALLET); | ||||
if (walletInstance->IsHDEnabled() && !useHD) { | if (walletInstance->IsHDEnabled() && !useHD) { | ||||
InitError(strprintf(_("Error loading %s: You can't disable HD on a " | InitError(strprintf(_("Error loading %s: You can't disable HD on a " | ||||
"already existing HD wallet"), | "already existing HD wallet"), | ||||
walletFile)); | walletFile)); | ||||
▲ Show 20 Lines • Show All 349 Lines • Show Last 20 Lines |