use quoted_string::strip_dquotes; use serde_json::Value; use std::collections::HashMap; use std::fs; use cw_client::{CliClient, CwClient}; use reqwest::Url; use quartz_tcbinfo_msgs::ExecuteMsg; type TcbInfo = Value; type Fmspc = String; type Update = String; const TCB_SIGNER: &str = include_str!("../tcb_signer.pem"); struct ExecMsg { msg: ExecuteMsg } impl ToString for ExecMsg { fn to_string (&self) -> String { format!("\"tcb_info\": {0}, \"certificate\": {1}", self.msg.tcb_info, self.msg.certificate) } } fn get_tcbinfo(fmspc: Fmspc, update: Update) -> Value { let url = format!("https://api.trustedservices.intel.com/sgx/certification/v4/tcb?fmspc={fmspc}&update={update}"); let body: String = reqwest::blocking::get(url) .expect("url retrieval failed") .text() .expect("could not read https response"); let tcbinfo: Value = serde_json::from_str(&body).expect("could not convert to JSON"); tcbinfo } fn get_fmspc_list() -> Vec { let body: String = reqwest::blocking::get("https://api.trustedservices.intel.com/sgx/certification/v4/fmspcs") .expect("url retrieval failed") .text() .expect("could not read https response"); let fmspc_data: Vec = serde_json::from_str(&body).expect("could not convert to JSON"); let mut fmspc_list: Vec = Vec::new(); for item in fmspc_data.iter() { let fmspc: String = format!("{}", item["fmspc"]); fmspc_list.push(strip_dquotes(&fmspc).unwrap().to_string()); } println!("{:?}", fmspc_list); fmspc_list } fn upsert_tcbinfo() -> Result<(), &'static str> { let data = fs::read_to_string("./standard").expect("Unable to read file"); let mut store: HashMap = serde_json::from_str(&data).unwrap(); let fmspc_list = get_fmspc_list(); for fmspc in fmspc_list { let tcbinfo = get_tcbinfo(fmspc.clone(), "standard".to_string()); let store_entry = &store[&fmspc]; if *store_entry != tcbinfo { println!("updating local TCBInfo for FMSPC: {fmspc}"); store.insert(fmspc.clone(), tcbinfo.clone()); println!("updating on-chain TCBInfo for FMSPC: {fmspc}"); let testnet = Url::parse("https://rpc-falcron.pion-1.ntrn.tech").expect("couldn't parse network URL"); let contract_address = cosmrs::AccountId::new("neutrontcbinfo", "neutron1r4m59786vmxrx866585ze5ugjx9egcyja0nuxhn2y6d7ht6680sspa89zk".as_bytes()).expect("failed to parse contract address"); let chain_id = tendermint::chain::id::Id::try_from("pion-1").expect("invalid chain id"); let sender = "ajinkya"; let client = CliClient::neutrond(testnet); let execute_msg = ExecMsg { msg: ExecuteMsg { tcb_info: tcbinfo.to_string(), certificate: TCB_SIGNER.to_string(), time: None, }}; // if let Err(e) = let _ = client.tx_execute(&contract_address, &chain_id, 200000, &sender, execute_msg, "200000untrn"); // { // eprintln!("Error: {}", e); // } println!("done"); } else { println!("TCBInfo for FMSPC: {fmspc} up to date") } } let serialized = serde_json::to_string(&store).unwrap(); fs::write("./standard", serialized).expect("Unable to write file"); Ok(()) // } // } } // #[tokio::main] pub fn main() { upsert_tcbinfo().expect("TCBInfo update failed"); }