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 <bitcoin-build-config.h> // IWYU pragma: keep
6 : :
7 : : #include <chainparams.h>
8 : : #include <chainparamsbase.h>
9 : : #include <clientversion.h>
10 : : #include <common/args.h>
11 : : #include <common/license_info.h>
12 : : #include <common/system.h>
13 : : #include <compat/compat.h>
14 : : #include <crypto/sha256.h>
15 : : #include <interfaces/init.h>
16 : : #include <key.h>
17 : : #include <logging.h>
18 : : #include <pubkey.h>
19 : : #include <tinyformat.h>
20 : : #include <util/exception.h>
21 : : #include <util/translation.h>
22 : : #include <wallet/wallettool.h>
23 : :
24 : : #include <exception>
25 : : #include <functional>
26 : : #include <string>
27 : : #include <tuple>
28 : :
29 : : using util::Join;
30 : :
31 : : const TranslateFn G_TRANSLATION_FUN{nullptr};
32 : :
33 : 38 : static void SetupWalletToolArgs(ArgsManager& argsman)
34 : : {
35 : 38 : SetupHelpOptions(argsman);
36 : 38 : SetupChainParamsBaseOptions(argsman);
37 : :
38 [ + - + - ]: 76 : argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
39 [ + - + - ]: 76 : argsman.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::OPTIONS);
40 [ + - + - ]: 76 : argsman.AddArg("-wallet=<wallet-name>", "Specify wallet name", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::OPTIONS);
41 [ + - + - ]: 76 : argsman.AddArg("-dumpfile=<file name>", "When used with 'dump', writes out the records to this file. When used with 'createfromdump', loads the records into a new wallet.", ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::COMMAND_OPTIONS);
42 [ + - + - ]: 76 : argsman.AddArg("-debug=<category>", "Output debugging information (default: 0).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
43 [ + - + - ]: 76 : argsman.AddArg("-printtoconsole", "Send trace/debug info to console (default: 1 when no -debug is true, 0 otherwise).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
44 : :
45 [ + - + - : 76 : argsman.AddCommand("info", "Get wallet info");
+ - ]
46 [ + - + - : 76 : argsman.AddCommand("create", "Create a new descriptor wallet file");
+ - ]
47 [ + - + - : 76 : argsman.AddCommand("dump", "Print out all of the wallet key-value records", {"-dumpfile"});
+ - ]
48 [ + - + - : 76 : argsman.AddCommand("createfromdump", "Create new wallet file from dumped records", {"-dumpfile"});
+ - ]
49 : 38 : }
50 : :
51 : 38 : static std::optional<int> WalletAppInit(ArgsManager& args, int argc, char* argv[])
52 : : {
53 : 38 : SetupWalletToolArgs(args);
54 [ + - ]: 38 : std::string error_message;
55 [ + - + + ]: 38 : if (!args.ParseParameters(argc, argv, error_message)) {
56 [ + - ]: 6 : tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error_message);
57 : 6 : return EXIT_FAILURE;
58 : : }
59 : 32 : const bool missing_args{argc < 2};
60 [ + - + - : 64 : if (missing_args || HelpRequested(args) || args.GetBoolArg("-version", false)) {
+ - + - +
- + - -
+ ]
61 [ # # # # : 0 : std::string strUsage = strprintf("%s bitcoin-wallet utility version", CLIENT_NAME) + " " + FormatFullVersion() + "\n";
# # ]
62 : :
63 [ # # # # : 0 : if (args.GetBoolArg("-version", false)) {
# # ]
64 [ # # # # ]: 0 : strUsage += FormatParagraph(LicenseInfo());
65 : : } else {
66 [ # # ]: 0 : strUsage += "\n"
67 : : "bitcoin-wallet is an offline tool for creating and interacting with " CLIENT_NAME " wallet files.\n\n"
68 : : "By default bitcoin-wallet will act on wallets in the default mainnet wallet directory in the datadir.\n\n"
69 : : "To change the target wallet, use the -datadir, -wallet and (test)chain selection arguments.\n"
70 : : "\n"
71 : : "Usage: bitcoin-wallet [options] <command>\n"
72 : 0 : "\n";
73 [ # # # # ]: 0 : strUsage += "\n" + args.GetHelpMessage();
74 : : }
75 [ # # ]: 0 : tfm::format(std::cout, "%s", strUsage);
76 [ # # ]: 0 : if (missing_args) {
77 [ # # ]: 0 : tfm::format(std::cerr, "Error: too few parameters\n");
78 : 0 : return EXIT_FAILURE;
79 : : }
80 : 0 : return EXIT_SUCCESS;
81 : 0 : }
82 : :
83 : : // check for printtoconsole, allow -debug
84 [ + - + - : 32 : LogInstance().m_print_to_console = args.GetBoolArg("-printtoconsole", args.GetBoolArg("-debug", false));
+ - + - +
- ]
85 : :
86 [ + - - + ]: 32 : if (!CheckDataDirOption(args)) {
87 [ # # # # : 0 : tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", args.GetArg("-datadir", ""));
# # # # ]
88 : 0 : return EXIT_FAILURE;
89 : : }
90 : : // Check for chain settings (Params() calls are only valid after this clause)
91 [ + - + - ]: 32 : SelectParams(args.GetChainType());
92 : :
93 : 32 : return std::nullopt;
94 : 38 : }
95 : :
96 : 38 : MAIN_FUNCTION
97 : : {
98 : 38 : ArgsManager& args = gArgs;
99 : :
100 : 38 : int exit_status;
101 : 38 : std::unique_ptr<interfaces::Init> init = interfaces::MakeWalletInit(argc, argv, exit_status);
102 [ - + ]: 38 : if (!init) {
103 : 0 : return exit_status;
104 : : }
105 : :
106 [ + - ]: 38 : SetupEnvironment();
107 [ + - ]: 38 : SHA256AutoDetect();
108 [ + - ]: 38 : RandomInit();
109 : 38 : try {
110 [ + - + + ]: 38 : if (const auto maybe_exit{WalletAppInit(args, argc, argv)}) return *maybe_exit;
111 [ - - ]: 0 : } catch (const std::exception& e) {
112 [ - - ]: 0 : PrintExceptionContinue(&e, "WalletAppInit()");
113 : 0 : return EXIT_FAILURE;
114 : 0 : } catch (...) {
115 [ - - ]: 0 : PrintExceptionContinue(nullptr, "WalletAppInit()");
116 : 0 : return EXIT_FAILURE;
117 [ - - ]: 0 : }
118 : :
119 [ + - ]: 32 : const auto command = args.GetCommand();
120 [ + + ]: 32 : if (!command) {
121 [ + - ]: 1 : tfm::format(std::cerr, "No method provided. Run `bitcoin-wallet -help` for valid methods.\n");
122 : : return EXIT_FAILURE;
123 : : }
124 [ - + + + ]: 31 : if (command->args.size() != 0) {
125 [ + - + - ]: 1 : tfm::format(std::cerr, "Error: Additional arguments provided (%s). Methods do not take arguments. Please refer to `-help`.\n", Join(command->args, ", "));
126 : 1 : return EXIT_FAILURE;
127 : : }
128 : :
129 [ + - ]: 30 : ECC_Context ecc_context{};
130 [ + - + + ]: 30 : if (!wallet::WalletTool::ExecuteWalletToolFunc(args, command->command)) {
131 : 19 : return EXIT_FAILURE;
132 : : }
133 : : return EXIT_SUCCESS;
134 : 68 : }
|