Send RPC command to bitcoin node to recreate multisig wallet

This commit is contained in:
Srdjan Obucina 2020-01-22 02:43:24 +01:00
parent c25108f228
commit f0a8e8e376
6 changed files with 52 additions and 15 deletions

View file

@ -17,7 +17,7 @@ public:
graphene::peerplays_sidechain::sidechain_type get_sidechain();
std::vector<std::string> get_sidechain_addresses();
virtual void recreate_primary_wallet(const vector<string>& participants) = 0;
virtual string recreate_primary_wallet(const vector<string>& participants) = 0;
protected:
std::shared_ptr<graphene::chain::database> database;

View file

@ -60,7 +60,7 @@ public:
sidechain_net_handler_bitcoin(std::shared_ptr<graphene::chain::database> db, const boost::program_options::variables_map& options);
virtual ~sidechain_net_handler_bitcoin();
void recreate_primary_wallet(const vector<string>& participants);
string recreate_primary_wallet(const vector<string>& participants);
bool connection_is_not_defined() const;

View file

@ -16,7 +16,7 @@ public:
virtual ~sidechain_net_manager();
bool create_handler(peerplays_sidechain::sidechain_type sidechain, const boost::program_options::variables_map& options);
void recreate_primary_wallet(peerplays_sidechain::sidechain_type sidechain, const vector<string>& participants);
string recreate_primary_wallet(peerplays_sidechain::sidechain_type sidechain, const vector<string>& participants);
private:
std::shared_ptr<graphene::chain::database> database;
std::vector<std::unique_ptr<sidechain_net_handler>> net_handlers;

View file

@ -1,5 +1,8 @@
#include <graphene/peerplays_sidechain/peerplays_sidechain_plugin.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <fc/log/logger.hpp>
#include <fc/smart_ref_impl.hpp>
@ -253,21 +256,40 @@ void peerplays_sidechain_plugin_impl::son_event_processing_loop()
void peerplays_sidechain_plugin_impl::recreate_primary_wallet()
{
ilog(__FUNCTION__);
chain::database& d = plugin.database();
ilog(__FUNCTION__);
const auto& idx_swi = d.get_index_type<son_wallet_index>().indices().get<by_id>();
auto obj = idx_swi.rbegin();
if (obj != idx_swi.rend()) {
if (obj->addresses.at(sidechain_type::bitcoin).empty()) {
ilog(__FUNCTION__);
if ((obj->addresses.find(sidechain_type::bitcoin) == obj->addresses.end()) ||
(obj->addresses.at(sidechain_type::bitcoin).empty())) {
ilog(__FUNCTION__);
auto active_sons = d.get_global_properties().active_sons;
vector<string> son_pubkeys_bitcoin;
for ( const son_info& si : active_sons ) {
son_pubkeys_bitcoin.push_back(si.sidechain_public_keys.at(sidechain_type::bitcoin));
}
net_manager->recreate_primary_wallet(sidechain_type::bitcoin, son_pubkeys_bitcoin);
}
ilog(__FUNCTION__);
string reply_str = net_manager->recreate_primary_wallet(sidechain_type::bitcoin, son_pubkeys_bitcoin);
ilog(reply_str);
std::stringstream ss(reply_str);
boost::property_tree::ptree json;
boost::property_tree::read_json( ss, json );
if (json.count( "result" )) {
//if (json.get_child( "result" ).count( "address" )) {
d.modify(*obj, [&, &obj, &json](son_wallet_object &swo) {
ilog(__FUNCTION__);
swo.addresses[sidechain_type::bitcoin] = json.get_child("result").get_value<std::string>();;
});
//}
}
}
}
}

View file

@ -132,23 +132,36 @@ void bitcoin_rpc_client::send_btc_tx( const std::string& tx_hex )
std::string bitcoin_rpc_client::add_multisig_address( const std::vector<std::string> public_keys )
{
std::string body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"addmultisigaddress\", \"method\": \"addmultisigaddress\", \"params\": [");
std::string params = "2, \"[";
std::string params = "2, [";
std::string pubkeys = "";
for (std::string pubkey : public_keys) {
if (!pubkeys.empty()) {
pubkeys = pubkeys + ", ";
pubkeys = pubkeys + ",";
}
pubkeys = pubkeys + std::string("\"") + pubkey + std::string("\"");
}
params = params + pubkeys + std::string("]\"");
params = params + pubkeys + std::string("]");
body = body + params + std::string("] }");
ilog(body);
const auto reply = send_post_request( body );
//const auto reply = send_post_request( body );
if( reply.body.empty() )
return "";
std::string reply_str( reply.body.begin(), reply.body.end() );
std::stringstream ss(reply_str);
boost::property_tree::ptree json;
boost::property_tree::read_json( ss, json );
if( reply.status == 200 ) {
return reply_str;
}
if( json.count( "error" ) && !json.get_child( "error" ).empty() ) {
wlog( "BTC multisig address creation failed! Reply: ${msg}", ("msg", reply_str) );
}
return "";
}
bool bitcoin_rpc_client::connection_is_not_defined() const
@ -237,9 +250,10 @@ sidechain_net_handler_bitcoin::sidechain_net_handler_bitcoin(std::shared_ptr<gra
sidechain_net_handler_bitcoin::~sidechain_net_handler_bitcoin() {
}
void sidechain_net_handler_bitcoin::recreate_primary_wallet( const vector<string>& participants ) {
string sidechain_net_handler_bitcoin::recreate_primary_wallet( const vector<string>& participants ) {
ilog(__FUNCTION__);
string result = create_multisignature_wallet(participants);
return result;
}
bool sidechain_net_handler_bitcoin::connection_is_not_defined() const

View file

@ -34,13 +34,14 @@ bool sidechain_net_manager::create_handler(peerplays_sidechain::sidechain_type s
return ret_val;
}
void sidechain_net_manager::recreate_primary_wallet(peerplays_sidechain::sidechain_type sidechain, const vector<string>& participants) {
string sidechain_net_manager::recreate_primary_wallet(peerplays_sidechain::sidechain_type sidechain, const vector<string>& participants) {
ilog(__FUNCTION__);
for ( size_t i = 0; i < net_handlers.size(); i++ ) {
if (net_handlers.at(i)->get_sidechain() == sidechain) {
net_handlers.at(i)->recreate_primary_wallet(participants);
return net_handlers.at(i)->recreate_primary_wallet(participants);
}
}
return "";
}
} } // graphene::peerplays_sidechain