Skip to content

Commit

Permalink
feat: add set_address_to_domain to override reverse resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal committed Oct 6, 2023
1 parent 37b895f commit 2f8ebc9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/interface/naming.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ trait INaming<TContractState> {

fn reset_subdomains(ref self: TContractState, domain: Span<felt252>);

fn set_address_to_domain(ref self: TContractState, domain: Span<felt252>);

// admin
fn set_admin(ref self: TContractState, new_admin: ContractAddress);

Expand Down
38 changes: 34 additions & 4 deletions src/naming/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ mod Naming {
enum Event {
DomainMint: DomainMint,
DomainRenewal: DomainRenewal,
DomainToResolver: DomainToResolver,
DomainResolverUpdate: DomainResolverUpdate,
AddressToDomainUpdate: AddressToDomainUpdate,
DomainTransfer: DomainTransfer,
SubdomainsReset: SubdomainsReset,
SaleMetadata: SaleMetadata,
Expand All @@ -51,12 +52,19 @@ mod Naming {
}

#[derive(Drop, starknet::Event)]
struct DomainToResolver {
struct DomainResolverUpdate {
#[key]
domain: Span<felt252>,
resolver: ContractAddress
}

#[derive(Drop, starknet::Event)]
struct AddressToDomainUpdate {
#[key]
address: ContractAddress,
domain: Span<felt252>,
}

#[derive(Drop, starknet::Event)]
struct DomainTransfer {
#[key]
Expand Down Expand Up @@ -360,6 +368,14 @@ mod Naming {
self.emit(Event::SubdomainsReset(SubdomainsReset { domain: domain, }));
}


fn set_address_to_domain(ref self: ContractState, domain: Span<felt252>) {
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) {
Expand Down Expand Up @@ -512,6 +528,20 @@ mod Naming {
}
}

fn set_address_to_domain_util(
ref self: ContractState, address: ContractAddress, mut domain: Span<felt252>
) {
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<felt252>, parent_start_id: u32
) -> (ContractAddress, u32) {
Expand Down Expand Up @@ -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 }
)
);
}
Expand Down

0 comments on commit 2f8ebc9

Please sign in to comment.