Skip to content

Commit

Permalink
feat: finish e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
Bisht13 committed Oct 15, 2024
1 parent c31af55 commit dc607f4
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 62 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@ zkout
example/contracts/artifacts
example/contracts/cache
example/contracts/node_modules
example/scripts/dist
example/scripts/dist
example/contracts/broadcast
10 changes: 7 additions & 3 deletions example/contracts/src/EmitEmailCommand.sol
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,31 @@ contract EmitEmailCommand {
/// @notice Returns a two-dimensional array of strings representing the command templates.
/// @return string[][] A two-dimensional array of strings, where each inner array represents a set of fixed strings and matchers for a command template.
function commandTemplates() public pure returns (string[][] memory) {
string[][] memory templates = new string[][](2);
templates[0] = new string[](5);
string[][] memory templates = new string[][](5); // Corrected size to 5
templates[0] = new string[](3); // Corrected size to 3
templates[0][0] = "Emit";
templates[0][1] = "string";
templates[0][2] = "{string}";

templates[1] = new string[](3); // Added missing initialization
templates[1][0] = "Emit";
templates[1][1] = "uint";
templates[1][2] = "{uint}";

templates[2] = new string[](3); // Added missing initialization
templates[2][0] = "Emit";
templates[2][1] = "int";
templates[2][2] = "{int}";

templates[3] = new string[](3); // Added missing initialization
templates[3][0] = "Emit";
templates[3][1] = "decimals";
templates[3][2] = "{decimals}";

templates[4] = new string[](4); // Corrected size to 4
templates[4][0] = "Emit";
templates[4][1] = "ethereum";
templates[4][2] = "adddress";
templates[4][2] = "address"; // Fixed typo: "adddress" to "address"
templates[4][3] = "{ethAddr}";

return templates;
Expand Down
54 changes: 22 additions & 32 deletions packages/relayer/src/chain.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use crate::*;
use abi::{Abi, Token};
use abi::{encode, Abi, ParamType, Token, Tokenize};
use abis::{ECDSAOwnedDKIMRegistry, EmailAuth, EmailAuthMsg, EmailProof};
use anyhow::anyhow;
use config::ChainConfig;
Expand All @@ -15,6 +15,16 @@ const CONFIRMATIONS: usize = 1;

type SignerM = SignerMiddleware<Provider<Http>, LocalWallet>;

struct CustomTokenVec {
tokens: Vec<Token>,
}

impl Tokenize for CustomTokenVec {
fn into_tokens(self) -> Vec<Token> {
self.tokens
}
}

/// Represents a client for interacting with the blockchain.
#[derive(Debug, Clone)]
pub struct ChainClient {
Expand Down Expand Up @@ -110,29 +120,6 @@ impl ChainClient {
Ok(is_valid)
}

/// Gets the DKIM from an email auth address.
///
/// # Arguments
///
/// * `email_auth_addr` - The email auth address as a string.
///
/// # Returns
///
/// A `Result` containing the ECDSA Owned DKIM Registry if successful, or an error if not.
pub async fn get_dkim_from_email_auth(
&self,
email_auth_address: Address,
) -> Result<ECDSAOwnedDKIMRegistry<SignerM>, anyhow::Error> {
// Create a new EmailAuth contract instance
let contract = EmailAuth::new(email_auth_address, self.client.clone());

// Call the dkim_registry_addr method to get the DKIM registry address
let dkim = contract.dkim_registry_addr().call().await?;

// Create and return a new ECDSAOwnedDKIMRegistry instance
Ok(ECDSAOwnedDKIMRegistry::new(dkim, self.client.clone()))
}

pub async fn call(&self, request: RequestModel, email_auth_msg: EmailAuthMsg) -> Result<()> {
let abi = Abi {
functions: vec![request.email_tx_auth.function_abi.clone()]
Expand All @@ -151,26 +138,29 @@ impl ChainClient {
abi,
self.client.clone(),
);

let function = request.email_tx_auth.function_abi;
let remaining_args = request.email_tx_auth.remaining_args;

// Assuming remaining_args is a Vec of some type that can be converted to tokens
let mut tokens = email_auth_msg.to_tokens();
// Initialize your tokens vector
let mut tokens = Vec::new();

// Add the first token (assuming email_auth_msg.to_tokens() returns Vec<Token>)
tokens.push(Token::Tuple(email_auth_msg.to_tokens()));

// Convert remaining_args to tokens and add them to the tokens vector
for arg in remaining_args {
// Convert each arg to a Token. This conversion depends on the type of arg.
// For example, if arg is a string, you might use Token::String(arg).
// Adjust the conversion based on the actual type of arg.
let token = Token::from(arg);
tokens.push(token);
}

let custom_tokens = CustomTokenVec { tokens };

// Now you can use the tokens vector to call the contract function
let call = contract.method::<_, ()>(&function.name, tokens)?;
let call = contract.method::<_, ()>(&function.name, custom_tokens)?;

let tx = call.send().await?;
let receipt = tx.await?;
let tx = call.send().await?.await?;
info!(LOG, "tx: {:?}", tx.expect("tx not found").transaction_hash);

Ok(())
}
Expand Down
28 changes: 18 additions & 10 deletions packages/relayer/src/dkim.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::env;

use abis::ECDSAOwnedDKIMRegistry;
use anyhow::anyhow;
use chain::ChainClient;
use ethers::types::Address;
Expand Down Expand Up @@ -134,7 +135,7 @@ impl<'a> DkimOracleClient<'a> {
/// A `Result<()>`.
pub async fn check_and_update_dkim(
parsed_email: &ParsedEmail,
email_auth_addr: Address,
dkim: Address,
chain_client: ChainClient,
relayer_state: RelayerState,
) -> Result<()> {
Expand All @@ -149,9 +150,7 @@ pub async fn check_and_update_dkim(
info!(LOG, "domain {:?}", domain);

// Get DKIM
let dkim = chain_client
.get_dkim_from_email_auth(email_auth_addr)
.await?;
let dkim = ECDSAOwnedDKIMRegistry::new(dkim, chain_client.client.clone());

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

Expand Down Expand Up @@ -182,13 +181,22 @@ pub async fn check_and_update_dkim(

// Generate IC agent and create oracle client
let ic_agent = DkimOracleClient::gen_agent(
&env::var(relayer_state.config.path.pem).unwrap(),
&env::var(relayer_state.config.icp.ic_replica_url).unwrap(),
)?;
let oracle_client = DkimOracleClient::new(
&env::var(relayer_state.config.icp.canister_id).unwrap(),
&ic_agent,
&relayer_state.config.path.pem,
&relayer_state.config.icp.ic_replica_url,
)?;
info!(LOG, "ic_agent {:?}", ic_agent);

info!(
LOG,
"icp canister id {:?}", &relayer_state.config.icp.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)?;
info!(LOG, "oracle_client {:?}", oracle_client);

// Request signature from oracle
let oracle_result = oracle_client.request_signature(&selector, &domain).await?;
Expand Down
1 change: 0 additions & 1 deletion packages/relayer/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ pub async fn get_status_handler(
})?;

let response = json!({
"status": "success",
"message": "request status",
"request": request,
});
Expand Down
7 changes: 4 additions & 3 deletions packages/relayer/src/mail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub async fn handle_email_event(event: EmailEvent, relayer_state: RelayerState)
"requestId": request_id,
});
let body_html = render_html(
"acceptance_success.html",
"completion_template.html",
render_data,
relayer_state.clone(),
)
Expand Down Expand Up @@ -216,7 +216,8 @@ pub async fn handle_email_event(event: EmailEvent, relayer_state: RelayerState)
"error": error,
"userEmailAddr": email_addr,
});
let body_html = render_html("error.html", render_data, relayer_state.clone()).await?;
let body_html =
render_html("error_template.html", render_data, relayer_state.clone()).await?;

// Create and send the email
let email = EmailMessage {
Expand Down Expand Up @@ -382,7 +383,7 @@ pub async fn handle_email(

check_and_update_dkim(
&parsed_email,
request.email_tx_auth.email_auth_contract_address,
request.email_tx_auth.dkim_contract_address,
chain_client.clone(),
relayer_state.clone(),
)
Expand Down
11 changes: 1 addition & 10 deletions packages/relayer/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize};
#[serde(rename_all = "camelCase")]
pub struct EmailTxAuthSchema {
pub contract_address: Address,
pub email_auth_contract_address: Address,
pub dkim_contract_address: Address,
pub account_code: AccountCode,
pub code_exists_in_email: bool,
pub function_abi: Function,
Expand All @@ -24,12 +24,3 @@ pub struct EmailTxAuthSchema {
pub body: String,
pub chain: String,
}

#[derive(Deserialize, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DKIMSchema {
dkim_contract_address: Address,
selector: String,
domain: String,
chain: String,
}
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4046,9 +4046,9 @@ node-int64@^0.4.0:
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
integrity sha512-O5lz91xSOeoXP6DulyHfllpq+Eg00MWitZIbtPfoSEvqIHdl5gfcY6hYzDWnj0qD5tz52PI08u9qUvSVeUBeHw==

"node-pre-gyp-github@git+https://github.com/ultamatt/node-pre-gyp-github.git":
"node-pre-gyp-github@https://github.com/ultamatt/node-pre-gyp-github.git":
version "1.4.3"
resolved "git+https://github.com/ultamatt/node-pre-gyp-github.git#e4961827f77751489bc8d4760a0479f3f985f34f"
resolved "https://github.com/ultamatt/node-pre-gyp-github.git#e4961827f77751489bc8d4760a0479f3f985f34f"
dependencies:
"@octokit/rest" "^15.9.5"
commander "^2.17.0"
Expand Down

0 comments on commit dc607f4

Please sign in to comment.