Skip to content

Commit

Permalink
Merge branch 'main' into feat/add_email_headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Bisht13 authored Oct 9, 2024
2 parents 6fb85e6 + 49d1559 commit 2b45e56
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
71 changes: 67 additions & 4 deletions src/cryptos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ use ethers::types::Bytes;
use halo2curves::ff::Field;
use poseidon_rs::{poseidon_bytes, poseidon_fields, Fr, PoseidonError};
use rand_core::RngCore;
use std::collections::hash_map::DefaultHasher;
use std::error::Error;
use std::hash::{Hash, Hasher};
use std::{
collections::hash_map::DefaultHasher,
error::Error,
fmt,
hash::{Hash, Hasher},
};
use serde::{
de::{self, Visitor},
Deserialize, Deserializer,
};
use zk_regex_apis::padding::pad_string;

use crate::{
converters::{
bytes_chunk_fields, bytes_to_fields, int64_to_bytes, int8_to_bytes, merge_u8_arrays,
},
MAX_EMAIL_ADDR_BYTES,
hex_to_field, MAX_EMAIL_ADDR_BYTES,
};

type ShaResult = Vec<u8>; // The result of a SHA-256 hash operation.
Expand Down Expand Up @@ -133,6 +140,62 @@ impl PaddedEmailAddr {
/// `AccountCode` is a structure that holds a single field element representing an account code.
pub struct AccountCode(pub Fr);

impl<'de> Deserialize<'de> for AccountCode {
/// Deserializes a string into an `AccountCode`.
///
/// # Arguments
///
/// * `deserializer` - The deserializer to use for converting the string into an `AccountCode`.
///
/// # Returns
///
/// A result that is either an `AccountCode` or a deserialization error.
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct AccountCodeVisitor;

impl<'de> Visitor<'de> for AccountCodeVisitor {
type Value = AccountCode;

/// Describes what the visitor expects to receive.
///
/// # Arguments
///
/// * `formatter` - A formatter to write the expected type description.
///
/// # Returns
///
/// A `fmt::Result` indicating success or failure.
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a valid field element for AccountCode")
}

/// Visits a string and attempts to convert it into an `AccountCode`.
///
/// # Arguments
///
/// * `value` - The string value to convert.
///
/// # Returns
///
/// A result that is either an `AccountCode` or a deserialization error.
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
// Convert the string to a field element
let fr_value = hex_to_field(value).map_err(de::Error::custom)?;
Ok(AccountCode(fr_value))
}
}

// Deserialize the string using the AccountCodeVisitor
deserializer.deserialize_str(AccountCodeVisitor)
}
}

impl AccountCode {
/// Constructs a new `AccountCode` using a random number generator.
///
Expand Down
10 changes: 8 additions & 2 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ fn init_logger() -> slog::Logger {
slog_async::Async::new(slog::Duplicate(log_terminal_json_drain, log_file_drain).fuse())
.build()
.fuse();
slog::Logger::root(log_drain, o!("version" => env!("CARGO_PKG_VERSION")))
slog::Logger::root(
log_drain,
o!("version" => env::var("CARGO_PKG_VERSION").unwrap_or_else(|_| "unknown".to_string())),
)
} else {
// Otherwise, use formatted text for terminal and JSON for file logging.
let log_drain =
Expand All @@ -75,6 +78,9 @@ fn init_logger() -> slog::Logger {
.overflow_strategy(slog_async::OverflowStrategy::Block) // Set overflow strategy to block when the channel is full.
.build()
.fuse();
slog::Logger::root(log_drain, o!("version" => env!("CARGO_PKG_VERSION")))
slog::Logger::root(
log_drain,
o!("version" => env::var("CARGO_PKG_VERSION").unwrap_or_else(|_| "unknown".to_string())),
)
}
}

0 comments on commit 2b45e56

Please sign in to comment.