mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-24 12:41:41 -05:00

c7376babd1
doc: Clarify distinction between util and common libraries in libraries.md (Ryan Ofsky)4f74c59334
util: Move util/string.h functions to util namespace (Ryan Ofsky)4d05d3f3b4
util: add TransactionError includes and namespace declarations (Ryan Ofsky)680eafdc74
util: move fees.h and error.h to common/messages.h (Ryan Ofsky)02e62c6c9a
common: Add PSBTError enum (Ryan Ofsky)0d44c44ae3
util: move error.h TransactionError enum to node/types.h (Ryan Ofsky)9bcce2608d
util: move spanparsing.h to script/parsing.h (Ryan Ofsky)6dd2ad4792
util: move spanparsing.h Split functions to string.h (Ryan Ofsky)23cc8ddff4
util: move HexStr and HexDigit from util to crypto (TheCharlatan)6861f954f8
util: move util/message to common/signmessage (Ryan Ofsky)cc5f29fbea
build: move memory_cleanse from util to crypto (Ryan Ofsky)5b9309420c
build: move chainparamsbase from util to common (Ryan Ofsky)ffa27af24d
test: Add check-deps.sh script to check for unexpected library dependencies (Ryan Ofsky) Pull request description: Remove `fees.h`, `errors.h`, and `spanparsing.h` from the util library. Specifically: - Move `Split` functions from `util/spanparsing.h` to `util/string.h`, using `util` namespace for clarity. - Move remaining spanparsing functions to `script/parsing.h` since they are used for descriptor and miniscript parsing. - Combine `util/fees.h` and `util/errors.h` into `common/messages.h` so there is a place for simple functions that generate user messages to live, and these functions are not part of the util library. Motivation for this change is that the util library is a dependency of the kernel, and we should remove functionality from util that shouldn't be called by kernel code or kernel applications. These changes should also improve code organization and make functions easier to discover. Some of these same moves are (or were) part of #28690, but did not help with code organization, or made it worse, so it is better to move them and clean them up in the same PR so code only has to change one time. ACKs for top commit: achow101: ACKc7376babd1
TheCharlatan: Re-ACKc7376babd1
hebasto: re-ACKc7376babd1
. Tree-SHA512: 5bcef16c1255463b1b69270548711e7ff78ca0dd34e300b95e3ca1ce52ceb34f83d9ddb2839e83800ba36b200de30396e504bbb04fa02c6d0c24a16d06ae523d
139 lines
6.1 KiB
C++
139 lines
6.1 KiB
C++
// Copyright (c) 2016-2022 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <config/bitcoin-config.h> // IWYU pragma: keep
|
|
|
|
#include <chainparams.h>
|
|
#include <chainparamsbase.h>
|
|
#include <clientversion.h>
|
|
#include <common/args.h>
|
|
#include <common/system.h>
|
|
#include <compat/compat.h>
|
|
#include <interfaces/init.h>
|
|
#include <key.h>
|
|
#include <logging.h>
|
|
#include <pubkey.h>
|
|
#include <tinyformat.h>
|
|
#include <util/exception.h>
|
|
#include <util/translation.h>
|
|
#include <wallet/wallettool.h>
|
|
|
|
#include <exception>
|
|
#include <functional>
|
|
#include <string>
|
|
#include <tuple>
|
|
|
|
using util::Join;
|
|
|
|
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
|
|
|
|
static void SetupWalletToolArgs(ArgsManager& argsman)
|
|
{
|
|
SetupHelpOptions(argsman);
|
|
SetupChainParamsBaseOptions(argsman);
|
|
|
|
argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
|
|
argsman.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
|
|
argsman.AddArg("-wallet=<wallet-name>", "Specify wallet name", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::OPTIONS);
|
|
argsman.AddArg("-dumpfile=<file name>", "When used with 'dump', writes out the records to this file. When used with 'createfromdump', loads the records into a new wallet.", ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::OPTIONS);
|
|
argsman.AddArg("-debug=<category>", "Output debugging information (default: 0).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
|
|
argsman.AddArg("-descriptors", "Create descriptors wallet. Only for 'create'", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
|
|
argsman.AddArg("-legacy", "Create legacy wallet. Only for 'create'", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
|
|
argsman.AddArg("-format=<format>", "The format of the wallet file to create. Either \"bdb\" or \"sqlite\". Only used with 'createfromdump'", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
|
|
argsman.AddArg("-printtoconsole", "Send trace/debug info to console (default: 1 when no -debug is true, 0 otherwise).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
|
|
argsman.AddArg("-withinternalbdb", "Use the internal Berkeley DB parser when dumping a Berkeley DB wallet file (default: false)", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
|
|
|
|
argsman.AddCommand("info", "Get wallet info");
|
|
argsman.AddCommand("create", "Create new wallet file");
|
|
argsman.AddCommand("salvage", "Attempt to recover private keys from a corrupt wallet. Warning: 'salvage' is experimental.");
|
|
argsman.AddCommand("dump", "Print out all of the wallet key-value records");
|
|
argsman.AddCommand("createfromdump", "Create new wallet file from dumped records");
|
|
}
|
|
|
|
static std::optional<int> WalletAppInit(ArgsManager& args, int argc, char* argv[])
|
|
{
|
|
SetupWalletToolArgs(args);
|
|
std::string error_message;
|
|
if (!args.ParseParameters(argc, argv, error_message)) {
|
|
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error_message);
|
|
return EXIT_FAILURE;
|
|
}
|
|
const bool missing_args{argc < 2};
|
|
if (missing_args || HelpRequested(args) || args.IsArgSet("-version")) {
|
|
std::string strUsage = strprintf("%s bitcoin-wallet version", PACKAGE_NAME) + " " + FormatFullVersion() + "\n";
|
|
|
|
if (args.IsArgSet("-version")) {
|
|
strUsage += FormatParagraph(LicenseInfo());
|
|
} else {
|
|
strUsage += "\n"
|
|
"bitcoin-wallet is an offline tool for creating and interacting with " PACKAGE_NAME " wallet files.\n"
|
|
"By default bitcoin-wallet will act on wallets in the default mainnet wallet directory in the datadir.\n"
|
|
"To change the target wallet, use the -datadir, -wallet and -regtest/-signet/-testnet arguments.\n\n"
|
|
"Usage:\n"
|
|
" bitcoin-wallet [options] <command>\n";
|
|
strUsage += "\n" + args.GetHelpMessage();
|
|
}
|
|
tfm::format(std::cout, "%s", strUsage);
|
|
if (missing_args) {
|
|
tfm::format(std::cerr, "Error: too few parameters\n");
|
|
return EXIT_FAILURE;
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
// check for printtoconsole, allow -debug
|
|
LogInstance().m_print_to_console = args.GetBoolArg("-printtoconsole", args.GetBoolArg("-debug", false));
|
|
|
|
if (!CheckDataDirOption(args)) {
|
|
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", args.GetArg("-datadir", ""));
|
|
return EXIT_FAILURE;
|
|
}
|
|
// Check for chain settings (Params() calls are only valid after this clause)
|
|
SelectParams(args.GetChainType());
|
|
|
|
return std::nullopt;
|
|
}
|
|
|
|
MAIN_FUNCTION
|
|
{
|
|
ArgsManager& args = gArgs;
|
|
#ifdef WIN32
|
|
common::WinCmdLineArgs winArgs;
|
|
std::tie(argc, argv) = winArgs.get();
|
|
#endif
|
|
|
|
int exit_status;
|
|
std::unique_ptr<interfaces::Init> init = interfaces::MakeWalletInit(argc, argv, exit_status);
|
|
if (!init) {
|
|
return exit_status;
|
|
}
|
|
|
|
SetupEnvironment();
|
|
RandomInit();
|
|
try {
|
|
if (const auto maybe_exit{WalletAppInit(args, argc, argv)}) return *maybe_exit;
|
|
} catch (const std::exception& e) {
|
|
PrintExceptionContinue(&e, "WalletAppInit()");
|
|
return EXIT_FAILURE;
|
|
} catch (...) {
|
|
PrintExceptionContinue(nullptr, "WalletAppInit()");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
const auto command = args.GetCommand();
|
|
if (!command) {
|
|
tfm::format(std::cerr, "No method provided. Run `bitcoin-wallet -help` for valid methods.\n");
|
|
return EXIT_FAILURE;
|
|
}
|
|
if (command->args.size() != 0) {
|
|
tfm::format(std::cerr, "Error: Additional arguments provided (%s). Methods do not take arguments. Please refer to `-help`.\n", Join(command->args, ", "));
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
ECC_Context ecc_context{};
|
|
if (!wallet::WalletTool::ExecuteWalletToolFunc(args, command->command)) {
|
|
return EXIT_FAILURE;
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|