Bitcoin Core 31.99.0
P2P Digital Currency
bitcoin-wallet.cpp
Go to the documentation of this file.
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 <interfaces/init.h>
15#include <key.h>
16#include <logging.h>
17#include <pubkey.h>
18#include <tinyformat.h>
19#include <util/exception.h>
20#include <util/translation.h>
21#include <wallet/wallettool.h>
22
23#include <exception>
24#include <functional>
25#include <string>
26#include <tuple>
27
28using util::Join;
29
31
32static void SetupWalletToolArgs(ArgsManager& argsman)
33{
34 SetupHelpOptions(argsman);
36
37 argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
38 argsman.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::OPTIONS);
39 argsman.AddArg("-wallet=<wallet-name>", "Specify wallet name", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::OPTIONS);
40 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);
41 argsman.AddArg("-debug=<category>", "Output debugging information (default: 0).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
42 argsman.AddArg("-printtoconsole", "Send trace/debug info to console (default: 1 when no -debug is true, 0 otherwise).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
43
44 argsman.AddCommand("info", "Get wallet info");
45 argsman.AddCommand("create", "Create a new descriptor wallet file");
46 argsman.AddCommand("dump", "Print out all of the wallet key-value records");
47 argsman.AddCommand("createfromdump", "Create new wallet file from dumped records");
48}
49
50static std::optional<int> WalletAppInit(ArgsManager& args, int argc, char* argv[])
51{
53 std::string error_message;
54 if (!args.ParseParameters(argc, argv, error_message)) {
55 tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error_message);
56 return EXIT_FAILURE;
57 }
58 const bool missing_args{argc < 2};
59 if (missing_args || HelpRequested(args) || args.GetBoolArg("-version", false)) {
60 std::string strUsage = strprintf("%s bitcoin-wallet utility version", CLIENT_NAME) + " " + FormatFullVersion() + "\n";
61
62 if (args.GetBoolArg("-version", false)) {
63 strUsage += FormatParagraph(LicenseInfo());
64 } else {
65 strUsage += "\n"
66 "bitcoin-wallet is an offline tool for creating and interacting with " CLIENT_NAME " wallet files.\n\n"
67 "By default bitcoin-wallet will act on wallets in the default mainnet wallet directory in the datadir.\n\n"
68 "To change the target wallet, use the -datadir, -wallet and (test)chain selection arguments.\n"
69 "\n"
70 "Usage: bitcoin-wallet [options] <command>\n"
71 "\n";
72 strUsage += "\n" + args.GetHelpMessage();
73 }
74 tfm::format(std::cout, "%s", strUsage);
75 if (missing_args) {
76 tfm::format(std::cerr, "Error: too few parameters\n");
77 return EXIT_FAILURE;
78 }
79 return EXIT_SUCCESS;
80 }
81
82 // check for printtoconsole, allow -debug
83 LogInstance().m_print_to_console = args.GetBoolArg("-printtoconsole", args.GetBoolArg("-debug", false));
84
86 tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", args.GetArg("-datadir", ""));
87 return EXIT_FAILURE;
88 }
89 // Check for chain settings (Params() calls are only valid after this clause)
91
92 return std::nullopt;
93}
94
96{
98
100 std::unique_ptr<interfaces::Init> init = interfaces::MakeWalletInit(argc, argv, exit_status);
101 if (!init) {
102 return exit_status;
103 }
104
106 RandomInit();
107 try {
108 if (const auto maybe_exit{WalletAppInit(args, argc, argv)}) return *maybe_exit;
109 } catch (const std::exception& e) {
110 PrintExceptionContinue(&e, "WalletAppInit()");
111 return EXIT_FAILURE;
112 } catch (...) {
113 PrintExceptionContinue(nullptr, "WalletAppInit()");
114 return EXIT_FAILURE;
115 }
116
117 const auto command = args.GetCommand();
118 if (!command) {
119 tfm::format(std::cerr, "No method provided. Run `bitcoin-wallet -help` for valid methods.\n");
120 return EXIT_FAILURE;
121 }
122 if (command->args.size() != 0) {
123 tfm::format(std::cerr, "Error: Additional arguments provided (%s). Methods do not take arguments. Please refer to `-help`.\n", Join(command->args, ", "));
124 return EXIT_FAILURE;
125 }
126
129 return EXIT_FAILURE;
130 }
132}
bool HelpRequested(const ArgsManager &args)
Definition: args.cpp:742
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: args.cpp:747
bool CheckDataDirOption(const ArgsManager &args)
Definition: args.cpp:814
ArgsManager gArgs
Definition: args.cpp:40
int exit_status
return EXIT_SUCCESS
static std::optional< int > WalletAppInit(ArgsManager &args, int argc, char *argv[])
const auto command
RandomInit()
Definition: random.cpp:696
static void SetupWalletToolArgs(ArgsManager &argsman)
SetupEnvironment()
Definition: system.cpp:64
const TranslateFn G_TRANSLATION_FUN
Translate string to current locale using Qt.
ECC_Context ecc_context
MAIN_FUNCTION
ArgsManager & args
Definition: bitcoind.cpp:278
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
void SetupChainParamsBaseOptions(ArgsManager &argsman)
Set the arguments for chainparams.
bool ParseParameters(int argc, const char *const argv[], std::string &error) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Definition: args.cpp:177
ChainType GetChainType() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Returns the appropriate chain type from the program arguments.
Definition: args.cpp:833
@ NETWORK_ONLY
Definition: args.h:124
@ ALLOW_ANY
disable validation
Definition: args.h:110
@ DISALLOW_NEGATION
disallow -nofoo syntax
Definition: args.h:115
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Add argument.
Definition: args.cpp:613
void AddCommand(const std::string &cmd, const std::string &help) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Add subcommand.
Definition: args.cpp:601
std::string GetArg(const std::string &strArg, const std::string &strDefault) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return string argument or default value.
Definition: args.cpp:485
std::optional< const Command > GetCommand() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get the command and command args (returns std::nullopt if no command provided)
Definition: args.cpp:370
bool GetBoolArg(const std::string &strArg, bool fDefault) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return boolean argument or default value.
Definition: args.cpp:539
std::string GetHelpMessage() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get the help string.
Definition: args.cpp:667
bool m_print_to_console
Definition: logging.h:178
RAII class initializing and deinitializing global state for elliptic curve support.
Definition: key.h:326
std::string FormatFullVersion()
void PrintExceptionContinue(const std::exception *pex, std::string_view thread_name)
Definition: exception.cpp:36
std::string LicenseInfo()
Returns licensing information (for -version)
BCLog::Logger & LogInstance()
Definition: logging.cpp:26
Definition: basic.cpp:8
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, FormatStringCheck< sizeof...(Args)> fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1079
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Definition: string.h:206
bool ExecuteWalletToolFunc(const ArgsManager &args, const std::string &command)
Definition: wallettool.cpp:94
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
std::function< std::string(const char *)> TranslateFn
Translate a message to the native language of the user.
Definition: translation.h:16
std::string FormatParagraph(std::string_view in, size_t width, size_t indent)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line.