Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/wallettool.cpp
Show All 32 Lines | static std::shared_ptr<CWallet> CreateWallet(const std::string &name, | ||||
auto chain = interfaces::MakeChain(); | auto chain = interfaces::MakeChain(); | ||||
std::shared_ptr<CWallet> wallet_instance( | std::shared_ptr<CWallet> wallet_instance( | ||||
new CWallet(Params(), *chain, WalletLocation(name), | new CWallet(Params(), *chain, WalletLocation(name), | ||||
WalletDatabase::Create(path)), | WalletDatabase::Create(path)), | ||||
WalletToolReleaseWallet); | WalletToolReleaseWallet); | ||||
bool first_run = true; | bool first_run = true; | ||||
DBErrors load_wallet_ret = wallet_instance->LoadWallet(first_run); | DBErrors load_wallet_ret = wallet_instance->LoadWallet(first_run); | ||||
if (load_wallet_ret != DBErrors::LOAD_OK) { | if (load_wallet_ret != DBErrors::LOAD_OK) { | ||||
tfm::format(std::cerr, "Error creating %s", name.c_str()); | tfm::format(std::cerr, "Error creating %s", name); | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
wallet_instance->SetMinVersion(FEATURE_HD_SPLIT); | wallet_instance->SetMinVersion(FEATURE_HD_SPLIT); | ||||
// generate a new HD seed | // generate a new HD seed | ||||
CPubKey seed = wallet_instance->GenerateNewSeed(); | CPubKey seed = wallet_instance->GenerateNewSeed(); | ||||
wallet_instance->SetHDSeed(seed); | wallet_instance->SetHDSeed(seed); | ||||
Show All 19 Lines | static std::shared_ptr<CWallet> LoadWallet(const std::string &name, | ||||
DBErrors load_wallet_ret; | DBErrors load_wallet_ret; | ||||
try { | try { | ||||
bool first_run; | bool first_run; | ||||
load_wallet_ret = wallet_instance->LoadWallet(first_run); | load_wallet_ret = wallet_instance->LoadWallet(first_run); | ||||
} catch (const std::runtime_error &) { | } catch (const std::runtime_error &) { | ||||
tfm::format( | tfm::format( | ||||
std::cerr, | std::cerr, | ||||
"Error loading %s. Is wallet being used by another process?\n", | "Error loading %s. Is wallet being used by another process?\n", | ||||
name.c_str()); | name); | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
if (load_wallet_ret != DBErrors::LOAD_OK) { | if (load_wallet_ret != DBErrors::LOAD_OK) { | ||||
wallet_instance = nullptr; | wallet_instance = nullptr; | ||||
if (load_wallet_ret == DBErrors::CORRUPT) { | if (load_wallet_ret == DBErrors::CORRUPT) { | ||||
tfm::format(std::cerr, "Error loading %s: Wallet corrupted", | tfm::format(std::cerr, "Error loading %s: Wallet corrupted", name); | ||||
name.c_str()); | |||||
return nullptr; | return nullptr; | ||||
} else if (load_wallet_ret == DBErrors::NONCRITICAL_ERROR) { | } else if (load_wallet_ret == DBErrors::NONCRITICAL_ERROR) { | ||||
tfm::format( | tfm::format( | ||||
std::cerr, | std::cerr, | ||||
"Error reading %s! All keys read correctly, but " | "Error reading %s! All keys read correctly, but transaction " | ||||
"transaction data" | "data or address book entries might be missing or incorrect.", | ||||
" or address book entries might be missing or incorrect.", | name); | ||||
name.c_str()); | |||||
} else if (load_wallet_ret == DBErrors::TOO_NEW) { | } else if (load_wallet_ret == DBErrors::TOO_NEW) { | ||||
tfm::format(std::cerr, | tfm::format(std::cerr, | ||||
"Error loading %s: Wallet requires newer version of %s", | "Error loading %s: Wallet requires newer version of %s", | ||||
name.c_str(), PACKAGE_NAME); | name, PACKAGE_NAME); | ||||
return nullptr; | return nullptr; | ||||
} else if (load_wallet_ret == DBErrors::NEED_REWRITE) { | } else if (load_wallet_ret == DBErrors::NEED_REWRITE) { | ||||
tfm::format(std::cerr, | tfm::format(std::cerr, | ||||
"Wallet needed to be rewritten: restart %s to complete", | "Wallet needed to be rewritten: restart %s to complete", | ||||
PACKAGE_NAME); | PACKAGE_NAME); | ||||
return nullptr; | return nullptr; | ||||
} else { | } else { | ||||
tfm::format(std::cerr, "Error loading %s", name.c_str()); | tfm::format(std::cerr, "Error loading %s", name); | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
} | } | ||||
return wallet_instance; | return wallet_instance; | ||||
} | } | ||||
static void WalletShowInfo(CWallet *wallet_instance) { | static void WalletShowInfo(CWallet *wallet_instance) { | ||||
Show All 20 Lines | bool ExecuteWalletToolFunc(const std::string &command, | ||||
if (command == "create") { | if (command == "create") { | ||||
std::shared_ptr<CWallet> wallet_instance = CreateWallet(name, path); | std::shared_ptr<CWallet> wallet_instance = CreateWallet(name, path); | ||||
if (wallet_instance) { | if (wallet_instance) { | ||||
WalletShowInfo(wallet_instance.get()); | WalletShowInfo(wallet_instance.get()); | ||||
wallet_instance->Flush(); | wallet_instance->Flush(); | ||||
} | } | ||||
} else if (command == "info") { | } else if (command == "info") { | ||||
if (!fs::exists(path)) { | if (!fs::exists(path)) { | ||||
tfm::format(std::cerr, "Error: no wallet file at %s\n", | tfm::format(std::cerr, "Error: no wallet file at %s\n", name); | ||||
name.c_str()); | |||||
return false; | return false; | ||||
} | } | ||||
std::string error; | std::string error; | ||||
if (!WalletBatch::VerifyEnvironment(path, error)) { | if (!WalletBatch::VerifyEnvironment(path, error)) { | ||||
tfm::format( | tfm::format( | ||||
std::cerr, | std::cerr, | ||||
"Error loading %s. Is wallet being used by other process?\n", | "Error loading %s. Is wallet being used by other process?\n", | ||||
name.c_str()); | name); | ||||
return false; | return false; | ||||
} | } | ||||
std::shared_ptr<CWallet> wallet_instance = LoadWallet(name, path); | std::shared_ptr<CWallet> wallet_instance = LoadWallet(name, path); | ||||
if (!wallet_instance) { | if (!wallet_instance) { | ||||
return false; | return false; | ||||
} | } | ||||
WalletShowInfo(wallet_instance.get()); | WalletShowInfo(wallet_instance.get()); | ||||
wallet_instance->Flush(); | wallet_instance->Flush(); | ||||
} else { | } else { | ||||
tfm::format(std::cerr, "Invalid command: %s\n", command.c_str()); | tfm::format(std::cerr, "Invalid command: %s\n", command); | ||||
return false; | return false; | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
} // namespace WalletTool | } // namespace WalletTool |