From 46526fd4be9772b7f67dfebfc44072fb02e42eed Mon Sep 17 00:00:00 2001 From: silverpill Date: Thu, 4 Aug 2022 15:44:48 +0000 Subject: [PATCH] Add currency() method to DidPkh type --- src/ethereum/identity.rs | 6 ++++++ src/mastodon_api/accounts/types.rs | 4 ++-- src/models/profiles/queries.rs | 4 ++-- src/utils/currencies.rs | 22 +++++----------------- 4 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/ethereum/identity.rs b/src/ethereum/identity.rs index 69590fc..ff442f9 100644 --- a/src/ethereum/identity.rs +++ b/src/ethereum/identity.rs @@ -1,3 +1,4 @@ +use std::convert::TryInto; use std::str::FromStr; use regex::Regex; @@ -28,6 +29,10 @@ impl DidPkh { let address = currency.normalize_address(address); Self { chain_id, address } } + + pub fn currency(&self) -> Option { + (&self.chain_id).try_into().ok() + } } impl ToString for DidPkh { @@ -138,6 +143,7 @@ mod tests { fn test_did_string_conversion() { let address = "0xB9C5714089478a327F09197987f16f9E5d936E8a"; let did = DidPkh::from_address(ÐEREUM, address); + assert_eq!(did.currency().unwrap(), ETHEREUM); assert_eq!(did.address, address.to_lowercase()); let did_str = did.to_string(); diff --git a/src/mastodon_api/accounts/types.rs b/src/mastodon_api/accounts/types.rs index 1312231..c17c58a 100644 --- a/src/mastodon_api/accounts/types.rs +++ b/src/mastodon_api/accounts/types.rs @@ -19,7 +19,6 @@ use crate::models::users::types::{ validate_local_username, User, }; -use crate::utils::currencies::get_identity_proof_field_name; use crate::utils::files::{FileError, save_validated_b64_file, get_file_url}; /// https://docs.joinmastodon.org/entities/field/ @@ -72,7 +71,8 @@ impl Account { let mut identity_proofs = vec![]; for proof in profile.identity_proofs.clone().into_inner() { // Skip proof if it doesn't map to field name - if let Some(field_name) = get_identity_proof_field_name(&proof.proof_type) { + if let Some(currency) = proof.issuer.currency() { + let field_name = currency.field_name(); let field = AccountField { name: field_name, value: proof.issuer.address, diff --git a/src/models/profiles/queries.rs b/src/models/profiles/queries.rs index b24a859..7235e26 100644 --- a/src/models/profiles/queries.rs +++ b/src/models/profiles/queries.rs @@ -10,7 +10,7 @@ use crate::models::cleanup::{ DeletionQueue, }; use crate::models::relationships::types::RelationshipType; -use crate::utils::currencies::{get_currency_field_name, Currency}; +use crate::utils::currencies::Currency; use crate::utils::id::new_uuid; use super::types::{ DbActorProfile, @@ -383,7 +383,7 @@ pub async fn search_profile_by_wallet_address( wallet_address: &str, prefer_verified: bool, ) -> Result, DatabaseError> { - let field_name = get_currency_field_name(currency); + let field_name = currency.field_name(); let did_str = DidPkh::from_address(currency, wallet_address).to_string(); // If currency is Ethereum, // search over extra fields must be case insensitive. diff --git a/src/utils/currencies.rs b/src/utils/currencies.rs index a13248a..7aa99a6 100644 --- a/src/utils/currencies.rs +++ b/src/utils/currencies.rs @@ -1,7 +1,6 @@ use std::convert::TryFrom; use crate::errors::ConversionError; -use crate::ethereum::identity::ETHEREUM_EIP191_PROOF; use super::caip2::ChainId; #[derive(Debug, PartialEq)] @@ -16,6 +15,10 @@ impl Currency { }.to_string() } + pub fn field_name(&self) -> String { + format!("${}", self.code()) + } + /// Returns CAIP-2 chain ID pub fn chain_id(&self) -> ChainId { self.into() @@ -55,18 +58,6 @@ impl TryFrom<&ChainId> for Currency { } } -pub fn get_currency_field_name(currency: &Currency) -> String { - format!("${}", currency.code()) -} - -pub fn get_identity_proof_field_name(proof_type: &str) -> Option { - let field_name = match proof_type { - ETHEREUM_EIP191_PROOF => "$ETH".to_string(), - _ => return None, - }; - Some(field_name) -} - #[cfg(test)] mod tests { use super::*; @@ -82,9 +73,6 @@ mod tests { #[test] fn test_get_currency_field_name() { let ethereum = Currency::Ethereum; - assert_eq!( - get_currency_field_name(ðereum), - "$ETH", - ); + assert_eq!(ethereum.field_name(), "$ETH"); } }