Add currency() method to DidPkh type

This commit is contained in:
silverpill 2022-08-04 15:44:48 +00:00
parent af0563759d
commit 46526fd4be
4 changed files with 15 additions and 21 deletions

View file

@ -1,3 +1,4 @@
use std::convert::TryInto;
use std::str::FromStr; use std::str::FromStr;
use regex::Regex; use regex::Regex;
@ -28,6 +29,10 @@ impl DidPkh {
let address = currency.normalize_address(address); let address = currency.normalize_address(address);
Self { chain_id, address } Self { chain_id, address }
} }
pub fn currency(&self) -> Option<Currency> {
(&self.chain_id).try_into().ok()
}
} }
impl ToString for DidPkh { impl ToString for DidPkh {
@ -138,6 +143,7 @@ mod tests {
fn test_did_string_conversion() { fn test_did_string_conversion() {
let address = "0xB9C5714089478a327F09197987f16f9E5d936E8a"; let address = "0xB9C5714089478a327F09197987f16f9E5d936E8a";
let did = DidPkh::from_address(&ETHEREUM, address); let did = DidPkh::from_address(&ETHEREUM, address);
assert_eq!(did.currency().unwrap(), ETHEREUM);
assert_eq!(did.address, address.to_lowercase()); assert_eq!(did.address, address.to_lowercase());
let did_str = did.to_string(); let did_str = did.to_string();

View file

@ -19,7 +19,6 @@ use crate::models::users::types::{
validate_local_username, validate_local_username,
User, User,
}; };
use crate::utils::currencies::get_identity_proof_field_name;
use crate::utils::files::{FileError, save_validated_b64_file, get_file_url}; use crate::utils::files::{FileError, save_validated_b64_file, get_file_url};
/// https://docs.joinmastodon.org/entities/field/ /// https://docs.joinmastodon.org/entities/field/
@ -72,7 +71,8 @@ impl Account {
let mut identity_proofs = vec![]; let mut identity_proofs = vec![];
for proof in profile.identity_proofs.clone().into_inner() { for proof in profile.identity_proofs.clone().into_inner() {
// Skip proof if it doesn't map to field name // 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 { let field = AccountField {
name: field_name, name: field_name,
value: proof.issuer.address, value: proof.issuer.address,

View file

@ -10,7 +10,7 @@ use crate::models::cleanup::{
DeletionQueue, DeletionQueue,
}; };
use crate::models::relationships::types::RelationshipType; 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 crate::utils::id::new_uuid;
use super::types::{ use super::types::{
DbActorProfile, DbActorProfile,
@ -383,7 +383,7 @@ pub async fn search_profile_by_wallet_address(
wallet_address: &str, wallet_address: &str,
prefer_verified: bool, prefer_verified: bool,
) -> Result<Vec<DbActorProfile>, DatabaseError> { ) -> Result<Vec<DbActorProfile>, 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(); let did_str = DidPkh::from_address(currency, wallet_address).to_string();
// If currency is Ethereum, // If currency is Ethereum,
// search over extra fields must be case insensitive. // search over extra fields must be case insensitive.

View file

@ -1,7 +1,6 @@
use std::convert::TryFrom; use std::convert::TryFrom;
use crate::errors::ConversionError; use crate::errors::ConversionError;
use crate::ethereum::identity::ETHEREUM_EIP191_PROOF;
use super::caip2::ChainId; use super::caip2::ChainId;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -16,6 +15,10 @@ impl Currency {
}.to_string() }.to_string()
} }
pub fn field_name(&self) -> String {
format!("${}", self.code())
}
/// Returns CAIP-2 chain ID /// Returns CAIP-2 chain ID
pub fn chain_id(&self) -> ChainId { pub fn chain_id(&self) -> ChainId {
self.into() 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<String> {
let field_name = match proof_type {
ETHEREUM_EIP191_PROOF => "$ETH".to_string(),
_ => return None,
};
Some(field_name)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -82,9 +73,6 @@ mod tests {
#[test] #[test]
fn test_get_currency_field_name() { fn test_get_currency_field_name() {
let ethereum = Currency::Ethereum; let ethereum = Currency::Ethereum;
assert_eq!( assert_eq!(ethereum.field_name(), "$ETH");
get_currency_field_name(&ethereum),
"$ETH",
);
} }
} }