Skip to content

Commit

Permalink
fix: regex json path
Browse files Browse the repository at this point in the history
  • Loading branch information
Bisht13 committed Sep 30, 2024
1 parent 4d5b1c4 commit 3da09ad
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 19 deletions.
4 changes: 1 addition & 3 deletions packages/relayer/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ PROVER_ADDRESS="https://zkemail--email-auth-prover-v1-3-0-flask-app.modal.run"
DATABASE_URL= "postgres://test@localhost/emailauth_test"
RELAYER_EMAIL_ADDR=
WEB_SERVER_ADDRESS="127.0.0.1:4500"
CIRCUITS_DIR_PATH= #Path to email-wallet/packages/circuits
EMAIL_TEMPLATES_PATH= #Path to email templates, e.g. ./packages/relayer/eml_templates/
SELECTOR_DEF_PATH="./src/regex_json/selector_def.json"
REQUEST_DEF_PATH="./src/regex_json/request_def.json"
REGEX_JSON_DIR_PATH=

CANISTER_ID="q7eci-dyaaa-aaaak-qdbia-cai"
PEM_PATH="./.ic.pem"
Expand Down
1 change: 0 additions & 1 deletion packages/relayer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ You can run the relayer either on your local environments or cloud instances (we
DATABASE_URL= "postgres://new_user:my_secure_password@localhost/my_new_database"
WEB_SERVER_ADDRESS="127.0.0.1:4500"
CIRCUITS_DIR_PATH= # Absolute path to packages/circuits
EMAIL_TEMPLATES_PATH= # Absolute path to packages/relayer/eml_templates
CANISTER_ID="q7eci-dyaaa-aaaak-qdbia-cai"
Expand Down
4 changes: 2 additions & 2 deletions packages/relayer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct RelayerConfig {
pub relayer_email_addr: String,
pub db_path: String,
pub web_server_address: String,
pub circuits_dir_path: PathBuf,
pub regex_json_dir_path: PathBuf,
pub prover_address: String,
pub chain_rpc_provider: String,
pub chain_rpc_explorer: String,
Expand Down Expand Up @@ -43,7 +43,7 @@ impl RelayerConfig {
relayer_email_addr: env::var(RELAYER_EMAIL_ADDR_KEY).unwrap(),
db_path: env::var(DATABASE_PATH_KEY).unwrap(),
web_server_address: env::var(WEB_SERVER_ADDRESS_KEY).unwrap(),
circuits_dir_path: env::var(CIRCUITS_DIR_PATH_KEY).unwrap().into(),
regex_json_dir_path: env::var(REGEX_JSON_DIR_PATH_KEY).unwrap().into(),
prover_address: env::var(PROVER_ADDRESS_KEY).unwrap(),
chain_rpc_provider: env::var(CHAIN_RPC_PROVIDER_KEY).unwrap(),
chain_rpc_explorer: env::var(CHAIN_RPC_EXPLORER_KEY).unwrap(),
Expand Down
13 changes: 9 additions & 4 deletions packages/relayer/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ pub async fn handle_email(email: String) -> Result<EmailAuthEvent, EmailError> {
trace!(LOG, "From address: {}", guardian_email_addr);
let email_body = parsed_email.get_cleaned_body()?;

let request_def_path = env::var(REQUEST_DEF_PATH_KEY)
.map_err(|_| anyhow!("ENV var {} not set", REQUEST_DEF_PATH_KEY))?;
let request_def_contents = fs::read_to_string(&request_def_path)
.map_err(|e| anyhow!("Failed to read file {}: {}", request_def_path, e))?;
let request_def_path =
PathBuf::from(REGEX_JSON_DIR_PATH.get().unwrap()).join("request_def.json");
let request_def_contents = fs::read_to_string(&request_def_path).map_err(|e| {
anyhow!(
"Failed to read file {:?}: {}",
request_def_path.display(),
e
)
})?;
let request_decomposed_def = serde_json::from_str(&request_def_contents)
.map_err(|e| EmailError::Parse(format!("Failed to parse request_def.json: {}", e)))?;
let request_idxes = extract_substr_idxes(&email, &request_decomposed_def)?;
Expand Down
4 changes: 2 additions & 2 deletions packages/relayer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use std::path::PathBuf;
use std::sync::{Arc, OnceLock};
use tokio::time::Duration;

pub static CIRCUITS_DIR_PATH: OnceLock<PathBuf> = OnceLock::new();
pub static REGEX_JSON_DIR_PATH: OnceLock<PathBuf> = OnceLock::new();
pub static WEB_SERVER_ADDRESS: OnceLock<String> = OnceLock::new();
pub static PROVER_ADDRESS: OnceLock<String> = OnceLock::new();
pub static PRIVATE_KEY: OnceLock<String> = OnceLock::new();
Expand Down Expand Up @@ -103,7 +103,7 @@ pub async fn run(config: RelayerConfig) -> Result<()> {
info!(LOG, "Starting relayer");

// Initialize global configuration
CIRCUITS_DIR_PATH.set(config.circuits_dir_path).unwrap();
REGEX_JSON_DIR_PATH.set(config.regex_json_dir_path).unwrap();
WEB_SERVER_ADDRESS.set(config.web_server_address).unwrap();
PROVER_ADDRESS.set(config.prover_address).unwrap();
PRIVATE_KEY.set(config.private_key).unwrap();
Expand Down
17 changes: 13 additions & 4 deletions packages/relayer/src/modules/dkim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fs;

use anyhow::anyhow;
use relayer_utils::extract_substr_idxes;
use relayer_utils::DecomposedRegexConfig;
use relayer_utils::LOG;

use crate::*;
Expand Down Expand Up @@ -177,16 +178,24 @@ pub async fn check_and_update_dkim(
}

// Get selector
let selector_def_path = env::var(SELECTOR_DEF_PATH_KEY)
.map_err(|_| anyhow!("ENV var {} not set", SELECTOR_DEF_PATH_KEY))?;
let selector_def_path =
PathBuf::from(env::var(REGEX_JSON_DIR_PATH_KEY).unwrap()).join("selector_def.json");
let selector_def_contents = fs::read_to_string(&selector_def_path)
.map_err(|e| anyhow!("Failed to read file {}: {}", selector_def_path, e))?;
let selector_decomposed_def = serde_json::from_str(&selector_def_path).unwrap();
.map_err(|e| anyhow!("Failed to read file {}: {}", selector_def_path.display(), e))?;
let selector_decomposed_def: DecomposedRegexConfig =
serde_json::from_str(&selector_def_contents).map_err(|e| {
anyhow!(
"Failed to parse JSON from file {}: {}",
selector_def_path.display(),
e
)
})?;
let selector = {
let idxes =
extract_substr_idxes(&parsed_email.canonicalized_header, &selector_decomposed_def)?[0];
parsed_email.canonicalized_header[idxes.0..idxes.1].to_string()
};

info!(LOG, "selector {}", selector);

// Generate IC agent and create oracle client
Expand Down
4 changes: 1 addition & 3 deletions packages/relayer/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ pub const SMTP_SERVER_KEY: &str = "SMTP_SERVER";
pub const RELAYER_EMAIL_ADDR_KEY: &str = "RELAYER_EMAIL_ADDR";
pub const DATABASE_PATH_KEY: &str = "DATABASE_URL";
pub const WEB_SERVER_ADDRESS_KEY: &str = "WEB_SERVER_ADDRESS";
pub const CIRCUITS_DIR_PATH_KEY: &str = "CIRCUITS_DIR_PATH";
pub const PROVER_ADDRESS_KEY: &str = "PROVER_ADDRESS";
pub const CHAIN_RPC_PROVIDER_KEY: &str = "CHAIN_RPC_PROVIDER";
pub const CHAIN_RPC_EXPLORER_KEY: &str = "CHAIN_RPC_EXPLORER";
pub const PRIVATE_KEY_KEY: &str = "PRIVATE_KEY";
pub const CHAIN_ID_KEY: &str = "CHAIN_ID";
pub const EMAIL_ACCOUNT_RECOVERY_VERSION_ID_KEY: &str = "EMAIL_ACCOUNT_RECOVERY_VERSION_ID";
pub const EMAIL_TEMPLATES_PATH_KEY: &str = "EMAIL_TEMPLATES_PATH";
pub const SELECTOR_DEF_PATH_KEY: &str = "SELECTOR_DEF_PATH";
pub const REQUEST_DEF_PATH_KEY: &str = "REQUEST_DEF_PATH";
pub const REGEX_JSON_DIR_PATH_KEY: &str = "REGEX_JSON_DIR_PATH";

// Log strings
pub const JSON_LOGGER_KEY: &str = "JSON_LOGGER";
Expand Down

0 comments on commit 3da09ad

Please sign in to comment.