From deef8b445676d641cdc88c256205f2846df5e524 Mon Sep 17 00:00:00 2001 From: satyakoneru Date: Mon, 17 Feb 2020 13:04:22 +0000 Subject: [PATCH] SON261 - Deposit transfer ( user address -> PW ) and Withdrawal transfer ( PW -> user address ) for m-of-n multisig --- .../sidechain_net_handler.hpp | 1 + .../sidechain_net_handler_bitcoin.hpp | 4 + .../sidechain_net_handler.cpp | 2 + .../sidechain_net_handler_bitcoin.cpp | 121 ++++++++++++++++++ 4 files changed, 128 insertions(+) diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler.hpp index da6321a9..8c2eb87e 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler.hpp @@ -31,6 +31,7 @@ protected: virtual std::string transfer( const std::string& from, const std::string& to, const uint64_t amount ) = 0; virtual std::string sign_transaction( const std::string& transaction ) = 0; virtual std::string send_transaction( const std::string& transaction ) = 0; + virtual std::string transfer_deposit_to_primary_wallet (const sidechain_event_data& sed) = 0; virtual void handle_event( const std::string& event_data ) = 0; diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp index 803b24de..12c73006 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp @@ -21,6 +21,9 @@ public: void send_btc_tx( const std::string& tx_hex ); std::string add_multisig_address( const std::vector public_keys ); bool connection_is_not_defined() const; + std::string create_raw_transaction(const sidechain_event_data& sed, const std::string& pw_address); + std::string sign_raw_transaction_with_wallet(const std::string& tx_hash); + std::string sign_raw_transaction_with_privkey(const std::string& tx_hash, const std::string& private_key); private: @@ -68,6 +71,7 @@ public: std::string transfer( const std::string& from, const std::string& to, const uint64_t amount ); std::string sign_transaction( const std::string& transaction ); std::string send_transaction( const std::string& transaction ); + std::string transfer_deposit_to_primary_wallet (const sidechain_event_data& sed); private: std::string ip; diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler.cpp index ac50974c..d18ddd99 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler.cpp @@ -55,6 +55,8 @@ void sidechain_net_handler::sidechain_event_data_received(const sidechain_event_ const chain::global_property_object& gpo = database.get_global_properties(); + transfer_deposit_to_primary_wallet(sed); + son_wallet_transfer_create_operation op; op.payer = gpo.parameters.get_son_btc_account_id(); op.timestamp = sed.timestamp; diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp index cdd3611e..daad3f6e 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp @@ -110,6 +110,8 @@ void bitcoin_rpc_client::send_btc_tx( const std::string& tx_hex ) const auto body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"send_tx\", \"method\": \"sendrawtransaction\", \"params\": [") + std::string("\"") + tx_hex + std::string("\"") + std::string("] }"); + ilog(body); + const auto reply = send_post_request( body ); if( reply.body.empty() ) @@ -168,6 +170,79 @@ std::string bitcoin_rpc_client::add_multisig_address( const std::vector().indices().get(); + auto obj = idx.rbegin(); + if (obj == idx.rend() || obj->addresses.find(sidechain_type::bitcoin) == obj->addresses.end()) { + return ""; + } + + std::string pw_address_json = obj->addresses.find(sidechain_type::bitcoin)->second; + + std::stringstream ss(pw_address_json); + boost::property_tree::ptree json; + boost::property_tree::read_json( ss, json ); + + std::string pw_address = json.get("address"); + + std::string reply_str = bitcoin_client->create_raw_transaction(sed, pw_address); + ilog(reply_str); + + std::stringstream ss_utx(reply_str); + boost::property_tree::ptree pt; + boost::property_tree::read_json( ss_utx, pt ); + + if( !(pt.count( "error" ) && pt.get_child( "error" ).empty()) || !pt.count("result") ) { + return ""; + } + + std::string unsigned_tx_hex = pt.get("result"); + + reply_str = bitcoin_client->sign_raw_transaction_with_wallet(unsigned_tx_hex); + ilog(reply_str); + std::stringstream ss_stx(reply_str); + boost::property_tree::ptree stx_json; + boost::property_tree::read_json( ss_stx, stx_json ); + + if( !(stx_json.count( "error" ) && stx_json.get_child( "error" ).empty()) || !stx_json.count("result") || !stx_json.get_child("result").count("hex") ) { + return ""; + } + + std::string signed_tx_hex = stx_json.get("result.hex"); + + bitcoin_client->send_btc_tx(signed_tx_hex); + + return reply_str; +} + void sidechain_net_handler_bitcoin::handle_event( const std::string& event_data ) { ilog("peerplays sidechain plugin: sidechain_net_handler_bitcoin::handle_event"); ilog(" event_data: ${event_data}", ("event_data", event_data));