diff --git a/src/data_types.rs b/src/data_types.rs index 385b0db..26043fe 100644 --- a/src/data_types.rs +++ b/src/data_types.rs @@ -20,14 +20,16 @@ impl Default for RPCPayload { #[derive(Serialize, Deserialize)] pub struct RPCParams { pub hash: Option, - pub txs_hashes: Option> + pub txs_hashes: Option>, + pub height: Option } impl Default for RPCParams { fn default() -> RPCParams { RPCParams { hash: None, - txs_hashes: None + txs_hashes: None, + height: None } } } @@ -105,6 +107,7 @@ pub struct BlockHeader { pub height: u32, pub major_version: u8, pub minor_version: u8, + pub miner_tx_hash: Option, pub nonce: u32, pub num_txes: u32, pub orphan_status: bool, @@ -126,3 +129,19 @@ pub struct GetTransactionsTxs { pub in_pool: bool, pub output_indices: Vec, } + +#[derive(Serialize, Deserialize)] +pub struct GetBlock { + pub id: String, + pub jsonrpc: String, + pub result: GetBlockResult +} + +#[derive(Serialize, Deserialize)] +pub struct GetBlockResult { + pub blob: String, + pub block_header: BlockHeader, + pub credits: u8, + pub miner_tx_hash: String, + pub tx_hashes: Option>, +} diff --git a/src/main.rs b/src/main.rs index 696ddc8..2026a4c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) -> 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) -> 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/")] +#[get("/block/hash/")] fn get_block_header_by_block_hash(block_hash: String) -> Json { let params = RPCParams { hash: Some(block_hash), @@ -48,7 +48,19 @@ fn get_block_header_by_block_hash(block_hash: String) -> Json { Json(res.result.block_header) } -#[get("/tx/")] +#[get("/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/")] fn get_block_header_by_transaction_hash(tx_hash: String) -> Json { 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")] +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 { - let res: GetInfo = issue_rpc(&"get_info", None) - .send().unwrap().json().unwrap(); - Json(res.result) -} + if sl < 10 { + match value.parse::() { + 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]) diff --git a/templates/index.html.tera b/templates/index.html.tera index 9e051e2..125d18f 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -12,8 +12,8 @@ @@ -32,7 +32,8 @@

Previous Block

- +

wide_difficulty: {{ wide_difficulty }}

diff --git a/templates/search.html.tera b/templates/search.html.tera deleted file mode 100644 index 45b983b..0000000 --- a/templates/search.html.tera +++ /dev/null @@ -1 +0,0 @@ -hi