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