|
|
@ -4,15 +4,17 @@
|
|
|
|
#[macro_use] extern crate serde_derive;
|
|
|
|
#[macro_use] extern crate serde_derive;
|
|
|
|
extern crate reqwest;
|
|
|
|
extern crate reqwest;
|
|
|
|
mod data_types;
|
|
|
|
mod data_types;
|
|
|
|
|
|
|
|
use rocket::http::RawStr;
|
|
|
|
|
|
|
|
use rocket::response::Redirect;
|
|
|
|
use rocket_contrib::json::{Json, JsonValue};
|
|
|
|
use rocket_contrib::json::{Json, JsonValue};
|
|
|
|
use rocket_contrib::templates::Template;
|
|
|
|
use rocket_contrib::templates::Template;
|
|
|
|
use rocket_contrib::serve::StaticFiles;
|
|
|
|
use rocket_contrib::serve::StaticFiles;
|
|
|
|
use reqwest::blocking::RequestBuilder;
|
|
|
|
use reqwest::blocking::{RequestBuilder, Client};
|
|
|
|
use std::env;
|
|
|
|
use std::env;
|
|
|
|
use data_types::*;
|
|
|
|
use data_types::*;
|
|
|
|
|
|
|
|
|
|
|
|
fn issue_rpc(method: &str, params: Option<RPCParams>) -> RequestBuilder {
|
|
|
|
fn issue_rpc(method: &str, params: Option<RPCParams>) -> RequestBuilder {
|
|
|
|
let http_client = reqwest::blocking::Client::new();
|
|
|
|
let http_client = Client::new();
|
|
|
|
let url = format!(
|
|
|
|
let url = format!(
|
|
|
|
"{}/json_rpc",
|
|
|
|
"{}/json_rpc",
|
|
|
|
env::var("DAEMON_URI").unwrap()
|
|
|
|
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 {
|
|
|
|
fn issue_raw_rpc(method: &str, params: JsonValue) -> RequestBuilder {
|
|
|
|
let http_client = reqwest::blocking::Client::new();
|
|
|
|
let http_client = Client::new();
|
|
|
|
let url = format!(
|
|
|
|
let url = format!(
|
|
|
|
"{}/{}",
|
|
|
|
"{}/{}",
|
|
|
|
env::var("DAEMON_URI").unwrap(),
|
|
|
|
env::var("DAEMON_URI").unwrap(),
|
|
|
@ -35,9 +37,7 @@ fn issue_raw_rpc(method: &str, params: JsonValue) -> RequestBuilder {
|
|
|
|
http_client.post(&url).json(¶ms)
|
|
|
|
http_client.post(&url).json(¶ms)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// /block
|
|
|
|
#[get("/block/hash/<block_hash>")]
|
|
|
|
|
|
|
|
|
|
|
|
#[get("/hash/<block_hash>")]
|
|
|
|
|
|
|
|
fn get_block_header_by_block_hash(block_hash: String) -> Json<BlockHeader> {
|
|
|
|
fn get_block_header_by_block_hash(block_hash: String) -> Json<BlockHeader> {
|
|
|
|
let params = RPCParams {
|
|
|
|
let params = RPCParams {
|
|
|
|
hash: Some(block_hash),
|
|
|
|
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)
|
|
|
|
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> {
|
|
|
|
fn get_block_header_by_transaction_hash(tx_hash: String) -> Json<GetTransactions> {
|
|
|
|
let params: JsonValue = json!({"txs_hashes": [&tx_hash]});
|
|
|
|
let params: JsonValue = json!({"txs_hashes": [&tx_hash]});
|
|
|
|
let res: GetTransactions = issue_raw_rpc(&"get_transactions", params)
|
|
|
|
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)
|
|
|
|
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")]
|
|
|
|
if sl < 10 {
|
|
|
|
fn get_daemon_info() -> Json<GetInfoResult> {
|
|
|
|
match value.parse::<u32>() {
|
|
|
|
let res: GetInfo = issue_rpc(&"get_info", None)
|
|
|
|
Ok(v) => {
|
|
|
|
.send().unwrap().json().unwrap();
|
|
|
|
println!("Found: {}", v);
|
|
|
|
Json(res.result)
|
|
|
|
// "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")]
|
|
|
|
// println!("No if stmt matched");
|
|
|
|
fn something() -> Template {
|
|
|
|
// return Redirect::found(uri!(index));
|
|
|
|
let res: GetInfo = issue_rpc(&"get_info", None)
|
|
|
|
|
|
|
|
.send().unwrap().json().unwrap();
|
|
|
|
|
|
|
|
Template::render("search", &res.result)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[get("/")]
|
|
|
|
#[get("/")]
|
|
|
@ -93,12 +155,11 @@ fn main() {
|
|
|
|
Ok(_) => {
|
|
|
|
Ok(_) => {
|
|
|
|
rocket::ignite()
|
|
|
|
rocket::ignite()
|
|
|
|
.mount("/", routes![
|
|
|
|
.mount("/", routes![
|
|
|
|
index, something,
|
|
|
|
index,
|
|
|
|
get_daemon_info
|
|
|
|
search,
|
|
|
|
])
|
|
|
|
get_block_by_height,
|
|
|
|
.mount("/block", routes![
|
|
|
|
|
|
|
|
get_block_header_by_block_hash,
|
|
|
|
get_block_header_by_block_hash,
|
|
|
|
get_block_header_by_transaction_hash
|
|
|
|
get_block_header_by_transaction_hash,
|
|
|
|
])
|
|
|
|
])
|
|
|
|
.mount("/static", StaticFiles::from("./static"))
|
|
|
|
.mount("/static", StaticFiles::from("./static"))
|
|
|
|
.register(catchers![not_found])
|
|
|
|
.register(catchers![not_found])
|
|
|
|