Skip to main content

atp_caip/identifiers/
namespace.rs

1use candid::CandidType;
2/// Common chain namespaces
3#[derive(Debug, Clone, Copy, PartialEq, Eq, CandidType)]
4pub enum ChainNamespace {
5    Eip155,   // Ethereum and EVM-compatible chains
6    Solana,   // Solana
7    Cosmos,   // Cosmos chains
8    Polkadot, // Polkadot chains
9    Bip155,   // Bitcoin and Bitcoin-compatible chains
10    Other(&'static str),
11}
12
13impl ChainNamespace {
14    pub fn as_str(&self) -> &str {
15        match self {
16            Self::Eip155 => "eip155",
17            Self::Solana => "solana",
18            Self::Cosmos => "cosmos",
19            Self::Polkadot => "polkadot",
20            Self::Bip155 => "bip155",
21            Self::Other(s) => s,
22        }
23    }
24}
25
26impl From<ChainNamespace> for String {
27    fn from(namespace: ChainNamespace) -> Self {
28        namespace.as_str().to_string()
29    }
30}
31
32/// Common asset namespaces
33#[derive(Debug, Clone, Copy, PartialEq, Eq, CandidType)]
34pub enum AssetNamespace {
35    Slip44,  // Native tokens
36    Erc20,   // ERC-20 tokens
37    Erc721,  // ERC-721 NFTs
38    Erc1155, // ERC-1155 tokens
39    Spl,     // Solana SPL tokens
40    Other(&'static str),
41}
42
43impl AssetNamespace {
44    pub fn as_str(&self) -> &str {
45        match self {
46            Self::Slip44 => "slip44",
47            Self::Erc20 => "erc20",
48            Self::Erc721 => "erc721",
49            Self::Erc1155 => "erc1155",
50            Self::Spl => "spl",
51            Self::Other(s) => s,
52        }
53    }
54}
55
56impl From<AssetNamespace> for String {
57    fn from(namespace: AssetNamespace) -> Self {
58        namespace.as_str().to_string()
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn test_chain_namespace() {
68        assert_eq!(ChainNamespace::Eip155.as_str(), "eip155");
69        assert_eq!(ChainNamespace::Solana.as_str(), "solana");
70        assert_eq!(ChainNamespace::Cosmos.as_str(), "cosmos");
71        assert_eq!(ChainNamespace::Polkadot.as_str(), "polkadot");
72        assert_eq!(ChainNamespace::Bip155.as_str(), "bip155");
73        assert_eq!(ChainNamespace::Other("custom").as_str(), "custom");
74
75        let ns_str: String = ChainNamespace::Eip155.into();
76        assert_eq!(ns_str, "eip155");
77    }
78
79    #[test]
80    fn test_asset_namespace() {
81        assert_eq!(AssetNamespace::Slip44.as_str(), "slip44");
82        assert_eq!(AssetNamespace::Erc20.as_str(), "erc20");
83        assert_eq!(AssetNamespace::Erc721.as_str(), "erc721");
84        assert_eq!(AssetNamespace::Erc1155.as_str(), "erc1155");
85        assert_eq!(AssetNamespace::Spl.as_str(), "spl");
86        assert_eq!(AssetNamespace::Other("custom").as_str(), "custom");
87
88        let ns_str: String = AssetNamespace::Erc20.into();
89        assert_eq!(ns_str, "erc20");
90    }
91}