Get UTXO set for watched addresses

This commit is contained in:
gladcow 2019-12-26 16:00:47 +03:00
parent 6d2039c67b
commit e1a99665c7
2 changed files with 49 additions and 2 deletions

View file

@ -11,6 +11,14 @@
namespace graphene { namespace peerplays_sidechain {
class btc_txout
{
public:
std::string txid_;
unsigned int out_num_;
double amount_;
};
class bitcoin_rpc_client {
public:
bitcoin_rpc_client( std::string _ip, uint32_t _rpc, std::string _user, std::string _password) ;
@ -22,6 +30,7 @@ public:
std::string add_multisig_address( const std::vector<std::string> public_keys );
bool connection_is_not_defined() const;
void import_address( const std::string& address_or_script);
std::vector<btc_txout> list_unspent();
private:

View file

@ -175,7 +175,7 @@ bool bitcoin_rpc_client::connection_is_not_defined() const
void bitcoin_rpc_client::import_address(const std::string &address_or_script)
{
const auto body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"init_wallet\", \"method\": \"importaddress\", \"params\": [") +
const auto body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"pp_plugin\", \"method\": \"importaddress\", \"params\": [") +
std::string("\"") + address_or_script + std::string("\"") + std::string("] }");
const auto reply = send_post_request( body );
@ -193,13 +193,51 @@ void bitcoin_rpc_client::import_address(const std::string &address_or_script)
boost::property_tree::read_json( ss, json );
if( reply.status == 200 ) {
idump(( address_or_script ));
idump((address_or_script)(reply_str));
return;
} else if( json.count( "error" ) && !json.get_child( "error" ).empty() ) {
wlog( "Failed to import address [${addr}]! Reply: ${msg}", ("addr", address_or_script)("msg", reply_str) );
}
}
std::vector<btc_txout> bitcoin_rpc_client::list_unspent()
{
const auto body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"pp_plugin\", \"method\": \"listunspent\", \"params\": [] }");
const auto reply = send_post_request( body );
std::vector<btc_txout> result;
if( reply.body.empty() )
{
wlog("Failed to list unspent txo");
return result;
}
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" ) )
{
for(auto& entry: json.get_child("result"))
{
btc_txout txo;
txo.txid_ = entry.second.get_child("txid").get_value<std::string>();
txo.out_num_ = entry.second.get_child("vout").get_value<unsigned int>();
txo.amount_ = entry.second.get_child("amount").get_value<double>();
result.push_back(txo);
}
}
} else if( json.count( "error" ) && !json.get_child( "error" ).empty() ) {
wlog( "Failed to list unspent txo! Reply: ${msg}", ("msg", reply_str) );
}
return result;
}
fc::http::reply bitcoin_rpc_client::send_post_request( std::string body )
{
fc::http::connection conn;