Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/wallet.cpp
Show First 20 Lines • Show All 147 Lines • ▼ Show 20 Lines | wallet.reset(); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
static const size_t OUTPUT_GROUP_MAX_ENTRIES = 10; | static const size_t OUTPUT_GROUP_MAX_ENTRIES = 10; | ||||
namespace { | namespace { | ||||
std::shared_ptr<CWallet> | std::shared_ptr<CWallet> | ||||
LoadWalletInternal(const CChainParams &chainParams, interfaces::Chain &chain, | LoadWalletInternal(interfaces::Chain &chain, const WalletLocation &location, | ||||
const WalletLocation &location, bilingual_str &error, | bilingual_str &error, std::vector<bilingual_str> &warnings) { | ||||
std::vector<bilingual_str> &warnings) { | |||||
try { | try { | ||||
if (!CWallet::Verify(chainParams, chain, location, error, warnings)) { | if (!CWallet::Verify(chain, location, error, warnings)) { | ||||
error = Untranslated("Wallet file verification failed.") + | error = Untranslated("Wallet file verification failed.") + | ||||
Untranslated(" ") + error; | Untranslated(" ") + error; | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
std::shared_ptr<CWallet> wallet = CWallet::CreateWalletFromFile( | std::shared_ptr<CWallet> wallet = | ||||
chainParams, chain, location, error, warnings); | CWallet::CreateWalletFromFile(chain, location, error, warnings); | ||||
if (!wallet) { | if (!wallet) { | ||||
error = Untranslated("Wallet loading failed.") + Untranslated(" ") + | error = Untranslated("Wallet loading failed.") + Untranslated(" ") + | ||||
error; | error; | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
AddWallet(wallet); | AddWallet(wallet); | ||||
wallet->postInitProcess(); | wallet->postInitProcess(); | ||||
return wallet; | return wallet; | ||||
} catch (const std::runtime_error &e) { | } catch (const std::runtime_error &e) { | ||||
error = Untranslated(e.what()); | error = Untranslated(e.what()); | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
} | } | ||||
} // namespace | } // namespace | ||||
std::shared_ptr<CWallet> LoadWallet(const CChainParams &chainParams, | std::shared_ptr<CWallet> LoadWallet(interfaces::Chain &chain, | ||||
interfaces::Chain &chain, | |||||
const WalletLocation &location, | const WalletLocation &location, | ||||
bilingual_str &error, | bilingual_str &error, | ||||
std::vector<bilingual_str> &warnings) { | std::vector<bilingual_str> &warnings) { | ||||
auto result = | auto result = | ||||
WITH_LOCK(g_loading_wallet_mutex, | WITH_LOCK(g_loading_wallet_mutex, | ||||
return g_loading_wallet_set.insert(location.GetName())); | return g_loading_wallet_set.insert(location.GetName())); | ||||
if (!result.second) { | if (!result.second) { | ||||
error = Untranslated("Wallet already being loading."); | error = Untranslated("Wallet already being loading."); | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
auto wallet = | auto wallet = LoadWalletInternal(chain, location, error, warnings); | ||||
LoadWalletInternal(chainParams, chain, location, error, warnings); | |||||
WITH_LOCK(g_loading_wallet_mutex, g_loading_wallet_set.erase(result.first)); | WITH_LOCK(g_loading_wallet_mutex, g_loading_wallet_set.erase(result.first)); | ||||
return wallet; | return wallet; | ||||
} | } | ||||
WalletCreationStatus CreateWallet(const CChainParams ¶ms, | WalletCreationStatus CreateWallet(interfaces::Chain &chain, | ||||
interfaces::Chain &chain, | |||||
const SecureString &passphrase, | const SecureString &passphrase, | ||||
uint64_t wallet_creation_flags, | uint64_t wallet_creation_flags, | ||||
const std::string &name, bilingual_str &error, | const std::string &name, bilingual_str &error, | ||||
std::vector<bilingual_str> &warnings, | std::vector<bilingual_str> &warnings, | ||||
std::shared_ptr<CWallet> &result) { | std::shared_ptr<CWallet> &result) { | ||||
// Indicate that the wallet is actually supposed to be blank and not just | // Indicate that the wallet is actually supposed to be blank and not just | ||||
// blank to make it encrypted | // blank to make it encrypted | ||||
bool create_blank = (wallet_creation_flags & WALLET_FLAG_BLANK_WALLET); | bool create_blank = (wallet_creation_flags & WALLET_FLAG_BLANK_WALLET); | ||||
// Born encrypted wallets need to be created blank first. | // Born encrypted wallets need to be created blank first. | ||||
if (!passphrase.empty()) { | if (!passphrase.empty()) { | ||||
wallet_creation_flags |= WALLET_FLAG_BLANK_WALLET; | wallet_creation_flags |= WALLET_FLAG_BLANK_WALLET; | ||||
} | } | ||||
// Check the wallet file location | // Check the wallet file location | ||||
WalletLocation location(name); | WalletLocation location(name); | ||||
if (location.Exists()) { | if (location.Exists()) { | ||||
error = strprintf(Untranslated("Wallet %s already exists."), | error = strprintf(Untranslated("Wallet %s already exists."), | ||||
location.GetName()); | location.GetName()); | ||||
return WalletCreationStatus::CREATION_FAILED; | return WalletCreationStatus::CREATION_FAILED; | ||||
} | } | ||||
// Wallet::Verify will check if we're trying to create a wallet with a | // Wallet::Verify will check if we're trying to create a wallet with a | ||||
// duplicate name. | // duplicate name. | ||||
if (!CWallet::Verify(params, chain, location, error, warnings)) { | if (!CWallet::Verify(chain, location, error, warnings)) { | ||||
error = Untranslated("Wallet file verification failed.") + | error = Untranslated("Wallet file verification failed.") + | ||||
Untranslated(" ") + error; | Untranslated(" ") + error; | ||||
return WalletCreationStatus::CREATION_FAILED; | return WalletCreationStatus::CREATION_FAILED; | ||||
} | } | ||||
// Do not allow a passphrase when private keys are disabled | // Do not allow a passphrase when private keys are disabled | ||||
if (!passphrase.empty() && | if (!passphrase.empty() && | ||||
(wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { | (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { | ||||
error = Untranslated( | error = Untranslated( | ||||
"Passphrase provided but private keys are disabled. A passphrase " | "Passphrase provided but private keys are disabled. A passphrase " | ||||
"is only used to encrypt private keys, so cannot be used for " | "is only used to encrypt private keys, so cannot be used for " | ||||
"wallets with private keys disabled."); | "wallets with private keys disabled."); | ||||
return WalletCreationStatus::CREATION_FAILED; | return WalletCreationStatus::CREATION_FAILED; | ||||
} | } | ||||
// Make the wallet | // Make the wallet | ||||
std::shared_ptr<CWallet> wallet = CWallet::CreateWalletFromFile( | std::shared_ptr<CWallet> wallet = CWallet::CreateWalletFromFile( | ||||
params, chain, location, error, warnings, wallet_creation_flags); | chain, location, error, warnings, wallet_creation_flags); | ||||
if (!wallet) { | if (!wallet) { | ||||
error = | error = | ||||
Untranslated("Wallet creation failed.") + Untranslated(" ") + error; | Untranslated("Wallet creation failed.") + Untranslated(" ") + error; | ||||
return WalletCreationStatus::CREATION_FAILED; | return WalletCreationStatus::CREATION_FAILED; | ||||
} | } | ||||
// Encrypt the wallet | // Encrypt the wallet | ||||
if (!passphrase.empty() && | if (!passphrase.empty() && | ||||
▲ Show 20 Lines • Show All 3,864 Lines • ▼ Show 20 Lines | for (const auto &address : m_address_book) { | ||||
if (!data.first.compare(0, prefix.size(), prefix)) { | if (!data.first.compare(0, prefix.size(), prefix)) { | ||||
values.emplace_back(data.second); | values.emplace_back(data.second); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
return values; | return values; | ||||
} | } | ||||
bool CWallet::Verify(const CChainParams &chainParams, interfaces::Chain &chain, | bool CWallet::Verify(interfaces::Chain &chain, const WalletLocation &location, | ||||
const WalletLocation &location, | |||||
bilingual_str &error_string, | bilingual_str &error_string, | ||||
std::vector<bilingual_str> &warnings) { | std::vector<bilingual_str> &warnings) { | ||||
// Do some checking on wallet path. It should be either a: | // Do some checking on wallet path. It should be either a: | ||||
// | // | ||||
// 1. Path where a directory can be created. | // 1. Path where a directory can be created. | ||||
// 2. Path to an existing directory. | // 2. Path to an existing directory. | ||||
// 3. Path to a symlink to a directory. | // 3. Path to a symlink to a directory. | ||||
// 4. For backwards compatibility, the name of a data file in -walletdir. | // 4. For backwards compatibility, the name of a data file in -walletdir. | ||||
Show All 34 Lines | try { | ||||
error_string = Untranslated( | error_string = Untranslated( | ||||
strprintf("Error loading wallet %s. %s", location.GetName(), | strprintf("Error loading wallet %s. %s", location.GetName(), | ||||
fsbridge::get_filesystem_error_message(e))); | fsbridge::get_filesystem_error_message(e))); | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
std::shared_ptr<CWallet> CWallet::CreateWalletFromFile( | std::shared_ptr<CWallet> CWallet::CreateWalletFromFile( | ||||
const CChainParams &chainParams, interfaces::Chain &chain, | interfaces::Chain &chain, const WalletLocation &location, | ||||
const WalletLocation &location, bilingual_str &error, | bilingual_str &error, std::vector<bilingual_str> &warnings, | ||||
std::vector<bilingual_str> &warnings, uint64_t wallet_creation_flags) { | uint64_t wallet_creation_flags) { | ||||
const std::string walletFile = | const std::string walletFile = | ||||
WalletDataFilePath(location.GetPath()).string(); | WalletDataFilePath(location.GetPath()).string(); | ||||
chain.initMessage(_("Loading wallet...").translated); | chain.initMessage(_("Loading wallet...").translated); | ||||
int64_t nStart = GetTimeMillis(); | int64_t nStart = GetTimeMillis(); | ||||
bool fFirstRun = true; | bool fFirstRun = true; | ||||
// TODO: Can't use std::make_shared because we need a custom deleter but | // TODO: Can't use std::make_shared because we need a custom deleter but | ||||
▲ Show 20 Lines • Show All 835 Lines • Show Last 20 Lines |