|
|
|
@ -4,15 +4,17 @@
|
|
|
|
|
#[macro_use] extern crate serde_derive;
|
|
|
|
|
extern crate reqwest;
|
|
|
|
|
mod data_types;
|
|
|
|
|
use rocket::http::RawStr;
|
|
|
|
|
use rocket::response::Redirect;
|
|
|
|
|
use rocket_contrib::json::{Json, JsonValue};
|
|
|
|
|
use rocket_contrib::templates::Template;
|
|
|
|
|
use rocket_contrib::serve::StaticFiles;
|
|
|
|
|
use reqwest::blocking::RequestBuilder;
|
|
|
|
|
use reqwest::blocking::{RequestBuilder, Client};
|
|
|
|
|
use std::env;
|
|
|
|
|
use data_types::*;
|
|
|
|
|
|
|
|
|
|
fn issue_rpc(method: &str, params: Option<RPCParams>) -> RequestBuilder {
|
|
|
|
|
let http_client = reqwest::blocking::Client::new();
|
|
|
|
|
let http_client = Client::new();
|
|
|
|
|
let url = format!(
|
|
|
|
|
"{}/json_rpc",
|
|
|
|
|
env::var("DAEMON_URI").unwrap()
|
|
|
|
@ -26,7 +28,7 @@ fn issue_rpc(method: &str, params: Option<RPCParams>) -> RequestBuilder {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn issue_raw_rpc(method: &str, params: JsonValue) -> RequestBuilder {
|
|
|
|
|
let http_client = reqwest::blocking::Client::new();
|
|
|
|
|
let http_client = Client::new();
|
|
|
|
|
let url = format!(
|
|
|
|
|
"{}/{}",
|
|
|
|
|
env::var("DAEMON_URI").unwrap(),
|
|
|
|
@ -35,9 +37,7 @@ fn issue_raw_rpc(method: &str, params: JsonValue) -> RequestBuilder {
|
|
|
|
|
http_client.post(&url).json(¶ms)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// /block
|
|
|
|
|
|
|
|
|
|
#[get("/hash/<block_hash>")]
|
|
|
|
|
#[get("/block/hash/<block_hash>")]
|
|
|
|
|
fn get_block_header_by_block_hash(block_hash: String) -> Json<BlockHeader> {
|
|
|
|
|
let params = RPCParams {
|
|
|
|
|
hash: Some(block_hash),
|
|
|
|
@ -48,7 +48,19 @@ fn get_block_header_by_block_hash(block_hash: String) -> Json<BlockHeader> {
|
|
|
|
|
Json(res.result.block_header)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[get("/tx/<tx_hash>")]
|
|
|
|
|
#[get("/block/height/<block_height>")]
|
|
|
|
|
fn get_block_by_height(block_height: String) -> String {
|
|
|
|
|
let params = RPCParams {
|
|
|
|
|
height: Some(block_height),
|
|
|
|
|
..Default::default()
|
|
|
|
|
};
|
|
|
|
|
let res: GetBlock = issue_rpc(&"get_block", Some(params))
|
|
|
|
|
.send().unwrap().json().unwrap();
|
|
|
|
|
// Json(res.result.block_header)
|
|
|
|
|
serde_json::to_string(&res).unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[get("/transaction/<tx_hash>")]
|
|
|
|
|
fn get_block_header_by_transaction_hash(tx_hash: String) -> Json<GetTransactions> {
|
|
|
|
|
let params: JsonValue = json!({"txs_hashes": [&tx_hash]});
|
|
|
|
|
let res: GetTransactions = issue_raw_rpc(&"get_transactions", params)
|
|
|
|
@ -56,20 +68,70 @@ fn get_block_header_by_transaction_hash(tx_hash: String) -> Json<GetTransactions
|
|
|
|
|
Json(res)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// /
|
|
|
|
|
#[get("/search?<value>")]
|
|
|
|
|
fn search(value: &RawStr) -> Redirect {
|
|
|
|
|
let sl: usize = value.len();
|
|
|
|
|
let first_byte = value.get(0..1).unwrap();
|
|
|
|
|
println!("Search value: {}", value);
|
|
|
|
|
println!("First byte: {}", first_byte);
|
|
|
|
|
|
|
|
|
|
#[get("/info")]
|
|
|
|
|
fn get_daemon_info() -> Json<GetInfoResult> {
|
|
|
|
|
let res: GetInfo = issue_rpc(&"get_info", None)
|
|
|
|
|
.send().unwrap().json().unwrap();
|
|
|
|
|
Json(res.result)
|
|
|
|
|
}
|
|
|
|
|
if sl < 10 {
|
|
|
|
|
match value.parse::<u32>() {
|
|
|
|
|
Ok(v) => {
|
|
|
|
|
println!("Found: {}", v);
|
|
|
|
|
// "this looks like a block height"
|
|
|
|
|
return Redirect::found(uri!(get_block_by_height: value.as_str()));
|
|
|
|
|
},
|
|
|
|
|
Err(e) => {
|
|
|
|
|
println!("Error: {}", e);
|
|
|
|
|
// "this is an invalid search query"
|
|
|
|
|
return Redirect::found(uri!(index));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if sl < 95 {
|
|
|
|
|
// "this looks like a tx or block hash"
|
|
|
|
|
return Redirect::found(uri!(index));
|
|
|
|
|
} else if sl == 95 {
|
|
|
|
|
match first_byte {
|
|
|
|
|
"9" => {
|
|
|
|
|
println!("This looks like a testnet address");
|
|
|
|
|
return Redirect::found(uri!(index));
|
|
|
|
|
},
|
|
|
|
|
"A" => {
|
|
|
|
|
println!("This looks like a testnet subaddress");
|
|
|
|
|
return Redirect::found(uri!(index));
|
|
|
|
|
},
|
|
|
|
|
"5" => {
|
|
|
|
|
println!("This looks like a stagenet address");
|
|
|
|
|
return Redirect::found(uri!(index));
|
|
|
|
|
},
|
|
|
|
|
"7" => {
|
|
|
|
|
println!("This looks like a stagenet subaddress");
|
|
|
|
|
return Redirect::found(uri!(index));
|
|
|
|
|
},
|
|
|
|
|
"4" => {
|
|
|
|
|
println!("This looks like a mainnet address");
|
|
|
|
|
return Redirect::found(uri!(index));
|
|
|
|
|
},
|
|
|
|
|
"8" => {
|
|
|
|
|
println!("This looks like a mainnet subaddress");
|
|
|
|
|
return Redirect::found(uri!(index));
|
|
|
|
|
},
|
|
|
|
|
_ => {
|
|
|
|
|
println!("Not sure what this is");
|
|
|
|
|
return Redirect::found(uri!(index));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if sl == 105 {
|
|
|
|
|
// "this looks like an integrated address"
|
|
|
|
|
return Redirect::found(uri!(index));
|
|
|
|
|
} else {
|
|
|
|
|
// "no idea what this is"
|
|
|
|
|
return Redirect::found(uri!(index));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[post("/", format = "application/x-www-form-urlencoded")]
|
|
|
|
|
fn something() -> Template {
|
|
|
|
|
let res: GetInfo = issue_rpc(&"get_info", None)
|
|
|
|
|
.send().unwrap().json().unwrap();
|
|
|
|
|
Template::render("search", &res.result)
|
|
|
|
|
// println!("No if stmt matched");
|
|
|
|
|
// return Redirect::found(uri!(index));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[get("/")]
|
|
|
|
@ -93,12 +155,11 @@ fn main() {
|
|
|
|
|
Ok(_) => {
|
|
|
|
|
rocket::ignite()
|
|
|
|
|
.mount("/", routes![
|
|
|
|
|
index, something,
|
|
|
|
|
get_daemon_info
|
|
|
|
|
])
|
|
|
|
|
.mount("/block", routes![
|
|
|
|
|
index,
|
|
|
|
|
search,
|
|
|
|
|
get_block_by_height,
|
|
|
|
|
get_block_header_by_block_hash,
|
|
|
|
|
get_block_header_by_transaction_hash
|
|
|
|
|
get_block_header_by_transaction_hash,
|
|
|
|
|
])
|
|
|
|
|
.mount("/static", StaticFiles::from("./static"))
|
|
|
|
|
.register(catchers![not_found])
|
|
|
|
|