#420 - serialize and deserialize functions update

This commit is contained in:
Vlad Dobromyslov 2022-08-30 08:20:30 +03:00
parent db244887c2
commit c420c38cc9
2 changed files with 47 additions and 35 deletions

View file

@ -99,32 +99,37 @@ signed_transaction raw_transaction::sign(const std::string &private_key) const {
} }
std::string raw_transaction::serialize() const { std::string raw_transaction::serialize() const {
rlp_encoder encoder; const std::string serialized = rlp_encoder::encode(remove_0x(nonce)) +
const std::string serialized = encoder.encode(remove_0x(nonce)) + rlp_encoder::encode(remove_0x(gas_price)) +
encoder.encode(remove_0x(gas_price)) + rlp_encoder::encode(remove_0x(gas_limit)) +
encoder.encode(remove_0x(gas_limit)) + rlp_encoder::encode(remove_0x(to)) +
encoder.encode(remove_0x(to)) + rlp_encoder::encode(remove_0x(value)) +
encoder.encode(remove_0x(value)) + rlp_encoder::encode(remove_0x(data)) +
encoder.encode(remove_0x(data)) + rlp_encoder::encode(remove_0x(chain_id)) +
encoder.encode(remove_0x(chain_id)) + rlp_encoder::encode("") +
encoder.encode("") + rlp_encoder::encode("");
encoder.encode("");
return add_0x(bytes2hex(encoder.encode_length(serialized.size(), 192) + serialized)); return add_0x(bytes2hex(rlp_encoder::encode_length(serialized.size(), 192) + serialized));
} }
void raw_transaction::deserialize(const std::string &raw_tx) { void raw_transaction::deserialize(const std::string &raw_tx) {
rlp_decoder decoder; const auto rlp_array = rlp_decoder::decode(remove_0x(raw_tx));
const auto rlp_array = decoder.decode(remove_0x(raw_tx));
FC_ASSERT(rlp_array.size() >= 7, "Wrong rlp format"); FC_ASSERT(rlp_array.size() >= 7, "Wrong rlp format");
nonce = add_0x(rlp_array.at(0)); nonce = !rlp_array.at(0).empty() ? add_0x(rlp_array.at(0)) : add_0x("0");
boost::algorithm::to_lower(nonce);
gas_price = add_0x(rlp_array.at(1)); gas_price = add_0x(rlp_array.at(1));
boost::algorithm::to_lower(gas_price);
gas_limit = add_0x(rlp_array.at(2)); gas_limit = add_0x(rlp_array.at(2));
boost::algorithm::to_lower(gas_limit);
to = add_0x(rlp_array.at(3)); to = add_0x(rlp_array.at(3));
value = add_0x(rlp_array.at(4)); boost::algorithm::to_lower(to);
data = add_0x(rlp_array.at(5)); value = !rlp_array.at(4).empty() ? add_0x(rlp_array.at(4)) : add_0x("0");
boost::algorithm::to_lower(value);
data = !rlp_array.at(5).empty() ? add_0x(rlp_array.at(5)) : "";
boost::algorithm::to_lower(data);
chain_id = add_0x(rlp_array.at(6)); chain_id = add_0x(rlp_array.at(6));
boost::algorithm::to_lower(chain_id);
} }
//! signed_transaction //! signed_transaction
@ -164,34 +169,41 @@ std::string signed_transaction::recover(const std::string &chain_id) const {
} }
std::string signed_transaction::serialize() const { std::string signed_transaction::serialize() const {
rlp_encoder encoder; const std::string serialized = rlp_encoder::encode(remove_0x(nonce)) +
const std::string serialized = encoder.encode(remove_0x(nonce)) + rlp_encoder::encode(remove_0x(gas_price)) +
encoder.encode(remove_0x(gas_price)) + rlp_encoder::encode(remove_0x(gas_limit)) +
encoder.encode(remove_0x(gas_limit)) + rlp_encoder::encode(remove_0x(to)) +
encoder.encode(remove_0x(to)) + rlp_encoder::encode(remove_0x(value)) +
encoder.encode(remove_0x(value)) + rlp_encoder::encode(remove_0x(data)) +
encoder.encode(remove_0x(data)) + rlp_encoder::encode(remove_0x(v)) +
encoder.encode(remove_0x(v)) + rlp_encoder::encode(remove_0x(r)) +
encoder.encode(remove_0x(r)) + rlp_encoder::encode(remove_0x(s));
encoder.encode(remove_0x(s));
return add_0x(bytes2hex(encoder.encode_length(serialized.size(), 192) + serialized)); return add_0x(bytes2hex(rlp_encoder::encode_length(serialized.size(), 192) + serialized));
} }
void signed_transaction::deserialize(const std::string &raw_tx) { void signed_transaction::deserialize(const std::string &raw_tx) {
rlp_decoder decoder; const auto rlp_array = rlp_decoder::decode(remove_0x(raw_tx));
const auto rlp_array = decoder.decode(remove_0x(raw_tx));
FC_ASSERT(rlp_array.size() >= 9, "Wrong rlp format"); FC_ASSERT(rlp_array.size() >= 9, "Wrong rlp format");
nonce = add_0x(rlp_array.at(0)); nonce = !rlp_array.at(0).empty() ? add_0x(rlp_array.at(0)) : add_0x("0");
boost::algorithm::to_lower(nonce);
gas_price = add_0x(rlp_array.at(1)); gas_price = add_0x(rlp_array.at(1));
boost::algorithm::to_lower(gas_price);
gas_limit = add_0x(rlp_array.at(2)); gas_limit = add_0x(rlp_array.at(2));
boost::algorithm::to_lower(gas_limit);
to = add_0x(rlp_array.at(3)); to = add_0x(rlp_array.at(3));
value = add_0x(rlp_array.at(4)); boost::algorithm::to_lower(to);
data = add_0x(rlp_array.at(5)); value = !rlp_array.at(4).empty() ? add_0x(rlp_array.at(4)) : add_0x("0");
boost::algorithm::to_lower(value);
data = !rlp_array.at(5).empty() ? add_0x(rlp_array.at(5)) : "";
boost::algorithm::to_lower(data);
v = add_0x(rlp_array.at(6)); v = add_0x(rlp_array.at(6));
boost::algorithm::to_lower(v);
r = add_0x(rlp_array.at(7)); r = add_0x(rlp_array.at(7));
boost::algorithm::to_lower(r);
s = add_0x(rlp_array.at(8)); s = add_0x(rlp_array.at(8));
boost::algorithm::to_lower(s);
} }
}}} // namespace graphene::peerplays_sidechain::ethereum }}} // namespace graphene::peerplays_sidechain::ethereum

