Skip to content

Commit

Permalink
feat: fixes and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Bisht13 committed Apr 2, 2024
1 parent bd01fc6 commit 4eae00a
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 164 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

24 changes: 0 additions & 24 deletions packages/relayer/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EmailAuth<SignerM>, 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,
Expand Down Expand Up @@ -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 {
Expand Down
89 changes: 0 additions & 89 deletions packages/relayer/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<Option<Request>> {
let row = sqlx::query("SELECT * FROM requests WHERE request_id = $1")
Expand Down Expand Up @@ -264,77 +248,4 @@ impl Database {
);
Ok(())
}

pub(crate) async fn get_request_status(&self, request_id: u64) -> Result<Option<Request>> {
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<bool> = row.get("is_success");
let email_nullifier: Option<String> = row.get("email_nullifier");
let account_salt: Option<String> = 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(())
}
}
2 changes: 1 addition & 1 deletion packages/relayer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions packages/relayer/src/modules/email_client/mail.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
42 changes: 0 additions & 42 deletions packages/relayer/src/utils/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<U256> {
let bytes: Vec<u8> = 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<Fr> {
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);
Expand Down
1 change: 1 addition & 0 deletions packages/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
41 changes: 41 additions & 0 deletions packages/utils/src/converters.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -150,3 +151,43 @@ pub fn vec_u8_to_bigint(bytes: Vec<u8>) -> 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<Fr, hex::FromHexError> {
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<U256, hex::FromHexError> {
let bytes: Vec<u8> = hex::decode(&hex[2..])?;
let mut array = [0u8; 32];
array.copy_from_slice(&bytes);
Ok(U256::from_big_endian(&array))
}
6 changes: 1 addition & 5 deletions packages/utils/src/cryptos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,10 +22,6 @@ impl RelayerRand {
let value = poseidon_bytes(seed)?;
Ok(Self(value))
}

// pub fn hash(&self) -> Result<Fr, PoseidonError> {
// poseidon_fields(&[self.0])
// }
}

#[derive(Debug, Clone)]
Expand Down

0 comments on commit 4eae00a

Please sign in to comment.