From 58c4b1437df844ffd9c6dff0a0b3726d5a1f0186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Rz=C4=85d?= Date: Fri, 9 Feb 2024 18:53:45 +0300 Subject: [PATCH] fix: lowercase addresses (#173) * fix: lowercase addresses * chore: format * chore: refactor and fix errors --------- Co-authored-by: Chris Smith --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/handlers/identity/register.rs | 2 ++ src/stores/keys.rs | 53 +++++++++++++++++++++++-------- 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2d06cb7..8426f8c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3525,7 +3525,7 @@ checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" [[package]] name = "relay_rpc" version = "0.1.0" -source = "git+https://github.com/WalletConnect/WalletConnectRust.git?tag=v0.26.0#7a205511a98011e4fc0cffb7a3652d435b191cd7" +source = "git+https://github.com/WalletConnect/WalletConnectRust.git?tag=v0.26.1#6a24d5f63a4c428e9a3d7832af3b0fdef2e70905" dependencies = [ "alloy-json-abi", "alloy-json-rpc", diff --git a/Cargo.toml b/Cargo.toml index d1358bb..f2a5212 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ build = "build.rs" [dependencies] wc = { git = "https://github.com/WalletConnect/utils-rs.git", tag = "v0.7.0", features = ["geoip", "geoblock"] } -relay_rpc = { git = "https://github.com/WalletConnect/WalletConnectRust.git", tag = "v0.26.0", features = ["cacao"] } +relay_rpc = { git = "https://github.com/WalletConnect/WalletConnectRust.git", tag = "v0.26.1", features = ["cacao"] } aws-config = "0.56" aws-sdk-s3 = "0.31" diff --git a/src/handlers/identity/register.rs b/src/handlers/identity/register.rs index cbb306a..2e97c9a 100644 --- a/src/handlers/identity/register.rs +++ b/src/handlers/identity/register.rs @@ -63,6 +63,8 @@ pub async fn handler( error })?; + // Note to future: accounts can have both ERC-55 and lowercase variants, with duplicates. Make sure these are merged/treated as the same account + // See for context: https://github.com/WalletConnect/keys-server/pull/173 state .keys_persitent_storage .create_account_if_not_exists_and_add_identity_key( diff --git a/src/stores/keys.rs b/src/stores/keys.rs index 32bfcfe..80bdcbe 100644 --- a/src/stores/keys.rs +++ b/src/stores/keys.rs @@ -166,24 +166,51 @@ impl KeysPersistentStorage for MongoPersistentStorage { account: &str, identity_key: &str, ) -> Result<(), StoreError> { - let filter = doc! { - "account": &account, + let err = || { + Err(StoreError::NotFound( + "Account".to_string(), + account.to_string(), + )) }; - let update = doc! { - "$pull": { - "identities" : { - "identity_key": &identity_key, + async fn query( + db: &Database, + account: &str, + identity_key: &str, + ) -> wither::Result> { + let filter = doc! { + "account": account, + }; + + let update = doc! { + "$pull": { + "identities" : { + "identity_key": identity_key, + } } - } - }; + }; - match MongoKeys::find_one_and_update(&self.db, filter, update, None).await? { + MongoKeys::find_one_and_update(db, filter, update, None).await + } + + match query(&self.db, account, identity_key).await? { Some(_) => Ok(()), - None => Err(StoreError::NotFound( - "Account".to_string(), - account.to_string(), - )), + None => { + // Note to future: accounts can have both ERC-55 and lowercase variants, with duplicates. Make sure these are merged/treated as the same account + // See for context: https://github.com/WalletConnect/keys-server/pull/173 + + // Checking if eip155 so we don't try to lowercase accounts from other chains that were + // not affected (and may have different case-sensitivity rules) + if account.starts_with("eip155") { + let lowercase_account = account.to_lowercase(); + match query(&self.db, &lowercase_account, identity_key).await? { + Some(_) => Ok(()), + None => err(), + } + } else { + err() + } + } } }