|
|
@ -19,7 +19,8 @@ use std::num::ParseIntError; |
|
|
|
use std::thread; |
|
|
|
use std::sync::{Arc,Mutex}; |
|
|
|
use url::form_urlencoded; |
|
|
|
use util::HeaderEntry; |
|
|
|
use daemon::Network; |
|
|
|
use util::{HeaderEntry,script_to_address}; |
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)] |
|
|
|
struct BlockValue { |
|
|
@ -127,6 +128,7 @@ impl From<TxIn> for TxInValue { |
|
|
|
struct TxOutValue { |
|
|
|
scriptpubkey_hex: Script, |
|
|
|
scriptpubkey_asm: String, |
|
|
|
scriptpubkey_address: Option<String>, |
|
|
|
asset: Option<String>, |
|
|
|
assetcommitment: Option<String>, |
|
|
|
value: Option<u64>, |
|
|
@ -173,6 +175,7 @@ impl From<TxOut> for TxOutValue { |
|
|
|
TxOutValue { |
|
|
|
scriptpubkey_hex: script, |
|
|
|
scriptpubkey_asm: (&script_asm[7..script_asm.len()-1]).to_string(), |
|
|
|
scriptpubkey_address: None, // added later
|
|
|
|
asset, |
|
|
|
assetcommitment, |
|
|
|
value, |
|
|
@ -181,19 +184,36 @@ impl From<TxOut> for TxOutValue { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
pub fn run_server(_config: &Config, query: Arc<Query>) { |
|
|
|
// @XXX we should ideally set the scriptpubkey_address inside TxOutValue::from(), but it
|
|
|
|
// cannot easily access the Network, so for now, we attach it later with mutation instead
|
|
|
|
|
|
|
|
fn attach_addresses_tx(tx: &mut TransactionValue, network: &Network) { |
|
|
|
for mut vout in tx.vout.iter_mut() { |
|
|
|
vout.scriptpubkey_address = script_to_address(&vout.scriptpubkey_hex, &network); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
fn attach_addresses_txs(txs: &mut Vec<TransactionValue>, network: &Network) { |
|
|
|
for mut tx in txs.iter_mut() { |
|
|
|
attach_addresses_tx(&mut tx, &network); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
pub fn run_server(config: &Config, query: Arc<Query>) { |
|
|
|
let addr = ([127, 0, 0, 1], 3000).into(); // TODO take from config
|
|
|
|
info!("REST server running on {}", addr); |
|
|
|
|
|
|
|
let cache = Arc::new(Mutex::new(LruCache::new(100))); |
|
|
|
|
|
|
|
let network = config.network_type; |
|
|
|
|
|
|
|
let new_service = move || { |
|
|
|
|
|
|
|
let query = query.clone(); |
|
|
|
let cache = cache.clone(); |
|
|
|
|
|
|
|
service_fn_ok(move |req: Request<Body>| { |
|
|
|
match handle_request(req,&query,&cache) { |
|
|
|
match handle_request(req,&query,&cache,&network) { |
|
|
|
Ok(response) => response, |
|
|
|
Err(e) => { |
|
|
|
warn!("{:?}",e); |
|
|
@ -212,7 +232,7 @@ pub fn run_server(_config: &Config, query: Arc<Query>) { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
fn handle_request(req: Request<Body>, query: &Arc<Query>, cache: &Arc<Mutex<LruCache<Sha256dHash, Block>>>) -> Result<Response<Body>, StringError> { |
|
|
|
fn handle_request(req: Request<Body>, query: &Arc<Query>, cache: &Arc<Mutex<LruCache<Sha256dHash, Block>>>, network: &Network) -> Result<Response<Body>, StringError> { |
|
|
|
// TODO it looks hyper does not have routing and query parsing :(
|
|
|
|
let uri = req.uri(); |
|
|
|
let path: Vec<&str> = uri.path().split('/').skip(1).collect(); |
|
|
@ -264,6 +284,7 @@ fn handle_request(req: Request<Body>, query: &Arc<Query>, cache: &Arc<Mutex<LruC |
|
|
|
tx_value.confirmations = value.block_summary.confirmations; |
|
|
|
tx_value.block_hash = Some(value.block_summary.id.clone()); |
|
|
|
} |
|
|
|
attach_addresses_txs(&mut value.txs, &network); |
|
|
|
json_response(value) |
|
|
|
} |
|
|
|
} |
|
|
@ -285,6 +306,7 @@ fn handle_request(req: Request<Body>, query: &Arc<Query>, cache: &Arc<Mutex<LruC |
|
|
|
tx_value.confirmations = Some(confirmations); |
|
|
|
tx_value.block_hash = Some(block_hash.clone()); |
|
|
|
} |
|
|
|
attach_addresses_txs(&mut value.txs, &network); |
|
|
|
json_response(value) |
|
|
|
}, |
|
|
|
(&Method::GET, Some(&"tx"), Some(hash), None) => { |
|
|
@ -304,6 +326,7 @@ fn handle_request(req: Request<Body>, query: &Arc<Query>, cache: &Arc<Mutex<LruC |
|
|
|
value.confirmations = Some(confirmations as u32); |
|
|
|
value.hex = Some(tx_hex.to_string()); |
|
|
|
value.block_hash = Some(blockhash.to_string()); |
|
|
|
attach_addresses_tx(&mut value, &network); |
|
|
|
json_response(value) |
|
|
|
}, |
|
|
|
_ => { |
|
|
|