Skip to content

Commit

Permalink
feat: fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bisht13 committed Apr 1, 2024
1 parent 58c233a commit f24731c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 28 deletions.
15 changes: 12 additions & 3 deletions packages/relayer/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ pub async fn handle_email<P: EmailsPool>(
let result = extract_template_vals_and_skipped_subject_idx(&subject, subject_template);
let (subject_params, skipped_subject_prefix) = match result {
Ok((subject_params, skipped_subject_prefix)) => (subject_params, skipped_subject_prefix),
Err(_) => {
Err(e) => {
return Ok(EmailAuthEvent::Error {
email_addr: guardian_email_addr,
error: format!("Invalid Subject"),
error: format!("Invalid Subject, {}", e),
});
}
};
Expand Down Expand Up @@ -138,6 +138,9 @@ pub async fn handle_email<P: EmailsPool>(
proof: email_proof.clone(),
};

info!(LOG, "Email Auth Msg: {:?}", email_auth_msg; "func" => function_name!());
info!(LOG, "Request: {:?}", request; "func" => function_name!());

match chain_client
.handle_acceptance(
&request.wallet_eth_addr,
Expand Down Expand Up @@ -244,6 +247,9 @@ pub async fn handle_email<P: EmailsPool>(
proof: email_proof.clone(),
};

info!(LOG, "Email Auth Msg: {:?}", email_auth_msg; "func" => function_name!());
info!(LOG, "Request: {:?}", request; "func" => function_name!());

match chain_client
.handle_recovery(
&request.wallet_eth_addr,
Expand Down Expand Up @@ -321,8 +327,11 @@ pub fn get_masked_subject(public_signals: Vec<U256>, start_idx: usize) -> Result
}

// Bytes to string, removing null bytes
let subject = String::from_utf8(subject_bytes.into_iter().filter(|&b| b != 0u8).collect())
let mut subject = String::from_utf8(subject_bytes.into_iter().filter(|&b| b != 0u8).collect())
.map_err(|e| anyhow!("Failed to convert bytes to string: {}", e))?;

// Remove trailing whitespace
subject = subject.trim_end().to_string();

Ok(subject)
}
10 changes: 5 additions & 5 deletions packages/relayer/src/utils/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ pub const NOT_MY_SENDER: &str = "NOT_MY_SENDER";
pub const WRONG_SUBJECT_FORMAT: &str = "Wrong subject format";

// Core REGEX'es and Commands
pub const STRING_RGEX: &str = ".+";
pub const UINT_REGEX: &str = "[0-9]+";
pub const INT_REGEX: &str = "-?[0-9]+";
pub const ETH_ADDR_REGEX: &str = "0x[0-9a-fA-F]{40}";
pub const DECIMALS_REGEX: &str = "[0-9]+(\\.[0-9]+)?";
pub const STRING_REGEX: &str = r"\S+";
pub const UINT_REGEX: &str = r"\d+";
pub const INT_REGEX: &str = r"-?\d+";
pub const ETH_ADDR_REGEX: &str = r"0x[a-fA-F0-9]{40}";
pub const DECIMALS_REGEX: &str = r"\d+\.\d+";

// DKIM ORACLE ARGS
pub const CANISTER_ID_KEY: &str = "CANISTER_ID";
Expand Down
50 changes: 30 additions & 20 deletions packages/relayer/src/utils/subject_templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,36 @@ pub fn extract_template_vals_and_skipped_subject_idx(
input: &str,
templates: Vec<String>,
) -> Result<(Vec<TemplateValue>, usize), anyhow::Error> {
let mut skipped_bytes = 0usize;
let mut current_input = input;

loop {
match extract_template_vals(current_input, templates.clone()) {
Ok(vals) => return Ok((vals, skipped_bytes)),
Err(_) => {
if let Some(next_start) = current_input.char_indices().nth(1) {
skipped_bytes += next_start.0;
current_input = &current_input[next_start.0..];
} else {
// If there's no more input to skip, return the error
return Err(anyhow!("Unable to match templates with input"));
}
}
// Convert the template to a regex pattern, escaping necessary characters and replacing placeholders
let pattern = templates
.iter()
.map(|template| match template.as_str() {
"{string}" => STRING_REGEX.to_string(),
"{uint}" => UINT_REGEX.to_string(),
"{int}" => INT_REGEX.to_string(),
"{decimals}" => DECIMALS_REGEX.to_string(),
"{ethAddr}" => ETH_ADDR_REGEX.to_string(),
_ => regex::escape(template),
})
.collect::<Vec<String>>()
.join("\\s+");

let regex = Regex::new(&pattern).map_err(|e| anyhow!("Regex compilation failed: {}", e))?;

// Attempt to find the pattern in the input
if let Some(matched) = regex.find(input) {
// Calculate the number of bytes to skip before the match
let skipped_bytes = matched.start();

// Extract the values based on the matched pattern
let current_input = &input[skipped_bytes..];
match extract_template_vals(current_input, templates) {
Ok(vals) => Ok((vals, skipped_bytes)),
Err(e) => Err(e),
}
} else {
// If there's no match, return an error indicating no match was found
Err(anyhow!("Unable to match templates with input"))
}
}

Expand All @@ -84,7 +98,7 @@ pub fn extract_template_vals(input: &str, templates: Vec<String>) -> Result<Vec<

match template.as_str() {
"{string}" => {
let string_match = Regex::new(STRING_RGEX)
let string_match = Regex::new(STRING_REGEX)
.unwrap()
.find(input_decomposed[input_idx])
.ok_or(anyhow!("No string found"))?;
Expand Down Expand Up @@ -151,10 +165,6 @@ pub fn extract_template_vals(input: &str, templates: Vec<String>) -> Result<Vec<
input_idx += 1; // Move to the next piece of input
}

if input_idx != input_decomposed.len() {
return Err(anyhow!("Input is not fully consumed"));
}

Ok(template_vals)
}

Expand Down

0 comments on commit f24731c

Please sign in to comment.