|
|
@ -20,7 +20,9 @@ use std::num::ParseIntError; |
|
|
|
use std::str::FromStr; |
|
|
|
use std::sync::Arc; |
|
|
|
use std::thread; |
|
|
|
use util::{get_script_asm, script_to_address, full_hash, BlockHeaderMeta, FullHash, TransactionStatus}; |
|
|
|
use util::{ |
|
|
|
full_hash, get_script_asm, script_to_address, BlockHeaderMeta, FullHash, TransactionStatus, |
|
|
|
}; |
|
|
|
|
|
|
|
const TX_LIMIT: usize = 25; |
|
|
|
const BLOCK_LIMIT: usize = 10; |
|
|
@ -487,12 +489,13 @@ fn handle_request( |
|
|
|
query |
|
|
|
.load_txn(&txid, Some(&hash)) |
|
|
|
.map(TransactionValue::from) |
|
|
|
}).collect::<Result<Vec<TransactionValue>, _>>()?; |
|
|
|
}) |
|
|
|
.collect::<Result<Vec<TransactionValue>, _>>()?; |
|
|
|
attach_txs_data(&mut txs, config, query); |
|
|
|
json_response(txs, TTL_LONG) |
|
|
|
} |
|
|
|
(&Method::GET, Some(script_type @ &"address"), Some(script_str), None, None) | |
|
|
|
(&Method::GET, Some(script_type @ &"scripthash"), Some(script_str), None, None) => { |
|
|
|
(&Method::GET, Some(script_type @ &"address"), Some(script_str), None, None) |
|
|
|
| (&Method::GET, Some(script_type @ &"scripthash"), Some(script_str), None, None) => { |
|
|
|
// @TODO create new AddressStatsValue struct?
|
|
|
|
let script_hash = to_scripthash(script_type, script_str, &config.network_type)?; |
|
|
|
match query.status(&script_hash[..]) { |
|
|
@ -517,8 +520,20 @@ fn handle_request( |
|
|
|
Err(err) => bail!(err), |
|
|
|
} |
|
|
|
} |
|
|
|
(&Method::GET, Some(script_type @ &"address"), Some(script_str), Some(&"txs"), start_index) | |
|
|
|
(&Method::GET, Some(script_type @ &"scripthash"), Some(script_str), Some(&"txs"), start_index) => { |
|
|
|
( |
|
|
|
&Method::GET, |
|
|
|
Some(script_type @ &"address"), |
|
|
|
Some(script_str), |
|
|
|
Some(&"txs"), |
|
|
|
start_index, |
|
|
|
) |
|
|
|
| ( |
|
|
|
&Method::GET, |
|
|
|
Some(script_type @ &"scripthash"), |
|
|
|
Some(script_str), |
|
|
|
Some(&"txs"), |
|
|
|
start_index, |
|
|
|
) => { |
|
|
|
let start_index = start_index |
|
|
|
.map_or(0u32, |el| el.parse().unwrap_or(0)) |
|
|
|
.max(0u32) as usize; |
|
|
@ -548,8 +563,14 @@ fn handle_request( |
|
|
|
|
|
|
|
json_response(txs, TTL_SHORT) |
|
|
|
} |
|
|
|
(&Method::GET, Some(script_type @ &"address"), Some(script_str), Some(&"utxo"), None) | |
|
|
|
(&Method::GET, Some(script_type @ &"scripthash"), Some(script_str), Some(&"utxo"), None) => { |
|
|
|
(&Method::GET, Some(script_type @ &"address"), Some(script_str), Some(&"utxo"), None) |
|
|
|
| ( |
|
|
|
&Method::GET, |
|
|
|
Some(script_type @ &"scripthash"), |
|
|
|
Some(script_str), |
|
|
|
Some(&"utxo"), |
|
|
|
None, |
|
|
|
) => { |
|
|
|
let script_hash = to_scripthash(script_type, script_str, &config.network_type)?; |
|
|
|
let status = query.status(&script_hash[..])?; |
|
|
|
let utxos: Vec<UtxoValue> = status |
|
|
@ -629,7 +650,8 @@ fn handle_request( |
|
|
|
|| SpendingValue::default(), |
|
|
|
|spend| SpendingValue::from(spend), |
|
|
|
) |
|
|
|
}).collect(); |
|
|
|
}) |
|
|
|
.collect(); |
|
|
|
// @TODO long ttl if all outputs are either spent long ago or unspendable
|
|
|
|
json_response(spends, TTL_SHORT) |
|
|
|
} |
|
|
@ -687,11 +709,15 @@ fn blocks(query: &Arc<Query>, start_height: Option<usize>) -> Result<Response<Bo |
|
|
|
json_response(values, TTL_SHORT) |
|
|
|
} |
|
|
|
|
|
|
|
fn to_scripthash(script_type: &str, script_str: &str, network: &Network) -> Result<FullHash, HttpError> { |
|
|
|
fn to_scripthash( |
|
|
|
script_type: &str, |
|
|
|
script_str: &str, |
|
|
|
network: &Network, |
|
|
|
) -> Result<FullHash, HttpError> { |
|
|
|
match script_type { |
|
|
|
"address" => address_to_scripthash(script_str, network), |
|
|
|
"scripthash" => Ok(full_hash(&hex::decode(script_str)?)), |
|
|
|
_ => bail!("Invalid script type".to_string()) |
|
|
|
_ => bail!("Invalid script type".to_string()), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -824,7 +850,8 @@ mod tests { |
|
|
|
.and_then(|el| el.as_u64()) |
|
|
|
.ok_or(HttpError::from( |
|
|
|
"confirmations absent or not a u64".to_string(), |
|
|
|
)).unwrap(); |
|
|
|
)) |
|
|
|
.unwrap(); |
|
|
|
|
|
|
|
assert_eq!(10, confirmations); |
|
|
|
|
|
|
|