2020-04-16 13:14:08 -04:00
// Copyright (c) 2016-2020 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>
# include <logging.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>
2019-06-17 03:56:52 -04:00
# include <functional>
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 ) ;
2020-06-01 18:00:22 -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_STRING , 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 ) ;
2020-12-17 15:52:36 -03:00
argsman . AddArg ( " -descriptors " , " Create descriptors wallet. Only for 'create' " , ArgsManager : : ALLOW_BOOL , 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 ) ;
argsman . AddArg ( " info " , " Get wallet info " , ArgsManager : : ALLOW_ANY , OptionsCategory : : COMMANDS ) ;
argsman . AddArg ( " create " , " Create new wallet file " , ArgsManager : : ALLOW_ANY , OptionsCategory : : COMMANDS ) ;
2020-10-30 09:54:03 -03:00
argsman . AddArg ( " salvage " , " Attempt to recover private keys from a corrupt wallet. Warning: 'salvage' is experimental. " , ArgsManager : : ALLOW_ANY , OptionsCategory : : COMMANDS ) ;
2020-06-01 16:31:25 -04:00
argsman . AddArg ( " dump " , " Print out all of the wallet key-value records " , ArgsManager : : ALLOW_ANY , OptionsCategory : : COMMANDS ) ;
2020-06-01 18:00:22 -04:00
argsman . AddArg ( " createfromdump " , " Create new wallet file from dumped records " , ArgsManager : : ALLOW_ANY , OptionsCategory : : COMMANDS ) ;
2016-09-16 11:45:36 -03:00
}
static bool WalletAppInit ( int argc , char * argv [ ] )
{
2020-07-19 03:31:51 -04:00
SetupWalletToolArgs ( gArgs ) ;
2016-09-16 11:45:36 -03:00
std : : string error_message ;
if ( ! gArgs . 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-11-28 20:43:55 -03:00
if ( argc < 2 | | HelpRequested ( gArgs ) | | gArgs . IsArgSet ( " -version " ) ) {
std : : string strUsage = strprintf ( " %s bitcoin-wallet version " , PACKAGE_NAME ) + " " + FormatFullVersion ( ) + " \n " ;
if ( ! gArgs . 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 "
" To change the target wallet, use the -datadir, -wallet and -testnet/-regtest arguments. \n \n "
" Usage: \n "
" bitcoin-wallet [options] <command> \n " ;
strUsage + = " \n " + gArgs . GetHelpMessage ( ) ;
}
tfm : : format ( std : : cout , " %s " , strUsage ) ;
2016-09-16 11:45:36 -03:00
return false ;
}
// check for printtoconsole, allow -debug
2019-02-04 20:27:39 -03:00
LogInstance ( ) . m_print_to_console = gArgs . GetBoolArg ( " -printtoconsole " , gArgs . GetBoolArg ( " -debug " , false ) ) ;
2016-09-16 11:45:36 -03:00
2019-07-24 11:54:52 -04:00
if ( ! CheckDataDirOption ( ) ) {
2019-10-28 09:30:20 -03:00
tfm : : format ( std : : cerr , " Error: Specified data directory \" %s \" does not exist. \n " , gArgs . 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)
2016-09-16 11:45:36 -03:00
SelectParams ( gArgs . GetChainName ( ) ) ;
return true ;
}
int main ( int argc , char * argv [ ] )
{
# ifdef WIN32
util : : WinCmdLineArgs winArgs ;
std : : tie ( argc , argv ) = winArgs . get ( ) ;
# endif
SetupEnvironment ( ) ;
RandomInit ( ) ;
try {
if ( ! WalletAppInit ( argc , argv ) ) return EXIT_FAILURE ;
} catch ( const std : : exception & e ) {
PrintExceptionContinue ( & e , " WalletAppInit() " ) ;
return EXIT_FAILURE ;
} catch ( . . . ) {
PrintExceptionContinue ( nullptr , " WalletAppInit() " ) ;
return EXIT_FAILURE ;
}
std : : string method { } ;
for ( int i = 1 ; i < argc ; + + i ) {
if ( ! IsSwitchChar ( argv [ i ] [ 0 ] ) ) {
if ( ! method . empty ( ) ) {
2019-10-28 09:30:20 -03:00
tfm : : format ( std : : cerr , " Error: two methods provided (%s and %s). Only one method should be provided. \n " , method , argv [ i ] ) ;
2016-09-16 11:45:36 -03:00
return EXIT_FAILURE ;
}
method = argv [ i ] ;
}
}
if ( method . empty ( ) ) {
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 ;
}
// A name must be provided when creating a file
if ( method = = " create " & & ! gArgs . IsArgSet ( " -wallet " ) ) {
2019-06-13 09:16:10 -04:00
tfm : : format ( std : : cerr , " Wallet name must be provided when creating a new wallet. \n " ) ;
2016-09-16 11:45:36 -03:00
return EXIT_FAILURE ;
}
std : : string name = gArgs . GetArg ( " -wallet " , " " ) ;
ECCVerifyHandle globalVerifyHandle ;
ECC_Start ( ) ;
2020-12-17 15:18:34 -03:00
if ( ! WalletTool : : ExecuteWalletToolFunc ( gArgs , method , name ) ) {
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 ;
}