Skip to content

Commit

Permalink
Update dkim client.
Browse files Browse the repository at this point in the history
  • Loading branch information
SoraSuegami committed Nov 16, 2023
1 parent c63ca25 commit 3874662
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions packages/relayer/src/dkim_oracle.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use ic_utils::*;
use ic_utils::canister::*;
use anyhow::anyhow;
use candid::utils::encode_args;
use candid::{CandidType, Principal};
use ic_agent::agent::*;
use ic_agent::identity::*;
use ic_utils::canister::*;
use ic_utils::interfaces::wallet::*;
use candid::utils::{encode_args};
use candid::{CandidType, Principal};
use ic_utils::*;
use serde::{Deserialize, Serialize};
use anyhow::{anyhow};



#[derive(Debug, Clone)]
pub struct DkimOracleClient<'a> {
pub(crate) canister: Canister<'a>

pub(crate) canister: Canister<'a>,
}

#[derive(Default, CandidType, Deserialize, Debug, Clone)]
Expand All @@ -27,26 +24,36 @@ pub struct SignedDkimPublicKey {
}

impl<'a> DkimOracleClient<'a> {
pub fn gen_agent(pem_path:&str) -> anyhow::Result<Agent> {
let identity = BasicIdentity::from_pem_file(pem_path)?;
pub fn gen_agent(pem_path: &str) -> anyhow::Result<Agent> {
let identity = Secp256k1Identity::from_pem_file(pem_path)?;
let agent = AgentBuilder::default().with_identity(identity).build()?;
Ok(agent)
}

pub fn new(canister_id: &str, agent:&'a Agent) -> anyhow::Result<Self> {
let canister = CanisterBuilder::new().with_canister_id(canister_id).with_agent(&agent).build()?;
Ok(Self {
canister
})
pub fn new(canister_id: &str, agent: &'a Agent) -> anyhow::Result<Self> {
let canister = CanisterBuilder::new()
.with_canister_id(canister_id)
.with_agent(&agent)
.build()?;
Ok(Self { canister })
}

pub async fn request_signature(&self, selector: &str, domain: &str) -> anyhow::Result<SignedDkimPublicKey> {
let request = self.canister.update("sign_dkim_public_key").with_arg(&encode_args((selector, domain))?).build::<(Result<SignedDkimPublicKey, String>,)>();
let response = request.call_and_wait_one::<Result<SignedDkimPublicKey, String>>().await?.map_err(|e| anyhow!(format!("Error from canister: {:?}", e)))?;

pub async fn request_signature(
&self,
selector: &str,
domain: &str,
) -> anyhow::Result<SignedDkimPublicKey> {
let request = self
.canister
.update("sign_dkim_public_key")
.with_arg(&encode_args((selector, domain))?)
.build::<(Result<SignedDkimPublicKey, String>,)>();
let response = request
.call_and_wait_one::<Result<SignedDkimPublicKey, String>>()
.await?
.map_err(|e| anyhow!(format!("Error from canister: {:?}", e)))?;

// let result = Decode!(&response, String)?;
Ok(response)
}


}
}

0 comments on commit 3874662

Please sign in to comment.