Move ChainId to Currency conversion code to utils::caip2 module

This commit is contained in:
silverpill 2023-02-08 00:24:55 +00:00
parent eeae9e3ad7
commit 344025ae2f
3 changed files with 27 additions and 30 deletions

View file

@ -28,7 +28,7 @@ impl DidPkh {
} }
pub fn currency(&self) -> Option<Currency> { pub fn currency(&self) -> Option<Currency> {
(&self.chain_id).try_into().ok() self.chain_id.currency()
} }
} }

View file

@ -11,6 +11,8 @@ use serde::{
de::Error as DeserializerError, de::Error as DeserializerError,
}; };
use super::currencies::Currency;
const CAIP2_RE: &str = r"(?P<namespace>[-a-z0-9]{3,8}):(?P<reference>[-a-zA-Z0-9]{1,32})"; const CAIP2_RE: &str = r"(?P<namespace>[-a-z0-9]{3,8}):(?P<reference>[-a-zA-Z0-9]{1,32})";
const CAIP2_ETHEREUM_NAMESPACE: &str = "eip155"; const CAIP2_ETHEREUM_NAMESPACE: &str = "eip155";
const CAIP2_MONERO_NAMESPACE: &str = "monero"; // unregistered namespace const CAIP2_MONERO_NAMESPACE: &str = "monero"; // unregistered namespace
@ -54,6 +56,15 @@ impl ChainId {
.map_err(|_| ChainIdError("invalid EIP-155 chain ID"))?; .map_err(|_| ChainIdError("invalid EIP-155 chain ID"))?;
Ok(chain_id) Ok(chain_id)
} }
pub fn currency(&self) -> Option<Currency> {
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)] #[derive(thiserror::Error, Debug)]
@ -181,4 +192,18 @@ mod tests {
let error = chain_id.ethereum_chain_id().err().unwrap(); let error = chain_id.ethereum_chain_id().err().unwrap();
assert!(matches!(error, ChainIdError("namespace is not eip155"))); 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);
}
} }

View file

@ -1,7 +1,6 @@
use regex::Regex; use regex::Regex;
use crate::errors::{ConversionError, ValidationError}; use crate::errors::ValidationError;
use super::caip2::ChainId;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Currency { pub enum Currency {
@ -29,19 +28,6 @@ impl Currency {
} }
} }
impl TryFrom<&ChainId> for Currency {
type Error = ConversionError;
fn try_from(value: &ChainId) -> Result<Self, Self::Error> {
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( pub fn validate_wallet_address(
currency: &Currency, currency: &Currency,
wallet_address: &str, wallet_address: &str,
@ -63,20 +49,6 @@ pub fn validate_wallet_address(
mod tests { mod tests {
use super::*; use super::*;
#[test]
fn test_chain_id_conversion() {
let ethereum_chain_id = ChainId::ethereum_mainnet();
let currency = Currency::try_from(&ethereum_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] #[test]
fn test_get_currency_field_name() { fn test_get_currency_field_name() {
let ethereum = Currency::Ethereum; let ethereum = Currency::Ethereum;