tap_caip/
asset_id.rs

1use crate::chain_id::ChainId;
2use crate::error::Error;
3use crate::validation::ValidationRegistry;
4use once_cell::sync::Lazy;
5use regex::Regex;
6use serde::{Deserialize, Serialize};
7use std::str::FromStr;
8
9/// Regular expression pattern for CAIP-19 asset ID validation
10static ASSET_ID_REGEX: Lazy<Regex> = Lazy::new(|| {
11    Regex::new(r"^[-a-z0-9]{3,8}:[-a-zA-Z0-9]{1,64}/[-a-z0-9]{3,8}:[-a-zA-Z0-9]{1,64}$")
12        .expect("Failed to compile ASSET_ID_REGEX")
13});
14
15/// CAIP-19 Asset ID implementation
16///
17/// An Asset ID is a string that identifies a blockchain asset and follows the format:
18/// `<chainId>/<assetNamespace>:<assetReference>`
19///
20/// - `chainId`: CAIP-2 Chain ID (e.g., "eip155:1" for Ethereum mainnet)
21/// - `assetNamespace`: Protocol or standard (e.g., "erc20")
22/// - `assetReference`: Asset-specific identifier (e.g., token contract address)
23///
24/// Example: "eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48" for USDC on Ethereum
25#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
26pub struct AssetId {
27    chain_id: ChainId,
28    namespace: String,
29    reference: String,
30}
31
32impl AssetId {
33    /// Create a new AssetId from ChainId, asset namespace, and reference
34    ///
35    /// # Arguments
36    ///
37    /// * `chain_id` - The CAIP-2 Chain ID
38    /// * `namespace` - The asset namespace (e.g., "erc20")
39    /// * `reference` - The asset reference (e.g., token contract address)
40    ///
41    /// # Returns
42    ///
43    /// * `Result<AssetId, Error>` - An AssetId or an error if validation fails
44    pub fn new(chain_id: ChainId, namespace: &str, reference: &str) -> Result<Self, Error> {
45        // Validate namespace format
46        Self::validate_namespace(namespace)?;
47
48        // Validate reference format
49        Self::validate_reference(namespace, reference)?;
50
51        // Validate full asset ID
52        let asset_id_str = format!("{}/{namespace}:{reference}", chain_id);
53        if !ASSET_ID_REGEX.is_match(&asset_id_str) {
54            return Err(Error::InvalidAssetId(asset_id_str));
55        }
56
57        Ok(Self {
58            chain_id,
59            namespace: namespace.to_string(),
60            reference: reference.to_string(),
61        })
62    }
63
64    /// Get the chain ID component
65    pub fn chain_id(&self) -> &ChainId {
66        &self.chain_id
67    }
68
69    /// Get the asset namespace component
70    pub fn namespace(&self) -> &str {
71        &self.namespace
72    }
73
74    /// Get the asset reference component
75    pub fn reference(&self) -> &str {
76        &self.reference
77    }
78
79    /// Validate the asset namespace
80    fn validate_namespace(namespace: &str) -> Result<(), Error> {
81        // Namespace must be 3-8 characters, lowercase alphanumeric with possible hyphens
82        if !Regex::new(r"^[-a-z0-9]{3,8}$")
83            .expect("Failed to compile namespace regex")
84            .is_match(namespace)
85        {
86            return Err(Error::InvalidAssetNamespace(namespace.to_string()));
87        }
88
89        Ok(())
90    }
91
92    /// Validate the asset reference with respect to the namespace
93    fn validate_reference(namespace: &str, reference: &str) -> Result<(), Error> {
94        // Reference must be 1-64 characters, alphanumeric with possible hyphens
95        if !Regex::new(r"^[-a-zA-Z0-9]{1,64}$")
96            .expect("Failed to compile reference regex")
97            .is_match(reference)
98        {
99            return Err(Error::InvalidAssetReference(reference.to_string()));
100        }
101
102        // Get the global validation registry
103        let registry = ValidationRegistry::global();
104        let registry_guard = registry.lock().unwrap();
105
106        // Apply namespace-specific validation rules
107        if let Some(validator) = registry_guard.get_asset_validator(namespace) {
108            validator(reference)
109                .map_err(|err| Error::InvalidAssetReference(format!("{}: {}", reference, err)))?;
110        }
111
112        Ok(())
113    }
114}
115
116impl FromStr for AssetId {
117    type Err = Error;
118
119    /// Parse a string into an AssetId
120    ///
121    /// # Arguments
122    ///
123    /// * `s` - A string in the format "namespace:reference/assetNamespace:assetReference"
124    ///
125    /// # Returns
126    ///
127    /// * `Result<AssetId, Error>` - An AssetId or an error if parsing fails
128    fn from_str(s: &str) -> Result<Self, Self::Err> {
129        // Check the overall format first
130        if !ASSET_ID_REGEX.is_match(s) {
131            return Err(Error::InvalidAssetId(s.to_string()));
132        }
133
134        // Split by "/" to separate chain ID from asset identifier
135        let parts: Vec<&str> = s.split('/').collect();
136        if parts.len() != 2 {
137            return Err(Error::InvalidAssetId(s.to_string()));
138        }
139
140        // Parse the chain ID
141        let chain_id = ChainId::from_str(parts[0])?;
142
143        // Split asset identifier by ":" to get namespace and reference
144        let asset_parts: Vec<&str> = parts[1].split(':').collect();
145        if asset_parts.len() != 2 {
146            return Err(Error::InvalidAssetId(s.to_string()));
147        }
148
149        // Create the asset ID
150        AssetId::new(chain_id, asset_parts[0], asset_parts[1])
151    }
152}
153
154// Removed the conflicting ToString implementation
155// Let the default implementation from Display be used
156
157impl std::fmt::Display for AssetId {
158    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
159        write!(f, "{}/{}:{}", self.chain_id, self.namespace, self.reference)
160    }
161}
162
163#[cfg(test)]
164mod tests {
165    use super::*;
166
167    #[test]
168    fn test_valid_asset_ids() {
169        // Test Ethereum ERC-20 token (USDC)
170        let usdc =
171            AssetId::from_str("eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
172        assert_eq!(usdc.chain_id().to_string(), "eip155:1");
173        assert_eq!(usdc.namespace(), "erc20");
174        assert_eq!(
175            usdc.reference(),
176            "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
177        );
178        assert_eq!(
179            usdc.to_string(),
180            "eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
181        );
182
183        // Test direct creation
184        let chain_id = ChainId::from_str("eip155:1").unwrap();
185        let dai = AssetId::new(
186            chain_id,
187            "erc20",
188            "0x6b175474e89094c44da98b954eedeac495271d0f",
189        )
190        .unwrap();
191        assert_eq!(
192            dai.to_string(),
193            "eip155:1/erc20:0x6b175474e89094c44da98b954eedeac495271d0f"
194        );
195    }
196
197    #[test]
198    fn test_invalid_asset_ids() {
199        // Invalid: empty string
200        assert!(AssetId::from_str("").is_err());
201
202        // Invalid: missing separators
203        assert!(AssetId::from_str("eip1551erc20address").is_err());
204
205        // Invalid: missing slash
206        assert!(
207            AssetId::from_str("eip155:1erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").is_err()
208        );
209
210        // Invalid: empty namespace
211        assert!(AssetId::from_str(":1/:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").is_err());
212
213        // Invalid: empty reference
214        assert!(AssetId::from_str("eip155:1/erc20:").is_err());
215
216        // Invalid: namespace too short
217        assert!(
218            AssetId::from_str("eip155:1/er:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").is_err()
219        );
220
221        // Invalid: reference too long
222        let long_reference = "a".repeat(65);
223        assert!(AssetId::from_str(&format!("eip155:1/erc20:{}", long_reference)).is_err());
224    }
225
226    #[test]
227    fn test_serialization() {
228        let asset_id =
229            AssetId::from_str("eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
230        let serialized = serde_json::to_string(&asset_id).unwrap();
231
232        // The JSON representation should include all components
233        assert!(serialized.contains("chain_id"));
234        assert!(serialized.contains("namespace"));
235        assert!(serialized.contains("reference"));
236
237        let deserialized: AssetId = serde_json::from_str(&serialized).unwrap();
238        assert_eq!(deserialized, asset_id);
239    }
240
241    #[test]
242    fn test_display_formatting() {
243        let asset_id =
244            AssetId::from_str("eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap();
245        assert_eq!(
246            format!("{}", asset_id),
247            "eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
248        );
249        assert_eq!(
250            asset_id.to_string(),
251            "eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
252        );
253    }
254}