2016-09-16 16:45:36 +02:00
// Copyright (c) 2016-2018 The Bitcoin Core developers
// 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>
# include <util/strencodings.h>
2019-06-17 09:56:52 +02:00
# include <util/system.h>
# include <util/translation.h>
2016-09-16 16:45:36 +02:00
# include <wallet/wallettool.h>
2019-06-17 09:56:52 +02:00
# include <functional>
2016-09-16 16:45:36 +02:00
# include <stdio.h>
const std : : function < std : : string ( const char * ) > G_TRANSLATION_FUN = nullptr ;
static void SetupWalletToolArgs ( )
{
2019-02-06 19:57:52 +01:00
SetupHelpOptions ( gArgs ) ;
2016-09-16 16:45:36 +02:00
SetupChainParamsBaseOptions ( ) ;
scripted-diff: Use ArgsManager::DEBUG_ONLY flag
-BEGIN VERIFY SCRIPT-
sed -i 's/unsigned int flags, const bool debug_only,/unsigned int flags,/' src/util/system.h src/util/system.cpp
sed -i 's/ArgsManager::NONE, debug_only/flags, false/' src/util/system.cpp
sed -i 's/arg.second.m_debug_only/(arg.second.m_flags \& ArgsManager::DEBUG_ONLY)/' src/util/system.cpp
sed -i 's/ArgsManager::ALLOW_ANY, true, OptionsCategory::/ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::/' $(git grep --files-with-matches 'AddArg(' src)
sed -i 's/ArgsManager::ALLOW_ANY, false, OptionsCategory::/ArgsManager::ALLOW_ANY, OptionsCategory::/' $(git grep --files-with-matches 'AddArg(' src)
-END VERIFY SCRIPT-
2019-07-27 11:06:32 +02:00
gArgs . AddArg ( " -datadir=<dir> " , " Specify data directory " , ArgsManager : : ALLOW_ANY , OptionsCategory : : OPTIONS ) ;
2019-07-27 11:37:09 +02:00
gArgs . AddArg ( " -wallet=<wallet-name> " , " Specify wallet name " , ArgsManager : : ALLOW_ANY | ArgsManager : : NETWORK_ONLY , OptionsCategory : : OPTIONS ) ;
scripted-diff: Use ArgsManager::DEBUG_ONLY flag
-BEGIN VERIFY SCRIPT-
sed -i 's/unsigned int flags, const bool debug_only,/unsigned int flags,/' src/util/system.h src/util/system.cpp
sed -i 's/ArgsManager::NONE, debug_only/flags, false/' src/util/system.cpp
sed -i 's/arg.second.m_debug_only/(arg.second.m_flags \& ArgsManager::DEBUG_ONLY)/' src/util/system.cpp
sed -i 's/ArgsManager::ALLOW_ANY, true, OptionsCategory::/ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::/' $(git grep --files-with-matches 'AddArg(' src)
sed -i 's/ArgsManager::ALLOW_ANY, false, OptionsCategory::/ArgsManager::ALLOW_ANY, OptionsCategory::/' $(git grep --files-with-matches 'AddArg(' src)
-END VERIFY SCRIPT-
2019-07-27 11:06:32 +02:00
gArgs . AddArg ( " -debug=<category> " , " Output debugging information (default: 0). " , ArgsManager : : ALLOW_ANY , OptionsCategory : : DEBUG_TEST ) ;
gArgs . AddArg ( " -printtoconsole " , " Send trace/debug info to console (default: 1 when no -debug is true, 0 otherwise. " , ArgsManager : : ALLOW_ANY , OptionsCategory : : DEBUG_TEST ) ;
2016-09-16 16:45:36 +02:00
scripted-diff: Use ArgsManager::DEBUG_ONLY flag
-BEGIN VERIFY SCRIPT-
sed -i 's/unsigned int flags, const bool debug_only,/unsigned int flags,/' src/util/system.h src/util/system.cpp
sed -i 's/ArgsManager::NONE, debug_only/flags, false/' src/util/system.cpp
sed -i 's/arg.second.m_debug_only/(arg.second.m_flags \& ArgsManager::DEBUG_ONLY)/' src/util/system.cpp
sed -i 's/ArgsManager::ALLOW_ANY, true, OptionsCategory::/ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::/' $(git grep --files-with-matches 'AddArg(' src)
sed -i 's/ArgsManager::ALLOW_ANY, false, OptionsCategory::/ArgsManager::ALLOW_ANY, OptionsCategory::/' $(git grep --files-with-matches 'AddArg(' src)
-END VERIFY SCRIPT-
2019-07-27 11:06:32 +02:00
gArgs . AddArg ( " info " , " Get wallet info " , ArgsManager : : ALLOW_ANY , OptionsCategory : : COMMANDS ) ;
gArgs . AddArg ( " create " , " Create new wallet file " , ArgsManager : : ALLOW_ANY , OptionsCategory : : COMMANDS ) ;
2016-09-16 16:45:36 +02:00
}
static bool WalletAppInit ( int argc , char * argv [ ] )
{
SetupWalletToolArgs ( ) ;
std : : string error_message ;
if ( ! gArgs . ParseParameters ( argc , argv , error_message ) ) {
2019-06-13 15:16:10 +02:00
tfm : : format ( std : : cerr , " Error parsing command line arguments: %s \n " , error_message . c_str ( ) ) ;
2016-09-16 16:45:36 +02:00
return false ;
}
if ( argc < 2 | | HelpRequested ( gArgs ) ) {
std : : string usage = strprintf ( " %s bitcoin-wallet version " , PACKAGE_NAME ) + " " + FormatFullVersion ( ) + " \n \n " +
" wallet-tool is an offline tool for creating and interacting with Bitcoin Core wallet files. \n " +
" By default wallet-tool 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 \n " +
gArgs . GetHelpMessage ( ) ;
2019-06-13 15:16:10 +02:00
tfm : : format ( std : : cout , " %s " , usage . c_str ( ) ) ;
2016-09-16 16:45:36 +02:00
return false ;
}
// check for printtoconsole, allow -debug
2019-02-05 00:27:39 +01:00
LogInstance ( ) . m_print_to_console = gArgs . GetBoolArg ( " -printtoconsole " , gArgs . GetBoolArg ( " -debug " , false ) ) ;
2016-09-16 16:45:36 +02:00
2019-07-24 17:54:52 +02:00
if ( ! CheckDataDirOption ( ) ) {
2019-06-13 15:16:10 +02:00
tfm : : format ( std : : cerr , " Error: Specified data directory \" %s \" does not exist. \n " , gArgs . GetArg ( " -datadir " , " " ) . c_str ( ) ) ;
2016-09-16 16:45:36 +02:00
return false ;
}
// Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
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-06-13 15:16:10 +02:00
tfm : : format ( std : : cerr , " Error: two methods provided (%s and %s). Only one method should be provided. \n " , method . c_str ( ) , argv [ i ] ) ;
2016-09-16 16:45:36 +02:00
return EXIT_FAILURE ;
}
method = argv [ i ] ;
}
}
if ( method . empty ( ) ) {
2019-06-13 15:16:10 +02:00
tfm : : format ( std : : cerr , " No method provided. Run `bitcoin-wallet -help` for valid methods. \n " ) ;
2016-09-16 16:45:36 +02:00
return EXIT_FAILURE ;
}
// A name must be provided when creating a file
if ( method = = " create " & & ! gArgs . IsArgSet ( " -wallet " ) ) {
2019-06-13 15:16:10 +02:00
tfm : : format ( std : : cerr , " Wallet name must be provided when creating a new wallet. \n " ) ;
2016-09-16 16:45:36 +02:00
return EXIT_FAILURE ;
}
std : : string name = gArgs . GetArg ( " -wallet " , " " ) ;
ECCVerifyHandle globalVerifyHandle ;
ECC_Start ( ) ;
if ( ! WalletTool : : ExecuteWalletToolFunc ( method , name ) )
return EXIT_FAILURE ;
ECC_Stop ( ) ;
return EXIT_SUCCESS ;
}