Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2016-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <bitcoin-build-config.h> // IWYU pragma: keep7#include <chainparams.h>8#include <chainparamsbase.h>9#include <clientversion.h>10#include <common/args.h>11#include <common/license_info.h>12#include <common/system.h>13#include <compat/compat.h>14#include <crypto/sha256.h>15#include <interfaces/init.h>16#include <key.h>17#include <logging.h>18#include <pubkey.h>19#include <tinyformat.h>20#include <util/exception.h>21#include <util/translation.h>22#include <wallet/wallettool.h>24#include <exception>25#include <functional>26#include <string>27#include <tuple>29using util::Join;31const TranslateFn G_TRANSLATION_FUN{nullptr};33static void SetupWalletToolArgs(ArgsManager& argsman)34{35SetupHelpOptions(argsman);36SetupChainParamsBaseOptions(argsman);38argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);39argsman.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_NEGATION, OptionsCategory::OPTIONS);40argsman.AddArg("-wallet=<wallet-name>", "Specify wallet name", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::OPTIONS);41argsman.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::COMMAND_OPTIONS);42argsman.AddArg("-debug=<category>", "Output debugging information (default: 0).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);43argsman.AddArg("-printtoconsole", "Send trace/debug info to console (default: 1 when no -debug is true, 0 otherwise).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);45argsman.AddCommand("info", "Get wallet info");46argsman.AddCommand("create", "Create a new descriptor wallet file");47argsman.AddCommand("dump", "Print out all of the wallet key-value records", {"-dumpfile"});48argsman.AddCommand("createfromdump", "Create new wallet file from dumped records", {"-dumpfile"});49}51static std::optional<int> WalletAppInit(ArgsManager& args, int argc, char* argv[])52{53SetupWalletToolArgs(args);54std::string error_message;55if (!args.ParseParameters(argc, argv, error_message)) {56tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error_message);57return EXIT_FAILURE;58}59const bool missing_args{argc < 2};60if (missing_args || HelpRequested(args) || args.GetBoolArg("-version", false)) {61std::string strUsage = strprintf("%s bitcoin-wallet utility version", CLIENT_NAME) + " " + FormatFullVersion() + "\n";63if (args.GetBoolArg("-version", false)) {64strUsage += FormatParagraph(LicenseInfo());65} else {66strUsage += "\n"67"bitcoin-wallet is an offline tool for creating and interacting with " CLIENT_NAME " wallet files.\n\n"68"By default bitcoin-wallet will act on wallets in the default mainnet wallet directory in the datadir.\n\n"69"To change the target wallet, use the -datadir, -wallet and (test)chain selection arguments.\n"70"\n"71"Usage: bitcoin-wallet [options] <command>\n"72"\n";73strUsage += "\n" + args.GetHelpMessage();74}75tfm::format(std::cout, "%s", strUsage);76if (missing_args) {77tfm::format(std::cerr, "Error: too few parameters\n");78return EXIT_FAILURE;79}80return EXIT_SUCCESS;81}83// check for printtoconsole, allow -debug84LogInstance().m_print_to_console = args.GetBoolArg("-printtoconsole", args.GetBoolArg("-debug", false));86if (!CheckDataDirOption(args)) {87tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", args.GetArg("-datadir", ""));88return EXIT_FAILURE;89}90// Check for chain settings (Params() calls are only valid after this clause)91SelectParams(args.GetChainType());93return std::nullopt;94}96MAIN_FUNCTION97{98ArgsManager& args = gArgs;100int exit_status;101std::unique_ptr<interfaces::Init> init = interfaces::MakeWalletInit(argc, argv, exit_status);102if (!init) {103return exit_status;104}106SetupEnvironment();107SHA256AutoDetect();108RandomInit();109try {110if (const auto maybe_exit{WalletAppInit(args, argc, argv)}) return *maybe_exit;111} catch (const std::exception& e) {112PrintExceptionContinue(&e, "WalletAppInit()");113return EXIT_FAILURE;114} catch (...) {115PrintExceptionContinue(nullptr, "WalletAppInit()");116return EXIT_FAILURE;117}119const auto command = args.GetCommand();120if (!command) {121tfm::format(std::cerr, "No method provided. Run `bitcoin-wallet -help` for valid methods.\n");122return EXIT_FAILURE;123}124if (command->args.size() != 0) {125tfm::format(std::cerr, "Error: Additional arguments provided (%s). Methods do not take arguments. Please refer to `-help`.\n", Join(command->args, ", "));126return EXIT_FAILURE;127}129ECC_Context ecc_context{};130if (!wallet::WalletTool::ExecuteWalletToolFunc(args, command->command)) {131return EXIT_FAILURE;132}133return EXIT_SUCCESS;134}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.