Skip to content

Commit

Permalink
Merge pull request #93 from zkfriendly/feat/generic-relayer-new-api-1130
Browse files Browse the repository at this point in the history
refactor: relocate `EmailAuthMsg` implementations
  • Loading branch information
Bisht13 authored Dec 3, 2024
2 parents 80fe712 + 37507ee commit 67b4bd5
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 63 deletions.
82 changes: 82 additions & 0 deletions packages/relayer/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

use anyhow::Result;
use axum::{
extract::State,
http::{request, StatusCode},
Expand All @@ -13,13 +14,94 @@ use slog::{error, info, trace};
use uuid::Uuid;

use crate::{
abis::EmailAuthMsg,
command::parse_command_template,
constants::REQUEST_ID_REGEX,
mail::{handle_email, handle_email_event, EmailEvent},
model::{create_request, get_request, update_request, RequestStatus},
schema::{AccountSaltSchema, EmailTxAuthSchema},
RelayerState,
};
use serde::Serialize;

use crate::abis::EmailProof;
use sqlx::PgPool;

/// Represents an email authentication message with associated proof and parameters.
/// This implementation provides database persistence capabilities.
impl EmailAuthMsg {
/// Saves the email authentication message to the database.
///
/// # Arguments
/// * `pool` - PostgreSQL connection pool
/// * `request_id` - Unique identifier for the request
///
/// # Returns
/// * `Result<()>` - Success or error during database operation
pub async fn save(&self, pool: &PgPool, request_id: Uuid) -> Result<()> {
sqlx::query!(
"INSERT INTO email_auth_messages (request_id, response) VALUES ($1, $2)",
request_id.to_string(),
serde_json::to_value(self)?
)
.execute(pool)
.await?;
Ok(())
}
}

/// Custom serialization implementation for EmailAuthMsg.
/// Ensures consistent JSON output format for the authentication message.
impl Serialize for EmailAuthMsg {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
use serde::ser::SerializeStruct;

let mut state = serializer.serialize_struct("EmailAuthMsg", 4)?;
state.serialize_field("templateId", &self.template_id)?;
state.serialize_field("commandParams", &self.command_params)?;
state.serialize_field(
"skippedCommandPrefix",
&self.skipped_command_prefix.as_u128(),
)?;
state.serialize_field("proof", &self.proof)?;
state.end()
}
}

/// Custom serialization implementation for EmailProof.
/// Formats fields with proper hexadecimal encoding and '0x' prefixes.
impl Serialize for EmailProof {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
use serde::ser::SerializeStruct;

// Helper function to format hex values consistently
fn format_hex(bytes: &[u8]) -> String {
format!("0x{}", ethers::utils::hex::encode(bytes))
}

let mut state = serializer.serialize_struct("EmailProof", 8)?;

// Basic fields
state.serialize_field("domainName", &self.domain_name)?;
state.serialize_field("publicKeyHash", &format_hex(&self.public_key_hash))?;
state.serialize_field("timestamp", &self.timestamp.as_u64())?;
state.serialize_field("maskedCommand", &self.masked_command)?;

// Proof fields
state.serialize_field("emailNullifier", &format_hex(&self.email_nullifier))?;
state.serialize_field("accountSalt", &format_hex(&self.account_salt))?;
state.serialize_field("isCodeExist", &self.is_code_exist)?;
state.serialize_field("proof", &format_hex(&self.proof))?;

state.end()
}
}

/// Checks the health of the service and returns a JSON response.
///
Expand Down
63 changes: 0 additions & 63 deletions packages/relayer/src/model.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use anyhow::{Error, Ok, Result};
use chrono::NaiveDateTime;
use ethers::utils::hex;
use serde::{Deserialize, Serialize};
use sqlx::types::Json;
use sqlx::{FromRow, PgPool};
use uuid::Uuid;

use crate::abis::{EmailAuthMsg, EmailProof};
use crate::schema::EmailTxAuthSchema;

/// Represents a request model with details about the request status and associated email transaction authentication.
Expand Down Expand Up @@ -39,67 +37,6 @@ pub struct ExpectedReplyModel {
pub created_at: chrono::DateTime<chrono::Utc>,
}

impl EmailAuthMsg {
pub async fn save(&self, pool: &PgPool, request_id: Uuid) -> Result<()> {
sqlx::query!(
"INSERT INTO email_auth_messages (request_id, response) VALUES ($1, $2)",
request_id.to_string(),
serde_json::to_value(self)?
)
.execute(pool)
.await?;
Ok(())
}
}

impl Serialize for EmailAuthMsg {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
use serde::ser::SerializeStruct;

let mut state = serializer.serialize_struct("EmailAuthMsg", 4)?;
state.serialize_field("templateId", &self.template_id)?;
state.serialize_field("commandParams", &self.command_params)?;
state.serialize_field(
"skippedCommandPrefix",
&self.skipped_command_prefix.as_u128(),
)?;
state.serialize_field("proof", &self.proof)?;
state.end()
}
}

impl Serialize for EmailProof {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
use serde::ser::SerializeStruct;

let mut state = serializer.serialize_struct("EmailProof", 8)?;
state.serialize_field("domainName", &self.domain_name)?;
state.serialize_field(
"publicKeyHash",
&format!("0x{}", hex::encode(self.public_key_hash)),
)?;
state.serialize_field("timestamp", &self.timestamp.as_u64())?;
state.serialize_field("maskedCommand", &self.masked_command)?;
state.serialize_field(
"emailNullifier",
&format!("0x{}", hex::encode(self.email_nullifier)),
)?;
state.serialize_field(
"accountSalt",
&format!("0x{}", hex::encode(self.account_salt)),
)?;
state.serialize_field("isCodeExist", &self.is_code_exist)?;
state.serialize_field("proof", &format!("0x{}", hex::encode(&self.proof)))?;
state.end()
}
}

#[derive(Debug, Serialize, Deserialize, sqlx::Type)]
#[sqlx(type_name = "status_enum")]
pub enum RequestStatus {
Expand Down

0 comments on commit 67b4bd5

Please sign in to comment.