Skip to content

Commit

Permalink
Merge branch 'feat/safe-2fa' of https://github.com/zkemail/email-wallet
Browse files Browse the repository at this point in the history
… into feat/safe-2fa
  • Loading branch information
Divide-By-0 committed May 22, 2024
2 parents d23291d + d592a0b commit f053391
Show file tree
Hide file tree
Showing 23 changed files with 399 additions and 406 deletions.
372 changes: 151 additions & 221 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions Relayer.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@ RUN cargo build
# Expose port
EXPOSE 4500

# Set the default command
CMD ["cargo", "run"]
# Make sure the script is executable
RUN chmod +x /relayer/packages/relayer/setup_and_run.sh

# Run the script
CMD ["/bin/bash", "/relayer/packages/relayer/setup_and_run.sh"]
3 changes: 2 additions & 1 deletion packages/relayer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ lettre = { version = "0.10.4", features = ["tokio1", "tokio1-native-tls"] }
ethers = { version = "2.0.10", features = ["abigen"] }
email-wallet-utils = { path = "../utils" }
futures = "0.3.28"
sqlx = { version = "=0.7.3", features = ["postgres", "runtime-tokio"] }
sqlx = { version = "=0.6.3", features = ["postgres", "runtime-tokio-native-tls"] }
regex = "1.10.2"
axum = "0.6.20"
rand = "0.8.5"
Expand All @@ -53,6 +53,7 @@ slog-json = "2.6.1"
file-rotate = "0.7.5"
function_name = "0.3.0"
base64 = "0.21.7"
serde_with = "3.8.1"


[build-dependencies]
Expand Down
13 changes: 13 additions & 0 deletions packages/relayer/setup_and_run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# Attempt to run the setup command
cargo run setup

# Check if the last command's exit code is not zero and matches the specific error
if [ $? -ne 0 ]; then
echo "Setup failed or encountered a known error, proceeding to run the main application..."
cargo run
fi

