move address creation to separate function

This commit is contained in:
gladcow 2020-03-20 17:34:44 +03:00
parent 727541d2b7
commit 5759fd73c9
2 changed files with 89 additions and 56 deletions

View file

@ -103,6 +103,7 @@ private:
fc::future<void> on_changed_objects_task; fc::future<void> on_changed_objects_task;
std::string create_multisig_address(const std::vector<std::string> &son_pubkeys_bitcoin);
std::string create_transaction(const std::vector<btc_txout> &inputs, const fc::flat_map<std::string, double> outputs, const std::string& extra_data); std::string create_transaction(const std::vector<btc_txout> &inputs, const fc::flat_map<std::string, double> outputs, const std::string& extra_data);
std::string sign_transaction(const sidechain_transaction_object &sto, bool &complete); std::string sign_transaction(const sidechain_transaction_object &sto, bool &complete);
bool send_transaction(const sidechain_transaction_object &sto, std::string &sidechain_transaction); bool send_transaction(const sidechain_transaction_object &sto, std::string &sidechain_transaction);

View file

@ -857,26 +857,23 @@ void sidechain_net_handler_bitcoin::recreate_primary_wallet() {
for (const son_info &si : active_sons) { for (const son_info &si : active_sons) {
son_pubkeys_bitcoin.push_back(si.sidechain_public_keys.at(sidechain_type::bitcoin)); son_pubkeys_bitcoin.push_back(si.sidechain_public_keys.at(sidechain_type::bitcoin));
} }
std::string full_address_info = create_multisig_address(son_pubkeys_bitcoin);
if (!wallet_password.empty()) { if(!full_address_info.size())
bitcoin_client->walletpassphrase(wallet_password, 5); {
elog("Failed to create multisig address");
return;
} }
uint32_t nrequired = son_pubkeys_bitcoin.size() * 2 / 3 + 1; std::stringstream address_info_ss(full_address_info);
string reply_str = bitcoin_client->addmultisigaddress(nrequired, son_pubkeys_bitcoin); boost::property_tree::ptree address_info_pt;
boost::property_tree::read_json(address_info_ss, address_info_pt);
std::stringstream active_pw_ss(reply_str); std::string address = address_info_pt.get_child("result").get<std::string>("address");
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"));
son_wallet_update_operation op; son_wallet_update_operation op;
op.payer = gpo.parameters.son_account(); op.payer = gpo.parameters.son_account();
op.son_wallet_id = active_sw->id; op.son_wallet_id = active_sw->id;
op.sidechain = sidechain_type::bitcoin; op.sidechain = sidechain_type::bitcoin;
op.address = res.str(); op.address = full_address_info;
proposal_create_operation proposal_op; proposal_create_operation proposal_op;
proposal_op.fee_paying_account = plugin.get_current_son_object().son_account; proposal_op.fee_paying_account = plugin.get_current_son_object().son_account;
@ -900,7 +897,7 @@ void sidechain_net_handler_bitcoin::recreate_primary_wallet() {
boost::property_tree::ptree prev_sw_pt; boost::property_tree::ptree prev_sw_pt;
boost::property_tree::read_json(prev_sw_ss, prev_sw_pt); boost::property_tree::read_json(prev_sw_ss, prev_sw_pt);
std::string active_pw_address = active_pw_pt.get_child("result").get<std::string>("address"); std::string active_pw_address = address;
std::string prev_pw_address = prev_sw_pt.get<std::string>("address"); std::string prev_pw_address = prev_sw_pt.get<std::string>("address");
if (prev_pw_address == active_pw_address) { if (prev_pw_address == active_pw_address) {
@ -935,6 +932,19 @@ void sidechain_net_handler_bitcoin::recreate_primary_wallet() {
std::string tx_str = create_transaction(inputs, outputs, ""); std::string tx_str = create_transaction(inputs, outputs, "");
if (!tx_str.empty()) {
auto signer_sons = prev_sw->sons;
std::vector<son_id_type> signers;
for (const son_info &si : signer_sons) {
signers.push_back(si.son_id);
}
fc::flat_map<std::string, double> outputs;
outputs[active_pw_address] = total_amount - min_amount;
std::string tx_str = create_transaction(inputs, outputs, "");
if (!tx_str.empty()) { if (!tx_str.empty()) {
sidechain_transaction_create_operation stc_op; sidechain_transaction_create_operation stc_op;
@ -1137,6 +1147,28 @@ bool sidechain_net_handler_bitcoin::send_sidechain_transaction(const sidechain_t
return send_transaction(sto, sidechain_transaction); return send_transaction(sto, sidechain_transaction);
} }
std::string sidechain_net_handler_bitcoin::create_multisig_address(const std::vector<std::string> &son_pubkeys_bitcoin)
{
if (!wallet_password.empty()) {
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);
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()) {
elog("Failed to recreate primary wallet");
return "";
}
std::stringstream res;
boost::property_tree::json_parser::write_json(res, active_pw_pt.get_child("result"));
return res.str();
}
// Creates transaction in any format // Creates transaction in any format
// Function to actually create transaction should return transaction string, or empty string in case of failure // Function to actually create transaction should return transaction string, or empty string in case of failure
std::string sidechain_net_handler_bitcoin::create_transaction(const std::vector<btc_txout> &inputs, const fc::flat_map<std::string, double> outputs, const std::string& extra_data) { std::string sidechain_net_handler_bitcoin::create_transaction(const std::vector<btc_txout> &inputs, const fc::flat_map<std::string, double> outputs, const std::string& extra_data) {