From 2f8ebc9ad438dd4a1ccd90f54cba612282f1894f Mon Sep 17 00:00:00 2001 From: Thomas Marchand Date: Fri, 6 Oct 2023 18:48:15 +0700 Subject: [PATCH] feat: add set_address_to_domain to override reverse resolving --- src/interface/naming.cairo | 2 ++ src/naming/main.cairo | 38 ++++++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/interface/naming.cairo b/src/interface/naming.cairo index f28046b..e95d744 100644 --- a/src/interface/naming.cairo +++ b/src/interface/naming.cairo @@ -39,6 +39,8 @@ trait INaming { fn reset_subdomains(ref self: TContractState, domain: Span); + fn set_address_to_domain(ref self: TContractState, domain: Span); + // admin fn set_admin(ref self: TContractState, new_admin: ContractAddress); diff --git a/src/naming/main.cairo b/src/naming/main.cairo index 101d4ad..d3499ca 100644 --- a/src/naming/main.cairo +++ b/src/naming/main.cairo @@ -29,7 +29,8 @@ mod Naming { enum Event { DomainMint: DomainMint, DomainRenewal: DomainRenewal, - DomainToResolver: DomainToResolver, + DomainResolverUpdate: DomainResolverUpdate, + AddressToDomainUpdate: AddressToDomainUpdate, DomainTransfer: DomainTransfer, SubdomainsReset: SubdomainsReset, SaleMetadata: SaleMetadata, @@ -51,12 +52,19 @@ mod Naming { } #[derive(Drop, starknet::Event)] - struct DomainToResolver { + struct DomainResolverUpdate { #[key] domain: Span, resolver: ContractAddress } + #[derive(Drop, starknet::Event)] + struct AddressToDomainUpdate { + #[key] + address: ContractAddress, + domain: Span, + } + #[derive(Drop, starknet::Event)] struct DomainTransfer { #[key] @@ -360,6 +368,14 @@ mod Naming { self.emit(Event::SubdomainsReset(SubdomainsReset { domain: domain, })); } + + fn set_address_to_domain(ref self: ContractState, domain: Span) { + let address = get_caller_address(); + assert(self.domain_to_address(domain) == address, 'domain not pointing back'); + self.emit(Event::AddressToDomainUpdate(AddressToDomainUpdate { address, domain })); + self.set_address_to_domain_util(address, domain); + } + // ADMIN fn set_admin(ref self: ContractState, new_admin: ContractAddress) { @@ -512,6 +528,20 @@ mod Naming { } } + fn set_address_to_domain_util( + ref self: ContractState, address: ContractAddress, mut domain: Span + ) { + match domain.pop_back() { + Option::Some(domain_part) => { + self._address_to_domain.write((address, domain.len()), *domain_part); + return self.set_address_to_domain_util(address, domain); + }, + Option::None => { + return; + } + } + } + fn domain_to_resolver( self: @ContractState, domain: Span, parent_start_id: u32 ) -> (ContractAddress, u32) { @@ -597,8 +627,8 @@ mod Naming { if (resolver.into() != 0) { self .emit( - Event::DomainToResolver( - DomainToResolver { domain: array![domain].span(), resolver } + Event::DomainResolverUpdate( + DomainResolverUpdate { domain: array![domain].span(), resolver } ) ); }