Bitcoin Core 22.99.0
P2P Digital Currency
bitcoin-wallet.cpp
Go to the documentation of this file.
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)
7#endif
8
9#include <chainparams.h>
10#include <chainparamsbase.h>
11#include <interfaces/init.h>
12#include <logging.h>
13#include <util/system.h>
14#include <util/translation.h>
15#include <util/url.h>
16#include <wallet/wallettool.h>
17
18#include <functional>
19
20const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
21UrlDecodeFn* const URL_DECODE = nullptr;
22
23static void SetupWalletToolArgs(ArgsManager& argsman)
24{
25 SetupHelpOptions(argsman);
27
28 argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
29 argsman.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
30 argsman.AddArg("-wallet=<wallet-name>", "Specify wallet name", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::OPTIONS);
31 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);
32 argsman.AddArg("-debug=<category>", "Output debugging information (default: 0).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
33 argsman.AddArg("-descriptors", "Create descriptors wallet. Only for 'create'", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
34 argsman.AddArg("-legacy", "Create legacy wallet. Only for 'create'", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
35 argsman.AddArg("-format=<format>", "The format of the wallet file to create. Either \"bdb\" or \"sqlite\". Only used with 'createfromdump'", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
36 argsman.AddArg("-printtoconsole", "Send trace/debug info to console (default: 1 when no -debug is true, 0 otherwise).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
37
38 argsman.AddCommand("info", "Get wallet info");
39 argsman.AddCommand("create", "Create new wallet file");
40 argsman.AddCommand("salvage", "Attempt to recover private keys from a corrupt wallet. Warning: 'salvage' is experimental.");
41 argsman.AddCommand("dump", "Print out all of the wallet key-value records");
42 argsman.AddCommand("createfromdump", "Create new wallet file from dumped records");
43}
44
45static bool WalletAppInit(ArgsManager& args, int argc, char* argv[])
46{
48 std::string error_message;
49 if (!args.ParseParameters(argc, argv, error_message)) {
50 tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error_message);
51 return false;
52 }
53 if (argc < 2 || HelpRequested(args) || args.IsArgSet("-version")) {
54 std::string strUsage = strprintf("%s bitcoin-wallet version", PACKAGE_NAME) + " " + FormatFullVersion() + "\n";
55 if (!args.IsArgSet("-version")) {
56 strUsage += "\n"
57 "bitcoin-wallet is an offline tool for creating and interacting with " PACKAGE_NAME " wallet files.\n"
58 "By default bitcoin-wallet will act on wallets in the default mainnet wallet directory in the datadir.\n"
59 "To change the target wallet, use the -datadir, -wallet and -testnet/-regtest arguments.\n\n"
60 "Usage:\n"
61 " bitcoin-wallet [options] <command>\n";
62 strUsage += "\n" + args.GetHelpMessage();
63 }
64 tfm::format(std::cout, "%s", strUsage);
65 return false;
66 }
67
68 // check for printtoconsole, allow -debug
69 LogInstance().m_print_to_console = args.GetBoolArg("-printtoconsole", args.GetBoolArg("-debug", false));
70
71 if (!CheckDataDirOption()) {
72 tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", args.GetArg("-datadir", ""));
73 return false;
74 }
75 // Check for chain settings (Params() calls are only valid after this clause)
77
78 return true;
79}
80
81int main(int argc, char* argv[])
82{
83 ArgsManager& args = gArgs;
84#ifdef WIN32
85 util::WinCmdLineArgs winArgs;
86 std::tie(argc, argv) = winArgs.get();
87#endif
88
89 int exit_status;
90 std::unique_ptr<interfaces::Init> init = interfaces::MakeWalletInit(argc, argv, exit_status);
91 if (!init) {
92 return exit_status;
93 }
94
96 RandomInit();
97 try {
98 if (!WalletAppInit(args, argc, argv)) return EXIT_FAILURE;
99 } catch (const std::exception& e) {
100 PrintExceptionContinue(&e, "WalletAppInit()");
101 return EXIT_FAILURE;
102 } catch (...) {
103 PrintExceptionContinue(nullptr, "WalletAppInit()");
104 return EXIT_FAILURE;
105 }
106
107 const auto command = args.GetCommand();
108 if (!command) {
109 tfm::format(std::cerr, "No method provided. Run `bitcoin-wallet -help` for valid methods.\n");
110 return EXIT_FAILURE;
111 }
112 if (command->args.size() != 0) {
113 tfm::format(std::cerr, "Error: Additional arguments provided (%s). Methods do not take arguments. Please refer to `-help`.\n", Join(command->args, ", "));
114 return EXIT_FAILURE;
115 }
116
118 ECC_Start();
119 if (!WalletTool::ExecuteWalletToolFunc(args, command->command)) {
120 return EXIT_FAILURE;
121 }
122 ECC_Stop();
123 return EXIT_SUCCESS;
124}
#define PACKAGE_NAME
int main(int argc, char *argv[])
UrlDecodeFn *const URL_DECODE
static bool WalletAppInit(ArgsManager &args, int argc, char *argv[])
const std::function< std::string(const char *)> G_TRANSLATION_FUN
Translate string to current locale using Qt.
static void SetupWalletToolArgs(ArgsManager &argsman)
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given chain name.
void SetupChainParamsBaseOptions(ArgsManager &argsman)
Set the arguments for chainparams.
std::optional< const Command > GetCommand() const
Get the command and command args (returns std::nullopt if no command provided)
Definition: system.cpp:467
@ NETWORK_ONLY
Definition: system.h:179
@ ALLOW_ANY
disable validation
Definition: system.h:166
@ DISALLOW_NEGATION
disallow -nofoo syntax
Definition: system.h:171
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: system.cpp:308
std::string GetHelpMessage() const
Get the help string.
Definition: system.cpp:670
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: system.cpp:496
void AddCommand(const std::string &cmd, const std::string &help)
Add subcommand.
Definition: system.cpp:630
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:590
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:602
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
Definition: system.cpp:642
std::string GetChainName() const
Returns the appropriate chain name from the program arguments.
Definition: system.cpp:989
bool m_print_to_console
Definition: logging.h:93
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:316
std::string FormatFullVersion()
static std::unique_ptr< ECCVerifyHandle > globalVerifyHandle
Definition: common.cpp:23
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:370
void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:387
BCLog::Logger & LogInstance()
Definition: logging.cpp:17
bool ExecuteWalletToolFunc(const ArgsManager &args, const std::string &command)
Definition: wallettool.cpp:109
std::unique_ptr< Init > MakeWalletInit(int argc, char *argv[], int &exit_status)
Return implementation of Init interface for the wallet process.
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1062
void RandomInit()
Initialize global RNG state and log any CPU features that are used.
Definition: random.cpp:710
auto Join(const std::vector< T > &list, const BaseType &separator, UnaryOp unary_op) -> decltype(unary_op(list.at(0)))
Join a list of items.
Definition: string.h:44
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
std::string(const std::string &url_encoded) UrlDecodeFn
Definition: url.h:10
bool HelpRequested(const ArgsManager &args)
Definition: system.cpp:739
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: system.cpp:744
bool CheckDataDirOption()
Definition: system.cpp:813
ArgsManager gArgs
Definition: system.cpp:85
void SetupEnvironment()
Definition: system.cpp:1296
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: system.cpp:781