Bitcoin ABC  0.24.7
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 }
ArgsManager::GetBoolArg
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:517
tinyformat::format
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
CheckDataDirOption
bool CheckDataDirOption()
Definition: system.cpp:818
SetupHelpOptions
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: system.cpp:676
ArgsManager::ALLOW_ANY
@ ALLOW_ANY
Definition: system.h:159
ArgsManager::GetHelpMessage
std::string GetHelpMessage() const
Get the help string.
Definition: system.cpp:598
SetupEnvironment
void SetupEnvironment()
Definition: system.cpp:1306
chainparamsbase.h
SetupChainParamsBaseOptions
void SetupChainParamsBaseOptions(ArgsManager &argsman)
Set the arguments for chainparams.
Definition: chainparamsbase.cpp:17
ArgsManager::GetChainName
std::string GetChainName() const
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
Definition: system.cpp:1033
ArgsManager::IsArgSet
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: system.cpp:400
globalVerifyHandle
static std::unique_ptr< ECCVerifyHandle > globalVerifyHandle
Definition: init.cpp:158
main
int main(int argc, char *argv[])
Definition: bitcoin-wallet.cpp:91
chainparams.h
SetupCurrencyUnitOptions
void SetupCurrencyUnitOptions(ArgsManager &argsman)
Definition: currencyunit.cpp:9
HelpRequested
bool HelpRequested(const ArgsManager &args)
Definition: system.cpp:671
ECC_Stop
void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:451
ArgsManager::AddArg
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
Definition: system.cpp:565
G_TRANSLATION_FUN
const std::function< std::string(const char *)> G_TRANSLATION_FUN
Translate string to current locale using Qt.
Definition: bitcoin-wallet.cpp:19
IsSwitchChar
bool IsSwitchChar(char c)
Definition: system.h:116
ArgsManager::GetArg
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:498
OptionsCategory::OPTIONS
@ OPTIONS
SelectParams
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given BIP70 chain name.
Definition: chainparams.cpp:530
wallettool.h
ArgsManager::ParseParameters
NODISCARD bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: system.cpp:317
RandomInit
void RandomInit()
Initialize global RNG state and log any CPU features that are used.
Definition: random.cpp:785
SetupWalletToolArgs
static void SetupWalletToolArgs(ArgsManager &argsman)
Definition: bitcoin-wallet.cpp:21
name
const char * name
Definition: rest.cpp:43
WalletAppInit
static bool WalletAppInit(int argc, char *argv[])
Definition: bitcoin-wallet.cpp:49
system.h
ECC_Start
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:434
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
ECCVerifyHandle
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:228
ArgsManager
Definition: system.h:152
currencyunit.h
translation.h
BCLog::Logger::m_print_to_console
bool m_print_to_console
Definition: logging.h:93
gArgs
ArgsManager gArgs
Definition: system.cpp:75
logging.h
LogInstance
BCLog::Logger & LogInstance()
Definition: logging.cpp:15
OptionsCategory::DEBUG_TEST
@ DEBUG_TEST
OptionsCategory::COMMANDS
@ COMMANDS
FormatFullVersion
std::string FormatFullVersion()
Definition: clientversion.cpp:58
ArgsManager::NETWORK_ONLY
@ NETWORK_ONLY
Definition: system.h:166
WalletTool::ExecuteWalletToolFunc
bool ExecuteWalletToolFunc(const std::string &command, const std::string &name)
Definition: wallettool.cpp:123
PrintExceptionContinue
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: system.cpp:716