Skip to content

Commit

Permalink
[SON-320] Added check for approving son_wallet_update_operation (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
obucina committed Apr 6, 2020
1 parent 17bf51e commit be4b55c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class bitcoin_rpc_client {

std::string addmultisigaddress(const uint32_t nrequired, const std::vector<std::string> public_keys);
std::string combinepsbt(const vector<std::string> &psbts);
std::string createmultisig(const uint32_t nrequired, const std::vector<std::string> public_keys);
std::string createpsbt(const std::vector<btc_txout> &ins, const fc::flat_map<std::string, double> outs);
std::string createrawtransaction(const std::vector<btc_txout> &ins, const fc::flat_map<std::string, double> outs);
std::string createwallet(const std::string &wallet_name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ void sidechain_net_handler::process_proposals() {

switch (op_idx_0) {
case chain::operation::tag<chain::son_wallet_update_operation>::value: {
should_process = true;
should_process = (op_obj_idx_0.get<son_wallet_update_operation>().sidechain == sidechain);
break;
}

Expand Down Expand Up @@ -243,9 +243,9 @@ void sidechain_net_handler::process_proposals() {

default:
should_process = false;
ilog("==================================================");
ilog("Proposal not processed ${po}", ("po", *po));
ilog("==================================================");
elog("==================================================");
elog("Proposal not processed ${po}", ("po", *po));
elog("==================================================");
}

if (should_process) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ std::string bitcoin_rpc_client::addmultisigaddress(const uint32_t nrequired, con
pubkeys = pubkeys + std::string("\"") + pubkey + std::string("\"");
}
params = params + pubkeys + std::string("]");
body = body + params + std::string("] }");
body = body + params + std::string(", null, \"p2sh-segwit\"] }");

const auto reply = send_post_request(body);
const auto reply = send_post_request(body, true);

if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
Expand Down Expand Up @@ -100,6 +100,41 @@ std::string bitcoin_rpc_client::combinepsbt(const vector<std::string> &psbts) {
return "";
}

std::string bitcoin_rpc_client::createmultisig(const uint32_t nrequired, const std::vector<std::string> public_keys) {
std::string body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"createmultisig\", "
"\"method\": \"createmultisig\", \"params\": [");
std::string params = std::to_string(nrequired) + ", [";
std::string pubkeys = "";
for (std::string pubkey : public_keys) {
if (!pubkeys.empty()) {
pubkeys = pubkeys + ",";
}
pubkeys = pubkeys + std::string("\"") + pubkey + std::string("\"");
}
params = params + pubkeys + std::string("]");
body = body + params + std::string(", \"p2sh-segwit\" ] }");

const auto reply = send_post_request(body, true);

if (reply.body.empty()) {
wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__));
return "";
}

std::stringstream ss(std::string(reply.body.begin(), reply.body.end()));
boost::property_tree::ptree json;
boost::property_tree::read_json(ss, json);

if (reply.status == 200) {
return ss.str();
}

if (json.count("error") && !json.get_child("error").empty()) {
wlog("Bitcoin RPC call ${function} with body ${body} failed with reply '${msg}'", ("function", __FUNCTION__)("body", body)("msg", ss.str()));
}
return "";
}

std::string bitcoin_rpc_client::createpsbt(const std::vector<btc_txout> &ins, const fc::flat_map<std::string, double> outs) {
std::string body("{\"jsonrpc\": \"1.0\", \"id\":\"createpsbt\", "
"\"method\": \"createpsbt\", \"params\": [");
Expand Down Expand Up @@ -892,7 +927,42 @@ bool sidechain_net_handler_bitcoin::process_proposal(const proposal_object &po)
switch (op_idx_0) {

case chain::operation::tag<chain::son_wallet_update_operation>::value: {
should_approve = true;
son_wallet_id_type swo_id = op_obj_idx_0.get<son_wallet_update_operation>().son_wallet_id;
const auto &idx = database.get_index_type<son_wallet_index>().indices().get<by_id>();
const auto swo = idx.find(swo_id);
if (swo != idx.end()) {
auto active_sons = gpo.active_sons;
vector<son_info> wallet_sons = swo->sons;

bool son_sets_equal = (active_sons.size() == wallet_sons.size());

if (son_sets_equal) {
for (size_t i = 0; i < active_sons.size(); i++) {
son_sets_equal = son_sets_equal && active_sons.at(i) == wallet_sons.at(i);
}
}

if (son_sets_equal) {
auto active_sons = gpo.active_sons;
vector<string> son_pubkeys_bitcoin;
for (const son_info &si : active_sons) {
son_pubkeys_bitcoin.push_back(si.sidechain_public_keys.at(sidechain_type::bitcoin));
}

uint32_t nrequired = son_pubkeys_bitcoin.size() * 2 / 3 + 1;
string reply_str = bitcoin_client->createmultisig(nrequired, son_pubkeys_bitcoin);

std::stringstream active_pw_ss(reply_str);
boost::property_tree::ptree active_pw_pt;
boost::property_tree::read_json(active_pw_ss, active_pw_pt);
if (active_pw_pt.count("error") && active_pw_pt.get_child("error").empty()) {
std::stringstream res;
boost::property_tree::json_parser::write_json(res, active_pw_pt.get_child("result"));

should_approve = (op_obj_idx_0.get<son_wallet_update_operation>().address == res.str());
}
}
}
break;
}

Expand Down Expand Up @@ -952,6 +1022,9 @@ bool sidechain_net_handler_bitcoin::process_proposal(const proposal_object &po)

default:
should_approve = false;
elog("==================================================");
elog("Proposal not considered for approval ${po}", ("po", po));
elog("==================================================");
}

return should_approve;
Expand All @@ -977,7 +1050,7 @@ void sidechain_net_handler_bitcoin::process_primary_wallet() {
bitcoin_client->walletpassphrase(wallet_password, 5);
}
uint32_t nrequired = son_pubkeys_bitcoin.size() * 2 / 3 + 1;
string reply_str = bitcoin_client->addmultisigaddress(nrequired, son_pubkeys_bitcoin);
string reply_str = bitcoin_client->createmultisig(nrequired, son_pubkeys_bitcoin);

std::stringstream active_pw_ss(reply_str);
boost::property_tree::ptree active_pw_pt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ bool sidechain_net_handler_peerplays::process_proposal(const proposal_object &po

default:
should_approve = false;
elog("==================================================");
elog("Proposal not considered for approval ${po}", ("po", po));
elog("==================================================");
}

return should_approve;
Expand Down

0 comments on commit be4b55c

Please sign in to comment.