2024-12-04 20:35:35 +00:00
|
|
|
use cw_client::{CliClient, CwClient};
|
2024-12-04 19:22:13 +00:00
|
|
|
use mc_attestation_verifier::SignedTcbInfo;
|
|
|
|
use p256::ecdsa::VerifyingKey;
|
2024-12-05 13:36:19 +00:00
|
|
|
use quartz_tcbinfo_msgs::{QueryMsg, ExecuteMsg};
|
2024-12-04 20:35:35 +00:00
|
|
|
use quoted_string::strip_dquotes;
|
|
|
|
use reqwest::Url;
|
2024-12-03 10:02:58 +00:00
|
|
|
use serde_json::{json, Value};
|
2024-12-04 21:00:18 +00:00
|
|
|
|
2024-12-05 13:29:35 +00:00
|
|
|
|
2024-11-29 19:07:22 +00:00
|
|
|
type Fmspc = String;
|
|
|
|
type Update = String;
|
|
|
|
|
2024-11-29 19:07:22 +00:00
|
|
|
const TCB_SIGNER: &str = include_str!("../tcb_signer.pem");
|
|
|
|
|
2024-12-04 19:48:38 +00:00
|
|
|
async fn get_tcbinfo(fmspc: Fmspc, update: Update) -> String {
|
2024-11-29 19:07:22 +00:00
|
|
|
let url = format!("https://api.trustedservices.intel.com/sgx/certification/v4/tcb?fmspc={fmspc}&update={update}");
|
2024-12-03 09:13:37 +00:00
|
|
|
let body: String = reqwest::get(url)
|
|
|
|
.await
|
2024-11-29 19:07:22 +00:00
|
|
|
.expect("url retrieval failed")
|
|
|
|
.text()
|
2024-12-03 09:38:50 +00:00
|
|
|
.await
|
2024-11-29 19:07:22 +00:00
|
|
|
.expect("could not read https response");
|
2024-12-04 20:35:35 +00:00
|
|
|
body
|
2024-11-29 19:07:22 +00:00
|
|
|
}
|
|
|
|
|
2024-12-03 09:13:37 +00:00
|
|
|
async fn get_fmspc_list() -> Vec<Fmspc> {
|
2024-11-29 19:07:22 +00:00
|
|
|
let body: String =
|
2024-12-04 20:35:35 +00:00
|
|
|
reqwest::get("https://api.trustedservices.intel.com/sgx/certification/v4/fmspcs")
|
|
|
|
.await
|
|
|
|
.expect("url retrieval failed")
|
|
|
|
.text()
|
|
|
|
.await
|
|
|
|
.expect("could not read https response");
|
2024-11-29 19:07:22 +00:00
|
|
|
let fmspc_data: Vec<Value> = serde_json::from_str(&body).expect("could not convert to JSON");
|
|
|
|
let mut fmspc_list: Vec<Fmspc> = 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
|
|
|
|
}
|
|
|
|
|
2024-12-04 20:35:35 +00:00
|
|
|
async fn upsert_tcbinfo() -> Result<(), &'static str> {
|
2024-12-05 12:03:39 +00:00
|
|
|
|
|
|
|
let testnet = Url::parse("https://rpc-falcron.pion-1.ntrn.tech").expect("couldn't parse network URL");
|
|
|
|
|
|
|
|
let client = CliClient::neutrond(testnet);
|
|
|
|
let fmspc_list = get_fmspc_list().await;
|
|
|
|
|
2024-11-29 19:07:22 +00:00
|
|
|
for fmspc in fmspc_list {
|
2024-12-05 12:03:39 +00:00
|
|
|
|
|
|
|
let tcbinfo_from_api = get_tcbinfo(fmspc.clone(), "standard".to_string()).await;
|
|
|
|
// assert!(verify_signature(tcbinfo.clone(), key));
|
|
|
|
let contract_address =
|
|
|
|
"neutron1r4m59786vmxrx866585ze5ugjx9egcyja0nuxhn2y6d7ht6680sspa89zk"
|
|
|
|
.parse()
|
|
|
|
.expect("failed to parse contract address");
|
2024-12-05 13:37:29 +00:00
|
|
|
let query_msg = QueryMsg::GetTcbInfo { fmspc };
|
2024-12-05 13:29:35 +00:00
|
|
|
let tcbinfo_on_chain: Value = client.query_smart(&contract_address, json!(query_msg)).await.expect("contract query failed");
|
|
|
|
println!("{tcbinfo_on_chain:?}");
|
|
|
|
|
|
|
|
if tcbinfo_on_chain.to_string() != tcbinfo_from_api {
|
2024-11-29 19:07:22 +00:00
|
|
|
println!("updating on-chain TCBInfo for FMSPC: {fmspc}");
|
|
|
|
let chain_id = tendermint::chain::id::Id::try_from("pion-1").expect("invalid chain id");
|
2024-11-29 19:07:22 +00:00
|
|
|
let sender = "ajinkya";
|
2024-12-05 12:03:39 +00:00
|
|
|
|
2024-12-03 09:58:52 +00:00
|
|
|
let execute_msg = ExecuteMsg {
|
2024-12-05 13:29:35 +00:00
|
|
|
tcb_info: tcbinfo_from_api.to_string(),
|
|
|
|
certificate: TCB_SIGNER.to_string(),
|
2024-11-29 19:07:22 +00:00
|
|
|
time: None,
|
2024-12-03 10:01:17 +00:00
|
|
|
};
|
2024-12-05 13:29:35 +00:00
|
|
|
let _ = client
|
2024-12-04 20:35:35 +00:00
|
|
|
.tx_execute(
|
|
|
|
&contract_address,
|
|
|
|
&chain_id,
|
2024-12-05 11:26:03 +00:00
|
|
|
1000000,
|
|
|
|
sender,
|
2024-12-04 20:35:35 +00:00
|
|
|
json!(execute_msg),
|
|
|
|
"11000untrn",
|
|
|
|
)
|
|
|
|
.await;
|
2024-12-05 12:03:39 +00:00
|
|
|
println!("done");
|
2024-12-03 10:42:57 +00:00
|
|
|
std::thread::sleep(std::time::Duration::from_secs(5));
|
2024-12-04 20:35:35 +00:00
|
|
|
} else {
|
2024-11-29 19:07:22 +00:00
|
|
|
println!("TCBInfo for FMSPC: {fmspc} up to date")
|
2024-11-29 19:07:22 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-29 19:07:22 +00:00
|
|
|
Ok(())
|
2024-11-29 19:07:22 +00:00
|
|
|
}
|
2024-11-29 19:07:22 +00:00
|
|
|
|
2024-12-05 13:29:35 +00:00
|
|
|
|
2024-12-04 19:22:13 +00:00
|
|
|
|
2024-12-03 09:13:37 +00:00
|
|
|
#[tokio::main]
|
|
|
|
pub async fn main() {
|
|
|
|
upsert_tcbinfo().await.expect("TCBInfo update failed");
|
2024-11-29 19:07:22 +00:00
|
|
|
}
|