Branch data Line data Source code
1 : : // Copyright (c) 2016-2022 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 <bitcoin-build-config.h> // IWYU pragma: keep
6 : :
7 : : #include <wallet/wallettool.h>
8 : :
9 : : #include <common/args.h>
10 : : #include <util/check.h>
11 : : #include <util/fs.h>
12 : : #include <util/translation.h>
13 : : #include <wallet/dump.h>
14 : : #include <wallet/wallet.h>
15 : : #include <wallet/walletutil.h>
16 : :
17 : : namespace wallet {
18 : : namespace WalletTool {
19 : :
20 : : // The standard wallet deleter function blocks on the validation interface
21 : : // queue, which doesn't exist for the bitcoin-wallet. Define our own
22 : : // deleter here.
23 : 0 : static void WalletToolReleaseWallet(CWallet* wallet)
24 : : {
25 : 0 : wallet->WalletLogPrintf("Releasing wallet\n");
26 : 0 : wallet->Close();
27 [ # # ]: 0 : delete wallet;
28 : 0 : }
29 : :
30 : 0 : static void WalletCreate(CWallet* wallet_instance, uint64_t wallet_creation_flags)
31 : : {
32 : 0 : LOCK(wallet_instance->cs_wallet);
33 : :
34 [ # # ]: 0 : wallet_instance->InitWalletFlags(wallet_creation_flags);
35 : :
36 [ # # # # ]: 0 : Assert(wallet_instance->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
37 [ # # ]: 0 : wallet_instance->SetupDescriptorScriptPubKeyMans();
38 : :
39 [ # # ]: 0 : tfm::format(std::cout, "Topping up keypool...\n");
40 [ # # ]: 0 : wallet_instance->TopUpKeyPool();
41 : 0 : }
42 : :
43 : 0 : static std::shared_ptr<CWallet> MakeWallet(const std::string& name, const fs::path& path, DatabaseOptions options)
44 : : {
45 : 0 : DatabaseStatus status;
46 [ # # ]: 0 : bilingual_str error;
47 [ # # ]: 0 : std::unique_ptr<WalletDatabase> database = MakeDatabase(path, options, status, error);
48 [ # # ]: 0 : if (!database) {
49 [ # # ]: 0 : tfm::format(std::cerr, "%s\n", error.original);
50 : 0 : return nullptr;
51 : : }
52 : :
53 : : // dummy chain interface
54 [ # # # # : 0 : std::shared_ptr<CWallet> wallet_instance{new CWallet(/*chain=*/nullptr, name, std::move(database)), WalletToolReleaseWallet};
# # # # ]
55 : 0 : DBErrors load_wallet_ret;
56 : 0 : try {
57 [ # # ]: 0 : load_wallet_ret = wallet_instance->LoadWallet();
58 [ - - ]: 0 : } catch (const std::runtime_error&) {
59 [ - - ]: 0 : tfm::format(std::cerr, "Error loading %s. Is wallet being used by another process?\n", name);
60 : 0 : return nullptr;
61 : 0 : }
62 : :
63 [ # # ]: 0 : if (load_wallet_ret != DBErrors::LOAD_OK) {
64 [ # # ]: 0 : if (load_wallet_ret == DBErrors::CORRUPT) {
65 [ # # ]: 0 : tfm::format(std::cerr, "Error loading %s: Wallet corrupted", name);
66 : 0 : return nullptr;
67 [ # # ]: 0 : } else if (load_wallet_ret == DBErrors::NONCRITICAL_ERROR) {
68 [ # # ]: 0 : tfm::format(std::cerr, "Error reading %s! All keys read correctly, but transaction data"
69 : : " or address book entries might be missing or incorrect.",
70 : : name);
71 [ # # ]: 0 : } else if (load_wallet_ret == DBErrors::TOO_NEW) {
72 [ # # ]: 0 : tfm::format(std::cerr, "Error loading %s: Wallet requires newer version of %s",
73 : : name, CLIENT_NAME);
74 : 0 : return nullptr;
75 [ # # ]: 0 : } else if (load_wallet_ret == DBErrors::NEED_REWRITE) {
76 [ # # ]: 0 : tfm::format(std::cerr, "Wallet needed to be rewritten: restart %s to complete", CLIENT_NAME);
77 : 0 : return nullptr;
78 [ # # ]: 0 : } else if (load_wallet_ret == DBErrors::NEED_RESCAN) {
79 [ # # ]: 0 : tfm::format(std::cerr, "Error reading %s! Some transaction data might be missing or"
80 : : " incorrect. Wallet requires a rescan.",
81 : : name);
82 : : } else {
83 [ # # ]: 0 : tfm::format(std::cerr, "Error loading %s", name);
84 : 0 : return nullptr;
85 : : }
86 : : }
87 : :
88 [ # # # # ]: 0 : if (options.require_create) WalletCreate(wallet_instance.get(), options.create_flags);
89 : :
90 : 0 : return wallet_instance;
91 : 0 : }
92 : :
93 : 0 : static void WalletShowInfo(CWallet* wallet_instance)
94 : : {
95 : 0 : LOCK(wallet_instance->cs_wallet);
96 : :
97 [ # # ]: 0 : tfm::format(std::cout, "Wallet info\n===========\n");
98 [ # # ]: 0 : tfm::format(std::cout, "Name: %s\n", wallet_instance->GetName());
99 [ # # # # ]: 0 : tfm::format(std::cout, "Format: %s\n", wallet_instance->GetDatabase().Format());
100 [ # # # # : 0 : tfm::format(std::cout, "Descriptors: %s\n", wallet_instance->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS) ? "yes" : "no");
# # ]
101 [ # # # # : 0 : tfm::format(std::cout, "Encrypted: %s\n", wallet_instance->IsCrypted() ? "yes" : "no");
# # ]
102 [ # # # # : 0 : tfm::format(std::cout, "HD (hd seed available): %s\n", wallet_instance->IsHDEnabled() ? "yes" : "no");
# # ]
103 [ # # # # ]: 0 : tfm::format(std::cout, "Keypool Size: %u\n", wallet_instance->GetKeyPoolSize());
104 [ # # ]: 0 : tfm::format(std::cout, "Transactions: %zu\n", wallet_instance->mapWallet.size());
105 [ # # ]: 0 : tfm::format(std::cout, "Address Book: %zu\n", wallet_instance->m_address_book.size());
106 : 0 : }
107 : :
108 : 0 : bool ExecuteWalletToolFunc(const ArgsManager& args, const std::string& command)
109 : : {
110 [ # # # # : 0 : if (args.IsArgSet("-dumpfile") && command != "dump" && command != "createfromdump") {
# # # # #
# ]
111 : 0 : tfm::format(std::cerr, "The -dumpfile option can only be used with the \"dump\" and \"createfromdump\" commands.\n");
112 : 0 : return false;
113 : : }
114 [ # # # # : 0 : if (command == "create" && !args.IsArgSet("-wallet")) {
# # # # ]
115 : 0 : tfm::format(std::cerr, "Wallet name must be provided when creating a new wallet.\n");
116 : 0 : return false;
117 : : }
118 [ # # # # ]: 0 : const std::string name = args.GetArg("-wallet", "");
119 [ # # # # : 0 : const fs::path path = fsbridge::AbsPathJoin(GetWalletDir(), fs::PathFromString(name));
# # ]
120 : :
121 [ # # ]: 0 : if (command == "create") {
122 [ # # ]: 0 : DatabaseOptions options;
123 [ # # ]: 0 : ReadDatabaseArgs(args, options);
124 : 0 : options.require_create = true;
125 : 0 : options.create_flags |= WALLET_FLAG_DESCRIPTORS;
126 : 0 : options.require_format = DatabaseFormat::SQLITE;
127 : :
128 [ # # # # ]: 0 : const std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
129 [ # # ]: 0 : if (wallet_instance) {
130 [ # # ]: 0 : WalletShowInfo(wallet_instance.get());
131 [ # # ]: 0 : wallet_instance->Close();
132 : : }
133 [ # # ]: 0 : } else if (command == "info") {
134 [ # # ]: 0 : DatabaseOptions options;
135 [ # # ]: 0 : ReadDatabaseArgs(args, options);
136 : 0 : options.require_existing = true;
137 [ # # # # ]: 0 : const std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
138 [ # # # # ]: 0 : if (!wallet_instance) return false;
139 [ # # ]: 0 : WalletShowInfo(wallet_instance.get());
140 [ # # ]: 0 : wallet_instance->Close();
141 [ # # ]: 0 : } else if (command == "dump") {
142 [ # # ]: 0 : DatabaseOptions options;
143 [ # # ]: 0 : ReadDatabaseArgs(args, options);
144 : 0 : options.require_existing = true;
145 : 0 : DatabaseStatus status;
146 : :
147 [ # # # # : 0 : if (IsBDBFile(BDBDataFile(path))) {
# # ]
148 : 0 : options.require_format = DatabaseFormat::BERKELEY_RO;
149 : : }
150 : :
151 [ # # ]: 0 : bilingual_str error;
152 [ # # ]: 0 : std::unique_ptr<WalletDatabase> database = MakeDatabase(path, options, status, error);
153 [ # # ]: 0 : if (!database) {
154 [ # # ]: 0 : tfm::format(std::cerr, "%s\n", error.original);
155 : : return false;
156 : : }
157 : :
158 [ # # ]: 0 : bool ret = DumpWallet(args, *database, error);
159 [ # # # # ]: 0 : if (!ret && !error.empty()) {
160 [ # # ]: 0 : tfm::format(std::cerr, "%s\n", error.original);
161 : : return ret;
162 : : }
163 [ # # ]: 0 : tfm::format(std::cout, "The dumpfile may contain private keys. To ensure the safety of your Bitcoin, do not share the dumpfile.\n");
164 : : return ret;
165 [ # # ]: 0 : } else if (command == "createfromdump") {
166 [ # # ]: 0 : bilingual_str error;
167 : 0 : std::vector<bilingual_str> warnings;
168 [ # # ]: 0 : bool ret = CreateFromDump(args, name, path, error, warnings);
169 [ # # ]: 0 : for (const auto& warning : warnings) {
170 [ # # ]: 0 : tfm::format(std::cout, "%s\n", warning.original);
171 : : }
172 [ # # # # ]: 0 : if (!ret && !error.empty()) {
173 [ # # ]: 0 : tfm::format(std::cerr, "%s\n", error.original);
174 : : }
175 : 0 : return ret;
176 : 0 : } else {
177 [ # # ]: 0 : tfm::format(std::cerr, "Invalid command: %s\n", command);
178 : : return false;
179 : : }
180 : :
181 : : return true;
182 : 0 : }
183 : : } // namespace WalletTool
184 : : } // namespace wallet
|