Bitcoin ABC  0.22.13
P2P Digital Currency
bitcoin-wallet.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-2018 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)
6 #include <config/bitcoin-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(ArgsManager &argsman) {
21  SetupHelpOptions(argsman);
23 
24  argsman.AddArg("-datadir=<dir>", "Specify data directory",
26  argsman.AddArg("-wallet=<wallet-name>", "Specify wallet name",
29  argsman.AddArg("-debug=<category>",
30  "Output debugging information (default: 0).",
32  argsman.AddArg(
33  "-printtoconsole",
34  "Send trace/debug info to console (default: 1 when no -debug "
35  "is true, 0 otherwise).",
37 
38  argsman.AddArg("info", "Get wallet info", ArgsManager::ALLOW_ANY,
40  argsman.AddArg("create", "Create new wallet file", ArgsManager::ALLOW_ANY,
42  argsman.AddArg("salvage",
43  "Attempt to recover private keys from a corrupt wallet",
45 }
46 
47 static bool WalletAppInit(int argc, char *argv[]) {
49  std::string error_message;
50  if (!gArgs.ParseParameters(argc, argv, error_message)) {
51  tfm::format(std::cerr, "Error parsing command line arguments: %s\n",
52  error_message);
53  return false;
54  }
55  if (argc < 2 || HelpRequested(gArgs)) {
56  std::string usage =
57  strprintf("%s bitcoin-wallet version", PACKAGE_NAME) + " " +
58  FormatFullVersion() + "\n\n" +
59  "bitcoin-wallet is an offline tool for creating and interacting "
60  "with " PACKAGE_NAME " wallet files.\n" +
61  "By default bitcoin-wallet will act on wallets in the default "
62  "mainnet wallet directory in the datadir.\n" +
63  "To change the target wallet, use the -datadir, -wallet and "
64  "-testnet/-regtest arguments.\n\n" +
65  "Usage:\n" + " bitcoin-wallet [options] <command>\n\n" +
67 
68  tfm::format(std::cout, "%s", usage);
69  return false;
70  }
71 
72  // check for printtoconsole, allow -debug
74  gArgs.GetBoolArg("-printtoconsole", gArgs.GetBoolArg("-debug", false));
75 
76  if (!CheckDataDirOption()) {
77  tfm::format(std::cerr,
78  "Error: Specified data directory \"%s\" does not exist.\n",
79  gArgs.GetArg("-datadir", ""));
80  return false;
81  }
82  // Check for -testnet or -regtest parameter (Params() calls are only valid
83  // after this clause)
85 
86  return true;
87 }
88 
89 int main(int argc, char *argv[]) {
90 #ifdef WIN32
91  util::WinCmdLineArgs winArgs;
92  std::tie(argc, argv) = winArgs.get();
93 #endif
95  RandomInit();
96  try {
97  if (!WalletAppInit(argc, argv)) {
98  return EXIT_FAILURE;
99  }
100  } catch (const std::exception &e) {
101  PrintExceptionContinue(&e, "WalletAppInit()");
102  return EXIT_FAILURE;
103  } catch (...) {
104  PrintExceptionContinue(nullptr, "WalletAppInit()");
105  return EXIT_FAILURE;
106  }
107 
108  std::string method{};
109  for (int i = 1; i < argc; ++i) {
110  if (!IsSwitchChar(argv[i][0])) {
111  if (!method.empty()) {
112  tfm::format(std::cerr,
113  "Error: two methods provided (%s and %s). Only one "
114  "method should be provided.\n",
115  method, argv[i]);
116  return EXIT_FAILURE;
117  }
118  method = argv[i];
119  }
120  }
121 
122  if (method.empty()) {
123  tfm::format(std::cerr,
124  "No method provided. Run `bitcoin-wallet -help` for "
125  "valid methods.\n");
126  return EXIT_FAILURE;
127  }
128 
129  // A name must be provided when creating a file
130  if (method == "create" && !gArgs.IsArgSet("-wallet")) {
131  tfm::format(
132  std::cerr,
133  "Wallet name must be provided when creating a new wallet.\n");
134  return EXIT_FAILURE;
135  }
136 
137  std::string name = gArgs.GetArg("-wallet", "");
138 
140  ECC_Start();
141  if (!WalletTool::ExecuteWalletToolFunc(method, name)) {
142  return EXIT_FAILURE;
143  }
144  ECC_Stop();
145  return EXIT_SUCCESS;
146 }
void RandomInit()
Initialize global RNG state and log any CPU features that are used.
Definition: random.cpp:786
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:438
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: system.cpp:390
void SetupChainParamsBaseOptions(ArgsManager &argsman)
Set the arguments for chainparams.
BCLog::Logger & LogInstance()
Definition: logging.cpp:15
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
void SetupEnvironment()
Definition: system.cpp:1287
bool m_print_to_console
Definition: logging.h:92
NODISCARD bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: system.cpp:318
static std::unique_ptr< ECCVerifyHandle > globalVerifyHandle
Definition: init.cpp:153
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:498
const std::function< std::string(const char *)> G_TRANSLATION_FUN
Translate string to current locale using Qt.
std::string GetHelpMessage() const
Get the help string.
Definition: system.cpp:579
bool CheckDataDirOption()
Definition: system.cpp:799
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:223
int main(int argc, char *argv[])
const char * name
Definition: rest.cpp:43
void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:455
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given BIP70 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:1111
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
Definition: system.cpp:546
bool HelpRequested(const ArgsManager &args)
Definition: system.cpp:652
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: system.cpp:697
std::string FormatFullVersion()
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:479
static void SetupWalletToolArgs(ArgsManager &argsman)
ArgsManager gArgs
Definition: system.cpp:76
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: system.cpp:657
std::string GetChainName() const
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
Definition: system.cpp:1014
bool IsSwitchChar(char c)
Definition: system.h:115
bool ExecuteWalletToolFunc(const std::string &command, const std::string &name)
Definition: wallettool.cpp:151
static bool WalletAppInit(int argc, char *argv[])