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