diff --git a/libraries/plugins/peerplays_sidechain/ethereum/transaction.cpp b/libraries/plugins/peerplays_sidechain/ethereum/transaction.cpp index 9bf3bf02..f2d117b5 100644 --- a/libraries/plugins/peerplays_sidechain/ethereum/transaction.cpp +++ b/libraries/plugins/peerplays_sidechain/ethereum/transaction.cpp @@ -1,22 +1,43 @@ #include +#include +#include + namespace graphene { namespace peerplays_sidechain { namespace ethereum { -std::string transaction::sign(std::string private_key) { - v = "signed"; +const transaction& transaction::sign(std::string private_key) const +{ +/* v = "signed"; r = "transaction"; s = "signed-transaction"; - return v + "|" + r + "|" + s; + return v + "|" + r + "|" + s;*/ + return *this; } -std::string transaction::serialize() { - return "serialized-transaction"; +std::string transaction::serialize() const +{ + boost::property_tree::ptree pt; + pt.put("from", from); + pt.put("to", to); + pt.put("data", data); + + std::stringstream ss; + boost::property_tree::json_parser::write_json(ss, pt); + return ss.str(); } -void transaction::deserialize(std::string raw_tx) { - block_hash = "1"; - block_number = "2"; - hash = "3"; +void transaction::deserialize(std::string raw_tx) +{ + std::stringstream ss_tx(raw_tx); + boost::property_tree::ptree tx_json; + boost::property_tree::read_json(ss_tx, tx_json); + + if(tx_json.count("from")) + from = tx_json.get("from"); + if(tx_json.count("to")) + to = tx_json.get("to"); + if(tx_json.count("data")) + data = tx_json.get("data"); } }}} // namespace graphene::peerplays_sidechain::ethereum diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/transaction.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/transaction.hpp index 1ce9497e..7c65b7d2 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/transaction.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/transaction.hpp @@ -8,29 +8,30 @@ namespace graphene { namespace peerplays_sidechain { namespace ethereum { class transaction { public: - std::string block_hash; - std::string block_number; + //std::string block_hash; + //std::string block_number; std::string from; - std::string gas; - std::string gas_price; - std::string max_fee_per_gas; - std::string max_priority_fee_per_gas; - std::string hash; - std::string input; - std::string nonce; + //std::string gas; + //std::string gas_price; + //std::string max_fee_per_gas; + //std::string max_priority_fee_per_gas; + //std::string hash; + //std::string input; + //std::string nonce; std::string to; - std::string transaction_index; - std::string value; - std::string type; - std::vector access_list; - std::string chain_id; - std::string v; - std::string r; - std::string s; + std::string data; + //std::string transaction_index; + //std::string value; + //std::string type; + //std::vector access_list; + //std::string chain_id; + //std::string v; + //std::string r; + //std::string s; - std::string sign(std::string private_key); + const transaction& sign(std::string private_key) const; - std::string serialize(); + std::string serialize() const; void deserialize(std::string raw_tx); }; diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp index 4dc5caff..8606c095 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp @@ -23,7 +23,7 @@ public: std::string get_chain_id(); std::string get_network_id(); - std::string send_transaction(const std::string& wallet_contract_address, const std::string& owner_address, const std::string& data); + std::string send_transaction(const std::string& params); std::string get_transaction_receipt(const std::string& params); //std::string withdraw(); }; diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp index 03778322..1553ef50 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp @@ -54,13 +54,12 @@ std::string ethereum_rpc_client::get_network_id() { return retrieve_value_from_reply(reply_str, "protocols.eth.network"); } -std::string ethereum_rpc_client::send_transaction(const std::string& wallet_contract_address, const std::string& owner_address, const std::string& data) { - const std::string params = "[ { \"from\": \"" + owner_address + "\", \"to\": \"" + wallet_contract_address + "\", \"data\": \"" + data + "\" } ]"; - return send_post_request("eth_sendTransaction", params, debug_rpc_calls); +std::string ethereum_rpc_client::send_transaction(const std::string& params) { + return send_post_request("eth_sendTransaction", "[" + params + "]", debug_rpc_calls); } std::string ethereum_rpc_client::get_transaction_receipt(const std::string& params) { - return send_post_request("eth_getTransactionReceipt", "[ \"" + params + "\"]", debug_rpc_calls); + return send_post_request("eth_getTransactionReceipt", "[\"" + params + "\"]", debug_rpc_calls); } /*std::string ethereum_rpc_client::withdraw() { @@ -542,21 +541,22 @@ std::string sidechain_net_handler_ethereum::send_sidechain_transaction(const sid boost::property_tree::ptree pt; boost::property_tree::ptree pt_array; for(const auto& signature : sto.signatures) { - const std::string sidechain_transaction = rpc_client->send_transaction(wallet_contract_address, signature.second, sto.transaction); + const auto& transaction = signature.second; + const std::string sidechain_transaction = rpc_client->send_transaction(transaction); std::stringstream ss_tx(sidechain_transaction); boost::property_tree::ptree tx_json; boost::property_tree::read_json(ss_tx, tx_json); if( tx_json.count("result") && !tx_json.count("error") ) { boost::property_tree::ptree node; - node.put("son", signature.second); - node.put("result", tx_json.get("result")); + node.put("transaction", transaction); + node.put("transaction_receipt", tx_json.get("result")); pt_array.push_back(std::make_pair("", node)); } else { //! Fixme //! How should we proceed with error in send_transaction - elog("Error in send_transaction for transaction ${id}, signature ${signature}", ("id", sto.id) ("signature", signature.second)); + elog("Error in send_transaction for transaction ${id}, transaction ${transaction}", ("id", sto.id) ("transaction", transaction)); } } pt.add_child("result_array", pt_array); @@ -577,7 +577,7 @@ bool sidechain_net_handler_ethereum::settle_sidechain_transaction(const sidechai size_t count = 0; for(const auto &entry : json.get_child("result_array")) { - const std::string receipt = rpc_client->get_transaction_receipt( entry.second.get("result") ); + const std::string receipt = rpc_client->get_transaction_receipt( entry.second.get("transaction_receipt") ); std::stringstream ss_receipt(receipt); boost::property_tree::ptree json_receipt; @@ -618,8 +618,10 @@ std::string sidechain_net_handler_ethereum::create_primary_wallet_transaction(co } //! Create data of transaction + ethereum::transaction transaction; ethereum::update_owners_encoder encoder; - return encoder.encode(owners_weights, object_id); + transaction.data = encoder.encode(owners_weights, object_id); + return transaction.serialize(); } std::string sidechain_net_handler_ethereum::create_deposit_transaction(const son_wallet_deposit_object &swdo) { @@ -631,11 +633,14 @@ std::string sidechain_net_handler_ethereum::create_withdrawal_transaction(const } std::string sidechain_net_handler_ethereum::sign_transaction(const sidechain_transaction_object &sto) { - //std::string key = get_private_key(plugin.get_current_son_object().sidechain_public_keys.at(sidechain)); - const auto& current_son = plugin.get_current_son_object(); FC_ASSERT(current_son.sidechain_public_keys.contains(sidechain_type::ethereum), "No public keys for current son: ${account_id}", ("account_id", current_son.son_account)); - return "0x" + current_son.sidechain_public_keys.at(sidechain); + + ethereum::transaction sign_transaction; + sign_transaction.deserialize(sto.transaction); + sign_transaction.to = wallet_contract_address; + sign_transaction.from = "0x" + current_son.sidechain_public_keys.at(sidechain); + return sign_transaction.sign(get_private_key(plugin.get_current_son_object().sidechain_public_keys.at(sidechain))).serialize(); } void sidechain_net_handler_ethereum::schedule_ethereum_listener() {