Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
wallettool.cpp
Go to the documentation of this file.
1// Copyright (c) 2016-2018 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 <chainparams.h>
6#include <common/system.h>
7#include <util/fs.h>
8#include <util/translation.h>
9#include <wallet/salvage.h>
10#include <wallet/wallet.h>
11#include <wallet/walletutil.h>
12
13#include <stdexcept>
14
15namespace WalletTool {
16
17// The standard wallet deleter function blocks on the validation interface
18// queue, which doesn't exist for the bitcoin-wallet. Define our own
19// deleter here.
21 wallet->WalletLogPrintf("Releasing wallet\n");
22 wallet->Close();
23 delete wallet;
24}
25
27 LOCK(wallet_instance->cs_wallet);
28
29 wallet_instance->SetMinVersion(FEATURE_HD_SPLIT);
30
31 // generate a new HD seed
32 auto spk_man = wallet_instance->GetOrCreateLegacyScriptPubKeyMan();
33 CPubKey seed = spk_man->GenerateNewSeed();
34 spk_man->SetHDSeed(seed);
35
36 tfm::format(std::cout, "Topping up keypool...\n");
37 wallet_instance->TopUpKeyPool();
38}
39
40static std::shared_ptr<CWallet> MakeWallet(const std::string &name,
41 const fs::path &path, bool create) {
42 DatabaseOptions options;
43 DatabaseStatus status;
44 if (create) {
45 options.require_create = true;
46 } else {
47 options.require_existing = true;
48 }
50 std::unique_ptr<WalletDatabase> database =
51 MakeDatabase(path, options, status, error);
52 if (!database) {
53 tfm::format(std::cerr, "%s\n", error.original);
54 return nullptr;
55 }
56
57 // dummy chain interface
58 std::shared_ptr<CWallet> wallet_instance{
59 new CWallet(nullptr /* chain */, name, std::move(database)),
62 try {
63 bool first_run;
65 } catch (const std::runtime_error &) {
67 std::cerr,
68 "Error loading %s. Is wallet being used by another process?\n",
69 name);
70 return nullptr;
71 }
72
74 wallet_instance = nullptr;
76 tfm::format(std::cerr, "Error loading %s: Wallet corrupted", name);
77 return nullptr;
80 std::cerr,
81 "Error reading %s! All keys read correctly, but transaction "
82 "data or address book entries might be missing or incorrect.",
83 name);
84 } else if (load_wallet_ret == DBErrors::TOO_NEW) {
85 tfm::format(std::cerr,
86 "Error loading %s: Wallet requires newer version of %s",
88 return nullptr;
90 tfm::format(std::cerr,
91 "Wallet needed to be rewritten: restart %s to complete",
93 return nullptr;
94 } else {
95 tfm::format(std::cerr, "Error loading %s", name);
96 return nullptr;
97 }
98 }
99
100 if (create) {
102 }
103
104 return wallet_instance;
105}
106
108 LOCK(wallet_instance->cs_wallet);
109
110 tfm::format(std::cout, "Wallet info\n===========\n");
111 tfm::format(std::cout, "Encrypted: %s\n",
112 wallet_instance->IsCrypted() ? "yes" : "no");
113 tfm::format(std::cout, "HD (hd seed available): %s\n",
114 wallet_instance->IsHDEnabled() ? "yes" : "no");
115 tfm::format(std::cout, "Keypool Size: %u\n",
116 wallet_instance->GetKeyPoolSize());
117 tfm::format(std::cout, "Transactions: %zu\n",
118 wallet_instance->mapWallet.size());
119 tfm::format(std::cout, "Address Book: %zu\n",
120 wallet_instance->m_address_book.size());
121}
122
123bool ExecuteWalletToolFunc(const std::string &command,
124 const std::string &name) {
125 const fs::path path =
127
128 if (command == "create") {
129 std::shared_ptr<CWallet> wallet_instance =
130 MakeWallet(name, path, /* create= */ true);
131 if (wallet_instance) {
133 wallet_instance->Close();
134 }
135 } else if (command == "info" || command == "salvage") {
136 if (command == "info") {
137 std::shared_ptr<CWallet> wallet_instance =
138 MakeWallet(name, path, /* create= */ false);
139 if (!wallet_instance) {
140 return false;
141 }
143 wallet_instance->Close();
144 } else if (command == "salvage") {
146 std::vector<bilingual_str> warnings;
147 bool ret = RecoverDatabaseFile(path, error, warnings);
148 if (!ret) {
149 for (const auto &warning : warnings) {
150 tfm::format(std::cerr, "%s\n", warning.original);
151 }
152 if (!error.empty()) {
153 tfm::format(std::cerr, "%s\n", error.original);
154 }
155 }
156 return ret;
157 }
158 } else {
159 tfm::format(std::cerr, "Invalid command: %s\n", command);
160 return false;
161 }
162
163 return true;
164}
165} // namespace WalletTool
An encapsulated public key.
Definition pubkey.h:31
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition wallet.h:254
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition fs.h:30
DBErrors LoadWallet(bool &fFirstRunRet)
Definition wallet.cpp:2171
bool error(const char *fmt, const Args &...args)
Definition logging.h:226
static void WalletCreate(CWallet *wallet_instance)
static void WalletToolReleaseWallet(CWallet *wallet)
bool ExecuteWalletToolFunc(const std::string &command, const std::string &name)
static std::shared_ptr< CWallet > MakeWallet(const std::string &name, const fs::path &path, bool create)
static void WalletShowInfo(CWallet *wallet_instance)
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition fs.h:165
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
Definition fs.cpp:39
void format(std::ostream &out, const char *fmt, const Args &...args)
Format list of arguments to the stream according to given format string.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
const char * name
Definition rest.cpp:47
bool RecoverDatabaseFile(const fs::path &file_path, bilingual_str &error, std::vector< bilingual_str > &warnings)
Definition salvage.cpp:24
bool require_create
Definition db.h:223
bool require_existing
Definition db.h:222
Bilingual messages:
Definition translation.h:17
#define LOCK(cs)
Definition sync.h:306
std::unique_ptr< WalletDatabase > MakeDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
DatabaseStatus
Definition db.h:229
DBErrors
Error statuses for the wallet database.
Definition walletdb.h:45
@ NONCRITICAL_ERROR
fs::path GetWalletDir()
Get the path of the wallet directory.
@ FEATURE_HD_SPLIT
Definition walletutil.h:28