Line data Source code
1 : // Copyright (c) 2016-2020 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 : #if defined(HAVE_CONFIG_H)
6 : #include <config/bitcoin-config.h>
7 : #endif
8 :
9 : #include <chainparams.h>
10 : #include <chainparamsbase.h>
11 : #include <logging.h>
12 : #include <util/system.h>
13 : #include <util/translation.h>
14 : #include <util/url.h>
15 : #include <wallet/wallettool.h>
16 :
17 : #include <functional>
18 :
19 10 : const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
20 : UrlDecodeFn* const URL_DECODE = nullptr;
21 :
22 10 : static void SetupWalletToolArgs(ArgsManager& argsman)
23 : {
24 10 : SetupHelpOptions(argsman);
25 10 : SetupChainParamsBaseOptions(argsman);
26 :
27 10 : argsman.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
28 10 : argsman.AddArg("-wallet=<wallet-name>", "Specify wallet name", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::OPTIONS);
29 10 : argsman.AddArg("-debug=<category>", "Output debugging information (default: 0).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
30 10 : argsman.AddArg("-printtoconsole", "Send trace/debug info to console (default: 1 when no -debug is true, 0 otherwise).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
31 :
32 10 : argsman.AddArg("info", "Get wallet info", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
33 10 : argsman.AddArg("create", "Create new wallet file", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
34 10 : argsman.AddArg("salvage", "Attempt to recover private keys from a corrupt wallet", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
35 10 : }
36 :
37 10 : static bool WalletAppInit(int argc, char* argv[])
38 : {
39 10 : SetupWalletToolArgs(gArgs);
40 10 : std::string error_message;
41 10 : if (!gArgs.ParseParameters(argc, argv, error_message)) {
42 1 : tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error_message);
43 1 : return false;
44 : }
45 9 : if (argc < 2 || HelpRequested(gArgs)) {
46 0 : std::string usage = strprintf("%s bitcoin-wallet version", PACKAGE_NAME) + " " + FormatFullVersion() + "\n\n" +
47 0 : "bitcoin-wallet is an offline tool for creating and interacting with " PACKAGE_NAME " wallet files.\n" +
48 0 : "By default bitcoin-wallet will act on wallets in the default mainnet wallet directory in the datadir.\n" +
49 0 : "To change the target wallet, use the -datadir, -wallet and -testnet/-regtest arguments.\n\n" +
50 0 : "Usage:\n" +
51 0 : " bitcoin-wallet [options] <command>\n\n" +
52 0 : gArgs.GetHelpMessage();
53 :
54 0 : tfm::format(std::cout, "%s", usage);
55 : return false;
56 0 : }
57 :
58 : // check for printtoconsole, allow -debug
59 9 : LogInstance().m_print_to_console = gArgs.GetBoolArg("-printtoconsole", gArgs.GetBoolArg("-debug", false));
60 :
61 9 : if (!CheckDataDirOption()) {
62 0 : tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", ""));
63 0 : return false;
64 : }
65 : // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
66 9 : SelectParams(gArgs.GetChainName());
67 :
68 9 : return true;
69 10 : }
70 :
71 10 : int main(int argc, char* argv[])
72 : {
73 : #ifdef WIN32
74 : util::WinCmdLineArgs winArgs;
75 : std::tie(argc, argv) = winArgs.get();
76 : #endif
77 10 : SetupEnvironment();
78 10 : RandomInit();
79 : try {
80 10 : if (!WalletAppInit(argc, argv)) return EXIT_FAILURE;
81 0 : } catch (const std::exception& e) {
82 0 : PrintExceptionContinue(&e, "WalletAppInit()");
83 : return EXIT_FAILURE;
84 0 : } catch (...) {
85 0 : PrintExceptionContinue(nullptr, "WalletAppInit()");
86 : return EXIT_FAILURE;
87 0 : }
88 :
89 9 : std::string method {};
90 42 : for(int i = 1; i < argc; ++i) {
91 34 : if (!IsSwitchChar(argv[i][0])) {
92 10 : if (!method.empty()) {
93 1 : tfm::format(std::cerr, "Error: two methods provided (%s and %s). Only one method should be provided.\n", method, argv[i]);
94 1 : return EXIT_FAILURE;
95 : }
96 9 : method = argv[i];
97 : }
98 : }
99 :
100 8 : if (method.empty()) {
101 0 : tfm::format(std::cerr, "No method provided. Run `bitcoin-wallet -help` for valid methods.\n");
102 0 : return EXIT_FAILURE;
103 : }
104 :
105 : // A name must be provided when creating a file
106 8 : if (method == "create" && !gArgs.IsArgSet("-wallet")) {
107 0 : tfm::format(std::cerr, "Wallet name must be provided when creating a new wallet.\n");
108 0 : return EXIT_FAILURE;
109 : }
110 :
111 8 : std::string name = gArgs.GetArg("-wallet", "");
112 :
113 8 : ECCVerifyHandle globalVerifyHandle;
114 8 : ECC_Start();
115 8 : if (!WalletTool::ExecuteWalletToolFunc(method, name))
116 4 : return EXIT_FAILURE;
117 4 : ECC_Stop();
118 4 : return EXIT_SUCCESS;
119 10 : }
|