View file

@ -546,7 +546,7 @@ std::string sidechain_net_handler_ethereum::send_sidechain_transaction(const sid
const auto &transaction = signature.second; const auto &transaction = signature.second;
//! Check if we have this signed transaction, if not, don't send it //! Check if we have this signed transaction, if not, don't send it
if(transaction.empty()) if (transaction.empty())
continue; continue;
#ifdef SEND_RAW_TRANSACTION #ifdef SEND_RAW_TRANSACTION
@ -625,7 +625,7 @@ std::string sidechain_net_handler_ethereum::create_primary_wallet_transaction(co
owners_weights.emplace_back(std::make_pair(pub_key_str, son.weight)); owners_weights.emplace_back(std::make_pair(pub_key_str, son.weight));
} }
ethereum::update_owners_encoder encoder; const ethereum::update_owners_encoder encoder;
return encoder.encode(owners_weights, object_id); return encoder.encode(owners_weights, object_id);
} }
@ -634,7 +634,7 @@ std::string sidechain_net_handler_ethereum::create_deposit_transaction(const son
} }
std::string sidechain_net_handler_ethereum::create_withdrawal_transaction(const son_wallet_withdraw_object &swwo) { std::string sidechain_net_handler_ethereum::create_withdrawal_transaction(const son_wallet_withdraw_object &swwo) {
ethereum::withdrawal_encoder encoder; const ethereum::withdrawal_encoder encoder;
return encoder.encode(swwo.withdraw_address.substr(2), swwo.withdraw_amount.value * 10000000000, swwo.id.operator std::string()); return encoder.encode(swwo.withdraw_address.substr(2), swwo.withdraw_amount.value * 10000000000, swwo.id.operator std::string());
} }