remove hardcodes, implementation build_transaction safe_transaction_encoder

This commit is contained in:
Pavel Baykov 2022-06-15 09:16:55 -03:00
parent 647a5369fc
commit 6e213fcfad
2 changed files with 1139 additions and 1097 deletions

View file

@ -21,194 +21,217 @@
namespace graphene { namespace peerplays_sidechain { namespace graphene { namespace peerplays_sidechain {
typedef websocketpp::client<websocketpp::config::asio_client> client; typedef websocketpp::client<websocketpp::config::asio_client> client;
using websocketpp::lib::placeholders::_1; using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2; using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind; using websocketpp::lib::bind;
// pull out the type of messages sent by our config // pull out the type of messages sent by our config
typedef websocketpp::config::asio_tls_client::message_type::ptr message_ptr; typedef websocketpp::config::asio_tls_client::message_type::ptr message_ptr;
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr; typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
typedef client::connection_ptr connection_ptr; typedef client::connection_ptr connection_ptr;
class eth_rpc_client { class eth_rpc_client {
public: public:
typedef eth_rpc_client type; typedef eth_rpc_client type;
enum req_t { enum req_t {
ETH_CHAIN_ID, ETH_CHAIN_ID,
ETH_GET_TRANSACTION_RECEIPT, ETH_GET_TRANSACTION_RECEIPT,
ETH_CALL, ETH_CALL,
ETH_SEND_TRANSACTION, ETH_SEND_TRANSACTION,
ETH_SEND_RAW_TRANSACTION, ETH_SEND_RAW_TRANSACTION,
ETH_GET_CODE, ETH_GET_CODE,
ETH_GET_BALANCE, ETH_GET_BALANCE,
ETH_SIGN, ETH_SIGN,
ETH_COINBASE, ETH_COINBASE,
GET_LIST_OWNERS, GET_LIST_OWNERS,
ADD_OWNER, ADD_OWNER,
REMOVE_OWNER REMOVE_OWNER
}; };
enum class multi_type { enum class multi_type {
script, script,
address address
}; };
struct multi_params { struct multi_params {
multi_params(multi_type _type, const std::string &_address_or_script, const std::string &_label = "") : multi_params(multi_type _type, const std::string &_address_or_script, const std::string &_label = "") :
type{_type}, type{_type},
address_or_script{_address_or_script}, address_or_script{_address_or_script},
label{_label} { label{_label} {
} }
multi_type type; multi_type type;
std::string address_or_script; std::string address_or_script;
std::string label; std::string label;
}; };
eth_rpc_client(const std::string &url, const std::string &user_name, const std::string &password, bool debug_rpc_calls); eth_rpc_client(const std::string &url, const std::string &user_name, const std::string &password, bool debug_rpc_calls);
void start(); void start();
void stop(); void stop();
void on_socket_init(websocketpp::connection_hdl); void on_socket_init(websocketpp::connection_hdl);
void on_fail(websocketpp::connection_hdl hdl); void on_fail(websocketpp::connection_hdl hdl);
void on_open(websocketpp::connection_hdl hdl); void on_open(websocketpp::connection_hdl hdl);
void on_message(websocketpp::connection_hdl hdl, message_ptr msg); void on_message(websocketpp::connection_hdl hdl, message_ptr msg);
void on_close(websocketpp::connection_hdl); void on_close(websocketpp::connection_hdl);
uint64_t get_chain_id(); uint64_t get_chain_id();
uint64_t eth_getTransactionReceipt(const std::string& tx_id); uint64_t eth_getTransactionReceipt(const std::string& tx_id);
uint64_t eth_call(const std::string& to, const std::string& data); uint64_t eth_call(const std::string& to, const std::string& data);
uint64_t eth_sendTransaction(const std::string& from, const std::string& to, const std::string& data); uint64_t eth_sendTransaction(const std::string& from, const std::string& to, const std::string& data);
uint64_t eth_sendRawTransaction(const std::string& params); uint64_t eth_sendRawTransaction(const std::string& params);
uint64_t eth_getCode(const std::string& addr); uint64_t eth_getCode(const std::string& addr);
uint64_t eth_getBalance(const std::string& addr); uint64_t eth_getBalance(const std::string& addr);
uint64_t eth_sign(const string& addr, const string& message); uint64_t eth_sign(const string& addr, const string& message);
uint64_t eth_coinbase(); uint64_t eth_coinbase();
uint64_t get_list_owners(const std::string& safe_account); uint64_t get_list_owners(const std::string& safe_account);
uint64_t add_owner(const std::string& addr ); uint64_t add_owner(const std::string& addr );
uint64_t remove_owner(const std::string& addr, uint32_t threshold); uint64_t remove_owner(const std::string& addr, uint32_t threshold);
std::string addmultisigaddress(const uint32_t nrequired, const std::vector<std::string> public_keys); std::string addmultisigaddress(const uint32_t nrequired, const std::vector<std::string> public_keys);
std::string combinepsbt(const vector<std::string> &psbts); std::string combinepsbt(const vector<std::string> &psbts);
uint64_t createmultisig(const uint32_t nrequired, const std::vector<std::string> owner_addresses, const std::string &private_key); uint64_t createmultisig(const uint32_t nrequired, const std::vector<std::string> owner_addresses, const std::string &private_key);
std::string createpsbt(); std::string createpsbt();
std::string createrawtransaction(); std::string createrawtransaction();
std::string createwallet(const std::string &wallet_name); std::string createwallet(const std::string &wallet_name);
std::string decodepsbt(std::string const &tx_psbt); std::string decodepsbt(std::string const &tx_psbt);
std::string decoderawtransaction(std::string const &tx_hex); std::string decoderawtransaction(std::string const &tx_hex);
std::string encryptwallet(const std::string &passphrase); std::string encryptwallet(const std::string &passphrase);
uint64_t estimatesmartfee(uint16_t conf_target = 128); uint64_t estimatesmartfee(uint16_t conf_target = 128);
std::string finalizepsbt(std::string const &tx_psbt); std::string finalizepsbt(std::string const &tx_psbt);
std::string getaddressinfo(const std::string &address); std::string getaddressinfo(const std::string &address);
std::string getblock(const std::string &block_hash, int32_t verbosity = 2); std::string getblock(const std::string &block_hash, int32_t verbosity = 2);
std::string getrawtransaction(const std::string &txid, const bool verbose = false); std::string getrawtransaction(const std::string &txid, const bool verbose = false);
std::string getnetworkinfo(); std::string getnetworkinfo();
std::string gettransaction(const std::string &txid, const bool include_watch_only = false); std::string gettransaction(const std::string &txid, const bool include_watch_only = false);
std::string getblockchaininfo(); std::string getblockchaininfo();
void importaddress(const std::string &address_or_script, const std::string &label = "", const bool rescan = true, const bool p2sh = false); void importaddress(const std::string &address_or_script, const std::string &label = "", const bool rescan = true, const bool p2sh = false);
void importmulti(const std::vector<multi_params> &address_or_script_array, const bool rescan = true); void importmulti(const std::vector<multi_params> &address_or_script_array, const bool rescan = true);
std::string loadwallet(const std::string &filename); std::string loadwallet(const std::string &filename);
std::string sendrawtransaction(const std::string &tx_hex); std::string sendrawtransaction(const std::string &tx_hex);
std::string signrawtransactionwithwallet(const std::string &tx_hash); std::string signrawtransactionwithwallet(const std::string &tx_hash);
std::string unloadwallet(const std::string &filename); std::string unloadwallet(const std::string &filename);
std::string walletlock(); std::string walletlock();
std::string walletprocesspsbt(std::string const &tx_psbt); std::string walletprocesspsbt(std::string const &tx_psbt);
bool walletpassphrase(const std::string &passphrase, uint32_t timeout = 60); bool walletpassphrase(const std::string &passphrase, uint32_t timeout = 60);
std::string chain_id;//256 bit value std::string chain_id;//256 bit value
std::vector<std::string> owners; std::vector<std::string> owners;
std::string safe_account_addr; std::string safe_account_addr;
private: private:
class ethereum_function_call_encoder { class ethereum_function_call_encoder {
public: public:
std::string encode_function_signature(const std::string& function_signature); enum operation_t {
std::string encode_address(const std::string& addr); OPERATION_CALL,
std::string encode_uint256(const std::string& value); OPERATION_DELEGATE_CALL
std::string encode_uint8(uint8_t value); };
std::string encode_bytes(const std::string& values);
};
ethereum_function_call_encoder m_ethereum_function_call_encoder; static constexpr const char*const default_prev_addr = "0000000000000000000000000000000000000001";
std::shared_ptr<fc::thread> _thread; std::string encode_function_signature(const std::string& function_signature);
std::string signature; std::string encode_address(const std::string& addr);
std::string geth_url; std::string encode_uint256(const std::string& value);
uint64_t t_id; std::string encode_uint8(uint8_t value);
std::string account_address; std::string encode_bytes(const std::string& values);
std::string transaction_id; };
uint32_t threshold;
std::unordered_map<uint64_t, req_t> m_requests;
std::string balance;
std::string code;
std::string ip; class safe_transaction_encoder {
uint32_t rpc_port; public:
std::string user; static constexpr const char*const default_safe_tx_gas = "0";
std::string password; static constexpr const char*const default_data_gas = "0";
std::string wallet; static constexpr const char*const default_gas_price = "0";
std::string wallet_password; static constexpr const char*const default_gas_token = "0000000000000000000000000000000000000000";
bool debug_rpc_calls; static constexpr const char*const default_refund_receiver = "0000000000000000000000000000000000000000";
fc::http::header authorization; std::string create_safe_address(const std::vector<std::string> owner_addresses, uint32_t threshold);
std::string build_transaction(const std::string& safe_account_addr, const std::string& value, const std::string& data, uint8_t operation, const std::string& safeTxGas, const std::string& dataGas, const std::string& gasPrice, const std::string& gasToken, const std::string& refundReceiver);
client m_endpoint; private:
websocketpp::connection_hdl m_hdl; ethereum_function_call_encoder m_ethereum_function_call_encoder;
}; };
// ============================================================================= ethereum_function_call_encoder m_ethereum_function_call_encoder;
safe_transaction_encoder m_safe_transaction_encoder;
class sidechain_net_handler_eth : public sidechain_net_handler { std::shared_ptr<fc::thread> _thread;
public: std::string signature;
sidechain_net_handler_eth(peerplays_sidechain_plugin &_plugin, const boost::program_options::variables_map &options); std::string geth_url;
virtual ~sidechain_net_handler_eth(); uint64_t t_id;
std::string account_address;
std::string transaction_id;
uint32_t threshold;
std::unordered_map<uint64_t, req_t> m_requests;
std::string balance;
std::string code;
bool process_proposal(const proposal_object &po); std::string ip;
void process_primary_wallet(); uint32_t rpc_port;
void process_sidechain_addresses(); std::string user;
bool process_deposit(const son_wallet_deposit_object &swdo); std::string password;
bool process_withdrawal(const son_wallet_withdraw_object &swwo); std::string wallet;
std::string process_sidechain_transaction(const sidechain_transaction_object &sto); std::string wallet_password;
std::string send_sidechain_transaction(const sidechain_transaction_object &sto); bool debug_rpc_calls;
bool settle_sidechain_transaction(const sidechain_transaction_object &sto, asset &settle_amount);
private: fc::http::header authorization;
std::string url;
uint32_t rpc_port;
std::string rpc_user; client m_endpoint;
std::string rpc_password; websocketpp::connection_hdl m_hdl;
std::string wallet; };
std::string wallet_password;
std::unique_ptr<eth_rpc_client> eth_client; // =============================================================================
fc::future<void> on_changed_objects_task; class sidechain_net_handler_eth : public sidechain_net_handler {
public:
sidechain_net_handler_eth(peerplays_sidechain_plugin &_plugin, const boost::program_options::variables_map &options);
virtual ~sidechain_net_handler_eth();
std::mutex event_handler_mutex; bool process_proposal(const proposal_object &po);
typedef std::lock_guard<decltype(event_handler_mutex)> scoped_lock; void process_primary_wallet();
void process_sidechain_addresses();
bool process_deposit(const son_wallet_deposit_object &swdo);
bool process_withdrawal(const son_wallet_withdraw_object &swwo);
std::string process_sidechain_transaction(const sidechain_transaction_object &sto);
std::string send_sidechain_transaction(const sidechain_transaction_object &sto);
bool settle_sidechain_transaction(const sidechain_transaction_object &sto, asset &settle_amount);
std::string create_primary_wallet_address(const std::vector<son_info> &son_pubkeys); private:
std::string url;
uint32_t rpc_port;
std::string create_primary_wallet_transaction(const son_wallet_object &prev_swo, std::string new_sw_address); std::string rpc_user;
std::string create_deposit_transaction(const son_wallet_deposit_object &swdo); std::string rpc_password;
std::string create_withdrawal_transaction(const son_wallet_withdraw_object &swwo); std::string wallet;
std::string wallet_password;
std::string create_transaction(); std::unique_ptr<eth_rpc_client> eth_client;
std::string sign_transaction(const sidechain_transaction_object &sto);
std::string send_transaction(const sidechain_transaction_object &sto);
void handle_event(const std::string &event_data); fc::future<void> on_changed_objects_task;
std::vector<info_for_vin> extract_info_from_block(const std::string &_block);
void on_changed_objects(const vector<object_id_type> &ids, const flat_set<account_id_type> &accounts);
void on_changed_objects_cb(const vector<object_id_type> &ids, const flat_set<account_id_type> &accounts);
std::vector<char> parse_hex(const std::string &str); std::mutex event_handler_mutex;
fc::ecc::public_key_data create_public_key_data(const std::vector<char> &public_key); typedef std::lock_guard<decltype(event_handler_mutex)> scoped_lock;
};
std::string create_primary_wallet_address(const std::vector<son_info> &son_pubkeys);
std::string create_primary_wallet_transaction(const son_wallet_object &prev_swo, std::string new_sw_address);
std::string create_deposit_transaction(const son_wallet_deposit_object &swdo);
std::string create_withdrawal_transaction(const son_wallet_withdraw_object &swwo);
std::string create_transaction();
std::string sign_transaction(const sidechain_transaction_object &sto);
std::string send_transaction(const sidechain_transaction_object &sto);
void handle_event(const std::string &event_data);
std::vector<info_for_vin> extract_info_from_block(const std::string &_block);
void on_changed_objects(const vector<object_id_type> &ids, const flat_set<account_id_type> &accounts);
void on_changed_objects_cb(const vector<object_id_type> &ids, const flat_set<account_id_type> &accounts);
std::vector<char> parse_hex(const std::string &str);
fc::ecc::public_key_data create_public_key_data(const std::vector<char> &public_key);
};
}} // namespace graphene::peerplays_sidechain }} // namespace graphene::peerplays_sidechain