diff --git a/src/identity/did_pkh.rs b/src/identity/did_pkh.rs index 4eb48a5..3382bc1 100644 --- a/src/identity/did_pkh.rs +++ b/src/identity/did_pkh.rs @@ -28,7 +28,7 @@ impl DidPkh { } pub fn currency(&self) -> Option { - (&self.chain_id).try_into().ok() + self.chain_id.currency() } } diff --git a/src/utils/caip2.rs b/src/utils/caip2.rs index 348b863..2babe1b 100644 --- a/src/utils/caip2.rs +++ b/src/utils/caip2.rs @@ -11,6 +11,8 @@ use serde::{ de::Error as DeserializerError, }; +use super::currencies::Currency; + const CAIP2_RE: &str = r"(?P[-a-z0-9]{3,8}):(?P[-a-zA-Z0-9]{1,32})"; const CAIP2_ETHEREUM_NAMESPACE: &str = "eip155"; const CAIP2_MONERO_NAMESPACE: &str = "monero"; // unregistered namespace @@ -54,6 +56,15 @@ impl ChainId { .map_err(|_| ChainIdError("invalid EIP-155 chain ID"))?; Ok(chain_id) } + + pub fn currency(&self) -> Option { + let currency = match self.namespace.as_str() { + CAIP2_ETHEREUM_NAMESPACE => Currency::Ethereum, + CAIP2_MONERO_NAMESPACE => Currency::Monero, + _ => return None, + }; + Some(currency) + } } #[derive(thiserror::Error, Debug)] @@ -181,4 +192,18 @@ mod tests { let error = chain_id.ethereum_chain_id().err().unwrap(); assert!(matches!(error, ChainIdError("namespace is not eip155"))); } + + #[test] + fn test_chain_id_conversion() { + let ethereum_chain_id = ChainId::ethereum_mainnet(); + let currency = ethereum_chain_id.currency().unwrap(); + assert_eq!(currency, Currency::Ethereum); + + let monero_chain_id = ChainId { + namespace: "monero".to_string(), + reference: "mainnet".to_string(), + }; + let currency = monero_chain_id.currency().unwrap(); + assert_eq!(currency, Currency::Monero); + } } diff --git a/src/utils/currencies.rs b/src/utils/currencies.rs index 5bcb336..2bf0546 100644 --- a/src/utils/currencies.rs +++ b/src/utils/currencies.rs @@ -1,7 +1,6 @@ use regex::Regex; -use crate::errors::{ConversionError, ValidationError}; -use super::caip2::ChainId; +use crate::errors::ValidationError; #[derive(Debug, PartialEq)] pub enum Currency { @@ -29,19 +28,6 @@ impl Currency { } } -impl TryFrom<&ChainId> for Currency { - type Error = ConversionError; - - fn try_from(value: &ChainId) -> Result { - let currency = match value.namespace.as_str() { - "eip155" => Self::Ethereum, - "monero" => Self::Monero, // not standard - _ => return Err(ConversionError), - }; - Ok(currency) - } -} - pub fn validate_wallet_address( currency: &Currency, wallet_address: &str, @@ -63,20 +49,6 @@ pub fn validate_wallet_address( mod tests { use super::*; - #[test] - fn test_chain_id_conversion() { - let ethereum_chain_id = ChainId::ethereum_mainnet(); - let currency = Currency::try_from(ðereum_chain_id).unwrap(); - assert_eq!(currency, Currency::Ethereum); - - let monero_chain_id = ChainId { - namespace: "monero".to_string(), - reference: "mainnet".to_string(), - }; - let currency = Currency::try_from(&monero_chain_id).unwrap(); - assert_eq!(currency, Currency::Monero); - } - #[test] fn test_get_currency_field_name() { let ethereum = Currency::Ethereum;