2021-04-09 00:22:17 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
use postgres_types::FromSql;
|
|
|
|
use regex::Regex;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use crate::errors::ValidationError;
|
|
|
|
use crate::models::profiles::types::DbActorProfile;
|
|
|
|
|
|
|
|
#[derive(FromSql)]
|
|
|
|
#[postgres(name = "user_account")]
|
|
|
|
pub struct DbUser {
|
|
|
|
pub id: Uuid,
|
2021-12-24 17:46:01 +00:00
|
|
|
pub wallet_address: Option<String>,
|
2021-04-09 00:22:17 +00:00
|
|
|
pub password_hash: String,
|
|
|
|
pub private_key: String,
|
|
|
|
pub invite_code: Option<String>,
|
|
|
|
pub created_at: DateTime<Utc>,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Represents local user
|
|
|
|
#[derive(Clone)]
|
2021-11-20 01:10:56 +00:00
|
|
|
#[cfg_attr(test, derive(Default))]
|
2021-04-09 00:22:17 +00:00
|
|
|
pub struct User {
|
|
|
|
pub id: Uuid,
|
2021-12-24 17:46:01 +00:00
|
|
|
pub wallet_address: Option<String>,
|
2021-04-09 00:22:17 +00:00
|
|
|
pub password_hash: String,
|
|
|
|
pub private_key: String,
|
|
|
|
pub profile: DbActorProfile,
|
|
|
|
}
|
|
|
|
|
2021-11-12 22:05:31 +00:00
|
|
|
impl User {
|
|
|
|
pub fn new(
|
|
|
|
db_user: DbUser,
|
|
|
|
db_profile: DbActorProfile,
|
|
|
|
) -> Self {
|
|
|
|
assert_eq!(db_user.id, db_profile.id);
|
|
|
|
Self {
|
|
|
|
id: db_user.id,
|
|
|
|
wallet_address: db_user.wallet_address,
|
|
|
|
password_hash: db_user.password_hash,
|
|
|
|
private_key: db_user.private_key,
|
|
|
|
profile: db_profile,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 11:20:48 +00:00
|
|
|
#[cfg_attr(test, derive(Default))]
|
2021-10-05 20:57:24 +00:00
|
|
|
pub struct UserCreateData {
|
2021-04-09 00:22:17 +00:00
|
|
|
pub username: String,
|
2022-01-06 19:06:35 +00:00
|
|
|
pub password_hash: String,
|
|
|
|
pub private_key_pem: String,
|
2021-12-24 17:46:01 +00:00
|
|
|
pub wallet_address: Option<String>,
|
2021-04-09 00:22:17 +00:00
|
|
|
pub invite_code: Option<String>,
|
|
|
|
}
|
|
|
|
|
2022-01-31 19:10:51 +00:00
|
|
|
pub fn validate_local_username(username: &str) -> Result<(), ValidationError> {
|
2021-11-21 14:38:27 +00:00
|
|
|
// The username regexp should not allow domain names and IP addresses
|
2021-04-09 00:22:17 +00:00
|
|
|
let username_regexp = Regex::new(r"^[a-z0-9_]+$").unwrap();
|
|
|
|
if !username_regexp.is_match(username) {
|
|
|
|
return Err(ValidationError("invalid username"));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-01 23:37:29 +00:00
|
|
|
pub const WALLET_CURRENCY_CODE: &str = "ETH";
|
|
|
|
|
2022-01-31 19:10:51 +00:00
|
|
|
/// 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(())
|
2021-04-09 00:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2021-12-27 19:20:21 +00:00
|
|
|
fn test_validate_local_username() {
|
|
|
|
let result_1 = validate_local_username("name_1");
|
2021-04-09 00:22:17 +00:00
|
|
|
assert_eq!(result_1.is_ok(), true);
|
2021-12-27 19:20:21 +00:00
|
|
|
let result_2 = validate_local_username("name&");
|
2021-04-09 00:22:17 +00:00
|
|
|
assert_eq!(result_2.is_ok(), false);
|
|
|
|
}
|
2022-01-31 19:10:51 +00:00
|
|
|
|
|
|
|
#[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);
|
|
|
|
}
|
2021-04-09 00:22:17 +00:00
|
|
|
}
|