From c0bf948003f7585d1dcdb57653211d591ac9e53f Mon Sep 17 00:00:00 2001 From: mtyszczak Date: Mon, 27 Sep 2021 11:15:02 +0200 Subject: [PATCH 1/2] Add merge function to the wallet_data --- .../wallet/include/hive/wallet/wallet.hpp | 5 ++- libraries/wallet/wallet.cpp | 33 +++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/libraries/wallet/include/hive/wallet/wallet.hpp b/libraries/wallet/include/hive/wallet/wallet.hpp index 5596a21d5..15722c890 100644 --- a/libraries/wallet/include/hive/wallet/wallet.hpp +++ b/libraries/wallet/include/hive/wallet/wallet.hpp @@ -65,9 +65,12 @@ struct wallet_data { vector cipher_keys; /** encrypted keys */ - string ws_server = "ws://localhost:8090"; + string ws_server; string ws_user; string ws_password; + + /// @brief Merges this wallet_data with the provided other wallet_data (other overrides data on conflict) + void merge( const wallet_data& other ); }; enum authority_type diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 44a4e4dd9..6931b9962 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -67,6 +67,35 @@ constexpr bool LOCK = false; //DECLARE_API addes lock argument to all wallet_br namespace hive { namespace wallet { +namespace detail { + struct wallet_data_merger + { + private: + wallet_data& lhs; // Old + const wallet_data& rhs; // New + + public: + wallet_data_merger( wallet_data& lhs, const wallet_data& rhs ) + : lhs( lhs ), rhs( rhs ) + {} + + /// @note requires the Member type to be default value initialized and to have != operator implemented + template + void operator()( const char* name )const + { + // ilog( "Wallet data merger[\"${name}\"]: lhs(${lhs}), rhs(${rhs})", ("name",name)("lhs",lhs.*member)("rhs",rhs.*member) ); + // If rhs member value is not default initialized then apply its value to the lhs + if( rhs.*member != Member{} ) + lhs.*member = rhs.*member; + } + }; +} + +void wallet_data::merge( const wallet_data& other ) +{ + fc::reflector< wallet_data >::visit( detail::wallet_data_merger{ *this, other } ); +} + namespace detail { template @@ -412,15 +441,13 @@ public: bool load_wallet_file(string wallet_filename = "") { - // TODO: Merge imported wallet with existing wallet, - // instead of replacing it if( wallet_filename == "" ) wallet_filename = _wallet_filename; if( ! fc::exists( wallet_filename ) ) return false; - _wallet = fc::json::from_file( wallet_filename ).as< wallet_data >(); + _wallet.merge( fc::json::from_file( wallet_filename ).as< wallet_data >() ); return true; } -- GitLab From 68c61dc1a689ed6474ffdc0f26fa36de7ddc983d Mon Sep 17 00:00:00 2001 From: mtyszczak Date: Mon, 27 Sep 2021 12:25:44 +0200 Subject: [PATCH 2/2] Fix merging of program options in cli_wallet --- libraries/wallet/wallet.cpp | 5 +---- programs/cli_wallet/main.cpp | 10 +--------- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 6931b9962..f60a8d17b 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -255,12 +255,9 @@ class wallet_api_impl public: wallet_api& self; wallet_api_impl( wallet_api& s, const wallet_data& initial_data, const chain_id_type& hive_chain_id, const fc::api< hive::plugins::wallet_bridge_api::wallet_bridge_api >& remote_api ) - : self( s ), _remote_wallet_bridge_api(remote_api) + : self( s ), _wallet( initial_data ), _hive_chain_id( hive_chain_id ), _remote_wallet_bridge_api(remote_api) { init_prototype_ops(); - - _wallet.ws_server = initial_data.ws_server; - _hive_chain_id = hive_chain_id; } virtual ~wallet_api_impl() diff --git a/programs/cli_wallet/main.cpp b/programs/cli_wallet/main.cpp index 0c4bbe015..6464f56e3 100644 --- a/programs/cli_wallet/main.cpp +++ b/programs/cli_wallet/main.cpp @@ -143,19 +143,12 @@ int main( int argc, char** argv ) cfg.loggers.back().level = fc::log_level::debug; cfg.loggers.back().appenders = {"rpc"}; - - // - // 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() : "wallet.json"); if( fc::exists( wallet_file ) ) { - wdata = fc::json::from_file( wallet_file ).as(); + wdata.merge( fc::json::from_file( wallet_file ).as() ); } else { @@ -194,7 +187,6 @@ int main( int argc, char** argv ) auto remote_api = apic->get_remote_api< hive::plugins::wallet_bridge_api::wallet_bridge_api >(0, "wallet_bridge_api"); auto wapiptr = std::make_shared( wdata, _hive_chain_id, remote_api ); wapiptr->set_wallet_filename( wallet_file.generic_string() ); - wapiptr->load_wallet_file(); fc::api wapi(wapiptr); -- GitLab