From 24c7d02b3d92a0e03545f969b1ad14b7950093c5 Mon Sep 17 00:00:00 2001 From: lalanza808 Date: Sat, 4 Apr 2020 23:49:57 -0700 Subject: [PATCH] improving json response parsing --- src/data_types.rs | 71 +++++++++++++++++++++------------------ src/main.rs | 14 ++++++-- templates/index.html.tera | 22 ++++++++++++ 3 files changed, 72 insertions(+), 35 deletions(-) diff --git a/src/data_types.rs b/src/data_types.rs index 686a6e0..0989f0b 100644 --- a/src/data_types.rs +++ b/src/data_types.rs @@ -180,35 +180,42 @@ pub struct Transactions { pub weight: u32 } -// #[derive(Serialize, Deserialize, Hash, Eq, PartialEq, Debug)] -// pub struct TransactionJSON { -// pub version: u32, -// pub unlock_time: u64, -// pub vin: TransactionInputs, -// pub vout: TransactionOutputs, -// pub extra: String, -// pub signatures: Vec -// } -// -// #[derive(Serialize, Deserialize, Hash, Eq, PartialEq, Debug)] -// pub struct TransactionInputs { -// pub pubkey: PreviousTransactionKey -// } -// -// #[derive(Serialize, Deserialize, Hash, Eq, PartialEq, Debug)] -// pub struct PreviousTransactionKey { -// pub amount: u32, -// pub key_offsets: Vec, -// pub k_image: String -// } -// -// #[derive(Serialize, Deserialize, Hash, Eq, PartialEq, Debug)] -// pub struct TransactionOutputs { -// pub amount: u32, -// pub target: OutputStealthAddress -// } -// -// #[derive(Serialize, Deserialize, Hash, Eq, PartialEq, Debug)] -// pub struct OutputStealthAddress { -// pub key: String -// } +#[derive(Serialize, Deserialize, Hash, Eq, PartialEq, Debug)] +pub struct TransactionJSON { + pub version: u32, + pub unlock_time: u64, + pub vin: Vec, + pub vout: Vec, + pub extra: Vec, + pub rct_signatures: RingSignatures +} + +#[derive(Serialize, Deserialize, Hash, Eq, PartialEq, Debug)] +pub struct TransactionInputs { + pub key: PreviousTransactionKey +} + +#[derive(Serialize, Deserialize, Hash, Eq, PartialEq, Debug)] +pub struct PreviousTransactionKey { + pub amount: u32, + pub key_offsets: Vec, + pub k_image: String +} + +#[derive(Serialize, Deserialize, Hash, Eq, PartialEq, Debug)] +pub struct TransactionOutputs { + pub amount: u32, + pub target: OutputStealthAddress +} + +#[derive(Serialize, Deserialize, Hash, Eq, PartialEq, Debug)] +pub struct OutputStealthAddress { + pub key: String +} + +#[derive(Serialize, Deserialize, Hash, Eq, PartialEq, Debug)] +pub struct RingSignatures { + pub r#type: u32, + pub txnFee: u64, + pub outPk: Vec +} diff --git a/src/main.rs b/src/main.rs index 1d71cff..589ea19 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ mod data_types; use rocket::http::RawStr; use rocket::response::Redirect; -use rocket_contrib::json::{Json, JsonValue}; +use rocket_contrib::json::JsonValue; use rocket_contrib::templates::Template; use rocket_contrib::serve::StaticFiles; use reqwest::blocking::{RequestBuilder, Client}; @@ -152,13 +152,21 @@ fn index() -> Template { let daemon_info: GetInfo = issue_rpc(&"get_info", None) .send().unwrap().json().unwrap(); - let tx_pool: GetTransactionPool = build_rpc( + let mut tx_pool: GetTransactionPool = build_rpc( &"get_transaction_pool", None, true ).send().unwrap().json().unwrap(); + let mut tx_json_raw: Vec = vec![]; + + for f in &mut tx_pool.transactions { + let j: TransactionJSON = serde_json::from_str(&f.tx_json).unwrap(); + tx_json_raw.push(j) + }; + let context: JsonValue = json!({ "daemon_info": daemon_info.result, - "tx_pool": tx_pool.transactions + "tx_pool": tx_pool.transactions, + "tx_json": tx_json_raw }); Template::render("index", context) diff --git a/templates/index.html.tera b/templates/index.html.tera index 52e3534..77cc120 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -92,6 +92,27 @@ Amount Fee + {% for tx in tx_json %} + +

Ring Sig Type: {{ tx.rct_signatures.type }}

+

Tx Fee: {{ tx.rct_signatures.txnFee }}

+

Extra: {{ tx.extra }}

+

Tx Inputs

+ {{ tx.vin | length }} + {% for vin in tx.vin %} +

Amount: {{ vin.key.amount / 1000000000000 }} XMR

+

Key Image: {{ vin.key.k_image }}

+

Key Offsets: {{ vin.key.key_offsets }}

+ {% endfor %} +

Tx Outputs

+ {% for vout in tx.vout %} +

Amount: {{ vout.amount / 1000000000000 }} XMR

+

Stealth Address: {{ vout.target.key }}

+ {% endfor %} + + {% endfor %} + + {% comment %} {% for tx in tx_pool %} {{ tx.id_hash | truncate(length=8) }} @@ -99,6 +120,7 @@ {{ tx.fee / 1000000000000 }} XMR {% endfor %} + {% endcomment %}