Rewrite wallet address validator to work with non-ethereum addresses
This commit is contained in:
parent
7a48563593
commit
596871e6e3
5 changed files with 42 additions and 30 deletions
|
@ -1,6 +1,7 @@
|
|||
# https://github.com/rust-lang/cargo/issues/5034#issuecomment-927105016
|
||||
[target.'cfg(feature = "cargo-clippy")']
|
||||
rustflags = [
|
||||
"-Aclippy::derive_partial_eq_without_eq",
|
||||
"-Aclippy::len_zero",
|
||||
"-Aclippy::let_and_return",
|
||||
"-Aclippy::map_entry",
|
||||
|
|
|
@ -10,8 +10,8 @@ use crate::models::users::queries::{
|
|||
get_user_by_name,
|
||||
get_user_by_wallet_address,
|
||||
};
|
||||
use crate::models::users::types::validate_wallet_address;
|
||||
use crate::utils::crypto::verify_password;
|
||||
use crate::utils::currencies::validate_wallet_address;
|
||||
use super::types::{TokenRequest, TokenResponse};
|
||||
use super::utils::generate_access_token;
|
||||
|
||||
|
@ -36,7 +36,7 @@ async fn token_view(
|
|||
// DEPRECATED
|
||||
let wallet_address = request_data.wallet_address.as_ref()
|
||||
.ok_or(ValidationError("wallet address is required"))?;
|
||||
validate_wallet_address(wallet_address)?;
|
||||
validate_wallet_address(&config.default_currency(), wallet_address)?;
|
||||
get_user_by_wallet_address(db_client, wallet_address).await?
|
||||
},
|
||||
"eip4361" => {
|
||||
|
|
|
@ -22,10 +22,8 @@ use crate::models::profiles::queries::{
|
|||
search_profile_by_wallet_address,
|
||||
};
|
||||
use crate::models::profiles::types::DbActorProfile;
|
||||
use crate::models::users::types::{
|
||||
validate_wallet_address,
|
||||
User,
|
||||
};
|
||||
use crate::models::users::types::User;
|
||||
use crate::utils::currencies::{validate_wallet_address, Currency};
|
||||
use super::types::SearchResults;
|
||||
|
||||
enum SearchQuery {
|
||||
|
@ -60,7 +58,11 @@ fn parse_search_query(search_query: &str) -> SearchQuery {
|
|||
if Url::parse(search_query).is_ok() {
|
||||
return SearchQuery::Url(search_query.to_string());
|
||||
};
|
||||
if validate_wallet_address(&search_query.to_lowercase()).is_ok() {
|
||||
// TODO: support other currencies
|
||||
if validate_wallet_address(
|
||||
&Currency::Ethereum,
|
||||
&search_query.to_lowercase(),
|
||||
).is_ok() {
|
||||
return SearchQuery::WalletAddress(search_query.to_string());
|
||||
};
|
||||
match parse_profile_query(search_query) {
|
||||
|
@ -163,11 +165,11 @@ pub async fn search(
|
|||
};
|
||||
},
|
||||
SearchQuery::WalletAddress(address) => {
|
||||
// Search by wallet address, assuming default currency (ethereum)
|
||||
// Search by wallet address, assuming it's ethereum address
|
||||
// TODO: support other currencies
|
||||
profiles = search_profile_by_wallet_address(
|
||||
db_client,
|
||||
&config.default_currency(),
|
||||
&Currency::Ethereum,
|
||||
&address,
|
||||
false,
|
||||
).await?;
|
||||
|
|
|
@ -71,16 +71,6 @@ pub fn validate_local_username(username: &str) -> Result<(), ValidationError> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Verifies that wallet address is valid ethereum address
|
||||
pub fn validate_wallet_address(wallet_address: &str) -> Result<(), ValidationError> {
|
||||
// Address should be lowercase
|
||||
let address_regexp = Regex::new(r"^0x[a-f0-9]{40}$").unwrap();
|
||||
if !address_regexp.is_match(wallet_address) {
|
||||
return Err(ValidationError("address is not lowercase"));
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -101,14 +91,4 @@ mod tests {
|
|||
let result_2 = validate_local_username("name&");
|
||||
assert_eq!(result_2.is_ok(), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_wallet_address() {
|
||||
let result_1 = validate_wallet_address("0xab5801a7d398351b8be11c439e05c5b3259aec9b");
|
||||
assert_eq!(result_1.is_ok(), true);
|
||||
let result_2 = validate_wallet_address("ab5801a7d398351b8be11c439e05c5b3259aec9b");
|
||||
assert_eq!(result_2.is_ok(), false);
|
||||
let result_3 = validate_wallet_address("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B");
|
||||
assert_eq!(result_3.is_ok(), false);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
use std::convert::TryFrom;
|
||||
|
||||
use crate::errors::ConversionError;
|
||||
use regex::Regex;
|
||||
|
||||
use crate::errors::{ConversionError, ValidationError};
|
||||
use super::caip2::ChainId;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
|
@ -58,6 +60,22 @@ impl TryFrom<&ChainId> for Currency {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn validate_wallet_address(
|
||||
currency: &Currency,
|
||||
wallet_address: &str,
|
||||
) -> Result<(), ValidationError> {
|
||||
match currency {
|
||||
Currency::Ethereum => {
|
||||
// Address should be lowercase
|
||||
let address_regexp = Regex::new(r"^0x[a-f0-9]{40}$").unwrap();
|
||||
if !address_regexp.is_match(wallet_address) {
|
||||
return Err(ValidationError("address is not lowercase"));
|
||||
};
|
||||
},
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -75,4 +93,15 @@ mod tests {
|
|||
let ethereum = Currency::Ethereum;
|
||||
assert_eq!(ethereum.field_name(), "$ETH");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_wallet_address() {
|
||||
let ethereum = Currency::Ethereum;
|
||||
let result_1 = validate_wallet_address(ðereum, "0xab5801a7d398351b8be11c439e05c5b3259aec9b");
|
||||
assert_eq!(result_1.is_ok(), true);
|
||||
let result_2 = validate_wallet_address(ðereum, "ab5801a7d398351b8be11c439e05c5b3259aec9b");
|
||||
assert_eq!(result_2.is_ok(), false);
|
||||
let result_3 = validate_wallet_address(ðereum, "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B");
|
||||
assert_eq!(result_3.is_ok(), false);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue