From c5927630a63346f079e3341aca63a5dc51af89b1 Mon Sep 17 00:00:00 2001 From: gladcow Date: Thu, 26 Mar 2020 16:07:45 +0300 Subject: [PATCH] use satoshi values for in_amounts for signing --- .../sidechain_net_handler_bitcoin.hpp | 2 +- .../sidechain_net_handler_bitcoin.cpp | 38 +++++++++++++++++-- 2 files changed, 36 insertions(+), 4 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 3f75ca0d..5c1382ce 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 @@ -43,7 +43,7 @@ public: std::string walletlock(); std::string walletprocesspsbt(std::string const &tx_psbt); bool walletpassphrase(const std::string &passphrase, uint32_t timeout = 60); - + uint64_t outputamount(const std::string& txid, uint32_t output_num); private: fc::http::reply send_post_request(std::string body, bool show_log = false); diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp index 7bf446ba..3aa90b98 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp @@ -706,6 +706,38 @@ bool bitcoin_rpc_client::walletpassphrase(const std::string &passphrase, uint32_ return false; } +uint64_t bitcoin_rpc_client::outputamount(const std::string &txid, uint32_t output_num) +{ + const auto body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"peerplays_plugin\", " + "\"method\": \"getrawtransaction\", \"params\": [\"") + + txid + std::string("\", false] }"); + + const auto reply = send_post_request(body, true); + + if (reply.body.empty()) { + wlog("Bitcoin RPC call ${function} failed", ("function", __FUNCTION__)); + return 0; + } + + 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) { + if (json.find("result") != json.not_found()) { + auto txraw_str = json.get("result"); + bytes txraw; + txraw.resize(txraw_str.size() / 2); + fc::from_hex(txraw_str, (char*)&txraw[0], txraw.size()); + btc_tx tx; + tx.fill_from_bytes(txraw); + return tx.vout[output_num].nValue; + } + } + + return 0; +} + fc::http::reply bitcoin_rpc_client::send_post_request(std::string body, bool show_log) { fc::http::connection conn; conn.connect_to(fc::ip::endpoint(fc::ip::address(ip), rpc_port)); @@ -938,7 +970,7 @@ void sidechain_net_handler_bitcoin::recreate_primary_wallet() { } if (min_amount >= total_amount) { - elog("Failed not enough BTC to transfer from ${fa}", ("fa", prev_pw_address)); + elog("Failed not enough BTC to transfer from ${fa}, ${ma} >= ${ta}", ("fa", prev_pw_address)("ma", min_amount)("ta", total_amount)); return; } } @@ -1277,8 +1309,8 @@ std::string sidechain_net_handler_bitcoin::create_transaction_standalone(const s vector in_amounts; for(const auto& in: inputs) { - tx.vin.push_back(btc_in(in.txid_, in.out_num_, in.amount_)); - in_amounts.push_back(in.amount_ * 100000000.0); + tx.vin.push_back(btc_in(in.txid_, in.out_num_)); + in_amounts.push_back(bitcoin_client->outputamount(in.txid_, in.out_num_)); } for(const auto& out: outputs) tx.vout.push_back(btc_out(out.first, out.second * 100000000.0));