Skip to content

Commit

Permalink
feat: add migrate domain
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal committed Dec 22, 2023
1 parent 116da37 commit 264284e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/interface/naming.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ trait INaming<TContractState> {

fn reset_address_to_domain(ref self: TContractState);

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

fn set_domain_to_resolver(
ref self: TContractState, domain: Span<felt252>, resolver: ContractAddress
);
Expand Down
18 changes: 18 additions & 0 deletions src/naming/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mod Naming {
LegacyDomainToAddressClear: LegacyDomainToAddressClear,
AddressToDomainUpdate: AddressToDomainUpdate,
DomainTransfer: DomainTransfer,
DomainMigrated: DomainMigrated,
SubdomainsReset: SubdomainsReset,
SaleMetadata: SaleMetadata,
StorageReadEvent: storage_read_component::Event
Expand Down Expand Up @@ -84,6 +85,12 @@ mod Naming {
new_owner: u128
}

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

#[derive(Drop, starknet::Event)]
struct SubdomainsReset {
#[key]
Expand Down Expand Up @@ -332,6 +339,10 @@ mod Naming {
}
};

// if a subdomain is created
if self._hash_to_domain.read((hashed_domain, 0)) == 0 {
self.store_unhashed_domain(domain, hashed_domain);
};
self._domain_data.write(hashed_domain, new_domain_data);
self
.emit(
Expand Down Expand Up @@ -408,6 +419,13 @@ mod Naming {
self.set_address_to_domain_util(address, array![0].span());
}

// allows to unhash domains minted in Cairo Zero
fn migrate_domain(ref self: ContractState, domain: Span<felt252>) {
let hashed_domain = self.hash_domain(domain);
self.store_unhashed_domain(domain, hashed_domain);
self.emit(Event::DomainMigrated(DomainMigrated { domain }));
}

fn set_domain_to_resolver(
ref self: ContractState, domain: Span<felt252>, resolver: ContractAddress
) {
Expand Down
12 changes: 12 additions & 0 deletions src/naming/utils.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::array::SpanTrait;
use naming::{
naming::main::{Naming, Naming::{_hash_to_domain, _hash_to_domainContractMemberStateTrait}}
};
Expand All @@ -16,6 +17,17 @@ impl UtilsImpl of UtilsTrait {
return hashed_domain;
}

fn store_unhashed_domain(
ref self: Naming::ContractState, mut domain: Span<felt252>, hashed: felt252
) {
loop {
match domain.pop_back() {
Option::Some(x) => { self._hash_to_domain.write((hashed, domain.len()), *x); },
Option::None => { break; }
}
};
}

fn unhash_domain(self: @Naming::ContractState, domain_hash: felt252) -> Span<felt252> {
let mut i = 0;
let mut domain = ArrayTrait::new();
Expand Down

0 comments on commit 264284e

Please sign in to comment.