Bitcoin ABC 0.30.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 <common/args.h>
12#include <common/system.h>
13#include <currencyunit.h>
14#include <logging.h>
15#include <util/exception.h>
16#include <util/translation.h>
17#include <wallet/wallettool.h>
18
19#include <functional>
20
21const std::function<std::string(const char *)> G_TRANSLATION_FUN = nullptr;
22
23static void SetupWalletToolArgs(ArgsManager &argsman) {
24 SetupHelpOptions(argsman);
27
28 argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY,
30 argsman.AddArg("-datadir=<dir>", "Specify data directory",
32 argsman.AddArg("-wallet=<wallet-name>", "Specify wallet name",
35 argsman.AddArg("-debug=<category>",
36 "Output debugging information (default: 0).",
38 argsman.AddArg(
39 "-printtoconsole",
40 "Send trace/debug info to console (default: 1 when no -debug "
41 "is true, 0 otherwise).",
43
44 argsman.AddArg("info", "Get wallet info", ArgsManager::ALLOW_ANY,
46 argsman.AddArg("create", "Create new wallet file", ArgsManager::ALLOW_ANY,
48 argsman.AddArg("salvage",
49 "Attempt to recover private keys from a corrupt wallet",
51}
52
53static bool WalletAppInit(int argc, char *argv[]) {
55 std::string error_message;
56 if (!gArgs.ParseParameters(argc, argv, error_message)) {
57 tfm::format(std::cerr, "Error parsing command line arguments: %s\n",
58 error_message);
59 return false;
60 }
61 if (argc < 2 || HelpRequested(gArgs) || gArgs.IsArgSet("-version")) {
62 std::string usage =
63 strprintf("%s bitcoin-wallet version", PACKAGE_NAME) + " " +
64 FormatFullVersion() + "\n";
65
66 if (gArgs.IsArgSet("-version")) {
67 usage += FormatParagraph(LicenseInfo());
68 } else {
69 usage +=
70 "\n"
71 "bitcoin-wallet is an offline tool for creating and "
72 "interacting with " PACKAGE_NAME " wallet files.\n"
73 "By default bitcoin-wallet will act on wallets in the default "
74 "mainnet wallet directory in the datadir.\n"
75 "To change the target wallet, use the -datadir, -wallet and "
76 "-testnet/-regtest arguments.\n\n"
77 "Usage:\n"
78 " bitcoin-wallet [options] <command>\n";
79 usage += "\n" + gArgs.GetHelpMessage();
80 }
81 tfm::format(std::cout, "%s", usage);
82 return false;
83 }
84
85 // check for printtoconsole, allow -debug
87 gArgs.GetBoolArg("-printtoconsole", gArgs.GetBoolArg("-debug", false));
88
90 tfm::format(std::cerr,
91 "Error: Specified data directory \"%s\" does not exist.\n",
92 gArgs.GetArg("-datadir", ""));
93 return false;
94 }
95 // Check for -testnet or -regtest parameter (Params() calls are only valid
96 // after this clause)
98
99 return true;
100}
101
102int main(int argc, char *argv[]) {
103#ifdef WIN32
104 common::WinCmdLineArgs winArgs;
105 std::tie(argc, argv) = winArgs.get();
106#endif
108 RandomInit();
109 try {
110 if (!WalletAppInit(argc, argv)) {
111 return EXIT_FAILURE;
112 }
113 } catch (const std::exception &e) {
114 PrintExceptionContinue(&e, "WalletAppInit()");
115 return EXIT_FAILURE;
116 } catch (...) {
117 PrintExceptionContinue(nullptr, "WalletAppInit()");
118 return EXIT_FAILURE;
119 }
120
121 std::string method{};
122 for (int i = 1; i < argc; ++i) {
123 if (!IsSwitchChar(argv[i][0])) {
124 if (!method.empty()) {
125 tfm::format(std::cerr,
126 "Error: two methods provided (%s and %s). Only one "
127 "method should be provided.\n",
128 method, argv[i]);
129 return EXIT_FAILURE;
130 }
131 method = argv[i];
132 }
133 }
134
135 if (method.empty()) {
136 tfm::format(std::cerr,
137 "No method provided. Run `bitcoin-wallet -help` for "
138 "valid methods.\n");
139 return EXIT_FAILURE;
140 }
141
142 // A name must be provided when creating a file
143 if (method == "create" && !gArgs.IsArgSet("-wallet")) {
145 std::cerr,
146 "Wallet name must be provided when creating a new wallet.\n");
147 return EXIT_FAILURE;
148 }
149
150 std::string name = gArgs.GetArg("-wallet", "");
151
153 ECC_Start();
155 return EXIT_FAILURE;
156 }
157 ECC_Stop();
158 return EXIT_SUCCESS;
159}
bool HelpRequested(const ArgsManager &args)
Definition: args.cpp:732
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: args.cpp:737
bool CheckDataDirOption(const ArgsManager &args)
Definition: args.cpp:784
ArgsManager gArgs
Definition: args.cpp:38
bool IsSwitchChar(char c)
Definition: args.h:47
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.
Definition: chainparams.cpp:51
void SetupChainParamsBaseOptions(ArgsManager &argsman)
Set the arguments for chainparams.
@ NETWORK_ONLY
Definition: args.h:110
@ ALLOW_ANY
Definition: args.h:103
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: args.cpp:201
std::string GetHelpMessage() const
Get the help string.
Definition: args.cpp:653
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: args.cpp:381
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: args.cpp:494
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:556
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
Definition: args.cpp:620
std::string GetChainName() const
Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
Definition: args.cpp:793
bool m_print_to_console
Definition: logging.h:111
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:223
std::string FormatFullVersion()
std::string LicenseInfo()
Returns licensing information (for -version)
static std::unique_ptr< ECCVerifyHandle > globalVerifyHandle
Definition: common.cpp:27
void SetupCurrencyUnitOptions(ArgsManager &argsman)
Definition: currencyunit.cpp:9
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: exception.cpp:38
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:21
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:1112
void RandomInit()
Initialize global RNG state and log any CPU features that are used.
Definition: random.cpp:786
const char * name
Definition: rest.cpp:47
void SetupEnvironment()
Definition: system.cpp:70
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
std::string FormatParagraph(std::string_view in, size_t width, size_t indent)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line.