verity_verify_remote/
config.rs

1use ic_agent::Agent;
2use ic_agent::{export::Principal, identity::Secp256k1Identity};
3use serde::{Deserialize, Serialize};
4
5use crate::ic::{DEFAULT_IC_GATEWAY_MAINNET, DEFAULT_IC_GATEWAY_MAINNET_TRAILING_SLASH};
6
7#[derive(Clone, Debug, Serialize, Deserialize)]
8#[serde(rename_all = "kebab-case")]
9pub struct Config {
10    /// The URL of the ICP server to connect to
11    pub url: String,
12    /// The path to the pem keyfile to generate an identity from
13    pub keyfile_path: String,
14    /// is this dev or prod env
15    pub is_dev: bool,
16    /// the principal of the calling canister
17    pub canister_principal: Principal,
18}
19
20impl Config {
21    pub fn new(rpc_url: String, keyfile_path: String, verifier_canister_principal: String) -> Self {
22        let is_mainnet = matches!(
23            &rpc_url[..],
24            DEFAULT_IC_GATEWAY_MAINNET | DEFAULT_IC_GATEWAY_MAINNET_TRAILING_SLASH
25        );
26
27        Self {
28            url: rpc_url,
29            keyfile_path,
30            canister_principal: Principal::from_text(verifier_canister_principal).unwrap(),
31            is_dev: !is_mainnet,
32        }
33    }
34
35    pub async fn create_agent(&self) -> anyhow::Result<Agent> {
36        let identity = Secp256k1Identity::from_pem_file(&self.keyfile_path)?;
37        let agent = Agent::builder()
38            .with_transport(ic_agent::agent::http_transport::ReqwestTransport::create(
39                self.url.clone(),
40            )?)
41            .with_boxed_identity(Box::new(identity))
42            .with_verify_query_signatures(true)
43            .build()?;
44
45        if self.is_dev {
46            agent.fetch_root_key().await?;
47        }
48
49        Ok(agent)
50    }
51}