From ad0b5afb796c5b5f46585ec7d43359c109281fc4 Mon Sep 17 00:00:00 2001 From: Vlad Dobromyslov Date: Fri, 1 Apr 2022 12:20:59 +0000 Subject: [PATCH] #337 lock/unlock importmulti --- .../sidechain_net_handler_bitcoin.hpp | 4 +- .../sidechain_net_handler_bitcoin.cpp | 122 ++++++++++-------- 2 files changed, 69 insertions(+), 57 deletions(-) diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp index 0c6a5cc7..9c067e0e 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp @@ -67,9 +67,9 @@ public: std::string sendrawtransaction(const std::string &tx_hex); std::string signrawtransactionwithwallet(const std::string &tx_hash); std::string unloadwallet(const std::string &filename); - //std::string walletlock(); + std::string walletlock(); 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); private: fc::http::reply send_post_request(std::string body, bool show_log); diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp index e9a66365..52d93f51 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp @@ -606,13 +606,13 @@ void bitcoin_rpc_client::importmulti(const std::vector &address_or for (const auto ¶m : address_or_script_array) { argument_1 += "{\"scriptPubKey\": "; if (param.type == multi_type::address) { - argument_1 += "{\"address\": \"" + param.address_or_script + "\"}"; + argument_1 += "{\"address\": \"" + param.address_or_script + "\"}, \"label\": \"" + param.label + "\""; } else if (param.type == multi_type::script) { - argument_1 += "\"" + param.address_or_script + "\""; + argument_1 += "\"" + param.address_or_script + "\", \"internal\": true"; } else { FC_THROW("Invalid multi_type."); } - argument_1 += ", \"label\": \"" + param.label + "\", \"timestamp\": " + std::to_string(fc::time_point_sec::from_iso_string("2022-01-01T00:00:00").sec_since_epoch()) + "}"; + argument_1 += ", \"timestamp\": " + std::to_string(fc::time_point_sec::from_iso_string("2022-01-01T00:00:00").sec_since_epoch()) + "}"; //! Note /* Creation time of the key expressed in UNIX epoch time, @@ -834,32 +834,32 @@ std::string bitcoin_rpc_client::unloadwallet(const std::string &filename) { return ""; } -//std::string bitcoin_rpc_client::walletlock() { -// std::string body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"walletlock\", \"method\": " -// "\"walletlock\", \"params\": [] }"); -// -// const auto reply = send_post_request(body, debug_rpc_calls); -// -// 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) { -// std::stringstream ss; -// boost::property_tree::json_parser::write_json(ss, json.get_child("result")); -// 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::walletlock() { + std::string body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"walletlock\", \"method\": " + "\"walletlock\", \"params\": [] }"); + + const auto reply = send_post_request(body, debug_rpc_calls); + + 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) { + std::stringstream ss; + boost::property_tree::json_parser::write_json(ss, json.get_child("result")); + 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::walletprocesspsbt(std::string const &tx_psbt) { std::string body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"walletprocesspsbt\", \"method\": " @@ -887,31 +887,31 @@ std::string bitcoin_rpc_client::walletprocesspsbt(std::string const &tx_psbt) { return ""; } -//bool bitcoin_rpc_client::walletpassphrase(const std::string &passphrase, uint32_t timeout) { -// std::string body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"walletpassphrase\", \"method\": " -// "\"walletpassphrase\", \"params\": [\"" + -// passphrase + "\", " + std::to_string(timeout) + "] }"); -// -// const auto reply = send_post_request(body, debug_rpc_calls); -// -// if (reply.body.empty()) { -// wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__)); -// return false; -// } -// -// 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 true; -// } -// -// 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 false; -//} +bool bitcoin_rpc_client::walletpassphrase(const std::string &passphrase, uint32_t timeout) { + std::string body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"walletpassphrase\", \"method\": " + "\"walletpassphrase\", \"params\": [\"" + + passphrase + "\", " + std::to_string(timeout) + "] }"); + + const auto reply = send_post_request(body, debug_rpc_calls); + + if (reply.body.empty()) { + wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__)); + return false; + } + + 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 true; + } + + 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 false; +} fc::http::reply bitcoin_rpc_client::send_post_request(std::string body, bool show_log) { fc::http::connection conn; @@ -2052,8 +2052,20 @@ void sidechain_net_handler_bitcoin::on_changed_objects_cb(const vectorwalletpassphrase(wallet_password) ) + return; + } + + //! importmulti bitcoin_client->importmulti(address_or_script_array); + + //! Lock wallet + bitcoin_client->walletlock(); + } } // =============================================================================