From f6614ab122a451da8368121d51968072aa16f58c Mon Sep 17 00:00:00 2001 From: Vlad Dobromyslov Date: Thu, 4 Aug 2022 10:27:09 +0300 Subject: [PATCH] base_encoder + update_owners_encoder --- .../peerplays_sidechain/ethereum/encoders.cpp | 40 ++++++++++++++++ .../peerplays_sidechain/ethereum/encoders.hpp | 18 ++++++- .../sidechain_net_handler_ethereum.hpp | 1 - .../sidechain_net_handler_ethereum.cpp | 48 ++----------------- 4 files changed, 59 insertions(+), 48 deletions(-) diff --git a/libraries/plugins/peerplays_sidechain/ethereum/encoders.cpp b/libraries/plugins/peerplays_sidechain/ethereum/encoders.cpp index 4359a605..2b8984f6 100644 --- a/libraries/plugins/peerplays_sidechain/ethereum/encoders.cpp +++ b/libraries/plugins/peerplays_sidechain/ethereum/encoders.cpp @@ -1,5 +1,45 @@ #include +#include +#include + namespace graphene { namespace peerplays_sidechain { namespace ethereum { +//! base_encoder +std::string base_encoder::encode_uint256(uint64_t value) +{ + return (boost::format("%x") % boost::io::group(std::setw(64), std::setfill('0'), value)).str(); +} + +std::string base_encoder::encode_address(const std::string& value) +{ + return (boost::format("%x") % boost::io::group(std::setw(64), std::setfill('0'), value)).str(); +} + +std::string base_encoder::encode_string(const std::string& value) +{ + std::string data = (boost::format("%x") % boost::io::group(std::setw(64), std::setfill('0'), value.size())).str(); + data += boost::algorithm::hex(value) + std::string( (64 - value.size() * 2 % 64), '0' ); + return data; +} + + +//! update_owners_encoder +std::string update_owners_encoder::encode(const std::vector>& owners_weights, const std::string& object_id) const +{ + std::string data = "0x" + function_signature; + data += base_encoder::encode_uint256(64); + data += base_encoder::encode_uint256((owners_weights.size() * 2 + 3) * 32); + data += base_encoder::encode_uint256(owners_weights.size()); + for(const auto& owner : owners_weights) + { + data += base_encoder::encode_address(owner.first); + data += base_encoder::encode_uint256(owner.second); + } + data += base_encoder::encode_string(object_id); + + return data; +} + + }}} // namespace graphene::peerplays_sidechain::ethereum diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/encoders.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/encoders.hpp index 43fc38dc..443720c1 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/encoders.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/encoders.hpp @@ -5,7 +5,21 @@ namespace graphene { namespace peerplays_sidechain { namespace ethereum { -class ethereum_function_call_encoder { +class base_encoder { +public: + static std::string encode_uint256(uint64_t value); + static std::string encode_address(const std::string& value); + static std::string encode_string(const std::string& value); +}; + +class update_owners_encoder { +public: + const std::string function_signature = "23ab6adf"; //! updateOwners((address,uint256)[],string) + + std::string encode(const std::vector>& owners_weights, const std::string& object_id) const; +}; + +/*class ethereum_function_call_encoder { public: enum operation_t { OPERATION_CALL, @@ -34,6 +48,6 @@ public: private: ethereum_function_call_encoder m_ethereum_function_call_encoder; -}; +};*/ }}} // namespace graphene::peerplays_sidechain::ethereum 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 1af1ab10..4dc5caff 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 @@ -25,7 +25,6 @@ public: std::string send_transaction(const std::string& wallet_contract_address, const std::string& owner_address, const std::string& data); std::string get_transaction_receipt(const std::string& params); - //std::string update_owners(const std::string& wallet_contract_address, const std::string& owner_address, const std::vector>& owners_weights, const std::string& object_id); //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 10531317..03778322 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp @@ -3,9 +3,7 @@ #include #include -#include #include -#include #include #include @@ -18,8 +16,8 @@ #include #include #include +#include #include -#include namespace graphene { namespace peerplays_sidechain { @@ -65,29 +63,6 @@ std::string ethereum_rpc_client::get_transaction_receipt(const std::string& para return send_post_request("eth_getTransactionReceipt", "[ \"" + params + "\"]", debug_rpc_calls); } -/*std::string ethereum_rpc_client::update_owners(const std::string& wallet_contract_address, const std::string& owner_address, const std::vector>& owners_weights, const std::string& object_id) { - const std::string function_hash = "23ab6adf"; //! updateOwners((address,uint256)[],string) - - const std::string data = [&function_hash, &owners_weights, &object_id] { - std::string data = "0x" + function_hash; - data += (boost::format("%x") % boost::io::group(std::setw(64), std::setfill('0'), 64)).str(); - data += (boost::format("%x") % boost::io::group(std::setw(64), std::setfill('0'), ((owners_weights.size() * 2 + 3) * 32))).str(); - data += (boost::format("%x") % boost::io::group(std::setw(64), std::setfill('0'), owners_weights.size())).str(); - for(const auto& owner : owners_weights) - { - data += (boost::format("%x") % boost::io::group(std::setw(64), std::setfill('0'), owner.first)).str(); - data += (boost::format("%x") % boost::io::group(std::setw(64), std::setfill('0'), owner.second)).str(); - } - data += (boost::format("%x") % boost::io::group(std::setw(64), std::setfill('0'), object_id.size())).str(); - data += boost::algorithm::hex(object_id) + std::string( (64 - object_id.size() * 2 % 64), '0' ); - - return 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::withdraw() { const std::string params = "[ { \"from\": \"0x5FbBb31BE52608D2F52247E8400B7fCaA9E0bC12\", \"to\": \"0x3E84f248Cd00A2FDaaDfa0dC5c3ff64D8767Fb01\", \"data\": \"0xe088747b00000000000000000000000009ee460834498a4ee361beb819470061b7381b490000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000006312e33362e330000000000000000000000000000000000000000000000000000\" } ]"; return send_post_request("eth_sendTransaction", params, debug_rpc_calls); @@ -643,25 +618,8 @@ std::string sidechain_net_handler_ethereum::create_primary_wallet_transaction(co } //! Create data of transaction - const std::string function_hash = "23ab6adf"; //! updateOwners((address,uint256)[],string) - - const std::string data = [&function_hash, &owners_weights, &object_id] { - std::string data = "0x" + function_hash; - data += (boost::format("%x") % boost::io::group(std::setw(64), std::setfill('0'), 64)).str(); - data += (boost::format("%x") % boost::io::group(std::setw(64), std::setfill('0'), ((owners_weights.size() * 2 + 3) * 32))).str(); - data += (boost::format("%x") % boost::io::group(std::setw(64), std::setfill('0'), owners_weights.size())).str(); - for(const auto& owner : owners_weights) - { - data += (boost::format("%x") % boost::io::group(std::setw(64), std::setfill('0'), owner.first)).str(); - data += (boost::format("%x") % boost::io::group(std::setw(64), std::setfill('0'), owner.second)).str(); - } - data += (boost::format("%x") % boost::io::group(std::setw(64), std::setfill('0'), object_id.size())).str(); - data += boost::algorithm::hex(object_id) + std::string( (64 - object_id.size() * 2 % 64), '0' ); - - return data; - }(); - - return data; + ethereum::update_owners_encoder encoder; + return encoder.encode(owners_weights, object_id); } std::string sidechain_net_handler_ethereum::create_deposit_transaction(const son_wallet_deposit_object &swdo) {