createrawtransaction call

This commit is contained in:
gladcow 2019-12-30 16:18:10 +03:00
parent e1a99665c7
commit 7e61feb2be
4 changed files with 56 additions and 0 deletions

View file

@ -89,4 +89,9 @@ std::string p2sh_address_from_redeem_script(const bytes& script, bitcoin_network
return fc::to_base58(reinterpret_cast<const char*>(&data[0]), data.size());
}
bytes sign_raw_transaction(const bytes& unsigned_tx, const fc::ecc::private_key& priv_key)
{
return bytes();
}
}}

View file

@ -11,5 +11,6 @@ enum bitcoin_network {
bytes generate_redeem_script(fc::flat_map<fc::ecc::public_key, int> key_data);
std::string p2sh_address_from_redeem_script(const bytes& script, bitcoin_network network = mainnet);
bytes sign_raw_transaction(const bytes& unsigned_tx, const fc::ecc::private_key& priv_key);
}}

View file

@ -31,6 +31,7 @@ public:
bool connection_is_not_defined() const;
void import_address( const std::string& address_or_script);
std::vector<btc_txout> list_unspent();
std::string prepare_tx(const std::vector<btc_txout>& ins, const fc::flat_map<std::string, double> outs);
private:

View file

@ -238,6 +238,55 @@ std::vector<btc_txout> bitcoin_rpc_client::list_unspent()
return result;
}
std::string bitcoin_rpc_client::prepare_tx(const std::vector<btc_txout> &ins, const fc::flat_map<std::string, double> outs)
{
std::string body("{\"jsonrpc\": \"1.0\", \"id\":\"pp_plugin\", \"method\": \"createrawtransaction\", \"params\": [");
body += "[";
bool first = true;
for(const auto& entry: ins)
{
if(!first)
body += ",";
body += "{\"txid\":\"" + entry.txid_ + "\",\"vout\":\"" + fc::to_string(entry.out_num_) + "\"}";
first = false;
}
body += "]";
first = true;
body += "{";
for(const auto& entry: outs)
{
if(!first)
body += ",";
body += "\"" + entry.first + "\":\"" + fc::to_string(entry.second) + "\"";
first = false;
}
body += "}";
body += std::string("] }");
const auto reply = send_post_request( body );
if( reply.body.empty() )
{
wlog("Failed to create raw transaction: [${body}]", ("body", body));
return std::string();
}
std::string reply_str( reply.body.begin(), reply.body.end() );
std::stringstream ss(reply_str);
boost::property_tree::ptree json;
boost::property_tree::read_json( ss, json );
if( reply.status == 200 ) {
idump((reply_str));
if( json.count( "result" ) )
return json.get_child("result").get_value<std::string>();
} else if( json.count( "error" ) && !json.get_child( "error" ).empty() ) {
wlog( "Failed to create raw transaction: [${body}]! Reply: ${msg}", ("body", body)("msg", reply_str) );
}
return std::string();
}
fc::http::reply bitcoin_rpc_client::send_post_request( std::string body )
{
fc::http::connection conn;