Bitcoin Core  21.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 <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 const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
20 UrlDecodeFn* const URL_DECODE = nullptr;
21 
22 static void SetupWalletToolArgs(ArgsManager& argsman)
23 {
24  SetupHelpOptions(argsman);
26 
27  argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
28  argsman.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
29  argsman.AddArg("-wallet=<wallet-name>", "Specify wallet name", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::OPTIONS);
30  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_STRING, OptionsCategory::OPTIONS);
31  argsman.AddArg("-debug=<category>", "Output debugging information (default: 0).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
32  argsman.AddArg("-descriptors", "Create descriptors wallet. Only for 'create'", ArgsManager::ALLOW_BOOL, OptionsCategory::OPTIONS);
33  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);
34  argsman.AddArg("-printtoconsole", "Send trace/debug info to console (default: 1 when no -debug is true, 0 otherwise).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
35 
36  argsman.AddArg("info", "Get wallet info", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
37  argsman.AddArg("create", "Create new wallet file", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
38  argsman.AddArg("salvage", "Attempt to recover private keys from a corrupt wallet. Warning: 'salvage' is experimental.", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
39  argsman.AddArg("dump", "Print out all of the wallet key-value records", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
40  argsman.AddArg("createfromdump", "Create new wallet file from dumped records", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
41 }
42 
43 static bool WalletAppInit(int argc, char* argv[])
44 {
46  std::string error_message;
47  if (!gArgs.ParseParameters(argc, argv, error_message)) {
48  tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error_message);
49  return false;
50  }
51  if (argc < 2 || HelpRequested(gArgs) || gArgs.IsArgSet("-version")) {
52  std::string strUsage = strprintf("%s bitcoin-wallet version", PACKAGE_NAME) + " " + FormatFullVersion() + "\n";
53  if (!gArgs.IsArgSet("-version")) {
54  strUsage += "\n"
55  "bitcoin-wallet is an offline tool for creating and interacting with " PACKAGE_NAME " wallet files.\n"
56  "By default bitcoin-wallet will act on wallets in the default mainnet wallet directory in the datadir.\n"
57  "To change the target wallet, use the -datadir, -wallet and -testnet/-regtest arguments.\n\n"
58  "Usage:\n"
59  " bitcoin-wallet [options] <command>\n";
60  strUsage += "\n" + gArgs.GetHelpMessage();
61  }
62  tfm::format(std::cout, "%s", strUsage);
63  return false;
64  }
65 
66  // check for printtoconsole, allow -debug
67  LogInstance().m_print_to_console = gArgs.GetBoolArg("-printtoconsole", gArgs.GetBoolArg("-debug", false));
68 
69  if (!CheckDataDirOption()) {
70  tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", ""));
71  return false;
72  }
73  // Check for chain settings (Params() calls are only valid after this clause)
75 
76  return true;
77 }
78 
79 int main(int argc, char* argv[])
80 {
81 #ifdef WIN32
82  util::WinCmdLineArgs winArgs;
83  std::tie(argc, argv) = winArgs.get();
84 #endif
86  RandomInit();
87  try {
88  if (!WalletAppInit(argc, argv)) return EXIT_FAILURE;
89  } catch (const std::exception& e) {
90  PrintExceptionContinue(&e, "WalletAppInit()");
91  return EXIT_FAILURE;
92  } catch (...) {
93  PrintExceptionContinue(nullptr, "WalletAppInit()");
94  return EXIT_FAILURE;
95  }
96 
97  std::string method {};
98  for(int i = 1; i < argc; ++i) {
99  if (!IsSwitchChar(argv[i][0])) {
100  if (!method.empty()) {
101  tfm::format(std::cerr, "Error: two methods provided (%s and %s). Only one method should be provided.\n", method, argv[i]);
102  return EXIT_FAILURE;
103  }
104  method = argv[i];
105  }
106  }
107 
108  if (method.empty()) {
109  tfm::format(std::cerr, "No method provided. Run `bitcoin-wallet -help` for valid methods.\n");
110  return EXIT_FAILURE;
111  }
112 
113  // A name must be provided when creating a file
114  if (method == "create" && !gArgs.IsArgSet("-wallet")) {
115  tfm::format(std::cerr, "Wallet name must be provided when creating a new wallet.\n");
116  return EXIT_FAILURE;
117  }
118 
119  std::string name = gArgs.GetArg("-wallet", "");
120 
122  ECC_Start();
123  if (!WalletTool::ExecuteWalletToolFunc(gArgs, method, name)) {
124  return EXIT_FAILURE;
125  }
126  ECC_Stop();
127  return EXIT_SUCCESS;
128 }
void RandomInit()
Initialize global RNG state and log any CPU features that are used.
Definition: random.cpp:711
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:348
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: system.cpp:371
void SetupChainParamsBaseOptions(ArgsManager &argsman)
Set the arguments for chainparams.
BCLog::Logger & LogInstance()
Definition: logging.cpp:14
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
void SetupEnvironment()
Definition: system.cpp:1237
#define PACKAGE_NAME
bool m_print_to_console
Definition: logging.h:87
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: system.cpp:284
static std::unique_ptr< ECCVerifyHandle > globalVerifyHandle
Definition: init.cpp:154
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:479
bool ExecuteWalletToolFunc(const ArgsManager &args, const std::string &command, const std::string &name)
Definition: wallettool.cpp:106
std::string GetHelpMessage() const
Get the help string.
Definition: system.cpp:533
bool CheckDataDirOption()
Definition: system.cpp:751
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:268
int main(int argc, char *argv[])
const char * name
Definition: rest.cpp:41
void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:365
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given chain name.
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 AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
Definition: system.cpp:507
bool HelpRequested(const ArgsManager &args)
Definition: system.cpp:602
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: system.cpp:644
std::string FormatFullVersion()
UrlDecodeFn *const URL_DECODE
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:467
static void SetupWalletToolArgs(ArgsManager &argsman)
ArgsManager gArgs
Definition: system.cpp:77
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: system.cpp:607
std::string GetChainName() const
Returns the appropriate chain name from the program arguments.
Definition: system.cpp:932
bool IsSwitchChar(char c)
Definition: system.h:134
static bool WalletAppInit(int argc, char *argv[])
std::string(const std::string &url_encoded) UrlDecodeFn
Definition: url.h:10
const std::function< std::string(const char *)> G_TRANSLATION_FUN
Translate string to current locale using Qt.