Skip to content

Commit

Permalink
Merge pull request #3 from zkemail/fix/subject-parsing
Browse files Browse the repository at this point in the history
fix: Subject Parsing
  • Loading branch information
Bisht13 authored Sep 3, 2024
2 parents 7715269 + af6dd50 commit aefb00f
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "relayer-utils"
version = "0.3.1"
version = "0.3.2"
authors = ["Sora Suegami", "Aditya Bisht"]
license = "MIT"
edition = "2018"
Expand Down
Binary file modified bin/binary-arm64.node
Binary file not shown.
Binary file modified bin/binary-x64.node
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zk-email/relayer-utils",
"version": "0.3.1",
"version": "0.3.2",
"description": "",
"main": "index.js",
"contributors": [
Expand Down
58 changes: 30 additions & 28 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,6 @@ fn generate_circuit_inputs(params: CircuitInputParams) -> Result<CircuitInput> {
cmp::max(params.max_body_length, body_sha_length),
);

// Ensure that the error type returned by `generate_partial_sha` is sized
// by converting it into an `anyhow::Error` if it's not already.
let result = generate_partial_sha(
body_padded,
body_padded_len,
params.sha_precompute_selector,
params.max_body_length,
);

// Use match to handle the result and convert any error into an anyhow::Error
let (precomputed_sha, body_remaining, body_remaining_length) = match result {
Ok((sha, remaining, len)) => (sha, remaining, len),
Err(e) => panic!("Failed to generate partial SHA: {:?}", e),
};

// Initialize the circuit input with the padded header and RSA information
let mut circuit_input = CircuitInput {
header_padded,
Expand All @@ -182,6 +167,21 @@ fn generate_circuit_inputs(params: CircuitInputParams) -> Result<CircuitInput> {

// If body hash check is not ignored, include the precomputed SHA and body information
if !params.ignore_body_hash_check {
// Ensure that the error type returned by `generate_partial_sha` is sized
// by converting it into an `anyhow::Error` if it's not already.
let result = generate_partial_sha(
body_padded,
body_padded_len,
params.sha_precompute_selector,
params.max_body_length,
);

// Use match to handle the result and convert any error into an anyhow::Error
let (precomputed_sha, body_remaining, body_remaining_length) = match result {
Ok((sha, remaining, len)) => (sha, remaining, len),
Err(e) => panic!("Failed to generate partial SHA: {:?}", e),
};

circuit_input.precomputed_sha = Some(precomputed_sha);
circuit_input.body_hash_idx = Some(params.body_hash_idx);
circuit_input.body_padded = Some(body_remaining);
Expand Down Expand Up @@ -267,14 +267,15 @@ pub async fn generate_email_circuit_input(
};

// Clean the body if necessary
let padded_cleaned_body = if parsed_email.need_soft_line_breaks()? {
email_circuit_inputs
.body_padded
.clone()
.map(remove_quoted_printable_soft_breaks)
} else {
None
};
let padded_cleaned_body =
if parsed_email.need_soft_line_breaks(circuit_input_params.ignore_body_hash_check)? {
email_circuit_inputs
.body_padded
.clone()
.map(remove_quoted_printable_soft_breaks)
} else {
None
};

if email_circuit_inputs.precomputed_sha.is_some() {
let code = parsed_email
Expand All @@ -283,11 +284,12 @@ pub async fn generate_email_circuit_input(
let command = parsed_email.get_command(circuit_input_params.ignore_body_hash_check)?;

// Determine the appropriate body to search in
let search_body = if parsed_email.need_soft_line_breaks()? {
padded_cleaned_body.as_ref()
} else {
email_circuit_inputs.body_padded.as_ref()
};
let search_body =
if parsed_email.need_soft_line_breaks(circuit_input_params.ignore_body_hash_check)? {
padded_cleaned_body.as_ref()
} else {
email_circuit_inputs.body_padded.as_ref()
};

// Find indices for the code and command in the body
code_idx = find_index_in_body(search_body, &code);
Expand Down
18 changes: 9 additions & 9 deletions src/parse_email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl ParsedEmail {
let idxes = extract_substr_idxes(&self.canonicalized_header, &regex_config)?[0];
let str = self.canonicalized_header[idxes.0..idxes.1].to_string();
Ok(str)
} else if self.need_soft_line_breaks()? {
} else if self.need_soft_line_breaks(ignore_body_hash_check)? {
let idxes = extract_substr_idxes(&self.cleaned_body, &regex_config)?[0];
let str = self.cleaned_body[idxes.0..idxes.1].to_string();
Ok(str)
Expand All @@ -174,7 +174,7 @@ impl ParsedEmail {
if ignore_body_hash_check {
let idxes = extract_substr_idxes(&self.canonicalized_header, &regex_config)?[0];
Ok(idxes)
} else if self.need_soft_line_breaks()? {
} else if self.need_soft_line_breaks(ignore_body_hash_check)? {
let idxes = extract_substr_idxes(&self.cleaned_body, &regex_config)?[0];
Ok(idxes)
} else {
Expand Down Expand Up @@ -211,9 +211,7 @@ impl ParsedEmail {
pub fn get_command(&self, ignore_body_hash_check: bool) -> Result<String> {
let regex_config = serde_json::from_str(include_str!("../regexes/command.json"))?;
if ignore_body_hash_check {
let idxes = extract_substr_idxes(&self.canonicalized_header, &regex_config)?[0];
let str = self.canonicalized_header[idxes.0..idxes.1].to_string();
Ok(str)
Ok("".to_string())
} else {
let idxes = extract_substr_idxes(&self.canonicalized_body, &regex_config)?[0];
let str = self.canonicalized_body[idxes.0..idxes.1].to_string();
Expand All @@ -235,9 +233,8 @@ impl ParsedEmail {
pub fn get_command_idxes(&self, ignore_body_hash_check: bool) -> Result<(usize, usize)> {
let regex_config = serde_json::from_str(include_str!("../regexes/command.json"))?;
if ignore_body_hash_check {
let idxes = extract_substr_idxes(&self.canonicalized_header, &regex_config)?[0];
Ok(idxes)
} else if self.need_soft_line_breaks()? {
Ok((0, 0))
} else if self.need_soft_line_breaks(ignore_body_hash_check)? {
let idxes = extract_substr_idxes(&self.cleaned_body, &regex_config)?[0];
Ok(idxes)
} else {
Expand All @@ -247,7 +244,10 @@ impl ParsedEmail {
}

/// Determines if the email body contains quoted-printable soft line breaks.
pub fn need_soft_line_breaks(&self) -> Result<bool> {
pub fn need_soft_line_breaks(&self, ignore_body_hash_check: bool) -> Result<bool> {
if ignore_body_hash_check {
return Ok(false);
}
let regex_config = serde_json::from_str(include_str!("../regexes/command.json"))?;
let idxes = extract_substr_idxes(&self.canonicalized_body, &regex_config)?[0];
let str = self.canonicalized_body[idxes.0..idxes.1].to_string();
Expand Down

0 comments on commit aefb00f

Please sign in to comment.