Skip to content

Commit

Permalink
Support new dkim registry
Browse files Browse the repository at this point in the history
  • Loading branch information
SoraSuegami committed Nov 13, 2024
1 parent 3aa3a00 commit c98501b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 38 deletions.
16 changes: 13 additions & 3 deletions packages/relayer/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@ fn main() {
.write_to_file("./src/abis/email_auth.rs")
.unwrap();

// Abigen::new(
// "ECDSAOwnedDKIMRegistry",
// "../contracts/artifacts/ECDSAOwnedDKIMRegistry.sol/ECDSAOwnedDKIMRegistry.json",
// )
// .unwrap()
// .generate()
// .unwrap()
// .write_to_file("./src/abis/ecdsa_owned_dkim_registry.rs")
// .unwrap();

Abigen::new(
"ECDSAOwnedDKIMRegistry",
"../contracts/artifacts/ECDSAOwnedDKIMRegistry.sol/ECDSAOwnedDKIMRegistry.json",
"UserOverridableDKIMRegistry",
"../contracts/artifacts/UserOverrideableDKIMRegistry.sol/UserOverrideableDKIMRegistry.json",
)
.unwrap()
.generate()
.unwrap()
.write_to_file("./src/abis/ecdsa_owned_dkim_registry.rs")
.write_to_file("./src/abis/user_overridable_dkim_registry.rs")
.unwrap();
}
4 changes: 2 additions & 2 deletions packages/relayer/src/abis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@


#[cfg_attr(rustfmt, rustfmt::skip)]
pub mod ecdsa_owned_dkim_registry;
pub mod user_overridable_dkim_registry;

#[cfg_attr(rustfmt, rustfmt::skip)]
pub mod email_auth;

pub use ecdsa_owned_dkim_registry::ECDSAOwnedDKIMRegistry;
pub use email_auth::*;
pub use user_overridable_dkim_registry::UserOverridableDKIMRegistry;
17 changes: 10 additions & 7 deletions packages/relayer/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashMap, time::Duration};

