diff --git a/config.template.toml b/config.template.toml index 0d99f7f..c87dd1a 100644 --- a/config.template.toml +++ b/config.template.toml @@ -18,4 +18,7 @@ pop_verifier = "0xXXXXXXXXXXXX" [starkscan] api_url = "https://api-testnet.starkscan.co/api/v0/" -api_key="xxxxxx" \ No newline at end of file +api_key = "xxxxxx" + +[custom_resolvers] +"0xXXXXXXXXXXXX" = ["domain.stark"] diff --git a/src/config.rs b/src/config.rs index 40f0d0c..f92a9a7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,6 @@ use serde::Deserialize; use starknet::core::types::FieldElement; +use std::collections::HashMap; use std::env; use std::fs; @@ -42,6 +43,7 @@ pub_struct!(Clone, Deserialize; Config { databases: Databases, contracts: Contracts, starkscan: Starkscan, + custom_resolvers: HashMap>, }); pub fn load() -> Config { diff --git a/src/endpoints/addr_to_external_domains.rs b/src/endpoints/addr_to_external_domains.rs index 533b447..0a953e4 100644 --- a/src/endpoints/addr_to_external_domains.rs +++ b/src/endpoints/addr_to_external_domains.rs @@ -1,4 +1,7 @@ -use crate::{models::AppState, utils::get_error}; +use crate::{ + models::AppState, + utils::{get_error, to_hex}, +}; use axum::{ extract::{Query, State}, http::{HeaderMap, HeaderValue, StatusCode}, @@ -7,6 +10,7 @@ use axum::{ use futures::StreamExt; use mongodb::bson::doc; use serde::{Deserialize, Serialize}; +use starknet::core::types::FieldElement; use std::sync::Arc; // for stream handling #[derive(Serialize)] @@ -16,21 +20,24 @@ pub struct DomainData { #[derive(Deserialize)] pub struct DomainQuery { - addr: String, + addr: FieldElement, } pub async fn handler( State(state): State>, Query(query): Query, ) -> impl IntoResponse { - let subdomains = state.starknetid_db.collection::("subdomains"); + let subdomains = state + .starknetid_db + .collection::("custom_resolutions"); let addr = &query.addr; let mut domains_list = Vec::new(); let cursor = subdomains .find( doc! { - "addr": addr, + "field" : "starknet", + "value": to_hex(addr), "_cursor.to": null, }, None, @@ -42,8 +49,21 @@ pub async fn handler( while let Some(result) = cursor.next().await { match result { Ok(doc) => { - let domain = doc.get_str("domain").unwrap_or_default().to_owned(); - domains_list.push(domain); + let domain_slice = + doc.get_str("domain_slice").unwrap_or_default().to_owned(); + let resolver = + FieldElement::from_hex_be(doc.get_str("resolver").unwrap_or_default()) + .unwrap(); + match state.conf.custom_resolvers.get(&to_hex(&resolver)) { + // a resolver can be associated to multiple domains, eg: argent.stark and ag.stark + Some(parents) => { + parents.iter().for_each(|parent| { + // we automatically add all domains + domains_list.push(format!("{}{}", domain_slice, parent)); + }); + } + None => {} + } } Err(_) => return get_error("Error while fetching from database".to_string()), }