tx and inputs amounts serialization

This commit is contained in:
gladcow 2020-03-21 13:02:41 +03:00
parent 4e42579086
commit 13076ee73e
3 changed files with 44 additions and 0 deletions

View file

@ -7,6 +7,8 @@
#include <graphene/peerplays_sidechain/bitcoin_utils.hpp>
#include <secp256k1.h>
#include <boost/property_tree/json_parser.hpp>
namespace graphene { namespace peerplays_sidechain {
static const unsigned char OP_0 = 0x00;
@ -783,4 +785,30 @@ bytes get_weighted_multisig_redeem_script(std::vector<std::pair<std::string, uin
return generate_redeem_script(key_data);
}
void read_tx_data_from_string(const std::string &string_buf, bytes &tx, std::vector<uint64_t> &in_amounts)
{
std::stringstream ss(string_buf);
boost::property_tree::ptree json;
boost::property_tree::read_json(ss, json);
std::string tx_hex = json.get<std::string>("tx_hex");
tx.clear();
tx.resize(tx_hex.size() / 2);
fc::from_hex(tx_hex, (char*)&tx[0], tx.size());
in_amounts.clear();
for(auto &v: json.get_child("in_amounts"))
in_amounts.push_back(fc::to_uint64(v.second.data()));
}
std::string save_tx_data_to_string(const bytes &tx, const std::vector<uint64_t> &in_amounts)
{
std::string res = "{\"tx_hex\":\"" + fc::to_hex((const char*)&tx[0], tx.size()) + "\",\"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 += "]}";
return res;
}
}} // namespace graphene::peerplays_sidechain

View file

@ -68,6 +68,10 @@ bytes add_signatures_to_unsigned_tx(const bytes &unsigned_tx,
const std::vector<std::vector<bytes>> &signatures,
const bytes &redeem_script);
void read_tx_data_from_string(const std::string &string_buf, bytes& tx, std::vector<uint64_t>& in_amounts);
std::string save_tx_data_to_string(const bytes& tx, const std::vector<uint64_t>& in_amounts);
struct btc_outpoint {
fc::uint256 hash;
uint32_t n;

View file

@ -28,6 +28,18 @@ BOOST_AUTO_TEST_CASE(tx_serialization)
BOOST_CHECK(bintx == buff);
}
BOOST_AUTO_TEST_CASE(tx_data_serialization)
{
bytes source_tx({0, 1, 2, 3, 4, 5});
std::vector<uint64_t> source_ins({6, 7, 8, 9, 10});
std::string buf = save_tx_data_to_string(source_tx, source_ins);
bytes destination_tx;
std::vector<uint64_t> destination_ins;
read_tx_data_from_string(buf, destination_tx, destination_ins);
BOOST_REQUIRE(source_tx == destination_tx);
BOOST_REQUIRE(source_ins == destination_ins);
}
BOOST_AUTO_TEST_CASE(pw_transfer)
{
// key set for the old Primary Wallet