use crate::*;
use abi::{Abi, Token, Tokenize};
use abis::{ECDSAOwnedDKIMRegistry, EmailAuthMsg, EmailProof};
use abis::{EmailAuthMsg, EmailProof, UserOverridableDKIMRegistry};
use anyhow::anyhow;
use config::ChainConfig;
use ethers::prelude::*;
Expand Down Expand Up @@ -76,14 +76,16 @@ impl ChainClient {
domain_name: String,
public_key_hash: [u8; 32],
signature: Bytes,
dkim: ECDSAOwnedDKIMRegistry<SignerM>,
dkim: UserOverridableDKIMRegistry<SignerM>,
) -> Result<String> {
// Mutex is used to prevent nonce conflicts.
let mut mutex = SHARED_MUTEX.lock().await;
*mutex += 1;

// Call the contract method
let call = dkim.set_dkim_public_key_hash(selector, domain_name, public_key_hash, signature);
let main_authorizer = dkim.main_authorizer().call().await?;
let call =
dkim.set_dkim_public_key_hash(domain_name, public_key_hash, main_authorizer, signature);
let tx = call.send().await?;

// Wait for the transaction to be confirmed
Expand Down Expand Up @@ -113,14 +115,15 @@ impl ChainClient {
&self,
domain_name: ::std::string::String,
public_key_hash: [u8; 32],
dkim: ECDSAOwnedDKIMRegistry<SignerM>,
dkim: UserOverridableDKIMRegistry<SignerM>,
) -> Result<bool> {
// Call the contract method to check if the hash is valid
let is_valid = dkim
.is_dkim_public_key_hash_valid(domain_name, public_key_hash)
let main_authorizer = dkim.main_authorizer().call().await?;
let is_set = dkim
.dkim_public_key_hashes(domain_name, public_key_hash, main_authorizer)
.call()
.await?;
Ok(is_valid)
Ok(is_set)
}

pub async fn call(
Expand Down
3 changes: 2 additions & 1 deletion packages/relayer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ pub struct PathConfig {
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct IcpConfig {
pub canister_id: String,
pub dkim_canister_id: String,
pub wallet_canister_id: String,
pub ic_replica_url: String,
}

Expand Down
76 changes: 51 additions & 25 deletions packages/relayer/src/dkim.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

use abis::ECDSAOwnedDKIMRegistry;
use abis::UserOverridableDKIMRegistry;
use anyhow::anyhow;
use candid::Encode;
use chain::ChainClient;
use ethers::types::Address;
use ethers::utils::hex;
Expand All @@ -17,14 +17,19 @@ use ic_agent::agent::http_transport::ReqwestTransport;
use ic_agent::agent::*;
use ic_agent::identity::*;
use ic_utils::canister::*;
use ic_utils::interfaces::WalletCanister;

use serde::Deserialize;

pub const SIGN_CHARGED_CYCLE: u128 = 39_246_898_590;

/// Represents a client for interacting with the DKIM Oracle.
#[derive(Debug, Clone)]
pub struct DkimOracleClient<'a> {
/// The canister used for communication
pub canister: Canister<'a>,
/// The dkim oracle canister.
pub dkim_canister: Canister<'a>,
/// The wallet canister.
pub wallet_canister: WalletCanister<'a>,
}

/// Represents a signed DKIM public key.
Expand Down Expand Up @@ -72,19 +77,34 @@ impl<'a> DkimOracleClient<'a> {
///
/// # Arguments
///
/// * `canister_id` - The ID of the canister.
/// * `dkim_canister_id` - The ID of the dkim canister.
/// * `wallet_canister_id` - The ID of the wallet canister.
/// * `agent` - The agent to use for communication.
///
/// # Returns
///
/// An `anyhow::Result<Self>`.
pub fn new(canister_id: &str, agent: &'a Agent) -> anyhow::Result<Self> {
pub async fn new(
dkim_canister_id: &str,
wallet_canister_id: &str,
agent: &'a Agent,
) -> anyhow::Result<Self> {
// Build the canister using the provided ID and agent
let canister = CanisterBuilder::new()
.with_canister_id(canister_id)
let dkim_canister = CanisterBuilder::new()
.with_canister_id(dkim_canister_id)
.with_agent(agent)
.build()?;
Ok(Self { canister })
let wallet_canister = WalletCanister::from_canister(
ic_utils::Canister::builder()
.with_agent(agent)
.with_canister_id(wallet_canister_id)
.build()?,
)
.await?;
Ok(Self {
dkim_canister,
wallet_canister,
})
}

/// Requests a signature for a DKIM public key.
Expand All @@ -103,19 +123,20 @@ impl<'a> DkimOracleClient<'a> {
domain: &str,
) -> anyhow::Result<SignedDkimPublicKey> {
// Build the request to sign the DKIM public key
let request = self
.canister
.update("sign_dkim_public_key")
.with_args((selector, domain))
.build::<(Result<SignedDkimPublicKey, String>,)>();

// Call the canister and wait for the response
let response = request
.call_and_wait_one::<Result<SignedDkimPublicKey, String>>()
.await?
.map_err(|e| anyhow!(format!("Error from canister: {:?}", e)))?;

Ok(response)
let mut arg = Argument::new();
arg.set_raw_arg(Encode!(&selector, &domain).unwrap());
let (response,) = self
.wallet_canister
.call128::<(Result<SignedDkimPublicKey, String>,), _>(
*self.dkim_canister.canister_id(),
"sign_dkim_public_key",
arg,
SIGN_CHARGED_CYCLE,
)
.call_and_wait()
.await?;
let sign = response.map_err(|e| anyhow!(format!("Error from canister: {:?}", e)))?;
Ok(sign)
}
}

Expand Down Expand Up @@ -149,7 +170,7 @@ pub async fn check_and_update_dkim(
info!(LOG, "domain {:?}", domain);

// Get DKIM
let dkim = ECDSAOwnedDKIMRegistry::new(dkim, chain_client.client.clone());
let dkim = UserOverridableDKIMRegistry::new(dkim, chain_client.client.clone());

info!(LOG, "dkim {:?}", dkim);

Expand Down Expand Up @@ -187,14 +208,19 @@ pub async fn check_and_update_dkim(

info!(
LOG,
"icp canister id {:?}", &relayer_state.config.icp.canister_id
"icp canister id {:?}", &relayer_state.config.icp.dkim_canister_id
);
info!(
LOG,
"icp replica url {:?}", &relayer_state.config.icp.ic_replica_url
);

let oracle_client = DkimOracleClient::new(&relayer_state.config.icp.canister_id, &ic_agent)?;
let oracle_client = DkimOracleClient::new(
&relayer_state.config.icp.dkim_canister_id,
&relayer_state.config.icp.wallet_canister_id,
&ic_agent,
)
.await?;
info!(LOG, "oracle_client {:?}", oracle_client);

// Request signature from oracle
Expand Down

0 comments on commit c98501b

Please sign in to comment.