Browse Source

Implement GET /address/:addr/utxo

(cherry picked from commit 47dce129f0e61304029acaf91fc0f4968a98bbfd)
liquid_e
Nadav Ivgi 6 years ago
committed by Lawrence Nahum
parent
commit
223a492e01
  1. 1
      src/query.rs
  2. 34
      src/rest.rs

1
src/query.rs

@ -19,6 +19,7 @@ use util::{FullHash, HashPrefix, HeaderEntry, Bytes, BlockMeta, BlockHeaderMeta,
use errors::*;
#[derive(Clone)]
pub struct FundingOutput {
pub txn: TxnHeight,
pub txn_id: Sha256dHash,

34
src/rest.rs

@ -10,7 +10,7 @@ use hex::{self, FromHexError};
use hyper::{Body, Response, Server, Method, Request, StatusCode};
use hyper::service::service_fn_ok;
use hyper::rt::{self, Future};
use query::{Query, TxnHeight};
use query::{Query, TxnHeight, FundingOutput};
use serde_json;
use serde::Serialize;
use std::collections::BTreeMap;
@ -208,6 +208,31 @@ impl From<TxOut> for TxOutValue {
}
}
#[derive(Serialize)]
struct UtxoValue {
txid: Sha256dHash,
vout: u32,
value: u64,
status: TransactionStatus,
}
impl From<FundingOutput> for UtxoValue {
fn from(out: FundingOutput) -> Self {
let FundingOutput { txn: t, txn_id, output_index, value, .. } = out;
let TxnHeight { height, blockhash, .. } = t;
UtxoValue {
txid: txn_id,
vout: output_index as u32,
value: value,
status: if height != 0 {
TransactionStatus { confirmed: true, block_height: Some(height), block_hash: Some(blockhash) }
} else {
TransactionStatus::unconfirmed()
}
}
}
}
fn get_script_asm(script: &Script) -> String {
let asm = format!("{:?}", script);
(&asm[7..asm.len()-1]).to_string()
@ -363,6 +388,13 @@ fn handle_request(req: Request<Body>, query: &Arc<Query>, network: &Network) ->
json_response(txs)
},
(&Method::GET, Some(&"address"), Some(address), Some(&"utxo"), None) => {
let script_hash = address_to_scripthash(address, &network)?;
let status = query.status(&script_hash[..])?;
let utxos: Vec<UtxoValue> = status.unspent().into_iter().map(|o| UtxoValue::from(o.clone())).collect();
// @XXX no paging, but query.status() is limited to 30 funding txs
json_response(utxos)
},
(&Method::GET, Some(&"tx"), Some(hash), None, None) => {
let hash = Sha256dHash::from_hex(hash)?;
let transaction = query.tx_get(&hash).ok_or(StringError("cannot find tx".to_string()))?;

Loading…
Cancel
Save