peerplays_migrated/programs/cli_wallet/main.cpp

289 lines
12 KiB
C++
Raw Normal View History

2015-06-08 15:50:35 +00:00
/*
2015-10-12 17:48:40 +00:00
* Copyright (c) 2015 Cryptonomex, Inc., and contributors.
*
* The MIT License
2015-10-12 17:48:40 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2015-10-12 17:48:40 +00:00
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
2015-10-12 17:02:59 +00:00
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
2015-06-08 15:50:35 +00:00
*/
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <fc/io/json.hpp>
#include <fc/io/stdio.hpp>
#include <fc/network/http/server.hpp>
2015-06-08 15:50:35 +00:00
#include <fc/network/http/websocket.hpp>
#include <fc/rpc/cli.hpp>
#include <fc/rpc/http_api.hpp>
2015-06-08 15:50:35 +00:00
#include <fc/rpc/websocket_api.hpp>
#include <graphene/app/api.hpp>
#include <graphene/chain/config.hpp>
#include <graphene/chain/protocol/protocol.hpp>
#include <graphene/egenesis/egenesis.hpp>
2015-06-08 15:50:35 +00:00
#include <graphene/utilities/key_conversion.hpp>
#include <graphene/wallet/wallet.hpp>
#include <fc/interprocess/signals.hpp>
#include <boost/program_options.hpp>
#include <fc/log/console_appender.hpp>
#include <fc/log/file_appender.hpp>
#include <fc/log/logger.hpp>
#include <fc/log/logger_config.hpp>
#include <graphene/utilities/git_revision.hpp>
#include <boost/version.hpp>
#include <boost/algorithm/string/replace.hpp>
#ifdef WIN32
# include <signal.h>
#else
# include <csignal>
2015-06-08 15:50:35 +00:00
#endif
using namespace graphene::app;
using namespace graphene::chain;
using namespace graphene::utilities;
using namespace graphene::wallet;
using namespace std;
namespace bpo = boost::program_options;
int main( int argc, char** argv )
{
try {
boost::program_options::options_description opts;
opts.add_options()
("help,h", "Print this help message and exit.")
("server-rpc-endpoint,s", bpo::value<string>()->implicit_value("ws://127.0.0.1:8090"), "Server websocket RPC endpoint")
("server-rpc-user,u", bpo::value<string>(), "Server Username")
("server-rpc-password,p", bpo::value<string>(), "Server Password")
("rpc-endpoint,r", bpo::value<string>()->implicit_value("127.0.0.1:8091"), "Endpoint for wallet websocket RPC to listen on")
("rpc-tls-endpoint,t", bpo::value<string>()->implicit_value("127.0.0.1:8092"), "Endpoint for wallet websocket TLS RPC to listen on")
("rpc-tls-certificate,c", bpo::value<string>()->implicit_value("server.pem"), "PEM certificate for wallet websocket TLS RPC")
("rpc-http-endpoint,H", bpo::value<string>()->implicit_value("127.0.0.1:8093"), "Endpoint for wallet HTTP RPC to listen on")
2015-06-08 15:50:35 +00:00
("daemon,d", "Run the wallet in daemon mode" )
2015-08-06 16:41:45 +00:00
("wallet-file,w", bpo::value<string>()->implicit_value("wallet.json"), "wallet to load")
("chain-id", bpo::value<string>(), "chain ID to connect to");
2015-06-08 15:50:35 +00:00
bpo::variables_map options;
bpo::store( bpo::parse_command_line(argc, argv, opts), options );
if( options.count("help") )
{
std::cout << opts << "\n";
return 0;
}
fc::path data_dir;
fc::logging_config cfg;
fc::path log_dir = data_dir / "logs";
fc::file_appender::config ac;
ac.filename = log_dir / "rpc" / "rpc.log";
ac.flush = true;
ac.rotate = true;
ac.rotation_interval = fc::hours( 1 );
ac.rotation_limit = fc::days( 1 );
std::cout << "Logging RPC to file: " << (data_dir / ac.filename).preferred_string() << "\n";
cfg.appenders.push_back(fc::appender_config( "default", "console", fc::variant(fc::console_appender::config(), 20)));
cfg.appenders.push_back(fc::appender_config( "rpc", "file", fc::variant(ac, 5)));
2015-06-08 15:50:35 +00:00
cfg.loggers = { fc::logger_config("default"), fc::logger_config( "rpc") };
Merge Elasticplugin, snapshot plugin and graphene updates to beatrice (#304) * check witness signature before adding block to fork db * Replace verify_no_send_in_progress with no_parallel_execution_guard * fixed cli_wallet log issue * Port plugin sanitization code * avoid directly overwriting wallet file * Implemented "plugins" config variable * allow plugin to have descriptions * Merge pull request #444 from oxarbitrage/elasticsearch Elasticsearch plugin * Merge pull request #500 from oxarbitrage/elasticsearch-extras es_objects plugin * Merge pull request #873 from pmconrad/585_fix_history_ids Fix history ids * Merge pull request #1201 from oxarbitrage/elasticsearch_tests2 Elasticsearch refactor * Merge pull request #1271 from oxarbitrage/es_objects refine es_objects plugin * Merge pull request #1429 from oxarbitrage/es_objects_templates Add an adaptor to es_objects and template function to reduce code * Merge pull request #1458 from oxarbitrage/issue1455 add option elasticsearch-start-es-after-block to es plugin * Merge pull request #1541 from oxarbitrage/es_objects_start_after_block add es-objects-start-es-after-block option * explicitly cleanup external library facilities * Merge pull request #1717 from oxarbitrage/issue1652 add genesis data to es_objects * Merge pull request #1073 from xiangxn/merge-impacted merge impacted into db_notify * Merge pull request #1725 from oxarbitrage/issue1682 elasticsearch history api #1682 * change ES index prefixes to Peerplays-specific * sync develop with beatrice * fix the data writing to ES during sync issues * fix CLI tests * brought updates from mainnet branch (#285) * Fix unit test failures (#289) * fixed unit test failures from the recent merges * fixed unit test failures from the recent merges * enable snapshot plugin (#288) * sync fc branch(build optimization changes) * update to es plugin * fix verify witness signature method (#295) * enable mandatory plugins to have smooth transition for next release * updated tests to keep in-line with plugin changes Co-authored-by: Sandip Patel <sandip@knackroot.com> Co-authored-by: Peter Conrad <conrad@quisquis.de> Co-authored-by: Alfredo <oxarbitrage@gmail.com> Co-authored-by: Abit <abitmore@users.noreply.github.com> Co-authored-by: crypto-ape <43807588+crypto-ape@users.noreply.github.com> Co-authored-by: gladcow <s.gladkov@pbsa.info>
2020-03-09 12:58:13 +00:00
cfg.loggers.front().level = fc::log_level::warn;
2015-06-08 15:50:35 +00:00
cfg.loggers.front().appenders = {"default"};
Merge Elasticplugin, snapshot plugin and graphene updates to beatrice (#304) * check witness signature before adding block to fork db * Replace verify_no_send_in_progress with no_parallel_execution_guard * fixed cli_wallet log issue * Port plugin sanitization code * avoid directly overwriting wallet file * Implemented "plugins" config variable * allow plugin to have descriptions * Merge pull request #444 from oxarbitrage/elasticsearch Elasticsearch plugin * Merge pull request #500 from oxarbitrage/elasticsearch-extras es_objects plugin * Merge pull request #873 from pmconrad/585_fix_history_ids Fix history ids * Merge pull request #1201 from oxarbitrage/elasticsearch_tests2 Elasticsearch refactor * Merge pull request #1271 from oxarbitrage/es_objects refine es_objects plugin * Merge pull request #1429 from oxarbitrage/es_objects_templates Add an adaptor to es_objects and template function to reduce code * Merge pull request #1458 from oxarbitrage/issue1455 add option elasticsearch-start-es-after-block to es plugin * Merge pull request #1541 from oxarbitrage/es_objects_start_after_block add es-objects-start-es-after-block option * explicitly cleanup external library facilities * Merge pull request #1717 from oxarbitrage/issue1652 add genesis data to es_objects * Merge pull request #1073 from xiangxn/merge-impacted merge impacted into db_notify * Merge pull request #1725 from oxarbitrage/issue1682 elasticsearch history api #1682 * change ES index prefixes to Peerplays-specific * sync develop with beatrice * fix the data writing to ES during sync issues * fix CLI tests * brought updates from mainnet branch (#285) * Fix unit test failures (#289) * fixed unit test failures from the recent merges * fixed unit test failures from the recent merges * enable snapshot plugin (#288) * sync fc branch(build optimization changes) * update to es plugin * fix verify witness signature method (#295) * enable mandatory plugins to have smooth transition for next release * updated tests to keep in-line with plugin changes Co-authored-by: Sandip Patel <sandip@knackroot.com> Co-authored-by: Peter Conrad <conrad@quisquis.de> Co-authored-by: Alfredo <oxarbitrage@gmail.com> Co-authored-by: Abit <abitmore@users.noreply.github.com> Co-authored-by: crypto-ape <43807588+crypto-ape@users.noreply.github.com> Co-authored-by: gladcow <s.gladkov@pbsa.info>
2020-03-09 12:58:13 +00:00
cfg.loggers.back().level = fc::log_level::info;
2015-06-08 15:50:35 +00:00
cfg.loggers.back().appenders = {"rpc"};
Merge Elasticplugin, snapshot plugin and graphene updates to beatrice (#304) * check witness signature before adding block to fork db * Replace verify_no_send_in_progress with no_parallel_execution_guard * fixed cli_wallet log issue * Port plugin sanitization code * avoid directly overwriting wallet file * Implemented "plugins" config variable * allow plugin to have descriptions * Merge pull request #444 from oxarbitrage/elasticsearch Elasticsearch plugin * Merge pull request #500 from oxarbitrage/elasticsearch-extras es_objects plugin * Merge pull request #873 from pmconrad/585_fix_history_ids Fix history ids * Merge pull request #1201 from oxarbitrage/elasticsearch_tests2 Elasticsearch refactor * Merge pull request #1271 from oxarbitrage/es_objects refine es_objects plugin * Merge pull request #1429 from oxarbitrage/es_objects_templates Add an adaptor to es_objects and template function to reduce code * Merge pull request #1458 from oxarbitrage/issue1455 add option elasticsearch-start-es-after-block to es plugin * Merge pull request #1541 from oxarbitrage/es_objects_start_after_block add es-objects-start-es-after-block option * explicitly cleanup external library facilities * Merge pull request #1717 from oxarbitrage/issue1652 add genesis data to es_objects * Merge pull request #1073 from xiangxn/merge-impacted merge impacted into db_notify * Merge pull request #1725 from oxarbitrage/issue1682 elasticsearch history api #1682 * change ES index prefixes to Peerplays-specific * sync develop with beatrice * fix the data writing to ES during sync issues * fix CLI tests * brought updates from mainnet branch (#285) * Fix unit test failures (#289) * fixed unit test failures from the recent merges * fixed unit test failures from the recent merges * enable snapshot plugin (#288) * sync fc branch(build optimization changes) * update to es plugin * fix verify witness signature method (#295) * enable mandatory plugins to have smooth transition for next release * updated tests to keep in-line with plugin changes Co-authored-by: Sandip Patel <sandip@knackroot.com> Co-authored-by: Peter Conrad <conrad@quisquis.de> Co-authored-by: Alfredo <oxarbitrage@gmail.com> Co-authored-by: Abit <abitmore@users.noreply.github.com> Co-authored-by: crypto-ape <43807588+crypto-ape@users.noreply.github.com> Co-authored-by: gladcow <s.gladkov@pbsa.info>
2020-03-09 12:58:13 +00:00
fc::configure_logging( cfg );
fc::ecc::private_key committee_private_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("null_key")));
2015-06-08 15:50:35 +00:00
idump( (key_to_wif( committee_private_key ) ) );
2015-06-08 15:50:35 +00:00
fc::ecc::private_key nathan_private_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("nathan")));
public_key_type nathan_pub_key = nathan_private_key.get_public_key();
idump( (nathan_pub_key) );
2015-06-08 15:50:35 +00:00
idump( (key_to_wif( nathan_private_key ) ) );
//
// TODO: We read wallet_data twice, once in main() to grab the
// socket info, again in wallet_api when we do
// load_wallet_file(). Seems like this could be better
// designed.
//
wallet_data wdata;
fc::path wallet_file( options.count("wallet-file") ? options.at("wallet-file").as<string>() : "wallet.json");
if( fc::exists( wallet_file ) )
2015-08-06 16:41:45 +00:00
{
wdata = fc::json::from_file( wallet_file ).as<wallet_data>( GRAPHENE_MAX_NESTED_OBJECTS );
2015-08-06 16:41:45 +00:00
if( options.count("chain-id") )
{
// the --chain-id on the CLI must match the chain ID embedded in the wallet file
if( chain_id_type(options.at("chain-id").as<std::string>()) != wdata.chain_id )
{
std::cout << "Chain ID in wallet file does not match specified chain ID\n";
return 1;
}
}
}
else
{
if( options.count("chain-id") )
{
wdata.chain_id = chain_id_type(options.at("chain-id").as<std::string>());
2015-08-07 20:16:33 +00:00
std::cout << "Starting a new wallet with chain ID " << wdata.chain_id.str() << " (from CLI)\n";
2015-08-06 16:41:45 +00:00
}
else
{
wdata.chain_id = graphene::egenesis::get_egenesis_chain_id();
2015-08-07 20:16:33 +00:00
std::cout << "Starting a new wallet with chain ID " << wdata.chain_id.str() << " (from egenesis)\n";
2015-08-06 16:41:45 +00:00
}
}
2015-06-08 15:50:35 +00:00
2015-08-06 16:41:45 +00:00
// but allow CLI to override
2015-06-08 15:50:35 +00:00
if( options.count("server-rpc-endpoint") )
wdata.ws_server = options.at("server-rpc-endpoint").as<std::string>();
if( options.count("server-rpc-user") )
wdata.ws_user = options.at("server-rpc-user").as<std::string>();
if( options.count("server-rpc-password") )
wdata.ws_password = options.at("server-rpc-password").as<std::string>();
fc::http::websocket_client client;
2015-07-14 21:30:15 +00:00
idump((wdata.ws_server));
2015-06-08 15:50:35 +00:00
auto con = client.connect( wdata.ws_server );
auto apic = std::make_shared<fc::rpc::websocket_api_connection>(*con, GRAPHENE_MAX_NESTED_OBJECTS);
2015-06-08 15:50:35 +00:00
auto remote_api = apic->get_remote_api< login_api >(1);
2015-07-14 21:30:15 +00:00
edump((wdata.ws_user)(wdata.ws_password) );
FC_ASSERT( remote_api->login( wdata.ws_user, wdata.ws_password ), "Failed to log in to API server" );
2015-06-08 15:50:35 +00:00
auto wapiptr = std::make_shared<wallet_api>( wdata, remote_api );
2015-06-08 15:50:35 +00:00
wapiptr->set_wallet_filename( wallet_file.generic_string() );
wapiptr->load_wallet_file();
fc::api<wallet_api> wapi(wapiptr);
auto wallet_cli = std::make_shared<fc::rpc::cli>( GRAPHENE_MAX_NESTED_OBJECTS );
2015-06-08 15:50:35 +00:00
for( auto& name_formatter : wapiptr->get_result_formatters() )
wallet_cli->format_result( name_formatter.first, name_formatter.second );
boost::signals2::scoped_connection closed_connection(con->closed.connect([wallet_cli]{
2015-06-08 15:50:35 +00:00
cerr << "Server has disconnected us.\n";
wallet_cli->stop();
2015-06-25 19:42:22 +00:00
}));
2015-06-08 15:50:35 +00:00
(void)(closed_connection);
if( wapiptr->is_new() )
{
std::cout << "Please use the set_password method to initialize a new wallet before continuing\n";
wallet_cli->set_prompt( "new >>> " );
} else
wallet_cli->set_prompt( "locked >>> " );
2015-06-25 19:42:22 +00:00
boost::signals2::scoped_connection locked_connection(wapiptr->lock_changed.connect([&](bool locked) {
2015-06-08 15:50:35 +00:00
wallet_cli->set_prompt( locked ? "locked >>> " : "unlocked >>> " );
2015-06-25 19:42:22 +00:00
}));
2015-06-08 15:50:35 +00:00
auto _websocket_server = std::make_shared<fc::http::websocket_server>();
if( options.count("rpc-endpoint") )
{
_websocket_server->on_connection([&wapi]( const fc::http::websocket_connection_ptr& c ){
2015-06-08 15:50:35 +00:00
std::cout << "here... \n";
wlog("." );
auto wsc = std::make_shared<fc::rpc::websocket_api_connection>(*c, GRAPHENE_MAX_NESTED_OBJECTS);
2015-06-08 15:50:35 +00:00
wsc->register_api(wapi);
c->set_session_data( wsc );
});
ilog( "Listening for incoming RPC requests on ${p}", ("p", options.at("rpc-endpoint").as<string>() ));
_websocket_server->listen( fc::ip::endpoint::from_string(options.at("rpc-endpoint").as<string>()) );
_websocket_server->start_accept();
}
string cert_pem = "server.pem";
if( options.count( "rpc-tls-certificate" ) )
cert_pem = options.at("rpc-tls-certificate").as<string>();
auto _websocket_tls_server = std::make_shared<fc::http::websocket_tls_server>(cert_pem);
if( options.count("rpc-tls-endpoint") )
{
_websocket_tls_server->on_connection([&]( const fc::http::websocket_connection_ptr& c ){
auto wsc = std::make_shared<fc::rpc::websocket_api_connection>(*c, GRAPHENE_MAX_NESTED_OBJECTS);
2015-06-08 15:50:35 +00:00
wsc->register_api(wapi);
c->set_session_data( wsc );
});
ilog( "Listening for incoming TLS RPC requests on ${p}", ("p", options.at("rpc-tls-endpoint").as<string>() ));
_websocket_tls_server->listen( fc::ip::endpoint::from_string(options.at("rpc-tls-endpoint").as<string>()) );
_websocket_tls_server->start_accept();
}
auto _http_server = std::make_shared<fc::http::server>();
if( options.count("rpc-http-endpoint" ) )
{
ilog( "Listening for incoming HTTP RPC requests on ${p}", ("p", options.at("rpc-http-endpoint").as<string>() ) );
_http_server->listen( fc::ip::endpoint::from_string( options.at( "rpc-http-endpoint" ).as<string>() ) );
//
// due to implementation, on_request() must come AFTER listen()
//
_http_server->on_request(
[&wapi]( const fc::http::request& req, const fc::http::server::response& resp )
{
std::shared_ptr< fc::rpc::http_api_connection > conn =
std::make_shared< fc::rpc::http_api_connection >( GRAPHENE_MAX_NESTED_OBJECTS );
conn->register_api( wapi );
conn->on_request( req, resp );
} );
}
2015-06-08 15:50:35 +00:00
if( !options.count( "daemon" ) )
{
wallet_cli->register_api( wapi );
wallet_cli->start();
wallet_cli->wait();
}
else
{
2019-04-27 20:01:49 +00:00
fc::promise<int>::ptr exit_promise = fc::promise<int>::create("UNIX Signal Handler");
2015-06-08 15:50:35 +00:00
fc::set_signal_handler([&exit_promise](int signal) {
exit_promise->set_value(signal);
}, SIGINT);
ilog( "Entering Daemon Mode, ^C to exit" );
exit_promise->wait();
}
wapi->save_wallet_file(wallet_file.generic_string());
locked_connection.disconnect();
closed_connection.disconnect();
}
catch ( const fc::exception& e )
{
std::cout << e.to_detail_string() << "\n";
return -1;
2015-06-08 15:50:35 +00:00
}
return 0;
2015-06-08 15:50:35 +00:00
}