Move util functions to Utils file
This commit is contained in:
parent
b81088f192
commit
9d912ba0f0
3 changed files with 59 additions and 51 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
#include <boost/property_tree/json_parser.hpp>
|
||||||
|
#include <boost/property_tree/ptree.hpp>
|
||||||
#include <fc/crypto/base58.hpp>
|
#include <fc/crypto/base58.hpp>
|
||||||
#include <graphene/peerplays_sidechain/bitcoin/utils.hpp>
|
#include <graphene/peerplays_sidechain/bitcoin/utils.hpp>
|
||||||
|
|
||||||
|
|
@ -45,4 +47,53 @@ bytes public_key_data_to_bytes(const fc::ecc::public_key_data &key) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<bytes> read_byte_arrays_from_string(const std::string &string_buf) {
|
||||||
|
std::stringstream ss(string_buf);
|
||||||
|
boost::property_tree::ptree json;
|
||||||
|
boost::property_tree::read_json(ss, json);
|
||||||
|
|
||||||
|
std::vector<bytes> data;
|
||||||
|
for (auto &v : json) {
|
||||||
|
std::string hex = v.second.data();
|
||||||
|
bytes item;
|
||||||
|
item.resize(hex.size() / 2);
|
||||||
|
fc::from_hex(hex, (char *)&item[0], item.size());
|
||||||
|
data.push_back(item);
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string write_transaction_signatures(const std::vector<bytes> &data) {
|
||||||
|
std::string res = "[";
|
||||||
|
for (unsigned int idx = 0; idx < data.size(); ++idx) {
|
||||||
|
res += "\"" + fc::to_hex((char *)&data[idx][0], data[idx].size()) + "\"";
|
||||||
|
if (idx != data.size() - 1)
|
||||||
|
res += ",";
|
||||||
|
}
|
||||||
|
res += "]";
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_transaction_data(const std::string &string_buf, std::string &tx_hex, std::vector<uint64_t> &in_amounts, std::string &redeem_script) {
|
||||||
|
std::stringstream ss(string_buf);
|
||||||
|
boost::property_tree::ptree json;
|
||||||
|
boost::property_tree::read_json(ss, json);
|
||||||
|
tx_hex = json.get<std::string>("tx_hex");
|
||||||
|
in_amounts.clear();
|
||||||
|
for (auto &v : json.get_child("in_amounts"))
|
||||||
|
in_amounts.push_back(fc::to_uint64(v.second.data()));
|
||||||
|
redeem_script = json.get<std::string>("redeem_script");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string write_transaction_data(const std::string &tx, const std::vector<uint64_t> &in_amounts, const std::string &redeem_script) {
|
||||||
|
std::string res = "{\"tx_hex\":\"" + tx + "\",\"in_amounts\":[";
|
||||||
|
for (unsigned int idx = 0; idx < in_amounts.size(); ++idx) {
|
||||||
|
res += fc::to_string(in_amounts[idx]);
|
||||||
|
if (idx != in_amounts.size() - 1)
|
||||||
|
res += ",";
|
||||||
|
}
|
||||||
|
res += "],\"redeem_script\":\"" + redeem_script + "\"}";
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
}}} // namespace graphene::peerplays_sidechain::bitcoin
|
}}} // namespace graphene::peerplays_sidechain::bitcoin
|
||||||
|
|
|
||||||
|
|
@ -15,4 +15,12 @@ std::vector<bytes> get_pubkey_from_redeemScript(bytes script);
|
||||||
|
|
||||||
bytes public_key_data_to_bytes(const fc::ecc::public_key_data &key);
|
bytes public_key_data_to_bytes(const fc::ecc::public_key_data &key);
|
||||||
|
|
||||||
|
std::vector<bytes> read_byte_arrays_from_string(const std::string &string_buf);
|
||||||
|
|
||||||
|
std::string write_transaction_signatures(const std::vector<bytes> &data);
|
||||||
|
|
||||||
|
void read_transaction_data(const std::string &string_buf, std::string &tx_hex, std::vector<uint64_t> &in_amounts, std::string &redeem_script);
|
||||||
|
|
||||||
|
std::string write_transaction_data(const std::string &tx, const std::vector<uint64_t> &in_amounts, const std::string &redeem_script);
|
||||||
|
|
||||||
}}} // namespace graphene::peerplays_sidechain::bitcoin
|
}}} // namespace graphene::peerplays_sidechain::bitcoin
|
||||||
|
|
@ -25,57 +25,6 @@ namespace graphene { namespace peerplays_sidechain {
|
||||||
|
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
|
||||||
std::vector<bitcoin::bytes> read_byte_arrays_from_string(const std::string &string_buf) {
|
|
||||||
std::stringstream ss(string_buf);
|
|
||||||
boost::property_tree::ptree json;
|
|
||||||
boost::property_tree::read_json(ss, json);
|
|
||||||
|
|
||||||
std::vector<bitcoin::bytes> data;
|
|
||||||
for (auto &v : json) {
|
|
||||||
std::string hex = v.second.data();
|
|
||||||
bitcoin::bytes item;
|
|
||||||
item.resize(hex.size() / 2);
|
|
||||||
fc::from_hex(hex, (char *)&item[0], item.size());
|
|
||||||
data.push_back(item);
|
|
||||||
}
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string write_transaction_signatures(const std::vector<bitcoin::bytes> &data) {
|
|
||||||
std::string res = "[";
|
|
||||||
for (unsigned int idx = 0; idx < data.size(); ++idx) {
|
|
||||||
res += "\"" + fc::to_hex((char *)&data[idx][0], data[idx].size()) + "\"";
|
|
||||||
if (idx != data.size() - 1)
|
|
||||||
res += ",";
|
|
||||||
}
|
|
||||||
res += "]";
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
void read_transaction_data(const std::string &string_buf, std::string &tx_hex, std::vector<uint64_t> &in_amounts, std::string &redeem_script) {
|
|
||||||
std::stringstream ss(string_buf);
|
|
||||||
boost::property_tree::ptree json;
|
|
||||||
boost::property_tree::read_json(ss, json);
|
|
||||||
tx_hex = json.get<std::string>("tx_hex");
|
|
||||||
in_amounts.clear();
|
|
||||||
for (auto &v : json.get_child("in_amounts"))
|
|
||||||
in_amounts.push_back(fc::to_uint64(v.second.data()));
|
|
||||||
redeem_script = json.get<std::string>("redeem_script");
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string write_transaction_data(const std::string &tx, const std::vector<uint64_t> &in_amounts, const std::string &redeem_script) {
|
|
||||||
std::string res = "{\"tx_hex\":\"" + tx + "\",\"in_amounts\":[";
|
|
||||||
for (unsigned int idx = 0; idx < in_amounts.size(); ++idx) {
|
|
||||||
res += fc::to_string(in_amounts[idx]);
|
|
||||||
if (idx != in_amounts.size() - 1)
|
|
||||||
res += ",";
|
|
||||||
}
|
|
||||||
res += "],\"redeem_script\":\"" + redeem_script + "\"}";
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
// =============================================================================
|
|
||||||
|
|
||||||
bitcoin_rpc_client::bitcoin_rpc_client(std::string _ip, uint32_t _rpc, std::string _user, std::string _password, std::string _wallet, std::string _wallet_password) :
|
bitcoin_rpc_client::bitcoin_rpc_client(std::string _ip, uint32_t _rpc, std::string _user, std::string _password, std::string _wallet, std::string _wallet_password) :
|
||||||
ip(_ip),
|
ip(_ip),
|
||||||
rpc_port(_rpc),
|
rpc_port(_rpc),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue