diff --git a/Settings.toml b/Settings.toml index e45be0a0..51e404ed 100644 --- a/Settings.toml +++ b/Settings.toml @@ -52,6 +52,9 @@ url = "https://tonapi.io" [name.tree] url = "https://api.eths.center" +[name.spaceid] +url = "https://api.prd.space.id" + [metrics] path = "/metrics" diff --git a/api/src/main.rs b/api/src/main.rs index 54492c69..9f77cd16 100644 --- a/api/src/main.rs +++ b/api/src/main.rs @@ -44,6 +44,7 @@ async fn rocket(settings: Settings) -> Rocket { settings.name.sns.url, settings.name.ton.url, settings.name.tree.url, + settings.name.spaceid.url, ); let plausible_client = PlausibleClient::new(&settings.plausible.url); let request_client = FiatClient::request_client(settings.fiat.timeout); diff --git a/name_resolver/src/client.rs b/name_resolver/src/client.rs index 72efdc91..3cd904d8 100644 --- a/name_resolver/src/client.rs +++ b/name_resolver/src/client.rs @@ -4,7 +4,7 @@ use async_trait::async_trait; use primitives::chain::Chain; use primitives::name::{NameRecord, NameProvider}; -use crate::{ens::ENSClient, ud::UDClient, sns::SNSClient, ton::TONClient, tree::TreeClient}; +use crate::{ens::ENSClient, ud::UDClient, sns::SNSClient, ton::TONClient, tree::TreeClient, spaceid::SpaceIdClient}; #[async_trait] pub trait NameClient { @@ -21,6 +21,7 @@ pub struct Client { sns_client: SNSClient, ton_client: TONClient, tree_client: TreeClient, + spaceid_client: SpaceIdClient, } impl Client { @@ -31,7 +32,8 @@ impl Client { ud_api_key: String, sns_url: String, ton_url: String, - tree_api_url: String + tree_api_url: String, + space_api_url: String, ) -> Self { let domains_mapping = Self::domains_mapping(); let ens_client = ENSClient::new(ens_url); @@ -39,6 +41,7 @@ impl Client { let sns_client = SNSClient::new(sns_url); let ton_client: TONClient = TONClient::new(ton_url); let tree_client: TreeClient = TreeClient::new(tree_api_url); + let spaceid_client: SpaceIdClient = SpaceIdClient::new(space_api_url); Self { domains_mapping, @@ -47,17 +50,13 @@ impl Client { sns_client, ton_client, tree_client, + spaceid_client, } } pub async fn resolve(&self, name: &str, chain: Chain) -> Result> { - let name_parts = name.split('.'); - let name_prefix = name_parts.clone().last(); - - println!("name_parts {}", name_parts.count()); - println!("name_prefix {:?}", name_prefix.clone()); - - let provider = self.domains_mapping.get(name_prefix.unwrap()).unwrap(); + let name_prefix = name.split('.').clone().last().unwrap_or_default(); + let provider = self.domains_mapping.get(name_prefix).expect("unable to get provider"); match provider { NameProvider::Ens => { @@ -89,7 +88,13 @@ impl Client { return Err("not supported chain".to_string().into()) } self.tree_client.resolve(name, chain).await - } + }, + NameProvider::SpaceId => { + if !SpaceIdClient::chains().contains(&chain) { + return Err("not supported chain".to_string().into()) + } + self.spaceid_client.resolve(name, chain).await + }, } } @@ -116,6 +121,10 @@ impl Client { result.insert(domain, NameProvider::Tree); } + for domain in SpaceIdClient::domains() { + result.insert(domain, NameProvider::SpaceId); + } + result } } diff --git a/name_resolver/src/lib.rs b/name_resolver/src/lib.rs index d0cca63c..f4156a91 100644 --- a/name_resolver/src/lib.rs +++ b/name_resolver/src/lib.rs @@ -4,3 +4,4 @@ pub mod ud; pub mod sns; pub mod ton; pub mod tree; +pub mod spaceid; \ No newline at end of file diff --git a/name_resolver/src/spaceid.rs b/name_resolver/src/spaceid.rs new file mode 100644 index 00000000..dcc43257 --- /dev/null +++ b/name_resolver/src/spaceid.rs @@ -0,0 +1,63 @@ +use primitives::chain::Chain; +use reqwest::Client; +use serde::{Deserialize, Serialize}; +use async_trait::async_trait; +use std::error::Error; + +use primitives::name::{NameRecord, NameProvider}; +use crate::client::NameClient; + +#[derive(Debug, Deserialize, Serialize)] +pub struct ResolveRecord { + pub code: i32, + pub address: String, +} + +pub struct SpaceIdClient { + api_url: String, + client: Client, +} + +impl SpaceIdClient { + pub fn new(api_url: String) -> Self { + let client = Client::new(); + Self { + api_url, + client, + } + } +} + +#[async_trait] +impl NameClient for SpaceIdClient { + + fn provider() -> NameProvider { + NameProvider::SpaceId + } + + async fn resolve(&self, name: &str, chain: Chain) -> Result> { + let tld = name.split('.').clone().last().unwrap_or_default(); + let url = format!("{}/v1/getAddress?tld={}&domain={}", self.api_url, tld, name); + let record: ResolveRecord = self.client.get(&url).send().await?.json().await?; + if record.code != 0 { + return Err("SpaceIdClient: code != 0".into()); + } + let address = record.address; + + Ok(NameRecord { name: name.to_string(), chain, address, provider: Self::provider() }) + } + + fn domains() -> Vec<&'static str> { + vec![ + "bnb", + "arb", + ] + } + + fn chains() -> Vec { + vec![ + Chain::SmartChain, + Chain::Arbitrum, + ] + } +} diff --git a/name_resolver/src/tree.rs b/name_resolver/src/tree.rs index 5e272b23..341d5fcb 100644 --- a/name_resolver/src/tree.rs +++ b/name_resolver/src/tree.rs @@ -2,7 +2,7 @@ use primitives::chain::Chain; use reqwest::Client; use serde::{Deserialize, Serialize}; use async_trait::async_trait; -use std::{error::Error}; +use std::error::Error; use primitives::name::{NameRecord, NameProvider}; use crate::client::NameClient; diff --git a/primitives/src/name.rs b/primitives/src/name.rs index a950c3d7..7add3e74 100644 --- a/primitives/src/name.rs +++ b/primitives/src/name.rs @@ -21,6 +21,7 @@ pub enum NameProvider { Sns, Ton, Tree, + SpaceId } impl NameProvider { @@ -31,6 +32,7 @@ impl NameProvider { Self::Sns => "sns", Self::Ton => "ton", Self::Tree => "tree", + Self::SpaceId => "spaceid" } } } \ No newline at end of file diff --git a/settings/src/lib.rs b/settings/src/lib.rs index 7afa6053..e157f8b4 100644 --- a/settings/src/lib.rs +++ b/settings/src/lib.rs @@ -100,6 +100,7 @@ pub struct Name { pub sns: SNS, pub ton: TON, pub tree: TREE, + pub spaceid: SpaceId, } #[derive(Debug, Deserialize)] @@ -120,6 +121,7 @@ pub struct TON { pub url: String, } + #[derive(Debug, Deserialize)] #[allow(unused)] pub struct UD { @@ -133,6 +135,12 @@ pub struct TREE { pub url: String, } +#[derive(Debug, Deserialize)] +#[allow(unused)] +pub struct SpaceId { + pub url: String, +} + #[derive(Debug, Deserialize)] #[allow(unused)] pub struct Metrics {