# Always run cargo run after attempting setup, regardless of the outcome
cargo run
4 changes: 2 additions & 2 deletions packages/relayer/src/abis/account_handler.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/relayer/src/abis/ecdsa_owned_dkim_registry.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/relayer/src/abis/erc20.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/relayer/src/abis/erc_721.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/relayer/src/abis/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,13 @@ pub mod email_wallet_events {
::ethers::core::abi::Abi,
> = ::ethers::contract::Lazy::new(__abi);
#[rustfmt::skip]
const __BYTECODE: &[u8] = b"`V`7`\x0B\x82\x82\x829\x80Q`\0\x1A`s\x14`*WcNH{q`\xE0\x1B`\0R`\0`\x04R`$`\0\xFD[0`\0R`s\x81S\x82\x81\xF3\xFEs\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x000\x14`\x80`@R`\0\x80\xFD\xFE\xA2dipfsX\"\x12 b\xCD\x16~\x03\xA9+@\xF1\x1C=\x87\xB2\xE5\x12\xD1k\xEE\xC4A\x1D*\xFF\xE6:\xE3_\xF6\x9C\x9Ei\xABdsolcC\0\x08\x17\x003";
const __BYTECODE: &[u8] = b"`V`7`\x0B\x82\x82\x829\x80Q`\0\x1A`s\x14`*WcNH{q`\xE0\x1B`\0R`\0`\x04R`$`\0\xFD[0`\0R`s\x81S\x82\x81\xF3\xFEs\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x000\x14`\x80`@R`\0\x80\xFD\xFE\xA2dipfsX\"\x12 \xF8\xB4K\xDA\x01\x9C'o\xC8\xE9>(\xD1\xAB\xC9S\x13n\x9F\xF9R\xBE\x13\x8B\x10R\xDAr\x80\xF0{\xDEdsolcC\0\x08\x17\x003";
/// The bytecode of the contract.
pub static EMAILWALLETEVENTS_BYTECODE: ::ethers::core::types::Bytes = ::ethers::core::types::Bytes::from_static(
__BYTECODE,
);
#[rustfmt::skip]
const __DEPLOYED_BYTECODE: &[u8] = b"s\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x000\x14`\x80`@R`\0\x80\xFD\xFE\xA2dipfsX\"\x12 b\xCD\x16~\x03\xA9+@\xF1\x1C=\x87\xB2\xE5\x12\xD1k\xEE\xC4A\x1D*\xFF\xE6:\xE3_\xF6\x9C\x9Ei\xABdsolcC\0\x08\x17\x003";
const __DEPLOYED_BYTECODE: &[u8] = b"s\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x000\x14`\x80`@R`\0\x80\xFD\xFE\xA2dipfsX\"\x12 \xF8\xB4K\xDA\x01\x9C'o\xC8\xE9>(\xD1\xAB\xC9S\x13n\x9F\xF9R\xBE\x13\x8B\x10R\xDAr\x80\xF0{\xDEdsolcC\0\x08\x17\x003";
/// The deployed bytecode of the contract.
pub static EMAILWALLETEVENTS_DEPLOYED_BYTECODE: ::ethers::core::types::Bytes = ::ethers::core::types::Bytes::from_static(
__DEPLOYED_BYTECODE,
Expand Down
4 changes: 2 additions & 2 deletions packages/relayer/src/abis/extension_handler.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/relayer/src/abis/nft_extension.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/relayer/src/abis/relayer_handler.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/relayer/src/abis/test_erc20.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/relayer/src/abis/token_registry.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/relayer/src/abis/unclaims_handler.rs

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions packages/relayer/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub async fn handle_email<P: EmailsPool>(
&field2hex(&account_key.0),
&res,
true,
&wallet_addr.to_string(),
&format!("0x{}", hex::encode(wallet_addr.as_bytes())),
)
.await?;
trace!(LOG, "User inserted"; "func" => function_name!());
Expand Down Expand Up @@ -132,7 +132,6 @@ pub async fn handle_email<P: EmailsPool>(
UNINSTALL_COMMAND => (0, extract_template_vals_uninstall(&subject)?),
EXIT_COMMAND => (0, extract_template_vals_exit(&subject)?),
DKIM_COMMAND => (0, extract_template_vals_dkim(&subject)?),
// SAFE_COMMAND => (0, extract_template_vals_safe_tx(&subject)?),
_ => {
let extension_addr = CLIENT
.query_user_extension_for_command(&wallet_salt, command.as_str())
Expand Down
74 changes: 54 additions & 20 deletions packages/relayer/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ impl Database {
email_address TEXT PRIMARY KEY,
account_key TEXT NOT NULL,
tx_hash TEXT NOT NULL,
is_onborded BOOLEAN NOT NULL DEFAULT FALSE
is_onborded BOOLEAN NOT NULL DEFAULT FALSE,
wallet_addr TEXT NOT NULL
);",
)
.execute(&self.db)
Expand All @@ -50,7 +51,8 @@ impl Database {

sqlx::query(
"CREATE TABLE IF NOT EXISTS safe (
wallet_addr TEXT PRIMARY KEY
wallet_addr TEXT PRIMARY KEY,
safe_addr TEXT[] NOT NULL
);",
)
.execute(&self.db)
Expand All @@ -59,8 +61,7 @@ impl Database {
sqlx::query(
"CREATE TABLE IF NOT EXISTS safe_txs (
tx_hash TEXT PRIMARY KEY,
wallet_addr TEXT NOT NULL,
is_completed BOOLEAN NOT NULL DEFAULT FALSE
wallet_addr TEXT NOT NULL
);",
)
.execute(&self.db)
Expand Down Expand Up @@ -334,18 +335,26 @@ impl Database {
}

pub async fn is_wallet_addr_exist(&self, wallet_addr: &str) -> Result<bool> {
let result = sqlx::query("SELECT 1 FROM users WHERE wallet_addr = $1")
let result = sqlx::query("SELECT 1 FROM users WHERE LOWER(wallet_addr) = LOWER($1)")
.bind(wallet_addr)
.fetch_optional(&self.db)
.await?;

Ok(result.is_some())
}

pub async fn add_user_with_safe(&self, wallet_addr: &str) -> Result<()> {
sqlx::query("INSERT INTO safe (wallet_addr) VALUES ($1)")
pub async fn add_user_with_safe(&self, wallet_addr: &str, safe_addr: &str) -> Result<()> {
let query = "
INSERT INTO safe (wallet_addr, safe_addr)
VALUES ($1, ARRAY[$2::TEXT])
ON CONFLICT (wallet_addr)
DO UPDATE SET safe_addr = array_append(safe.safe_addr, EXCLUDED.safe_addr[1])
RETURNING *;
";
sqlx::query(query)
.bind(wallet_addr)
.execute(&self.db)
.bind(safe_addr)
.fetch_one(&self.db)
.await?;
Ok(())
}
Expand All @@ -363,19 +372,48 @@ impl Database {
Ok(vec)
}

pub async fn delete_user_with_safe(&self, wallet_addr: &str) -> Result<()> {
sqlx::query("DELETE FROM safe WHERE wallet_addr = $1")
pub async fn get_safes_by_user(&self, wallet_addr: &str) -> Result<Vec<String>> {
let row = sqlx::query("SELECT safe_addr FROM safe WHERE wallet_addr = $1")
.bind(wallet_addr)
.execute(&self.db)
.fetch_one(&self.db)
.await?;
Ok(())
Ok(row.get("safe_addr"))
}

pub async fn get_email_by_wallet(&self, wallet_addr: &str) -> Result<String> {
let row = sqlx::query("SELECT email_address FROM users WHERE wallet_addr = $1")
pub async fn remove_safe_from_user(&self, wallet_addr: &str, safe_addr: &str) -> Result<()> {
let query = "
UPDATE safe
SET safe_addr = array_remove(safe_addr, $2)
WHERE wallet_addr = $1;
";
sqlx::query(query)
.bind(wallet_addr)
.bind(safe_addr)
.execute(&self.db)
.await?;

// If the safe_addr is empty, remove the wallet_addr from the safe table
let row = sqlx::query("SELECT safe_addr FROM safe WHERE wallet_addr = $1")
.bind(wallet_addr)
.fetch_one(&self.db)
.await?;
let safe_addr: Vec<String> = row.get("safe_addr");
if safe_addr.is_empty() {
sqlx::query("DELETE FROM safe WHERE wallet_addr = $1")
.bind(wallet_addr)
.execute(&self.db)
.await?;
}

Ok(())
}

pub async fn get_email_by_wallet(&self, wallet_addr: &str) -> Result<String> {
let row =
sqlx::query("SELECT email_address FROM users WHERE LOWER(wallet_addr) = LOWER($1)")
.bind(wallet_addr)
.fetch_one(&self.db)
.await?;
Ok(row.get("email_address"))
}

Expand All @@ -388,12 +426,8 @@ impl Database {
Ok(())
}

pub async fn has_approved_safe_tx_by_addr(
&self,
tx_hash: &str,
wallet_addr: &str,
) -> Result<bool> {
let result = sqlx::query("SELECT 1 FROM safe_txs WHERE tx_hash = $1 AND wallet_addr = $2 AND is_completed = TRUE")
pub async fn has_safe_tx_by_addr(&self, tx_hash: &str, wallet_addr: &str) -> Result<bool> {
let result = sqlx::query("SELECT 1 FROM safe_txs WHERE LOWER(tx_hash) = LOWER($1) AND LOWER(wallet_addr) = LOWER($2)")
.bind(tx_hash)
.bind(wallet_addr)
.fetch_optional(&self.db)
Expand Down
2 changes: 1 addition & 1 deletion packages/relayer/src/modules/claimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub async fn claim_unclaims(mut claim: Claim) -> Result<EmailWalletEvent> {
&account_key_str,
"",
false,
&wallet_addr.to_string(),
&format!("0x{}", hex::encode(wallet_addr.as_bytes())),
)
.await?;
return Ok(EmailWalletEvent::AccountNotCreated {
Expand Down
Loading

0 comments on commit f053391

Please sign in to comment.