Skip to content

Commit

Permalink
wip: address_to_domain
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal committed Aug 11, 2023
1 parent b6856b6 commit 35fe06d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/interface/naming.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ trait INaming<TContractState> {

fn resolve(self: @TContractState, domain: Span<felt252>, field: felt252) -> felt252;

fn domain_to_id(self: @TContractState, domain: Span<felt252>) -> u128;

fn domain_to_address(self: @TContractState, domain: Span<felt252>) -> ContractAddress;

fn address_to_domain(self: @TContractState, address: ContractAddress) -> Array<felt252>;
}
29 changes: 29 additions & 0 deletions src/naming/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ mod Naming {
_referral_contract: ContractAddress,
_admin_address: ContractAddress,
_domain_data: LegacyMap<felt252, DomainData>,
_address_to_domain: LegacyMap<(ContractAddress, usize), felt252>
}

#[constructor]
Expand Down Expand Up @@ -126,6 +127,22 @@ mod Naming {
contract_address: self.starknetid_contract.read()
}.owner_of(data.owner)
}

// This returns the identity (StarknetID) owning the domain
fn domain_to_id(self: @ContractState, domain: Span<felt252>) -> u128 {
self._domain_data.read(self.hash_domain(domain)).owner
}

// This function allows to find which domain to use to display an account
fn address_to_domain(self: @ContractState, address: ContractAddress) -> Array<felt252> {
let mut domain = ArrayTrait::new();
self._address_to_domain_util(address, ref domain);
if domain.len() != 0 && self.domain_to_address(domain.span()) == address {
domain
} else {
self.domain_to_id(domain)
}
}
}

#[generate_trait]
Expand Down Expand Up @@ -174,6 +191,18 @@ mod Naming {
);
}

fn _address_to_domain_util(
self: @ContractState, address: ContractAddress, ref domain: Array<felt252>
) -> usize {
let subdomain = self._address_to_domain.read((address, domain.len()));
if subdomain == 0 {
domain.len()
} else {
domain.append(subdomain);
self._address_to_domain_util(address, ref domain)
}
}

fn domain_to_resolver(
self: @ContractState, domain: Span<felt252>, parent_start_id: u32
) -> (ContractAddress, u32) {
Expand Down

0 comments on commit 35fe06d

Please sign in to comment.