Skip to content

Commit

Permalink
Integrate aptos name service
Browse files Browse the repository at this point in the history
  • Loading branch information
gemcoder21 committed Dec 18, 2023
1 parent 911db49 commit d28c8a7
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 20 deletions.
3 changes: 3 additions & 0 deletions Settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ url = "https://indexer-basic.did.id"
[name.suins]
url = "https://suins-rpc.mainnet.sui.io"

[name.aptos]
url = "https://aptosnames.com"

[metrics]
path = "/metrics"

Expand Down
1 change: 1 addition & 0 deletions api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ async fn rocket(settings: Settings) -> Rocket<Build> {
settings.name.spaceid.url,
settings.name.did.url,
settings.name.suins.url,
settings.name.aptos.url,
);

let pusher_client = PusherClient::new(settings.pusher.url);
Expand Down
69 changes: 69 additions & 0 deletions name_resolver/src/aptos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::client::NameClient;
use async_trait::async_trait;
use primitives::{
chain::Chain,
name::{NameProvider, NameRecord},
};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::error::Error;

#[derive(Debug, Deserialize, Serialize)]
pub struct ResolveName {
pub address: String,
}

pub struct AptosClient {
url: String,
client: Client,
}

impl AptosClient {
pub fn new(url: String) -> Self {
let client = Client::new();
Self { url, client }
}

async fn resolve_name(&self, name: &str) -> Result<String, Box<dyn Error>> {
let url = format!("{}/api/mainnet/v1/address/{}", self.url, name);
let response = self
.client
.get(&url)
.send()
.await?
.json::<ResolveName>()
.await?;

Ok(response.address)
}
}

#[async_trait]
impl NameClient for AptosClient {
fn provider() -> NameProvider {
NameProvider::Aptos
}

async fn resolve(&self, name: &str, chain: Chain) -> Result<NameRecord, Box<dyn Error>> {
match chain {
Chain::Aptos => {
let address = self.resolve_name(name).await?;
Ok(NameRecord {
name: name.to_string(),
chain,
address,
provider: NameProvider::Ud, //Self::provider(),
})
}
_ => return Err("error".to_string().into()),
}
}

fn domains() -> Vec<&'static str> {
vec!["apt"]
}

fn chains() -> Vec<Chain> {
vec![Chain::Aptos]
}
}
20 changes: 17 additions & 3 deletions name_resolver/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use primitives::chain::Chain;
use primitives::name::{NameProvider, NameRecord};

use crate::{
did::DidClient, ens::ENSClient, eths::EthsClient, sns::SNSClient, spaceid::SpaceIdClient,
suins::SuinsClient, ton::TONClient, ud::UDClient,
aptos::AptosClient, did::DidClient, ens::ENSClient, eths::EthsClient, sns::SNSClient,
spaceid::SpaceIdClient, suins::SuinsClient, ton::TONClient, ud::UDClient,
};

#[async_trait]
Expand All @@ -27,6 +27,7 @@ pub struct Client {
spaceid_client: SpaceIdClient,
did_client: DidClient,
suins_client: SuinsClient,
aptos_client: AptosClient,
}

impl Client {
Expand All @@ -40,6 +41,7 @@ impl Client {
space_api_url: String,
did_api_url: String,
suins_api_url: String,
aptos_api_url: String,
) -> Self {
let domains_mapping = Self::domains_mapping();
let ens_client = ENSClient::new(ens_url);
Expand All @@ -50,6 +52,7 @@ impl Client {
let spaceid_client: SpaceIdClient = SpaceIdClient::new(space_api_url);
let did_client: DidClient = DidClient::new(did_api_url);
let suins_client: SuinsClient = SuinsClient::new(suins_api_url);
let aptos_client: AptosClient = AptosClient::new(aptos_api_url);

Self {
domains_mapping,
Expand All @@ -61,6 +64,7 @@ impl Client {
spaceid_client,
did_client,
suins_client,
aptos_client,
}
}

Expand All @@ -71,7 +75,7 @@ impl Client {
.get(name_prefix)
.expect("unable to get provider");

println!("provider: {}", provider.as_str());
println!("provider: {}", provider.as_ref());
println!("provider chain: {}", chain.as_ref());

match provider {
Expand Down Expand Up @@ -123,6 +127,12 @@ impl Client {
}
self.suins_client.resolve(name, chain).await
}
NameProvider::Aptos => {
if !AptosClient::chains().contains(&chain) {
return Err("not supported chain".to_string().into());
}
self.aptos_client.resolve(name, chain).await
}
}
}

Expand Down Expand Up @@ -161,6 +171,10 @@ impl Client {
result.insert(domain, NameProvider::Suins);
}

for domain in AptosClient::domains() {
result.insert(domain, NameProvider::Aptos);
}

result
}
}
1 change: 1 addition & 0 deletions name_resolver/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod aptos;
pub mod client;
pub mod did;
pub mod ens;
Expand Down
21 changes: 4 additions & 17 deletions primitives/src/name.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::chain::Chain;
use serde::Serialize;
use strum_macros::{AsRefStr, EnumString};
use typeshare::typeshare;

#[derive(Debug, Serialize)]
Expand All @@ -12,9 +13,10 @@ pub struct NameRecord {
pub provider: NameProvider,
}

#[derive(Debug, Serialize)]
#[derive(Debug, Serialize, AsRefStr, EnumString)]
#[typeshare(swift = "Codable")]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum NameProvider {
Ud,
Ens,
Expand All @@ -25,20 +27,5 @@ pub enum NameProvider {
Eths,
Did,
Suins,
}

impl NameProvider {
pub fn as_str(&self) -> &'static str {
match self {
Self::Ud => "ud",
Self::Ens => "ens",
Self::Sns => "sns",
Self::Ton => "ton",
Self::Tree => "tree",
Self::SpaceId => "spaceid",
Self::Eths => "eths",
Self::Did => "did",
Self::Suins => "suins",
}
}
Aptos,
}
1 change: 1 addition & 0 deletions settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub struct Name {
pub spaceid: URL,
pub did: URL,
pub suins: URL,
pub aptos: URL,
}

#[derive(Debug, Deserialize, Clone)]
Expand Down

0 comments on commit d28c8a7

Please sign in to comment.