Bitcoin Core  0.20.99
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()
23 {
26 
27  gArgs.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
28  gArgs.AddArg("-wallet=<wallet-name>", "Specify wallet name", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::OPTIONS);
29  gArgs.AddArg("-debug=<category>", "Output debugging information (default: 0).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
30  gArgs.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  gArgs.AddArg("info", "Get wallet info", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
33  gArgs.AddArg("create", "Create new wallet file", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
34  gArgs.AddArg("salvage", "Attempt to recover private keys from a corrupt wallet", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
35 }
36 
37 static bool WalletAppInit(int argc, char* argv[])
38 {
40  std::string error_message;
41  if (!gArgs.ParseParameters(argc, argv, error_message)) {
42  tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error_message);
43  return false;
44  }
45  if (argc < 2 || HelpRequested(gArgs)) {
46  std::string usage = strprintf("%s bitcoin-wallet version", PACKAGE_NAME) + " " + FormatFullVersion() + "\n\n" +
47  "bitcoin-wallet is an offline tool for creating and interacting with " PACKAGE_NAME " wallet files.\n" +
48  "By default bitcoin-wallet will act on wallets in the default mainnet wallet directory in the datadir.\n" +
49  "To change the target wallet, use the -datadir, -wallet and -testnet/-regtest arguments.\n\n" +
50  "Usage:\n" +
51  " bitcoin-wallet [options] <command>\n\n" +
53 
54  tfm::format(std::cout, "%s", usage);
55  return false;
56  }
57 
58  // check for printtoconsole, allow -debug
59  LogInstance().m_print_to_console = gArgs.GetBoolArg("-printtoconsole", gArgs.GetBoolArg("-debug", false));
60 
61  if (!CheckDataDirOption()) {
62  tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", ""));
63  return false;
64  }
65  // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
67 
68  return true;
69 }
70 
71 int main(int argc, char* argv[])
72 {
73 #ifdef WIN32
74  util::WinCmdLineArgs winArgs;
75  std::tie(argc, argv) = winArgs.get();
76 #endif
78  RandomInit();
79  try {
80  if (!WalletAppInit(argc, argv)) return EXIT_FAILURE;
81  } catch (const std::exception& e) {
82  PrintExceptionContinue(&e, "WalletAppInit()");
83  return EXIT_FAILURE;
84  } catch (...) {
85  PrintExceptionContinue(nullptr, "WalletAppInit()");
86  return EXIT_FAILURE;
87  }
88 
89  std::string method {};
90  for(int i = 1; i < argc; ++i) {
91  if (!IsSwitchChar(argv[i][0])) {
92  if (!method.empty()) {
93  tfm::format(std::cerr, "Error: two methods provided (%s and %s). Only one method should be provided.\n", method, argv[i]);
94  return EXIT_FAILURE;
95  }
96  method = argv[i];
97  }
98  }
99 
100  if (method.empty()) {
101  tfm::format(std::cerr, "No method provided. Run `bitcoin-wallet -help` for valid methods.\n");
102  return EXIT_FAILURE;
103  }
104 
105  // A name must be provided when creating a file
106  if (method == "create" && !gArgs.IsArgSet("-wallet")) {
107  tfm::format(std::cerr, "Wallet name must be provided when creating a new wallet.\n");
108  return EXIT_FAILURE;
109  }
110 
111  std::string name = gArgs.GetArg("-wallet", "");
112 
114  ECC_Start();
115  if (!WalletTool::ExecuteWalletToolFunc(method, name))
116  return EXIT_FAILURE;
117  ECC_Stop();
118  return EXIT_SUCCESS;
119 }
void RandomInit()
Initialize global RNG state and log any CPU features that are used.
Definition: random.cpp:705
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:370
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:1082
#define PACKAGE_NAME
bool m_print_to_console
Definition: logging.h:87
NODISCARD bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: system.cpp:283
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:392
std::string GetHelpMessage() const
Get the help string.
Definition: system.cpp:446
bool CheckDataDirOption()
Definition: system.cpp:650
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:237
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:420
bool HelpRequested(const ArgsManager &args)
Definition: system.cpp:515
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: system.cpp:557
std::string FormatFullVersion()
UrlDecodeFn *const URL_DECODE
void SetupChainParamsBaseOptions()
Set the arguments for chainparams.
static void SetupWalletToolArgs()
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:380
ArgsManager gArgs
Definition: system.cpp:77
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: system.cpp:520
std::string GetChainName() const
Returns the appropriate chain name from the program arguments.
Definition: system.cpp:831
bool IsSwitchChar(char c)
Definition: system.h:109
bool ExecuteWalletToolFunc(const std::string &command, const std::string &name)
Definition: wallettool.cpp:128
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.