Branch data Line data Source code
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 : :
15 : : namespace wallet {
16 : : namespace 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.
21 : 4 : static void WalletToolReleaseWallet(CWallet* wallet)
22 : : {
23 : 4 : wallet->WalletLogPrintf("Releasing wallet");
24 : 4 : wallet->Close();
25 [ + - ]: 4 : delete wallet;
26 : 4 : }
27 : :
28 : 1 : static void WalletCreate(CWallet* wallet_instance, uint64_t wallet_creation_flags)
29 : : {
30 : 1 : LOCK(wallet_instance->cs_wallet);
31 : :
32 [ + - ]: 1 : wallet_instance->InitWalletFlags(wallet_creation_flags);
33 : :
34 [ + - + - ]: 1 : Assert(wallet_instance->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
35 [ + - ]: 1 : wallet_instance->SetupDescriptorScriptPubKeyMans();
36 : :
37 [ + - ]: 1 : tfm::format(std::cout, "Topping up keypool...\n");
38 [ + - ]: 1 : wallet_instance->TopUpKeyPool();
39 : 1 : }
40 : :
41 : 7 : static std::shared_ptr<CWallet> MakeWallet(const std::string& name, const fs::path& path, DatabaseOptions options)
42 : : {
43 : 7 : DatabaseStatus status;
44 [ + - ]: 7 : bilingual_str error;
45 : 7 : std::vector<bilingual_str> warnings;
46 [ + - ]: 7 : std::unique_ptr<WalletDatabase> database = MakeDatabase(path, options, status, error);
47 [ + + ]: 7 : if (!database) {
48 [ + - ]: 3 : tfm::format(std::cerr, "%s\n", error.original);
49 : 3 : return nullptr;
50 : : }
51 : :
52 : : // dummy chain interface
53 [ + - + - : 8 : std::shared_ptr<CWallet> wallet_instance{new CWallet(/*chain=*/nullptr, name, std::move(database)), WalletToolReleaseWallet};
+ - - - ]
54 : 4 : DBErrors load_wallet_ret;
55 : 4 : try {
56 [ + - ]: 4 : load_wallet_ret = wallet_instance->PopulateWalletFromDB(error, warnings);
57 [ - - ]: 0 : } catch (const std::runtime_error&) {
58 [ - - ]: 0 : tfm::format(std::cerr, "Error loading %s. Is wallet being used by another process?\n", name);
59 : 0 : return nullptr;
60 : 0 : }
61 : :
62 [ - + ]: 4 : if (!error.empty()) {
63 [ # # ]: 0 : tfm::format(std::cerr, "%s", error.original);
64 : : }
65 : :
66 [ - + ]: 4 : for (const auto &warning : warnings) {
67 [ # # ]: 0 : tfm::format(std::cerr, "%s", warning.original);
68 : : }
69 : :
70 [ - + - - ]: 4 : if (load_wallet_ret != DBErrors::LOAD_OK && load_wallet_ret != DBErrors::NONCRITICAL_ERROR && load_wallet_ret != DBErrors::NEED_RESCAN) {
71 : 0 : return nullptr;
72 : : }
73 : :
74 [ + + + - ]: 4 : if (options.require_create) WalletCreate(wallet_instance.get(), options.create_flags);
75 : :
76 : 4 : return wallet_instance;
77 : 14 : }
78 : :
79 : 4 : static void WalletShowInfo(CWallet* wallet_instance)
80 : : {
81 : 4 : LOCK(wallet_instance->cs_wallet);
82 : :
83 [ + - ]: 4 : tfm::format(std::cout, "Wallet info\n===========\n");
84 [ + - ]: 4 : tfm::format(std::cout, "Name: %s\n", wallet_instance->GetName());
85 [ + - + - ]: 4 : tfm::format(std::cout, "Format: %s\n", wallet_instance->GetDatabase().Format());
86 [ + - - + : 4 : tfm::format(std::cout, "Descriptors: %s\n", wallet_instance->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS) ? "yes" : "no");
+ - ]
87 [ + - + - : 8 : tfm::format(std::cout, "Encrypted: %s\n", wallet_instance->HasEncryptionKeys() ? "yes" : "no");
+ - ]
88 [ + - - + : 4 : tfm::format(std::cout, "HD (hd seed available): %s\n", wallet_instance->IsHDEnabled() ? "yes" : "no");
+ - ]
89 [ + - + - ]: 4 : tfm::format(std::cout, "Keypool Size: %u\n", wallet_instance->GetKeyPoolSize());
90 [ + - ]: 4 : tfm::format(std::cout, "Transactions: %zu\n", wallet_instance->mapWallet.size());
91 [ + - ]: 4 : tfm::format(std::cout, "Address Book: %zu\n", wallet_instance->m_address_book.size());
92 : 4 : }
93 : :
94 : 31 : bool ExecuteWalletToolFunc(const ArgsManager& args, const std::string& command)
95 : : {
96 : 31 : {
97 : 31 : std::vector<std::string> details;
98 [ + - + + ]: 31 : if (!args.CheckCommandOptions(command, &details)) {
99 [ + - + - ]: 1 : tfm::format(std::cerr, "Error: Invalid arguments provided:\n%s\n", util::MakeUnorderedList(details));
100 : 1 : return false;
101 : : }
102 : 31 : }
103 [ + + + + : 48 : if ((command == "create" || command == "createfromdump") && !args.IsArgSet("-wallet")) {
+ - + + +
+ ]
104 : 2 : tfm::format(std::cerr, "Wallet name must be provided when creating a new wallet.\n");
105 : 2 : return false;
106 : : }
107 [ + - + - ]: 56 : const std::string name = args.GetArg("-wallet", "");
108 [ + - ]: 28 : util::Result<fs::path> path_res = GetWalletPath(name);
109 [ - + ]: 28 : if (!path_res) {
110 [ # # # # ]: 0 : tfm::format(std::cerr, "%s\n", util::ErrorString(path_res).original);
111 : 0 : return false;
112 : : }
113 : 28 : const fs::path& path = *path_res;
114 : :
115 [ + + ]: 28 : if (command == "create") {
116 [ + + ]: 3 : if (name.empty()) {
117 [ + - ]: 1 : tfm::format(std::cerr, "Wallet name cannot be empty\n");
118 : : return false;
119 : : }
120 [ + - ]: 2 : DatabaseOptions options;
121 [ + - ]: 2 : ReadDatabaseArgs(args, options);
122 : 2 : options.require_create = true;
123 : 2 : options.create_flags |= WALLET_FLAG_DESCRIPTORS;
124 : 2 : options.require_format = DatabaseFormat::SQLITE;
125 : :
126 [ + - + - ]: 2 : const std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
127 [ + + - + ]: 2 : if (!wallet_instance) return false;
128 [ + - ]: 1 : WalletShowInfo(wallet_instance.get());
129 [ + - ]: 1 : wallet_instance->Close();
130 [ + + ]: 27 : } else if (command == "info") {
131 [ + - ]: 5 : DatabaseOptions options;
132 [ + - ]: 5 : ReadDatabaseArgs(args, options);
133 : 5 : options.require_existing = true;
134 [ + - + - ]: 5 : const std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
135 [ + + - + ]: 5 : if (!wallet_instance) return false;
136 [ + - ]: 3 : WalletShowInfo(wallet_instance.get());
137 [ + - ]: 3 : wallet_instance->Close();
138 [ + + ]: 25 : } else if (command == "dump") {
139 [ + - ]: 7 : DatabaseOptions options;
140 [ + - ]: 7 : ReadDatabaseArgs(args, options);
141 : 7 : options.require_existing = true;
142 : 7 : DatabaseStatus status;
143 : :
144 [ + - + - : 14 : if (IsBDBFile(BDBDataFile(path))) {
- + ]
145 : 0 : options.require_format = DatabaseFormat::BERKELEY_RO;
146 : : }
147 : :
148 [ + - ]: 7 : bilingual_str error;
149 [ + - ]: 7 : std::unique_ptr<WalletDatabase> database = MakeDatabase(path, options, status, error);
150 [ - + ]: 7 : if (!database) {
151 [ # # ]: 0 : tfm::format(std::cerr, "%s\n", error.original);
152 : : return false;
153 : : }
154 : :
155 [ + - ]: 7 : bool ret = DumpWallet(args, *database, error);
156 [ + + - + ]: 7 : if (!ret && !error.empty()) {
157 [ + - ]: 2 : tfm::format(std::cerr, "%s\n", error.original);
158 : : return ret;
159 : : }
160 [ + - ]: 5 : 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 [ + - ]: 27 : } else if (command == "createfromdump") {
163 [ + - ]: 13 : bilingual_str error;
164 [ + - ]: 13 : bool ret = CreateFromDump(args, name, path, error);
165 [ + + + + ]: 13 : if (!ret && !error.empty()) {
166 [ + - ]: 10 : tfm::format(std::cerr, "%s\n", error.original);
167 : : }
168 : 13 : return ret;
169 : 13 : } else {
170 [ # # ]: 0 : tfm::format(std::cerr, "Invalid command: %s\n", command);
171 : : return false;
172 : : }
173 : :
174 : : return true;
175 : 28 : }
176 : : } // namespace WalletTool
177 : : } // namespace wallet
|