Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/wallettool.cpp
Show All 20 Lines | static void WalletToolReleaseWallet(CWallet *wallet) { | ||||
wallet->WalletLogPrintf("Releasing wallet\n"); | wallet->WalletLogPrintf("Releasing wallet\n"); | ||||
wallet->Flush(); | wallet->Flush(); | ||||
delete wallet; | delete wallet; | ||||
} | } | ||||
static std::shared_ptr<CWallet> CreateWallet(const std::string &name, | static std::shared_ptr<CWallet> CreateWallet(const std::string &name, | ||||
const fs::path &path) { | const fs::path &path) { | ||||
if (fs::exists(path)) { | if (fs::exists(path)) { | ||||
fprintf(stderr, "Error: File exists already\n"); | tfm::format(std::cerr, "Error: File exists already\n"); | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
// dummy chain interface | // dummy chain interface | ||||
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) { | ||||
fprintf(stderr, "Error creating %s", name.c_str()); | tfm::format(std::cerr, "Error creating %s", name.c_str()); | ||||
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); | ||||
fprintf(stdout, "Topping up keypool...\n"); | tfm::format(std::cout, "Topping up keypool...\n"); | ||||
wallet_instance->TopUpKeyPool(); | wallet_instance->TopUpKeyPool(); | ||||
return wallet_instance; | return wallet_instance; | ||||
} | } | ||||
static std::shared_ptr<CWallet> LoadWallet(const std::string &name, | static std::shared_ptr<CWallet> LoadWallet(const std::string &name, | ||||
const fs::path &path) { | const fs::path &path) { | ||||
if (!fs::exists(path)) { | if (!fs::exists(path)) { | ||||
fprintf(stderr, "Error: Wallet files does not exist\n"); | tfm::format(std::cerr, "Error: Wallet files does not exist\n"); | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
// dummy chain interface | // dummy chain interface | ||||
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); | ||||
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 &) { | ||||
fprintf(stderr, | tfm::format( | ||||
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.c_str()); | ||||
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) { | ||||
fprintf(stderr, "Error loading %s: Wallet corrupted", name.c_str()); | tfm::format(std::cerr, "Error loading %s: Wallet corrupted", | ||||
name.c_str()); | |||||
return nullptr; | return nullptr; | ||||
} else if (load_wallet_ret == DBErrors::NONCRITICAL_ERROR) { | } else if (load_wallet_ret == DBErrors::NONCRITICAL_ERROR) { | ||||
fprintf(stderr, | tfm::format( | ||||
std::cerr, | |||||
"Error reading %s! All keys read correctly, but " | "Error reading %s! All keys read correctly, but " | ||||
"transaction data" | "transaction data" | ||||
" or address book entries might be missing or incorrect.", | " or address book entries might be missing or incorrect.", | ||||
name.c_str()); | name.c_str()); | ||||
} else if (load_wallet_ret == DBErrors::TOO_NEW) { | } else if (load_wallet_ret == DBErrors::TOO_NEW) { | ||||
fprintf(stderr, | 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.c_str(), PACKAGE_NAME); | ||||
return nullptr; | return nullptr; | ||||
} else if (load_wallet_ret == DBErrors::NEED_REWRITE) { | } else if (load_wallet_ret == DBErrors::NEED_REWRITE) { | ||||
fprintf(stderr, | 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 { | ||||
fprintf(stderr, "Error loading %s", name.c_str()); | tfm::format(std::cerr, "Error loading %s", name.c_str()); | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
} | } | ||||
return wallet_instance; | return wallet_instance; | ||||
} | } | ||||
static void WalletShowInfo(CWallet *wallet_instance) { | static void WalletShowInfo(CWallet *wallet_instance) { | ||||
// lock required because of some AssertLockHeld() | // lock required because of some AssertLockHeld() | ||||
LOCK(wallet_instance->cs_wallet); | LOCK(wallet_instance->cs_wallet); | ||||
fprintf(stdout, "Wallet info\n===========\n"); | tfm::format(std::cout, "Wallet info\n===========\n"); | ||||
fprintf(stdout, "Encrypted: %s\n", | tfm::format(std::cout, "Encrypted: %s\n", | ||||
wallet_instance->IsCrypted() ? "yes" : "no"); | wallet_instance->IsCrypted() ? "yes" : "no"); | ||||
fprintf(stdout, "HD (hd seed available): %s\n", | tfm::format(std::cout, "HD (hd seed available): %s\n", | ||||
wallet_instance->GetHDChain().seed_id.IsNull() ? "no" : "yes"); | wallet_instance->GetHDChain().seed_id.IsNull() ? "no" : "yes"); | ||||
fprintf(stdout, "Keypool Size: %u\n", wallet_instance->GetKeyPoolSize()); | tfm::format(std::cout, "Keypool Size: %u\n", | ||||
fprintf(stdout, "Transactions: %zu\n", wallet_instance->mapWallet.size()); | wallet_instance->GetKeyPoolSize()); | ||||
fprintf(stdout, "Address Book: %zu\n", | tfm::format(std::cout, "Transactions: %zu\n", | ||||
wallet_instance->mapWallet.size()); | |||||
tfm::format(std::cout, "Address Book: %zu\n", | |||||
wallet_instance->mapAddressBook.size()); | wallet_instance->mapAddressBook.size()); | ||||
} | } | ||||
bool ExecuteWalletToolFunc(const std::string &command, | bool ExecuteWalletToolFunc(const std::string &command, | ||||
const std::string &name) { | const std::string &name) { | ||||
fs::path path = fs::absolute(name, GetWalletDir()); | fs::path path = fs::absolute(name, GetWalletDir()); | ||||
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)) { | ||||
fprintf(stderr, "Error: no wallet file at %s\n", name.c_str()); | tfm::format(std::cerr, "Error: no wallet file at %s\n", | ||||
name.c_str()); | |||||
return false; | return false; | ||||
} | } | ||||
std::string error; | std::string error; | ||||
if (!WalletBatch::VerifyEnvironment(path, error)) { | if (!WalletBatch::VerifyEnvironment(path, error)) { | ||||
fprintf( | tfm::format( | ||||
stderr, | 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.c_str()); | ||||
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 { | ||||
fprintf(stderr, "Invalid command: %s\n", command.c_str()); | tfm::format(std::cerr, "Invalid command: %s\n", command.c_str()); | ||||
return false; | return false; | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
} // namespace WalletTool | } // namespace WalletTool |