Skip to content

Commit

Permalink
refactor: reorder
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal committed Aug 23, 2023
1 parent 91712e5 commit 700ef82
Showing 1 changed file with 82 additions and 74 deletions.
156 changes: 82 additions & 74 deletions src/naming/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,73 @@ mod Naming {

#[external(v0)]
impl NamingImpl of INaming<ContractState> {
// VIEW

// This function allows to read the single felt target of any domain for a specific field
// For example, it allows to find the Bitcoin address of Alice.stark by calling
// naming.resolve(['alice'], 'bitcoin')
fn resolve(self: @ContractState, domain: Span<felt252>, field: felt252) -> felt252 {
let (resolver, parent_start) = self.domain_to_resolver(domain, 0);
if (resolver != ContractAddressZeroable::zero()) {
IResolverDispatcher {
contract_address: resolver
}.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)
}
}

// This functions allows to resolve a domain to a native address. Its output is designed
// to be used as a parameter for other functions (for example if you want to send ERC20
// to a .stark)
fn domain_to_address(self: @ContractState, domain: Span<felt252>) -> ContractAddress {
let resolve_result = self.resolve(domain, 'starknet');
if resolve_result != 0 {
let addr: Option<ContractAddress> = resolve_result.try_into();
return addr.unwrap();
}
let data = self._domain_data.read(self.hash_domain(domain));
if data.address.into() != 0 {
return data.address;
}
IIdentityDispatcher {
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 {
let identity = IIdentityDispatcher {
contract_address: self.starknetid_contract.read()
};
let id = identity.get_main_id(address);
assert(id != 0, 'an id cannot be null');
let id_hashed_domain = identity
.get_verifier_data(id, 'name', get_contract_address(), 0);
let domain = self.unhash_domain(id_hashed_domain);
assert(
self.domain_to_address(domain.span()) == address, 'domain not pointing back'
);
domain
}
}


// EXTERNAL

fn buy(
ref self: ContractState,
id: u128,
Expand All @@ -113,25 +180,6 @@ mod Naming {
self.mint_domain(expiry, resolver, hashed_domain, id, domain);
}

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

fn claim_balance(ref self: ContractState, erc20: ContractAddress) {
let balance = IERC20Dispatcher {
contract_address: erc20
}.balance_of(get_contract_address());
assert(get_caller_address() == self._admin_address.read(), 'you are not admin');
IERC20Dispatcher { contract_address: erc20 }.transfer(get_caller_address(), balance);
}

fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
// todo: use components
assert(!new_class_hash.is_zero(), 'Class hash cannot be zero');
starknet::replace_class_syscall(new_class_hash).unwrap_syscall();
}

fn transfer_domain(ref self: ContractState, domain: Span<felt252>, target_id: u128) {
self.assert_control_domain(domain, get_caller_address());

Expand Down Expand Up @@ -180,66 +228,26 @@ mod Naming {
return;
}

// This function allows to read the single felt target of any domain for a specific field
// For example, it allows to find the Bitcoin address of Alice.stark by calling
// naming.resolve(['alice'], 'bitcoin')
fn resolve(self: @ContractState, domain: Span<felt252>, field: felt252) -> felt252 {
let (resolver, parent_start) = self.domain_to_resolver(domain, 0);
if (resolver != ContractAddressZeroable::zero()) {
IResolverDispatcher {
contract_address: resolver
}.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)
}
}

// This functions allows to resolve a domain to a native address. Its output is designed
// to be used as a parameter for other functions (for example if you want to send ERC20
// to a .stark)
fn domain_to_address(self: @ContractState, domain: Span<felt252>) -> ContractAddress {
let resolve_result = self.resolve(domain, 'starknet');
if resolve_result != 0 {
let addr: Option<ContractAddress> = resolve_result.try_into();
return addr.unwrap();
}
let data = self._domain_data.read(self.hash_domain(domain));
if data.address.into() != 0 {
return data.address;
}
IIdentityDispatcher {
contract_address: self.starknetid_contract.read()
}.owner_of(data.owner)
// ADMIN

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

// 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
fn claim_balance(ref self: ContractState, erc20: ContractAddress) {
let balance = IERC20Dispatcher {
contract_address: erc20
}.balance_of(get_contract_address());
assert(get_caller_address() == self._admin_address.read(), 'you are not admin');
IERC20Dispatcher { contract_address: erc20 }.transfer(get_caller_address(), balance);
}

// 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 {
let identity = IIdentityDispatcher {
contract_address: self.starknetid_contract.read()
};
let id = identity.get_main_id(address);
assert(id != 0, 'an id cannot be null');
let id_hashed_domain = identity
.get_verifier_data(id, 'name', get_contract_address(), 0);
let domain = self.unhash_domain(id_hashed_domain);
assert(
self.domain_to_address(domain.span()) == address, 'domain not pointing back'
);
domain
}
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
// todo: use components
assert(!new_class_hash.is_zero(), 'Class hash cannot be zero');
starknet::replace_class_syscall(new_class_hash).unwrap_syscall();
}
}

Expand Down

0 comments on commit 700ef82

Please sign in to comment.