Skip to content

Commit

Permalink
feat: add revoke_domain and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal committed Jul 13, 2024
1 parent af6ed6d commit b23ed6c
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/interface/naming.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ trait INaming<TContractState> {

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

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

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

fn clear_legacy_domain_to_address(ref self: TContractState, domain: Span<felt252>);
Expand Down
45 changes: 43 additions & 2 deletions src/naming/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,9 @@ mod Naming {
// self.domain_to_address(domain, array![].span()) == address,
// 'domain not pointing back'
// );
println!("echo: {:?}", domain);
if self.domain_to_address(domain, array![].span()) != address {
return array![].span();
}
println!("hey");
domain
}
}
Expand Down Expand Up @@ -682,6 +680,49 @@ mod Naming {
self.emit(Event::SubdomainsReset(SubdomainsReset { domain: domain, }));
}

// this function will reset the native resolving of a domain,
// it is intended to be called by the owner of a parent domain
// who would like to take back a specific subdomain and its
// recursive subdomains but could also be used as a way to burn
// a root domain
fn revoke_domain(ref self: ContractState, domain: Span<felt252>) {
self.assert_control_domain(domain, get_caller_address());
let hashed_domain = self.hash_domain(domain);
let current_domain_data = self._domain_data.read(hashed_domain);
let new_domain_data = DomainData {
owner: 0,
resolver: ContractAddressZeroable::zero(),
address: ContractAddressZeroable::zero(),
expiry: current_domain_data.expiry,
key: current_domain_data.key + 1,
parent_key: current_domain_data.parent_key,
};
self._domain_data.write(hashed_domain, new_domain_data);
if current_domain_data.resolver != new_domain_data.resolver {
self
.emit(
Event::DomainResolverUpdate(
DomainResolverUpdate {
domain, resolver: ContractAddressZeroable::zero()
}
)
);
}
if current_domain_data.address != new_domain_data.address {
self.emit(Event::LegacyDomainToAddressClear(LegacyDomainToAddressClear { domain }));
}
self.emit(Event::SubdomainsReset(SubdomainsReset { domain: domain, }));
self
.emit(
Event::DomainTransfer(
DomainTransfer {
domain: domain,
prev_owner: current_domain_data.owner,
new_owner: new_domain_data.owner
}
)
);
}

// will override your main id
fn set_address_to_domain(
Expand Down
72 changes: 72 additions & 0 deletions src/tests/naming/test_abuses.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,75 @@ fn test_subdomain_reverse() {
let result = naming.address_to_domain(bravo, array![].span());
assert(result == array![].span(), 'unexpected result');
}


#[test]
#[available_gas(2000000000)]
fn test_use_revoke_domain() {
// setup
let (eth, pricing, identity, naming) = deploy();
let alpha = contract_address_const::<0x123>();
let bravo = contract_address_const::<0x456>();

// we mint the ids

set_contract_address(alpha);
identity.mint(1);
set_contract_address(bravo);
identity.mint(2);

set_contract_address(alpha);
let aller: felt252 = 35683102;

// we check how much a domain costs
let (_, price) = pricing.compute_buy_price(5, 365);

// we allow the naming to take our money
eth.approve(naming.contract_address, price);

// we buy with no resolver, no sponsor, no discount and empty metadata
naming
.buy(1, aller, 365, ContractAddressZeroable::zero(), ContractAddressZeroable::zero(), 0, 0);

let root_domain = array![aller].span();
let subdomain = array![aller, aller].span();

// we transfer aller.aller.stark to id2
naming.transfer_domain(subdomain, 2);

// and make sure the owner has been updated
assert(naming.domain_to_id(subdomain) == 2, 'owner not updated correctly');

// now bravo should be able to create a subsubdomain (charlie.aller.aller.stark):
set_contract_address(bravo);
let subsubdomain = array!['charlie', aller, aller].span();
naming.transfer_domain(subsubdomain, 3);

// alpha resets subdomains of aller.stark
set_contract_address(alpha);
naming.revoke_domain(subdomain);

// ensure aller.stark still resolves
assert(naming.domain_to_id(root_domain) == 1, 'owner not updated correctly');
// ensure the subdomain was reset
assert(naming.domain_to_id(subdomain) == 0, 'target not updated correctly');
assert(
naming.domain_to_address(subdomain, array![].span()) == ContractAddressZeroable::zero(),
'owner not updated correctly'
);
assert(
naming.domain_to_data(subdomain).resolver == ContractAddressZeroable::zero(),
'resolver not updated correctly'
);

// ensure subsubdomain too
assert(naming.domain_to_id(subsubdomain) == 0, 'owner not updated correctly');
assert(
naming.domain_to_address(subsubdomain, array![].span()) == ContractAddressZeroable::zero(),
'target not updated correctly'
);
assert(
naming.domain_to_data(subsubdomain).resolver == ContractAddressZeroable::zero(),
'resolver not updated correctly'
);
}

0 comments on commit b23ed6c

Please sign in to comment.