tx and inputs amounts serialization
This commit is contained in:
parent
4e42579086
commit
13076ee73e
3 changed files with 44 additions and 0 deletions
|
|
@ -7,6 +7,8 @@
|
||||||
#include <graphene/peerplays_sidechain/bitcoin_utils.hpp>
|
#include <graphene/peerplays_sidechain/bitcoin_utils.hpp>
|
||||||
#include <secp256k1.h>
|
#include <secp256k1.h>
|
||||||
|
|
||||||
|
#include <boost/property_tree/json_parser.hpp>
|
||||||
|
|
||||||
namespace graphene { namespace peerplays_sidechain {
|
namespace graphene { namespace peerplays_sidechain {
|
||||||
|
|
||||||
static const unsigned char OP_0 = 0x00;
|
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);
|
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
|
}} // namespace graphene::peerplays_sidechain
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,10 @@ bytes add_signatures_to_unsigned_tx(const bytes &unsigned_tx,
|
||||||
const std::vector<std::vector<bytes>> &signatures,
|
const std::vector<std::vector<bytes>> &signatures,
|
||||||
const bytes &redeem_script);
|
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 {
|
struct btc_outpoint {
|
||||||
fc::uint256 hash;
|
fc::uint256 hash;
|
||||||
uint32_t n;
|
uint32_t n;
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,18 @@ BOOST_AUTO_TEST_CASE(tx_serialization)
|
||||||
BOOST_CHECK(bintx == buff);
|
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)
|
BOOST_AUTO_TEST_CASE(pw_transfer)
|
||||||
{
|
{
|
||||||
// key set for the old Primary Wallet
|
// key set for the old Primary Wallet
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue