Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/wallet.cpp
Show First 20 Lines • Show All 160 Lines • ▼ Show 20 Lines | |||||
std::shared_ptr<CWallet> LoadWallet(const CChainParams &chainParams, | std::shared_ptr<CWallet> LoadWallet(const CChainParams &chainParams, | ||||
interfaces::Chain &chain, | interfaces::Chain &chain, | ||||
const std::string &name, std::string &error, | const std::string &name, std::string &error, | ||||
std::string &warning) { | std::string &warning) { | ||||
return LoadWallet(chainParams, chain, WalletLocation(name), error, warning); | return LoadWallet(chainParams, chain, WalletLocation(name), error, warning); | ||||
} | } | ||||
std::shared_ptr<CWallet> | WalletCreationStatus | ||||
CreateWallet(const CChainParams ¶ms, interfaces::Chain &chain, | CreateWallet(const CChainParams ¶ms, interfaces::Chain &chain, | ||||
const SecureString &passphrase, uint64_t wallet_creation_flags, | const SecureString &passphrase, uint64_t wallet_creation_flags, | ||||
const std::string &name, std::string &error, std::string &warning, | const std::string &name, std::string &error, std::string &warning, | ||||
WalletCreationStatus &status) { | 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 = "Wallet " + location.GetName() + " already exists."; | error = "Wallet " + location.GetName() + " already exists."; | ||||
status = WalletCreationStatus::CREATION_FAILED; | return WalletCreationStatus::CREATION_FAILED; | ||||
return nullptr; | |||||
} | } | ||||
// 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. | ||||
std::string wallet_error; | std::string wallet_error; | ||||
if (!CWallet::Verify(params, chain, location, false, wallet_error, | if (!CWallet::Verify(params, chain, location, false, wallet_error, | ||||
warning)) { | warning)) { | ||||
error = "Wallet file verification failed: " + wallet_error; | error = "Wallet file verification failed: " + wallet_error; | ||||
status = WalletCreationStatus::CREATION_FAILED; | return WalletCreationStatus::CREATION_FAILED; | ||||
return nullptr; | } | ||||
// Do not allow a passphrase when private keys are disabled | |||||
if (!passphrase.empty() && | |||||
(wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { | |||||
error = "Passphrase provided but private keys are disabled. A " | |||||
"passphrase is only used to encrypt private keys, so cannot be " | |||||
"used for wallets with private keys disabled."; | |||||
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, wallet_creation_flags); | params, chain, location, wallet_creation_flags); | ||||
if (!wallet) { | if (!wallet) { | ||||
error = "Wallet creation failed"; | error = "Wallet creation failed"; | ||||
status = WalletCreationStatus::CREATION_FAILED; | return WalletCreationStatus::CREATION_FAILED; | ||||
return nullptr; | |||||
} | } | ||||
// Encrypt the wallet | // Encrypt the wallet | ||||
if (!passphrase.empty() && | if (!passphrase.empty() && | ||||
!(wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { | !(wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { | ||||
if (!wallet->EncryptWallet(passphrase)) { | if (!wallet->EncryptWallet(passphrase)) { | ||||
error = "Error: Wallet created but failed to encrypt."; | error = "Error: Wallet created but failed to encrypt."; | ||||
status = WalletCreationStatus::ENCRYPTION_FAILED; | return WalletCreationStatus::ENCRYPTION_FAILED; | ||||
return nullptr; | |||||
} | } | ||||
if (!create_blank) { | if (!create_blank) { | ||||
// Unlock the wallet | // Unlock the wallet | ||||
if (!wallet->Unlock(passphrase)) { | if (!wallet->Unlock(passphrase)) { | ||||
error = "Error: Wallet was encrypted but could not be unlocked"; | error = "Error: Wallet was encrypted but could not be unlocked"; | ||||
status = WalletCreationStatus::ENCRYPTION_FAILED; | return WalletCreationStatus::ENCRYPTION_FAILED; | ||||
return nullptr; | |||||
} | } | ||||
// Set a seed for the wallet | // Set a seed for the wallet | ||||
CPubKey master_pub_key = wallet->GenerateNewSeed(); | CPubKey master_pub_key = wallet->GenerateNewSeed(); | ||||
wallet->SetHDSeed(master_pub_key); | wallet->SetHDSeed(master_pub_key); | ||||
wallet->NewKeyPool(); | wallet->NewKeyPool(); | ||||
// Relock the wallet | // Relock the wallet | ||||
wallet->Lock(); | wallet->Lock(); | ||||
} | } | ||||
} | } | ||||
AddWallet(wallet); | AddWallet(wallet); | ||||
wallet->postInitProcess(); | wallet->postInitProcess(); | ||||
status = WalletCreationStatus::SUCCESS; | result = wallet; | ||||
return wallet; | return WalletCreationStatus::SUCCESS; | ||||
} | } | ||||
const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000; | const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000; | ||||
const BlockHash CMerkleTx::ABANDON_HASH(uint256S( | const BlockHash CMerkleTx::ABANDON_HASH(uint256S( | ||||
"0000000000000000000000000000000000000000000000000000000000000001")); | "0000000000000000000000000000000000000000000000000000000000000001")); | ||||
/** @defgroup mapWallet | /** @defgroup mapWallet | ||||
▲ Show 20 Lines • Show All 5,188 Lines • Show Last 20 Lines |