diff --git a/services/identity/src/client_service.rs b/services/identity/src/client_service.rs --- a/services/identity/src/client_service.rs +++ b/services/identity/src/client_service.rs @@ -36,11 +36,11 @@ use crate::id::generate_uuid; use crate::nonce::generate_nonce_data; use crate::reserved_users::{ - is_valid_ethereum_address, validate_add_reserved_usernames_message, + validate_add_reserved_usernames_message, validate_remove_reserved_username_message, validate_signed_account_ownership_message, }; -use crate::siwe::parse_and_verify_siwe_message; +use crate::siwe::{is_valid_ethereum_address, parse_and_verify_siwe_message}; use crate::token::{AccessTokenData, AuthType}; pub use client_proto::identity_client_service_server::{ IdentityClientService, IdentityClientServiceServer, diff --git a/services/identity/src/reserved_users.rs b/services/identity/src/reserved_users.rs --- a/services/identity/src/reserved_users.rs +++ b/services/identity/src/reserved_users.rs @@ -2,7 +2,6 @@ use chrono::{DateTime, Utc}; use constant_time_eq::constant_time_eq; use ed25519_dalek::{PublicKey, Signature, Verifier}; -use regex::Regex; use serde::Deserialize; use tonic::Status; @@ -113,52 +112,3 @@ Ok(deserialized_message.payload) } - -pub fn is_valid_ethereum_address(candidate: &str) -> bool { - let ethereum_address_regex = Regex::new(r"^0x[a-fA-F0-9]{40}$").unwrap(); - ethereum_address_regex.is_match(candidate) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_valid_ethereum_address() { - assert!(is_valid_ethereum_address( - "0x1234567890123456789012345678901234567890" - ),); - assert!(is_valid_ethereum_address( - "0xABCDEF123456789012345678901234567890ABCD" - )); - assert!(is_valid_ethereum_address( - "0xabcdef123456789012345678901234567890abcd" - )); - } - - #[test] - fn test_invalid_ethereum_address() { - // Shorter than 42 characters - assert_eq!( - is_valid_ethereum_address("0x12345678901234567890123456789012345678"), - false - ); - // Longer than 42 characters - assert_eq!( - is_valid_ethereum_address("0x123456789012345678901234567890123456789012"), - false - ); - // Missing 0x prefix - assert_eq!( - is_valid_ethereum_address("1234567890123456789012345678901234567890"), - false - ); - // Contains invalid characters - assert_eq!( - is_valid_ethereum_address("0x1234567890GHIJKL9012345678901234567890"), - false - ); - // Empty string - assert_eq!(is_valid_ethereum_address(""), false); - } -} diff --git a/services/identity/src/siwe.rs b/services/identity/src/siwe.rs --- a/services/identity/src/siwe.rs +++ b/services/identity/src/siwe.rs @@ -1,4 +1,5 @@ use chrono::Utc; +use regex::Regex; use siwe::Message; use tonic::Status; use tracing::error; @@ -32,3 +33,52 @@ Ok(siwe_message) } + +pub fn is_valid_ethereum_address(candidate: &str) -> bool { + let ethereum_address_regex = Regex::new(r"^0x[a-fA-F0-9]{40}$").unwrap(); + ethereum_address_regex.is_match(candidate) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_valid_ethereum_address() { + assert!(is_valid_ethereum_address( + "0x1234567890123456789012345678901234567890" + ),); + assert!(is_valid_ethereum_address( + "0xABCDEF123456789012345678901234567890ABCD" + )); + assert!(is_valid_ethereum_address( + "0xabcdef123456789012345678901234567890abcd" + )); + } + + #[test] + fn test_invalid_ethereum_address() { + // Shorter than 42 characters + assert_eq!( + is_valid_ethereum_address("0x12345678901234567890123456789012345678"), + false + ); + // Longer than 42 characters + assert_eq!( + is_valid_ethereum_address("0x123456789012345678901234567890123456789012"), + false + ); + // Missing 0x prefix + assert_eq!( + is_valid_ethereum_address("1234567890123456789012345678901234567890"), + false + ); + // Contains invalid characters + assert_eq!( + is_valid_ethereum_address("0x1234567890GHIJKL9012345678901234567890"), + false + ); + // Empty string + assert_eq!(is_valid_ethereum_address(""), false); + } +}