Bitcoin Core  0.19.99
P2P Digital Currency
bitcoin-wallet.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-2019 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 <wallet/wallettool.h>
15 
16 #include <functional>
17 
18 const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
19 
20 static void SetupWalletToolArgs()
21 {
24 
25  gArgs.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
26  gArgs.AddArg("-wallet=<wallet-name>", "Specify wallet name", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::OPTIONS);
27  gArgs.AddArg("-debug=<category>", "Output debugging information (default: 0).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
28  gArgs.AddArg("-printtoconsole", "Send trace/debug info to console (default: 1 when no -debug is true, 0 otherwise).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
29 
30  gArgs.AddArg("info", "Get wallet info", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
31  gArgs.AddArg("create", "Create new wallet file", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
32 }
33 
34 static bool WalletAppInit(int argc, char* argv[])
35 {
37  std::string error_message;
38  if (!gArgs.ParseParameters(argc, argv, error_message)) {
39  tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error_message);
40  return false;
41  }
42  if (argc < 2 || HelpRequested(gArgs)) {
43  std::string usage = strprintf("%s bitcoin-wallet version", PACKAGE_NAME) + " " + FormatFullVersion() + "\n\n" +
44  "bitcoin-wallet is an offline tool for creating and interacting with " PACKAGE_NAME " wallet files.\n" +
45  "By default bitcoin-wallet will act on wallets in the default mainnet wallet directory in the datadir.\n" +
46  "To change the target wallet, use the -datadir, -wallet and -testnet/-regtest arguments.\n\n" +
47  "Usage:\n" +
48  " bitcoin-wallet [options] <command>\n\n" +
50 
51  tfm::format(std::cout, "%s", usage);
52  return false;
53  }
54 
55  // check for printtoconsole, allow -debug
56  LogInstance().m_print_to_console = gArgs.GetBoolArg("-printtoconsole", gArgs.GetBoolArg("-debug", false));
57 
58  if (!CheckDataDirOption()) {
59  tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", ""));
60  return false;
61  }
62  // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
64 
65  return true;
66 }
67 
68 int main(int argc, char* argv[])
69 {
70 #ifdef WIN32
71  util::WinCmdLineArgs winArgs;
72  std::tie(argc, argv) = winArgs.get();
73 #endif
75  RandomInit();
76  try {
77  if (!WalletAppInit(argc, argv)) return EXIT_FAILURE;
78  } catch (const std::exception& e) {
79  PrintExceptionContinue(&e, "WalletAppInit()");
80  return EXIT_FAILURE;
81  } catch (...) {
82  PrintExceptionContinue(nullptr, "WalletAppInit()");
83  return EXIT_FAILURE;
84  }
85 
86  std::string method {};
87  for(int i = 1; i < argc; ++i) {
88  if (!IsSwitchChar(argv[i][0])) {
89  if (!method.empty()) {
90  tfm::format(std::cerr, "Error: two methods provided (%s and %s). Only one method should be provided.\n", method, argv[i]);
91  return EXIT_FAILURE;
92  }
93  method = argv[i];
94  }
95  }
96 
97  if (method.empty()) {
98  tfm::format(std::cerr, "No method provided. Run `bitcoin-wallet -help` for valid methods.\n");
99  return EXIT_FAILURE;
100  }
101 
102  // A name must be provided when creating a file
103  if (method == "create" && !gArgs.IsArgSet("-wallet")) {
104  tfm::format(std::cerr, "Wallet name must be provided when creating a new wallet.\n");
105  return EXIT_FAILURE;
106  }
107 
108  std::string name = gArgs.GetArg("-wallet", "");
109 
111  ECC_Start();
112  if (!WalletTool::ExecuteWalletToolFunc(method, name))
113  return EXIT_FAILURE;
114  ECC_Stop();
115  return EXIT_SUCCESS;
116 }
void RandomInit()
Initialize global RNG state and log any CPU features that are used.
Definition: random.cpp:712
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:361
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:1064
#define PACKAGE_NAME
bool m_print_to_console
Definition: logging.h:85
NODISCARD bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: system.cpp:274
static std::unique_ptr< ECCVerifyHandle > globalVerifyHandle
Definition: init.cpp:157
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:383
std::string GetHelpMessage() const
Get the help string.
Definition: system.cpp:437
bool CheckDataDirOption()
Definition: system.cpp:641
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:229
int main(int argc, char *argv[])
const char * name
Definition: rest.cpp:40
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:411
bool HelpRequested(const ArgsManager &args)
Definition: system.cpp:506
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: system.cpp:548
std::string FormatFullVersion()
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:371
ArgsManager gArgs
Definition: system.cpp:75
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: system.cpp:511
std::string GetChainName() const
Returns the appropriate chain name from the program arguments.
Definition: system.cpp:822
bool IsSwitchChar(char c)
Definition: system.h:98
bool ExecuteWalletToolFunc(const std::string &command, const std::string &name)
Definition: wallettool.cpp:105
static bool WalletAppInit(int argc, char *argv[])
const std::function< std::string(const char *)> G_TRANSLATION_FUN
Translate string to current locale using Qt.