Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
wallettool.cpp
Go to the documentation of this file.
1// Copyright (c) 2016-present 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 <wallet/wallettool.h>
6
7#include <common/args.h>
8#include <util/check.h>
9#include <util/fs.h>
10#include <util/translation.h>
11#include <wallet/dump.h>
12#include <wallet/wallet.h>
13#include <wallet/walletutil.h>
14
15namespace wallet {
16namespace WalletTool {
17
18// The standard wallet deleter function blocks on the validation interface
19// queue, which doesn't exist for the bitcoin-wallet. Define our own
20// deleter here.
22{
23 wallet->WalletLogPrintf("Releasing wallet");
24 wallet->Close();
25 delete wallet;
26}
27
29{
30 LOCK(wallet_instance->cs_wallet);
31
32 wallet_instance->InitWalletFlags(wallet_creation_flags);
33
35 wallet_instance->SetupDescriptorScriptPubKeyMans();
36
37 tfm::format(std::cout, "Topping up keypool...\n");
38 wallet_instance->TopUpKeyPool();
39}
40
41static std::shared_ptr<CWallet> MakeWallet(const std::string& name, const fs::path& path, DatabaseOptions options)
42{
43 DatabaseStatus status;
44 bilingual_str error;
45 std::vector<bilingual_str> warnings;
46 std::unique_ptr<WalletDatabase> database = MakeDatabase(path, options, status, error);
47 if (!database) {
48 tfm::format(std::cerr, "%s\n", error.original);
49 return nullptr;
50 }
51
52 // dummy chain interface
53 std::shared_ptr<CWallet> wallet_instance{new CWallet(/*chain=*/nullptr, name, std::move(database)), WalletToolReleaseWallet};
55 try {
57 } catch (const std::runtime_error&) {
58 tfm::format(std::cerr, "Error loading %s. Is wallet being used by another process?\n", name);
59 return nullptr;
60 }
61
62 if (!error.empty()) {
63 tfm::format(std::cerr, "%s", error.original);
64 }
65
66 for (const auto &warning : warnings) {
67 tfm::format(std::cerr, "%s", warning.original);
68 }
69
71 return nullptr;
72 }
73
74 if (options.require_create) WalletCreate(wallet_instance.get(), options.create_flags);
75
76 return wallet_instance;
77}
78
80{
81 LOCK(wallet_instance->cs_wallet);
82
83 tfm::format(std::cout, "Wallet info\n===========\n");
84 tfm::format(std::cout, "Name: %s\n", wallet_instance->GetName());
85 tfm::format(std::cout, "Format: %s\n", wallet_instance->GetDatabase().Format());
86 tfm::format(std::cout, "Descriptors: %s\n", wallet_instance->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS) ? "yes" : "no");
87 tfm::format(std::cout, "Encrypted: %s\n", wallet_instance->HasEncryptionKeys() ? "yes" : "no");
88 tfm::format(std::cout, "HD (hd seed available): %s\n", wallet_instance->IsHDEnabled() ? "yes" : "no");
89 tfm::format(std::cout, "Keypool Size: %u\n", wallet_instance->GetKeyPoolSize());
90 tfm::format(std::cout, "Transactions: %zu\n", wallet_instance->mapWallet.size());
91 tfm::format(std::cout, "Address Book: %zu\n", wallet_instance->m_address_book.size());
92}
93
94bool ExecuteWalletToolFunc(const ArgsManager& args, const std::string& command)
95{
96 {
97 std::vector<std::string> details;
98 if (!args.CheckCommandOptions(command, &details)) {
99 tfm::format(std::cerr, "Error: Invalid arguments provided:\n%s\n", util::MakeUnorderedList(details));
100 return false;
101 }
102 }
103 if ((command == "create" || command == "createfromdump") && !args.IsArgSet("-wallet")) {
104 tfm::format(std::cerr, "Wallet name must be provided when creating a new wallet.\n");
105 return false;
106 }
107 const std::string name = args.GetArg("-wallet", "");
109 if (!path_res) {
110 tfm::format(std::cerr, "%s\n", util::ErrorString(path_res).original);
111 return false;
112 }
113 const fs::path& path = *path_res;
114
115 if (command == "create") {
116 if (name.empty()) {
117 tfm::format(std::cerr, "Wallet name cannot be empty\n");
118 return false;
119 }
120 DatabaseOptions options;
121 ReadDatabaseArgs(args, options);
122 options.require_create = true;
125
126 const std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
127 if (!wallet_instance) return false;
129 wallet_instance->Close();
130 } else if (command == "info") {
131 DatabaseOptions options;
132 ReadDatabaseArgs(args, options);
133 options.require_existing = true;
134 const std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
135 if (!wallet_instance) return false;
137 wallet_instance->Close();
138 } else if (command == "dump") {
139 DatabaseOptions options;
140 ReadDatabaseArgs(args, options);
141 options.require_existing = true;
142 DatabaseStatus status;
143
144 if (IsBDBFile(BDBDataFile(path))) {
146 }
147
148 bilingual_str error;
149 std::unique_ptr<WalletDatabase> database = MakeDatabase(path, options, status, error);
150 if (!database) {
151 tfm::format(std::cerr, "%s\n", error.original);
152 return false;
153 }
154
155 bool ret = DumpWallet(args, *database, error);
156 if (!ret && !error.empty()) {
157 tfm::format(std::cerr, "%s\n", error.original);
158 return ret;
159 }
160 tfm::format(std::cout, "The dumpfile may contain private keys. To ensure the safety of your Bitcoin, do not share the dumpfile.\n");
161 return ret;
162 } else if (command == "createfromdump") {
163 bilingual_str error;
164 bool ret = CreateFromDump(args, name, path, error);
165 if (!ret && !error.empty()) {
166 tfm::format(std::cerr, "%s\n", error.original);
167 }
168 return ret;
169 } else {
170 tfm::format(std::cerr, "Invalid command: %s\n", command);
171 return false;
172 }
173
174 return true;
175}
176} // namespace WalletTool
177} // namespace wallet
int ret
const auto command
ArgsManager & args
Definition bitcoind.cpp:280
#define Assert(val)
Identity function.
Definition check.h:119
bool CheckCommandOptions(const std::string &command, std::vector< std::string > *errors=nullptr) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Check that any command-specific options the user specified are valid for the given command.
Definition args.cpp:388
std::string GetArg(const std::string &strArg, const std::string &strDefault) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return string argument or default value.
Definition args.cpp:517
bool IsArgSet(const std::string &strArg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return true if the given argument has been manually set.
Definition args.cpp:431
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition wallet.h:318
DBErrors PopulateWalletFromDB(bilingual_str &error, std::vector< bilingual_str > &warnings)
Definition wallet.cpp:2185
void format(std::ostream &out, FormatStringCheck< sizeof...(Args)> fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
bilingual_str ErrorString(const Result< T > &result)
Definition result.h:93
std::string MakeUnorderedList(const std::vector< std::string > &items)
Create an unordered multi-line list of items.
Definition string.h:230
static std::shared_ptr< CWallet > MakeWallet(const std::string &name, const fs::path &path, DatabaseOptions options)
static void WalletShowInfo(CWallet *wallet_instance)
static void WalletToolReleaseWallet(CWallet *wallet)
bool ExecuteWalletToolFunc(const ArgsManager &args, const std::string &command)
static void WalletCreate(CWallet *wallet_instance, uint64_t wallet_creation_flags)
void ReadDatabaseArgs(const ArgsManager &args, DatabaseOptions &options)
Definition db.cpp:153
std::unique_ptr< WalletDatabase > MakeDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
DBErrors
Overview of wallet database classes:
Definition walletdb.h:46
bool CreateFromDump(const ArgsManager &args, const std::string &name, const fs::path &wallet_path, bilingual_str &error)
Definition dump.cpp:122
bool IsBDBFile(const fs::path &path)
Definition db.cpp:94
fs::path BDBDataFile(const fs::path &wallet_path)
Definition db.cpp:75
bool DumpWallet(const ArgsManager &args, WalletDatabase &db, bilingual_str &error)
Definition dump.cpp:24
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition walletutil.h:53
util::Result< fs::path > GetWalletPath(const std::string &name)
Determine the path that the wallet is stored in.
Definition wallet.cpp:2738
DatabaseStatus
Definition db.h:180
const char * name
Definition rest.cpp:71
Bilingual messages:
Definition translation.h:24
bool empty() const
Definition translation.h:35
std::string original
Definition translation.h:25
std::optional< DatabaseFormat > require_format
Definition db.h:171
uint64_t create_flags
Definition db.h:172
#define LOCK(cs)
Definition sync.h:268
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82