From 4eae00aeb17d1fe1797bc3f2cba85d45b12df1e0 Mon Sep 17 00:00:00 2001 From: Aditya Bisht Date: Wed, 3 Apr 2024 00:56:50 +0530 Subject: [PATCH] feat: fixes and refactoring --- Cargo.lock | 1 + packages/relayer/src/chain.rs | 24 ----- packages/relayer/src/database.rs | 89 ------------------- packages/relayer/src/lib.rs | 2 +- .../relayer/src/modules/email_client/mail.rs | 6 +- packages/relayer/src/utils/utils.rs | 42 --------- packages/utils/Cargo.toml | 1 + packages/utils/src/converters.rs | 41 +++++++++ packages/utils/src/cryptos.rs | 6 +- 9 files changed, 48 insertions(+), 164 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c68d2d68..2370d864 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3985,6 +3985,7 @@ dependencies = [ "anyhow", "base64 0.21.7", "cfdkim", + "ethers", "fancy-regex", "halo2curves 0.4.0", "hex", diff --git a/packages/relayer/src/chain.rs b/packages/relayer/src/chain.rs index fb22fe53..90dd8582 100644 --- a/packages/relayer/src/chain.rs +++ b/packages/relayer/src/chain.rs @@ -25,26 +25,6 @@ impl ChainClient { Ok(Self { client }) } - pub fn self_eth_addr(&self) -> Address { - self.client.address() - } - - pub async fn get_email_auth( - &self, - wallet_addr: &String, - account_key: &String, - ) -> Result, anyhow::Error> { - let wallet_address: H160 = wallet_addr.parse()?; - let account_salt: H256 = account_key.parse()?; - let contract = EmailAccountRecovery::new(wallet_address, self.client.clone()); - let email_auth = contract - .compute_email_auth_address(account_salt.into()) - .call() - .await?; - - Ok(EmailAuth::new(email_auth, self.client.clone())) - } - pub async fn set_dkim_public_key_hash( &self, selector: String, @@ -93,10 +73,6 @@ impl ChainClient { Ok(ECDSAOwnedDKIMRegistry::new(dkim, self.client.clone())) } - pub async fn get_latest_block_number(&self) -> U64 { - self.client.get_block_number().await.unwrap() - } - pub async fn is_wallet_deployed(&self, wallet_addr_str: &String) -> bool { let wallet_addr: H160 = wallet_addr_str.parse().unwrap(); match self.client.get_code(wallet_addr, None).await { diff --git a/packages/relayer/src/database.rs b/packages/relayer/src/database.rs index f4d5fd8d..4faa6103 100644 --- a/packages/relayer/src/database.rs +++ b/packages/relayer/src/database.rs @@ -96,7 +96,6 @@ impl Database { } } - #[named] pub(crate) async fn is_email_registered(&self, email_addr: &str) -> bool { let row = sqlx::query("SELECT * FROM codes WHERE guardian_email_addr = $1") .bind(email_addr) @@ -155,21 +154,6 @@ impl Database { } } - #[named] - pub async fn set_guardian_in_credentials(&self, account_code: &str) -> Result<()> { - info!(LOG, "account_code {}", account_code; "func" => function_name!()); - let res = sqlx::query("UPDATE codes SET is_set = TRUE WHERE account_code = $1") - .bind(account_code) - .execute(&self.db) - .await?; - info!( - LOG, - "updated result: {:?}", - res; "func" => function_name!() - ); - Ok(()) - } - #[named] pub(crate) async fn get_request(&self, request_id: u64) -> Result> { let row = sqlx::query("SELECT * FROM requests WHERE request_id = $1") @@ -264,77 +248,4 @@ impl Database { ); Ok(()) } - - pub(crate) async fn get_request_status(&self, request_id: u64) -> Result> { - let row = sqlx::query("SELECT * FROM requests WHERE request_id = $1") - .bind(request_id as i64) - .fetch_optional(&self.db) - .await?; - - match row { - Some(row) => { - let request_id: i64 = row.get("request_id"); - let wallet_eth_addr: String = row.get("wallet_eth_addr"); - let guardian_email_addr: String = row.get("guardian_email_addr"); - let is_for_recovery: bool = row.get("is_for_recovery"); - let template_idx: i32 = row.get("template_idx"); - let is_processed: bool = row.get("is_processed"); - let is_success: Option = row.get("is_success"); - let email_nullifier: Option = row.get("email_nullifier"); - let account_salt: Option = row.get("account_salt"); - let requests_row = Request { - request_id: request_id as u64, - wallet_eth_addr, - guardian_email_addr, - is_for_recovery, - template_idx: template_idx as u64, - is_processed, - is_success, - email_nullifier, - account_salt, - }; - Ok(Some(requests_row)) - } - None => Ok(None), - } - } - - #[named] - pub(crate) async fn request_completed( - &self, - request_id: u64, - email_nullifier: &str, - account_salt: &str, - ) -> Result<()> { - info!(LOG, "request_id {}", request_id; "func" => function_name!()); - let res = sqlx::query("UPDATE requests SET is_processed = TRUE, is_success = TRUE, email_nullifier = $1, account_salt = $2, WHERE request_id = $3") - .bind(email_nullifier) - .bind(account_salt) - .bind(request_id as i64) - .execute(&self.db) - .await?; - info!( - LOG, - "updated result: {:?}", - res; "func" => function_name!() - ); - Ok(()) - } - - #[named] - pub(crate) async fn request_failed(&self, request_id: u64) -> Result<()> { - info!(LOG, "request_id {}", request_id; "func" => function_name!()); - let res = sqlx::query( - "UPDATE requests SET is_processed = TRUE, is_success = FALSE WHERE request_id = $1", - ) - .bind(request_id as i64) - .execute(&self.db) - .await?; - info!( - LOG, - "updated result: {:?}", - res; "func" => function_name!() - ); - Ok(()) - } } diff --git a/packages/relayer/src/lib.rs b/packages/relayer/src/lib.rs index ff97d34b..a8bf3c26 100644 --- a/packages/relayer/src/lib.rs +++ b/packages/relayer/src/lib.rs @@ -26,7 +26,7 @@ use anyhow::{anyhow, bail, Result}; use dotenv::dotenv; use ethers::prelude::*; use lazy_static::lazy_static; -use relayer_utils::{converters::*, cryptos::*, parse_email::ParsedEmail, Fr}; +use relayer_utils::{converters::*, cryptos::*, parse_email::ParsedEmail}; use slog::{error, info, trace}; use std::env; use std::future::Future; diff --git a/packages/relayer/src/modules/email_client/mail.rs b/packages/relayer/src/modules/email_client/mail.rs index edb58af3..b0bafa50 100644 --- a/packages/relayer/src/modules/email_client/mail.rs +++ b/packages/relayer/src/modules/email_client/mail.rs @@ -1,8 +1,8 @@ use crate::{ - check_domain_sign_reply_to, error, render_html, split_email_address, EmailForwardSender, - EmailMessage, Future, Result, DB, LOG, RELAYER_EMAIL_ADDRESS, + error, render_html, EmailForwardSender, + EmailMessage, Future, Result, LOG, }; -use anyhow::anyhow; + use std::pin::Pin; #[derive(Debug, Clone)] diff --git a/packages/relayer/src/utils/utils.rs b/packages/relayer/src/utils/utils.rs index b6e387b0..c3042813 100644 --- a/packages/relayer/src/utils/utils.rs +++ b/packages/relayer/src/utils/utils.rs @@ -10,7 +10,6 @@ use ::serde::{Deserialize, Serialize}; use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; -use std::path::Path; const DOMAIN_FIELDS: usize = 9; const SUBJECT_FIELDS: usize = 17; @@ -135,47 +134,6 @@ pub async fn generate_proof( Ok((proof, pub_signals)) } -pub fn u256_to_bytes32(x: &U256) -> [u8; 32] { - let mut bytes = [0u8; 32]; - x.to_big_endian(&mut bytes); - bytes -} - -pub fn u256_to_bytes32_little(x: &U256) -> [u8; 32] { - let mut bytes = [0u8; 32]; - x.to_little_endian(&mut bytes); - bytes -} - -pub fn u256_to_hex(x: &U256) -> String { - "0x".to_string() + &hex::encode(u256_to_bytes32(x)) -} - -pub fn bytes32_to_hex(bytes: &[u8; 32]) -> String { - "0x".to_string() + &hex::encode(bytes) -} - -pub fn hex_to_u256(hex: &str) -> Result { - let bytes: Vec = hex::decode(&hex[2..])?; - let mut array = [0u8; 32]; - array.copy_from_slice(&bytes); - Ok(U256::from_big_endian(&array)) -} - -pub fn fr_to_bytes32(fr: &Fr) -> Result<[u8; 32]> { - let hex = field2hex(fr); - let bytes = hex::decode(&hex[2..])?; - let mut result = [0u8; 32]; - result.copy_from_slice(&bytes); - Ok(result) -} - -pub fn bytes32_to_fr(bytes32: &[u8; 32]) -> Result { - let hex: String = "0x".to_string() + &hex::encode(bytes32); - let field = hex2field(&hex)?; - Ok(field) -} - pub fn calculate_default_hash(input: &str) -> String { let mut hasher = DefaultHasher::new(); input.hash(&mut hasher); diff --git a/packages/utils/Cargo.toml b/packages/utils/Cargo.toml index 180c9c07..a18523ec 100644 --- a/packages/utils/Cargo.toml +++ b/packages/utils/Cargo.toml @@ -41,4 +41,5 @@ num-traits = "0.2.15" cfdkim = { version = "0.3.0", git = "https://github.com/SoraSuegami/dkim.git" } slog = "2.7.0" sha2 = "0.10.8" +ethers = "2.0.14" # wasm-bindgen = { version = "0.2.90", default-features = false } diff --git a/packages/utils/src/converters.rs b/packages/utils/src/converters.rs index 98ee9a86..194833e2 100644 --- a/packages/utils/src/converters.rs +++ b/packages/utils/src/converters.rs @@ -1,6 +1,7 @@ use std::convert::TryInto; use anyhow; +use ethers::types::U256; use halo2curves::ff::PrimeField; use itertools::Itertools; use num_bigint::BigInt; @@ -150,3 +151,43 @@ pub fn vec_u8_to_bigint(bytes: Vec) -> BigInt { .iter() .fold(BigInt::from(0), |acc, &b| (acc << 8) | BigInt::from(b)) } + +pub fn fr_to_bytes32(fr: &Fr) -> Result<[u8; 32], hex::FromHexError> { + let hex = field2hex(fr); + let bytes = hex::decode(&hex[2..])?; + let mut result = [0u8; 32]; + result.copy_from_slice(&bytes); + Ok(result) +} + +pub fn bytes32_to_fr(bytes32: &[u8; 32]) -> Result { + let hex: String = "0x".to_string() + &hex::encode(bytes32); + hex2field(&hex).map_err(|_e| hex::FromHexError::InvalidStringLength) +} + +pub fn u256_to_bytes32(x: &U256) -> [u8; 32] { + let mut bytes = [0u8; 32]; + x.to_big_endian(&mut bytes); + bytes +} + +pub fn u256_to_bytes32_little(x: &U256) -> [u8; 32] { + let mut bytes = [0u8; 32]; + x.to_little_endian(&mut bytes); + bytes +} + +pub fn u256_to_hex(x: &U256) -> String { + "0x".to_string() + &hex::encode(u256_to_bytes32(x)) +} + +pub fn bytes32_to_hex(bytes: &[u8; 32]) -> String { + "0x".to_string() + &hex::encode(bytes) +} + +pub fn hex_to_u256(hex: &str) -> Result { + let bytes: Vec = hex::decode(&hex[2..])?; + let mut array = [0u8; 32]; + array.copy_from_slice(&bytes); + Ok(U256::from_big_endian(&array)) +} diff --git a/packages/utils/src/cryptos.rs b/packages/utils/src/cryptos.rs index 2d23c71e..85346f93 100644 --- a/packages/utils/src/cryptos.rs +++ b/packages/utils/src/cryptos.rs @@ -4,7 +4,7 @@ use crate::converters::*; use halo2curves::ff::Field; use poseidon_rs::*; -use rand_core::{OsRng, RngCore}; +use rand_core::RngCore; use rsa::sha2::{Digest, Sha256}; pub use zk_regex_apis::padding::pad_string; @@ -22,10 +22,6 @@ impl RelayerRand { let value = poseidon_bytes(seed)?; Ok(Self(value)) } - - // pub fn hash(&self) -> Result { - // poseidon_fields(&[self.0]) - // } } #[derive(Debug, Clone)]