Proper account_update_operation serialization and transaction signing
This commit is contained in:
parent
44f81d4d08
commit
a2c5c29005
7 changed files with 168 additions and 14 deletions
|
|
@ -15,6 +15,7 @@ add_library( peerplays_sidechain
|
||||||
bitcoin/utils.cpp
|
bitcoin/utils.cpp
|
||||||
bitcoin/sign_bitcoin_transaction.cpp
|
bitcoin/sign_bitcoin_transaction.cpp
|
||||||
common/rpc_client.cpp
|
common/rpc_client.cpp
|
||||||
|
hive/operations.cpp
|
||||||
hive/transaction.cpp
|
hive/transaction.cpp
|
||||||
hive/types.cpp
|
hive/types.cpp
|
||||||
)
|
)
|
||||||
|
|
|
||||||
101
libraries/plugins/peerplays_sidechain/hive/operations.cpp
Normal file
101
libraries/plugins/peerplays_sidechain/hive/operations.cpp
Normal file
|
|
@ -0,0 +1,101 @@
|
||||||
|
#include <graphene/peerplays_sidechain/hive/operations.hpp>
|
||||||
|
|
||||||
|
#include <fc/io/raw.hpp>
|
||||||
|
|
||||||
|
namespace graphene { namespace peerplays_sidechain { namespace hive {
|
||||||
|
|
||||||
|
}}} // namespace graphene::peerplays_sidechain::hive
|
||||||
|
|
||||||
|
namespace fc {
|
||||||
|
|
||||||
|
static std::string trim_typename_namespace(const std::string &name) {
|
||||||
|
auto start = name.find_last_of(':');
|
||||||
|
start = (start == std::string::npos) ? 0 : start + 1;
|
||||||
|
return name.substr(start);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct from_static_variant_for_hive {
|
||||||
|
variant &var;
|
||||||
|
from_static_variant_for_hive(variant &dv) :
|
||||||
|
var(dv) {
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef void result_type;
|
||||||
|
template <typename T>
|
||||||
|
void operator()(const T &v) const {
|
||||||
|
auto name = trim_typename_namespace(fc::get_typename<T>::name());
|
||||||
|
variant value;
|
||||||
|
to_variant(v, value, 5);
|
||||||
|
var = mutable_variant_object("type", name).set("value", value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct to_static_variant_for_hive {
|
||||||
|
const variant &var;
|
||||||
|
to_static_variant_for_hive(const variant &dv) :
|
||||||
|
var(dv) {
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef void result_type;
|
||||||
|
template <typename T>
|
||||||
|
void operator()(T &v) const {
|
||||||
|
from_variant(var, v, 5);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct get_static_variant_name {
|
||||||
|
string &name;
|
||||||
|
get_static_variant_name(string &n) :
|
||||||
|
name(n) {
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef void result_type;
|
||||||
|
template <typename T>
|
||||||
|
void operator()(T &v) const {
|
||||||
|
name = trim_typename_namespace(fc::get_typename<T>::name());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void to_variant(const graphene::peerplays_sidechain::hive::hive_operation &var, fc::variant &vo, uint32_t max_depth) {
|
||||||
|
var.visit(from_static_variant_for_hive(vo));
|
||||||
|
}
|
||||||
|
|
||||||
|
void from_variant(const fc::variant &var, graphene::peerplays_sidechain::hive::hive_operation &vo, uint32_t max_depth) {
|
||||||
|
static std::map<string, int64_t> to_tag = []() {
|
||||||
|
std::map<string, int64_t> name_map;
|
||||||
|
for (int i = 0; i < graphene::peerplays_sidechain::hive::hive_operation::count(); ++i) {
|
||||||
|
graphene::peerplays_sidechain::hive::hive_operation tmp;
|
||||||
|
tmp.set_which(i);
|
||||||
|
string n;
|
||||||
|
tmp.visit(get_static_variant_name(n));
|
||||||
|
name_map[n] = i;
|
||||||
|
}
|
||||||
|
return name_map;
|
||||||
|
}();
|
||||||
|
|
||||||
|
auto ar = var.get_array();
|
||||||
|
if (ar.size() < 2)
|
||||||
|
return;
|
||||||
|
auto var_second = ar[1];
|
||||||
|
|
||||||
|
FC_ASSERT(var_second.is_object(), "Input data have to treated as object.");
|
||||||
|
auto v_object = var_second.get_object();
|
||||||
|
|
||||||
|
FC_ASSERT(v_object.contains("type"), "Type field doesn't exist.");
|
||||||
|
FC_ASSERT(v_object.contains("value"), "Value field doesn't exist.");
|
||||||
|
|
||||||
|
int64_t which = -1;
|
||||||
|
|
||||||
|
if (v_object["type"].is_integer()) {
|
||||||
|
which = v_object["type"].as_int64();
|
||||||
|
} else {
|
||||||
|
auto itr = to_tag.find(v_object["type"].as_string());
|
||||||
|
FC_ASSERT(itr != to_tag.end(), "Invalid object name: ${n}", ("n", v_object["type"]));
|
||||||
|
which = itr->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
vo.set_which(which);
|
||||||
|
vo.visit(fc::to_static_variant_for_hive(v_object["value"]));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace fc
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <fc/container/flat_fwd.hpp>
|
#include <fc/container/flat_fwd.hpp>
|
||||||
|
|
||||||
|
|
@ -21,8 +20,8 @@ struct authority {
|
||||||
};
|
};
|
||||||
|
|
||||||
uint32_t weight_threshold = 0;
|
uint32_t weight_threshold = 0;
|
||||||
fc::flat_map<account_name_type, uint16_t> account_auths;
|
fc::flat_map<hive::account_name_type, uint16_t> account_auths;
|
||||||
fc::flat_map<public_key_type, uint16_t> key_auths;
|
fc::flat_map<hive::public_key_type, uint16_t> key_auths;
|
||||||
};
|
};
|
||||||
|
|
||||||
}}} // namespace graphene::peerplays_sidechain::hive
|
}}} // namespace graphene::peerplays_sidechain::hive
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,28 @@
|
||||||
|
|
||||||
namespace graphene { namespace peerplays_sidechain { namespace hive {
|
namespace graphene { namespace peerplays_sidechain { namespace hive {
|
||||||
|
|
||||||
|
struct vote_operation {};
|
||||||
|
|
||||||
|
struct comment_operation {};
|
||||||
|
|
||||||
|
struct transfer_operation {};
|
||||||
|
|
||||||
|
struct transfer_to_vesting_operation {};
|
||||||
|
|
||||||
|
struct withdraw_vesting_operation {};
|
||||||
|
|
||||||
|
struct limit_order_create_operation {};
|
||||||
|
|
||||||
|
struct limit_order_cancel_operation {};
|
||||||
|
|
||||||
|
struct feed_publish_operation {};
|
||||||
|
|
||||||
|
struct convert_operation {};
|
||||||
|
|
||||||
|
struct account_create_operation {};
|
||||||
|
|
||||||
struct account_update_operation {
|
struct account_update_operation {
|
||||||
std::string account;
|
hive::account_name_type account;
|
||||||
fc::optional<authority> owner;
|
fc::optional<authority> owner;
|
||||||
fc::optional<authority> active;
|
fc::optional<authority> active;
|
||||||
fc::optional<authority> posting;
|
fc::optional<authority> posting;
|
||||||
|
|
@ -22,5 +42,15 @@ struct account_update_operation {
|
||||||
|
|
||||||
}}} // namespace graphene::peerplays_sidechain::hive
|
}}} // namespace graphene::peerplays_sidechain::hive
|
||||||
|
|
||||||
|
FC_REFLECT(graphene::peerplays_sidechain::hive::vote_operation, )
|
||||||
|
FC_REFLECT(graphene::peerplays_sidechain::hive::comment_operation, )
|
||||||
|
FC_REFLECT(graphene::peerplays_sidechain::hive::transfer_operation, )
|
||||||
|
FC_REFLECT(graphene::peerplays_sidechain::hive::transfer_to_vesting_operation, )
|
||||||
|
FC_REFLECT(graphene::peerplays_sidechain::hive::withdraw_vesting_operation, )
|
||||||
|
FC_REFLECT(graphene::peerplays_sidechain::hive::limit_order_create_operation, )
|
||||||
|
FC_REFLECT(graphene::peerplays_sidechain::hive::limit_order_cancel_operation, )
|
||||||
|
FC_REFLECT(graphene::peerplays_sidechain::hive::feed_publish_operation, )
|
||||||
|
FC_REFLECT(graphene::peerplays_sidechain::hive::convert_operation, )
|
||||||
|
FC_REFLECT(graphene::peerplays_sidechain::hive::account_create_operation, )
|
||||||
FC_REFLECT(graphene::peerplays_sidechain::hive::account_update_operation,
|
FC_REFLECT(graphene::peerplays_sidechain::hive::account_update_operation,
|
||||||
(account)(owner)(active)(posting)(memo_key)(json_metadata))
|
(account)(owner)(active)(posting)(memo_key)(json_metadata))
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,30 @@
|
||||||
namespace graphene { namespace peerplays_sidechain { namespace hive {
|
namespace graphene { namespace peerplays_sidechain { namespace hive {
|
||||||
|
|
||||||
typedef fc::static_variant<
|
typedef fc::static_variant<
|
||||||
|
vote_operation,
|
||||||
|
comment_operation,
|
||||||
|
|
||||||
|
transfer_operation,
|
||||||
|
transfer_to_vesting_operation,
|
||||||
|
withdraw_vesting_operation,
|
||||||
|
|
||||||
|
limit_order_create_operation,
|
||||||
|
limit_order_cancel_operation,
|
||||||
|
|
||||||
|
feed_publish_operation,
|
||||||
|
convert_operation,
|
||||||
|
|
||||||
|
account_create_operation,
|
||||||
account_update_operation>
|
account_update_operation>
|
||||||
hive_operation;
|
hive_operation;
|
||||||
|
|
||||||
}}} // namespace graphene::peerplays_sidechain::hive
|
}}} // namespace graphene::peerplays_sidechain::hive
|
||||||
|
|
||||||
|
namespace fc {
|
||||||
|
|
||||||
|
void to_variant(const graphene::peerplays_sidechain::hive::hive_operation &var, fc::variant &vo, uint32_t max_depth = 5);
|
||||||
|
void from_variant(const fc::variant &var, graphene::peerplays_sidechain::hive::hive_operation &vo, uint32_t max_depth = 5);
|
||||||
|
|
||||||
|
} // namespace fc
|
||||||
|
|
||||||
FC_REFLECT_TYPENAME(graphene::peerplays_sidechain::hive::hive_operation)
|
FC_REFLECT_TYPENAME(graphene::peerplays_sidechain::hive::hive_operation)
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ public:
|
||||||
std::string block_api_get_block(uint32_t block_number);
|
std::string block_api_get_block(uint32_t block_number);
|
||||||
std::string database_api_get_dynamic_global_properties();
|
std::string database_api_get_dynamic_global_properties();
|
||||||
std::string database_api_get_version();
|
std::string database_api_get_version();
|
||||||
|
std::string network_broadcast_api_broadcast_transaction(std::string htrx);
|
||||||
|
|
||||||
std::string get_chain_id();
|
std::string get_chain_id();
|
||||||
std::string get_head_block_id();
|
std::string get_head_block_id();
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
#include <fc/crypto/base64.hpp>
|
#include <fc/crypto/base64.hpp>
|
||||||
#include <fc/crypto/hex.hpp>
|
#include <fc/crypto/hex.hpp>
|
||||||
|
#include <fc/io/json.hpp>
|
||||||
#include <fc/log/logger.hpp>
|
#include <fc/log/logger.hpp>
|
||||||
#include <fc/network/ip.hpp>
|
#include <fc/network/ip.hpp>
|
||||||
#include <fc/smart_ref_impl.hpp>
|
#include <fc/smart_ref_impl.hpp>
|
||||||
|
|
@ -43,6 +44,11 @@ std::string hive_node_rpc_client::database_api_get_version() {
|
||||||
return send_post_request("database_api.get_version", "", false);
|
return send_post_request("database_api.get_version", "", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string hive_node_rpc_client::network_broadcast_api_broadcast_transaction(std::string htrx) {
|
||||||
|
std::string params = "{ \"trx\": " + htrx + ", \"max_block_age\": -1 }";
|
||||||
|
return send_post_request("network_broadcast_api.broadcast_transaction", params, true);
|
||||||
|
}
|
||||||
|
|
||||||
std::string hive_node_rpc_client::get_chain_id() {
|
std::string hive_node_rpc_client::get_chain_id() {
|
||||||
std::string reply_str = database_api_get_version();
|
std::string reply_str = database_api_get_version();
|
||||||
return retrieve_value_from_reply(reply_str, "chain_id");
|
return retrieve_value_from_reply(reply_str, "chain_id");
|
||||||
|
|
@ -278,7 +284,7 @@ void sidechain_net_handler_hive::process_primary_wallet() {
|
||||||
}
|
}
|
||||||
|
|
||||||
hive::authority active;
|
hive::authority active;
|
||||||
active.weight_threshold = 1;
|
active.weight_threshold = total_weight * 2 / 3 + 1;
|
||||||
active.account_auths = account_auths;
|
active.account_auths = account_auths;
|
||||||
|
|
||||||
hive::account_update_operation auo;
|
hive::account_update_operation auo;
|
||||||
|
|
@ -442,14 +448,7 @@ std::string sidechain_net_handler_hive::process_sidechain_transaction(const side
|
||||||
hive::signed_transaction htrx;
|
hive::signed_transaction htrx;
|
||||||
fc::raw::unpack(ss_trx, htrx, 1000);
|
fc::raw::unpack(ss_trx, htrx, 1000);
|
||||||
|
|
||||||
// temp
|
|
||||||
// sign_transaction {"ref_block_num":0,"ref_block_prefix":0,"expiration":"1970-01-01T00:00:0","operations":[["account_update",{"account":"son-account","active":{"weight_threshold":1,"account_auths":[["sonaccount01",1],["sonaccount02",1],["sonaccount03",1],["sonaccount04",1],["sonaccount05",1],["sonaccount06",1],["sonaccount07",1],["sonaccount08",1],["sonaccount09",1],["sonaccount10",1],["sonaccount11",1],["sonaccount12",1],["sonaccount13",1],["sonaccount14",1],["sonaccount15",1]],"key_auths":[]},"memo_key":"TST78bZV5JsuNKUVM7WDVKBnuiuXBTR8gsPEaev2fj96iXqq5R13u","json_metadata":""}]],"extensions":[]} false
|
|
||||||
htrx.set_reference_block(block_id_type("0000d68fd4d7abb7e8975398b9e93f93db990ac8"));
|
|
||||||
htrx.set_expiration(fc::time_point::from_iso_string("2021-03-23T19:21:24"));
|
|
||||||
// temp
|
|
||||||
|
|
||||||
ilog("TRX: ${htrx}", ("htrx", htrx));
|
ilog("TRX: ${htrx}", ("htrx", htrx));
|
||||||
ilog("EXP: {\"ref_block_num\":54927,\"ref_block_prefix\":3081492436,\"expiration\":\"2021-03-23T19:21:24\",\"operations\":[{\"type\":\"account_update_operation\",\"value\":{\"account\":\"son-account\",\"active\":{\"weight_threshold\":1,\"account_auths\":[[\"sonaccount01\",1],[\"sonaccount02\",1],[\"sonaccount03\",1],[\"sonaccount04\",1],[\"sonaccount05\",1],[\"sonaccount06\",1],[\"sonaccount07\",1],[\"sonaccount08\",1],[\"sonaccount09\",1],[\"sonaccount10\",1],[\"sonaccount11\",1],[\"sonaccount12\",1],[\"sonaccount13\",1],[\"sonaccount14\",1],[\"sonaccount15\",1]],\"key_auths\":[]},\"memo_key\":\"TST1111111111111111111111111111111114T1Anm\",\"json_metadata\":\"\"}}],\"extensions\":[],\"signatures\":[\"1f323561e46aa7703850d6afc5fdf00b338424eed695c9513568135bb5dbc051f3242a463f29d17257d982ae616214239b5405073ae7771469a7ea9e36c1577c45\"]}");
|
|
||||||
|
|
||||||
std::string chain_id_str = node_rpc_client->get_chain_id();
|
std::string chain_id_str = node_rpc_client->get_chain_id();
|
||||||
const hive::chain_id_type chain_id(chain_id_str);
|
const hive::chain_id_type chain_id(chain_id_str);
|
||||||
|
|
@ -458,13 +457,11 @@ std::string sidechain_net_handler_hive::process_sidechain_transaction(const side
|
||||||
signature_type st = htrx.sign(*privkey, chain_id);
|
signature_type st = htrx.sign(*privkey, chain_id);
|
||||||
|
|
||||||
ilog("TRX: ${htrx}", ("htrx", htrx));
|
ilog("TRX: ${htrx}", ("htrx", htrx));
|
||||||
ilog("EXP: {\"ref_block_num\":54927,\"ref_block_prefix\":3081492436,\"expiration\":\"2021-03-23T19:21:24\",\"operations\":[{\"type\":\"account_update_operation\",\"value\":{\"account\":\"son-account\",\"active\":{\"weight_threshold\":1,\"account_auths\":[[\"sonaccount01\",1],[\"sonaccount02\",1],[\"sonaccount03\",1],[\"sonaccount04\",1],[\"sonaccount05\",1],[\"sonaccount06\",1],[\"sonaccount07\",1],[\"sonaccount08\",1],[\"sonaccount09\",1],[\"sonaccount10\",1],[\"sonaccount11\",1],[\"sonaccount12\",1],[\"sonaccount13\",1],[\"sonaccount14\",1],[\"sonaccount15\",1]],\"key_auths\":[]},\"memo_key\":\"TST1111111111111111111111111111111114T1Anm\",\"json_metadata\":\"\"}}],\"extensions\":[],\"signatures\":[\"1f323561e46aa7703850d6afc5fdf00b338424eed695c9513568135bb5dbc051f3242a463f29d17257d982ae616214239b5405073ae7771469a7ea9e36c1577c45\"]}");
|
|
||||||
|
|
||||||
std::stringstream ss_st;
|
std::stringstream ss_st;
|
||||||
fc::raw::pack(ss_st, st, 1000);
|
fc::raw::pack(ss_st, st, 1000);
|
||||||
std::string st_str = boost::algorithm::hex(ss_st.str());
|
std::string st_str = boost::algorithm::hex(ss_st.str());
|
||||||
|
|
||||||
st_str = "";
|
|
||||||
return st_str;
|
return st_str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -484,6 +481,10 @@ std::string sidechain_net_handler_hive::send_sidechain_transaction(const sidecha
|
||||||
}
|
}
|
||||||
ilog("HTRX: ${htrx}", ("htrx", htrx));
|
ilog("HTRX: ${htrx}", ("htrx", htrx));
|
||||||
|
|
||||||
|
std::string params = fc::json::to_string(htrx);
|
||||||
|
ilog("HTRX: ${htrx}", ("htrx", params));
|
||||||
|
node_rpc_client->network_broadcast_api_broadcast_transaction(params);
|
||||||
|
|
||||||
// try {
|
// try {
|
||||||
// trx.validate();
|
// trx.validate();
|
||||||
// database.push_transaction(trx, database::validation_steps::skip_block_size_check);
|
// database.push_transaction(trx, database::validation_steps::skip_block_size_check);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue