Skip to content

Commit

Permalink
Merge pull request #3 from starknet-id/wip2bis
Browse files Browse the repository at this point in the history
Wip2bis
  • Loading branch information
Th0rgal authored Sep 4, 2023
2 parents dc12cb4 + 8d46d0b commit 639fa3c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ casm = true

# Emit Python-powered hints in order to run compiled CASM class with legacy Cairo VM.
casm-add-pythonic-hints = true

[lib]
sierra = true
casm = false
6 changes: 6 additions & 0 deletions src/interface/naming.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ trait INaming<TContractState> {

fn transfer_domain(ref self: TContractState, domain: Span<felt252>, target_id: u128);

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

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

fn claim_balance(ref self: TContractState, erc20: ContractAddress);

fn set_discount(ref self: TContractState, discount_id: felt252, discount: Discount);

fn set_pricing_contract(ref self: TContractState, pricing_contract: ContractAddress);

fn set_referral_contract(ref self: TContractState, referral_contract: ContractAddress);

fn upgrade(ref self: TContractState, new_class_hash: ClassHash);
}
77 changes: 73 additions & 4 deletions src/naming/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod Naming {
DomainRenewal: DomainRenewal,
DomainToResolver: DomainToResolver,
DomainTransfer: DomainTransfer,
SubdomainsReset: SubdomainsReset,
SaleMetadata: SaleMetadata,
}

Expand Down Expand Up @@ -62,6 +63,12 @@ mod Naming {
new_owner: u128
}

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

#[derive(Drop, starknet::Event)]
struct SaleMetadata {
domain: felt252,
Expand Down Expand Up @@ -128,8 +135,25 @@ mod Naming {
.resolve(domain.slice(parent_start, domain.len() - parent_start), field)
} else {
let domain_data = self._domain_data.read(self.hash_domain(domain));
IIdentityDispatcher { contract_address: self.starknetid_contract.read() }
.get_crosschecked_user_data(domain_data.owner, field)
// circuit breaker for root domain
if (domain.len() == 1) {
IIdentityDispatcher { contract_address: self.starknetid_contract.read() }
.get_crosschecked_user_data(domain_data.owner, field)
// handle reset subdomains
} else {
// todo: optimize by changing the hash definition from H(b, a) to H(a, b)
let parent_key = self
._domain_data
.read(self.hash_domain(domain.slice(1, domain.len() - 1)))
.key;

if parent_key == domain_data.parent_key {
IIdentityDispatcher { contract_address: self.starknetid_contract.read() }
.get_crosschecked_user_data(domain_data.owner, field)
} else {
0
}
}
}
}

Expand All @@ -145,10 +169,19 @@ mod Naming {
}
let data = self._domain_data.read(self.hash_domain(domain));
if data.address.into() != 0 {
if domain.len() != 1 {
let parent_key = self
._domain_data
.read(self.hash_domain(domain.slice(1, domain.len() - 1)))
.key;
if parent_key == data.parent_key {
return data.address;
}
}
return data.address;
}
IIdentityDispatcher { contract_address: self.starknetid_contract.read() }
.owner_of(data.owner)
.owner_of(self.domain_to_id(domain))
}

// This returns the stored DomainData associated to this domain
Expand All @@ -158,7 +191,17 @@ mod Naming {

// 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
let data = self._domain_data.read(self.hash_domain(domain));
if domain.len() != 1 {
let parent_key = self
._domain_data
.read(self.hash_domain(domain.slice(1, domain.len() - 1)))
.key;
if parent_key != data.parent_key {
return 0;
}
}
data.owner
}

// This function allows to find which domain to use to display an account
Expand Down Expand Up @@ -299,6 +342,21 @@ mod Naming {
.set_verifier_data(target_id, 'name', hashed_domain, 0);
}

fn reset_subdomains(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: current_domain_data.owner,
resolver: current_domain_data.resolver,
address: current_domain_data.address,
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);
self.emit(Event::SubdomainsReset(SubdomainsReset { domain: domain, }));
}

// ADMIN

Expand All @@ -319,6 +377,16 @@ mod Naming {
self.discounts.write(discount_id, discount);
}

fn set_pricing_contract(ref self: ContractState, pricing_contract: ContractAddress) {
assert(get_caller_address() == self._admin_address.read(), 'you are not admin');
self._pricing_contract.write(pricing_contract);
}

fn set_referral_contract(ref self: ContractState, referral_contract: ContractAddress) {
assert(get_caller_address() == self._admin_address.read(), 'you are not admin');
self._referral_contract.write(referral_contract);
}

fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
assert(get_caller_address() == self._admin_address.read(), 'you are not admin');
// todo: use components
Expand All @@ -329,6 +397,7 @@ mod Naming {

#[generate_trait]
impl InternalImpl of InternalTrait {
// hash(alpha.bravo.stark) = pedersen(bravo, pedersen(alpha, 0))
fn hash_domain(self: @ContractState, domain: Span<felt252>) -> felt252 {
if domain.len() == 0 {
return 0;
Expand Down
21 changes: 18 additions & 3 deletions src/tests/naming/test_features.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ fn test_subdomains() {
let id2: u128 = 2;
let th0rgal: felt252 = 33133781693;
let hello: felt252 = 29811539;
let altdomain: felt252 = 57437602667574;

//we mint an id
//we mint the ids id
identity.mint(id1);
identity.mint(id2);

// we check how much a domain costs
let (_, price) = pricing.compute_buy_price(7, 365);
Expand All @@ -60,10 +60,25 @@ fn test_subdomains() {
// we transfer hello.th0rgal.stark to id2
naming.transfer_domain(subdomain, id2);

assert(naming.domain_to_address(subdomain) == caller, 'wrong subdomain initial target');

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

let root_domain = array![th0rgal].span();

// we reset subdomains
naming.reset_subdomains(root_domain);

// ensure th0rgal still resolves
assert(naming.domain_to_id(root_domain) == id1, 'owner not updated correctly');

// ensure the subdomain was reset
assert(
naming.domain_to_address(subdomain) == ContractAddressZeroable::zero(),
'target not updated correctly'
);
}

#[test]
#[available_gas(2000000000)]
Expand Down

0 comments on commit 639fa3c

Please sign in to comment.