Commit 76700cc5 authored by Chiguireitor's avatar Chiguireitor
Browse files

Added utxo retrieval rpc

parent 3bbbee47
......@@ -20,11 +20,11 @@ pub struct Txo {
//
// Input of a Transaction
//
type OutPoint = (Sha256dHash, usize); // (txid, vout)
pub type OutPoint = (Sha256dHash, usize); // (txid, vout)
struct SpendingInput {
txid: Sha256dHash,
outpoint: OutPoint,
pub struct SpendingInput {
pub txid: Sha256dHash,
pub outpoint: OutPoint,
}
//
......@@ -37,11 +37,11 @@ pub struct Status {
}
impl Status {
fn funding(&self) -> impl Iterator<Item = &Txo> {
pub fn funding(&self) -> impl Iterator<Item = &Txo> {
self.confirmed.0.iter().chain(self.mempool.0.iter())
}
fn spending(&self) -> impl Iterator<Item = &SpendingInput> {
pub fn spending(&self) -> impl Iterator<Item = &SpendingInput> {
self.confirmed.1.iter().chain(self.mempool.1.iter())
}
......
......@@ -72,9 +72,31 @@ impl Connection {
)))
}
fn blockchain_scripthash_get_utxos(&self, params: &[Value]) -> Result<Value> {
let script_hash = hash_from_value(params.get(0)).chain_err(|| "bad script_hash")?;
let status = self.query.status(&script_hash[..])?;
let mut dict = HashMap::new();
for item in status.funding().into_iter() {
dict.insert(item.txid.to_hex() + ":" + &item.vout.to_string(), "");
}
for item in status.spending().into_iter() {
dict.remove( &(item.outpoint.0.to_hex() + ":" + &item.outpoint.1.to_string()) );
}
let mut utxos = vec![];
for (outpoint, _drop) in &dict {
utxos.push(outpoint)
}
Ok(json!(utxos))
}
fn handle_command(&mut self, method: &str, params: &[Value], id: &Value) -> Result<Value> {
let result = match method {
"blockchain.scripthash.get_history" => self.blockchain_scripthash_get_history(&params),
"blockchain.scripthash.get_utxos" => self.blockchain_scripthash_get_utxos(&params),
"server.ping" => Ok(Value::Null),
"server.version" => self.server_version(),
&_ => bail!("unknown method {} {:?}", method, params),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment