2021-12-30 14:36:57 -03:00
// Copyright (c) 2016-2021 The Bitcoin Core developers
2016-09-16 11:45:36 -03:00
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
# if defined(HAVE_CONFIG_H)
# include <config/bitcoin-config.h>
# endif
# include <chainparams.h>
# include <chainparamsbase.h>
2021-10-24 13:27:51 -03:00
# include <clientversion.h>
2017-12-05 17:57:12 -03:00
# include <interfaces/init.h>
2021-10-24 13:27:51 -03:00
# include <key.h>
2016-09-16 11:45:36 -03:00
# include <logging.h>
2021-10-24 13:27:51 -03:00
# include <pubkey.h>
# include <tinyformat.h>
2019-06-17 03:56:52 -04:00
# include <util/system.h>
# include <util/translation.h>
2020-04-02 09:35:10 -03:00
# include <util/url.h>
2016-09-16 11:45:36 -03:00
# include <wallet/wallettool.h>
2021-10-24 13:27:51 -03:00
# include <exception>
2019-06-17 03:56:52 -04:00
# include <functional>
2021-10-24 13:27:51 -03:00
# include <string>
# include <tuple>
2016-09-16 11:45:36 -03:00
const std : : function < std : : string ( const char * ) > G_TRANSLATION_FUN = nullptr ;
2020-04-02 09:35:10 -03:00
UrlDecodeFn * const URL_DECODE = nullptr ;
2016-09-16 11:45:36 -03:00
2020-07-19 03:31:51 -04:00
static void SetupWalletToolArgs ( ArgsManager & argsman )
2016-09-16 11:45:36 -03:00
{
2020-07-19 03:31:51 -04:00
SetupHelpOptions ( argsman ) ;
SetupChainParamsBaseOptions ( argsman ) ;
2016-09-16 11:45:36 -03:00
2020-11-28 20:43:55 -03:00
argsman . AddArg ( " -version " , " Print version and exit " , ArgsManager : : ALLOW_ANY , OptionsCategory : : OPTIONS ) ;
2020-07-19 03:47:05 -04:00
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 ) ;
2021-08-18 21:08:08 -04:00
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 ) ;
2020-07-19 03:47:05 -04:00
argsman . AddArg ( " -debug=<category> " , " Output debugging information (default: 0). " , ArgsManager : : ALLOW_ANY , OptionsCategory : : DEBUG_TEST ) ;
2021-08-18 21:08:08 -04:00
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 ) ;
2020-06-01 18:00:22 -04:00
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 ) ;
2020-07-19 03:47:05 -04:00
argsman . AddArg ( " -printtoconsole " , " Send trace/debug info to console (default: 1 when no -debug is true, 0 otherwise). " , ArgsManager : : ALLOW_ANY , OptionsCategory : : DEBUG_TEST ) ;
2021-06-17 08:01:57 -04:00
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 " ) ;
2016-09-16 11:45:36 -03:00
}
2020-12-18 11:14:08 -03:00
static bool WalletAppInit ( ArgsManager & args , int argc , char * argv [ ] )
2016-09-16 11:45:36 -03:00
{
2020-12-18 11:14:08 -03:00
SetupWalletToolArgs ( args ) ;
2016-09-16 11:45:36 -03:00
std : : string error_message ;
2020-12-18 11:14:08 -03:00
if ( ! args . ParseParameters ( argc , argv , error_message ) ) {
2019-10-28 09:30:20 -03:00
tfm : : format ( std : : cerr , " Error parsing command line arguments: %s \n " , error_message ) ;
2016-09-16 11:45:36 -03:00
return false ;
}
2020-12-18 11:14:08 -03:00
if ( argc < 2 | | HelpRequested ( args ) | | args . IsArgSet ( " -version " ) ) {
2020-11-28 20:43:55 -03:00
std : : string strUsage = strprintf ( " %s bitcoin-wallet version " , PACKAGE_NAME ) + " " + FormatFullVersion ( ) + " \n " ;
2020-12-18 11:14:08 -03:00
if ( ! args . IsArgSet ( " -version " ) ) {
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 "
2022-02-18 02:29:06 -03:00
" To change the target wallet, use the -datadir, -wallet and -regtest/-signet/-testnet arguments. \n \n "
2020-12-18 11:14:08 -03:00
" Usage: \n "
" bitcoin-wallet [options] <command> \n " ;
strUsage + = " \n " + args . GetHelpMessage ( ) ;
}
2020-11-28 20:43:55 -03:00
tfm : : format ( std : : cout , " %s " , strUsage ) ;
2016-09-16 11:45:36 -03:00
return false ;
}
// check for printtoconsole, allow -debug
2020-12-18 11:14:08 -03:00
LogInstance ( ) . m_print_to_console = args . GetBoolArg ( " -printtoconsole " , args . GetBoolArg ( " -debug " , false ) ) ;
2016-09-16 11:45:36 -03:00
2019-07-24 11:54:52 -04:00
if ( ! CheckDataDirOption ( ) ) {
2020-12-18 11:14:08 -03:00
tfm : : format ( std : : cerr , " Error: Specified data directory \" %s \" does not exist. \n " , args . GetArg ( " -datadir " , " " ) ) ;
2016-09-16 11:45:36 -03:00
return false ;
}
2020-09-22 10:57:30 -03:00
// Check for chain settings (Params() calls are only valid after this clause)
2020-12-18 11:14:08 -03:00
SelectParams ( args . GetChainName ( ) ) ;
2016-09-16 11:45:36 -03:00
return true ;
}
int main ( int argc , char * argv [ ] )
{
2020-12-18 11:14:08 -03:00
ArgsManager & args = gArgs ;
2016-09-16 11:45:36 -03:00
# ifdef WIN32
util : : WinCmdLineArgs winArgs ;
std : : tie ( argc , argv ) = winArgs . get ( ) ;
# endif
2017-12-05 17:57:12 -03:00
int exit_status ;
std : : unique_ptr < interfaces : : Init > init = interfaces : : MakeWalletInit ( argc , argv , exit_status ) ;
if ( ! init ) {
return exit_status ;
}
2016-09-16 11:45:36 -03:00
SetupEnvironment ( ) ;
RandomInit ( ) ;
try {
2020-12-18 11:14:08 -03:00
if ( ! WalletAppInit ( args , argc , argv ) ) return EXIT_FAILURE ;
2016-09-16 11:45:36 -03:00
} catch ( const std : : exception & e ) {
PrintExceptionContinue ( & e , " WalletAppInit() " ) ;
return EXIT_FAILURE ;
} catch ( . . . ) {
PrintExceptionContinue ( nullptr , " WalletAppInit() " ) ;
return EXIT_FAILURE ;
}
2021-01-13 05:00:43 -03:00
const auto command = args . GetCommand ( ) ;
if ( ! command ) {
2019-06-13 09:16:10 -04:00
tfm : : format ( std : : cerr , " No method provided. Run `bitcoin-wallet -help` for valid methods. \n " ) ;
2016-09-16 11:45:36 -03:00
return EXIT_FAILURE ;
}
2021-01-13 05:00:43 -03:00
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 ;
}
2016-09-16 11:45:36 -03:00
ECCVerifyHandle globalVerifyHandle ;
ECC_Start ( ) ;
2021-11-12 13:13:29 -03:00
if ( ! wallet : : WalletTool : : ExecuteWalletToolFunc ( args , command - > command ) ) {
2016-09-16 11:45:36 -03:00
return EXIT_FAILURE ;
2020-12-17 15:18:34 -03:00
}
2016-09-16 11:45:36 -03:00
ECC_Stop ( ) ;
return EXIT_SUCCESS ;
}