Bitcoin Core  0.19.99
P2P Digital Currency
wallettool.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-2019 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <fs.h>
6 #include <util/system.h>
7 #include <wallet/wallet.h>
8 #include <wallet/walletutil.h>
9 
10 namespace WalletTool {
11 
12 // The standard wallet deleter function blocks on the validation interface
13 // queue, which doesn't exist for the bitcoin-wallet. Define our own
14 // deleter here.
15 static void WalletToolReleaseWallet(CWallet* wallet)
16 {
17  wallet->WalletLogPrintf("Releasing wallet\n");
18  wallet->Flush(true);
19  delete wallet;
20 }
21 
22 static std::shared_ptr<CWallet> CreateWallet(const std::string& name, const fs::path& path)
23 {
24  if (fs::exists(path)) {
25  tfm::format(std::cerr, "Error: File exists already\n");
26  return nullptr;
27  }
28  // dummy chain interface
29  std::shared_ptr<CWallet> wallet_instance(new CWallet(nullptr /* chain */, WalletLocation(name), WalletDatabase::Create(path)), WalletToolReleaseWallet);
30  LOCK(wallet_instance->cs_wallet);
31  bool first_run = true;
32  DBErrors load_wallet_ret = wallet_instance->LoadWallet(first_run);
33  if (load_wallet_ret != DBErrors::LOAD_OK) {
34  tfm::format(std::cerr, "Error creating %s", name);
35  return nullptr;
36  }
37 
38  wallet_instance->SetMinVersion(FEATURE_HD_SPLIT);
39 
40  // generate a new HD seed
41  auto spk_man = wallet_instance->GetOrCreateLegacyScriptPubKeyMan();
42  CPubKey seed = spk_man->GenerateNewSeed();
43  spk_man->SetHDSeed(seed);
44 
45  tfm::format(std::cout, "Topping up keypool...\n");
46  wallet_instance->TopUpKeyPool();
47  return wallet_instance;
48 }
49 
50 static std::shared_ptr<CWallet> LoadWallet(const std::string& name, const fs::path& path)
51 {
52  if (!fs::exists(path)) {
53  tfm::format(std::cerr, "Error: Wallet files does not exist\n");
54  return nullptr;
55  }
56 
57  // dummy chain interface
58  std::shared_ptr<CWallet> wallet_instance(new CWallet(nullptr /* chain */, WalletLocation(name), WalletDatabase::Create(path)), WalletToolReleaseWallet);
59  DBErrors load_wallet_ret;
60  try {
61  bool first_run;
62  load_wallet_ret = wallet_instance->LoadWallet(first_run);
63  } catch (const std::runtime_error&) {
64  tfm::format(std::cerr, "Error loading %s. Is wallet being used by another process?\n", name);
65  return nullptr;
66  }
67 
68  if (load_wallet_ret != DBErrors::LOAD_OK) {
69  wallet_instance = nullptr;
70  if (load_wallet_ret == DBErrors::CORRUPT) {
71  tfm::format(std::cerr, "Error loading %s: Wallet corrupted", name);
72  return nullptr;
73  } else if (load_wallet_ret == DBErrors::NONCRITICAL_ERROR) {
74  tfm::format(std::cerr, "Error reading %s! All keys read correctly, but transaction data"
75  " or address book entries might be missing or incorrect.",
76  name);
77  } else if (load_wallet_ret == DBErrors::TOO_NEW) {
78  tfm::format(std::cerr, "Error loading %s: Wallet requires newer version of %s",
79  name, PACKAGE_NAME);
80  return nullptr;
81  } else if (load_wallet_ret == DBErrors::NEED_REWRITE) {
82  tfm::format(std::cerr, "Wallet needed to be rewritten: restart %s to complete", PACKAGE_NAME);
83  return nullptr;
84  } else {
85  tfm::format(std::cerr, "Error loading %s", name);
86  return nullptr;
87  }
88  }
89 
90  return wallet_instance;
91 }
92 
93 static void WalletShowInfo(CWallet* wallet_instance)
94 {
95  LOCK(wallet_instance->cs_wallet);
96 
97  tfm::format(std::cout, "Wallet info\n===========\n");
98  tfm::format(std::cout, "Encrypted: %s\n", wallet_instance->IsCrypted() ? "yes" : "no");
99  tfm::format(std::cout, "HD (hd seed available): %s\n", wallet_instance->IsHDEnabled() ? "yes" : "no");
100  tfm::format(std::cout, "Keypool Size: %u\n", wallet_instance->GetKeyPoolSize());
101  tfm::format(std::cout, "Transactions: %zu\n", wallet_instance->mapWallet.size());
102  tfm::format(std::cout, "Address Book: %zu\n", wallet_instance->mapAddressBook.size());
103 }
104 
105 bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
106 {
107  fs::path path = fs::absolute(name, GetWalletDir());
108 
109  if (command == "create") {
110  std::shared_ptr<CWallet> wallet_instance = CreateWallet(name, path);
111  if (wallet_instance) {
112  WalletShowInfo(wallet_instance.get());
113  wallet_instance->Flush(true);
114  }
115  } else if (command == "info") {
116  if (!fs::exists(path)) {
117  tfm::format(std::cerr, "Error: no wallet file at %s\n", name);
118  return false;
119  }
120  std::string error;
121  if (!WalletBatch::VerifyEnvironment(path, error)) {
122  tfm::format(std::cerr, "Error loading %s. Is wallet being used by other process?\n", name);
123  return false;
124  }
125  std::shared_ptr<CWallet> wallet_instance = LoadWallet(name, path);
126  if (!wallet_instance) return false;
127  WalletShowInfo(wallet_instance.get());
128  wallet_instance->Flush(true);
129  } else {
130  tfm::format(std::cerr, "Invalid command: %s\n", command);
131  return false;
132  }
133 
134  return true;
135 }
136 } // namespace WalletTool
unsigned int GetKeyPoolSize() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:3128
static void WalletToolReleaseWallet(CWallet *wallet)
Definition: wallettool.cpp:15
static std::shared_ptr< CWallet > CreateWallet(const std::string &name, const fs::path &path)
Definition: wallettool.cpp:22
static void WalletShowInfo(CWallet *wallet_instance)
Definition: wallettool.cpp:93
static std::shared_ptr< CWallet > LoadWallet(const std::string &name, const fs::path &path)
Definition: wallettool.cpp:50
#define PACKAGE_NAME
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:45
void WalletLogPrintf(std::string fmt, Params... parameters) const
Prepends the wallet name in logging output to ease debugging in multi-wallet use cases.
Definition: wallet.h:1138
static bool VerifyEnvironment(const fs::path &wallet_path, std::string &errorStr)
Definition: walletdb.cpp:731
bool IsHDEnabled() const
Definition: wallet.cpp:1328
#define LOCK(cs)
Definition: sync.h:179
const char * name
Definition: rest.cpp:40
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1062
An encapsulated public key.
Definition: pubkey.h:30
void Flush(bool shutdown=false)
Flush wallet (bitdb flush)
Definition: wallet.cpp:420
static std::unique_ptr< BerkeleyDatabase > Create(const fs::path &path)
Return object for accessing database at specified path.
Definition: db.h:136
bool IsCrypted() const
Definition: wallet.cpp:4082
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:606
fs::path GetWalletDir()
Get the path of the wallet directory.
Definition: dummywallet.cpp:60
RecursiveMutex cs_wallet
Definition: wallet.h:717
The WalletLocation class provides wallet information.
Definition: walletutil.h:67
bool ExecuteWalletToolFunc(const std::string &command, const std::string &name)
Definition: wallettool.cpp:105
bool error(const char *fmt, const Args &... args)
Definition: system.h:49