From 09c8f5e71e4f279b3f8568685983533d4f4aa564 Mon Sep 17 00:00:00 2001 From: Thomas Marchand Date: Tue, 31 Oct 2023 16:05:01 +0700 Subject: [PATCH 1/3] feat: load storage read component --- Scarb.toml | 1 + src/naming/main.cairo | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Scarb.toml b/Scarb.toml index 42ac34c..afafa0b 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -7,6 +7,7 @@ version = "0.1.0" [dependencies] starknet = "2.3.0-rc0" openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.8.0-beta.0" } +storage_read = { git = "https://github.com/starknet-id/storage_read_component", branch = "master" } identity = { git = "https://github.com/starknet-id/identity.git", branch = "master" } [[target.starknet-contract]] diff --git a/src/naming/main.cairo b/src/naming/main.cairo index c1f1545..ccc8a15 100644 --- a/src/naming/main.cairo +++ b/src/naming/main.cairo @@ -13,18 +13,19 @@ mod Naming { use naming::{ naming::{asserts::AssertionsTrait, internal::InternalTrait, utils::UtilsTrait}, interface::{ - naming::{INaming, INamingDispatcher, INamingDispatcherTrait}, - resolver::{IResolver, IResolverDispatcher, IResolverDispatcherTrait}, - pricing::{IPricing, IPricingDispatcher, IPricingDispatcherTrait}, - referral::{IReferral, IReferralDispatcher, IReferralDispatcherTrait}, - }}; + naming::{INaming, INamingDispatcher, INamingDispatcherTrait}, + resolver::{IResolver, IResolverDispatcher, IResolverDispatcherTrait}, + pricing::{IPricing, IPricingDispatcher, IPricingDispatcherTrait}, + referral::{IReferral, IReferralDispatcher, IReferralDispatcherTrait}, + } + }; use clone::Clone; use array::ArrayTCloneImpl; use identity::interface::identity::{IIdentity, IIdentityDispatcher, IIdentityDispatcherTrait}; use openzeppelin::token::erc20::interface::{ IERC20Camel, IERC20CamelDispatcher, IERC20CamelDispatcherTrait }; - use debug::PrintTrait; + use storage_read::{main::storage_read_component, interface::IStorageRead}; #[event] #[derive(Drop, starknet::Event)] @@ -36,6 +37,7 @@ mod Naming { DomainTransfer: DomainTransfer, SubdomainsReset: SubdomainsReset, SaleMetadata: SaleMetadata, + StorageReadEvent: storage_read_component::Event } #[derive(Drop, starknet::Event)] @@ -115,6 +117,8 @@ mod Naming { _domain_data: LegacyMap, _hash_to_domain: LegacyMap<(felt252, usize), felt252>, _address_to_domain: LegacyMap<(ContractAddress, usize), felt252>, + #[substorage(v0)] + storage_read: storage_read_component::Storage, } #[constructor] @@ -131,6 +135,10 @@ mod Naming { self._admin_address.write(admin); } + component!(path: storage_read_component, storage: storage_read, event: StorageReadEvent); + + #[abi(embed_v0)] + impl StorageReadComponent = storage_read_component::StorageRead; #[external(v0)] impl NamingImpl of INaming { From 371d95eb1a33d0db6f90fad0322a1b2052179dc1 Mon Sep 17 00:00:00 2001 From: Thomas Marchand Date: Fri, 3 Nov 2023 16:01:09 +0700 Subject: [PATCH 2/3] fix: custom resolver called on wrong slice --- .github/workflows/test.yml | 2 +- Scarb.toml | 2 +- src/naming/internal.cairo | 4 +- src/tests/naming.cairo | 1 + src/tests/naming/test_custom_resolver.cairo | 99 +++++++++++++++++++++ 5 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 src/tests/naming/test_custom_resolver.cairo diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7c17e88..d7c686f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -3,7 +3,7 @@ name: CI Tests on: [push, pull_request, pull_request_target] env: - SCARB_VERSION: 2.3.0 + SCARB_VERSION: 2.3.1 jobs: scarb-tests: diff --git a/Scarb.toml b/Scarb.toml index afafa0b..2c9c7c9 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -5,7 +5,7 @@ version = "0.1.0" # See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest [dependencies] -starknet = "2.3.0-rc0" +starknet = "2.3.1" openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.8.0-beta.0" } storage_read = { git = "https://github.com/starknet-id/storage_read_component", branch = "master" } identity = { git = "https://github.com/starknet-id/identity.git", branch = "master" } diff --git a/src/naming/internal.cairo b/src/naming/internal.cairo index a5d2bf1..9dcecdc 100644 --- a/src/naming/internal.cairo +++ b/src/naming/internal.cairo @@ -147,12 +147,12 @@ impl InternalImpl of InternalTrait { fn resolve_util( self: @Naming::ContractState, domain: Span, field: felt252 ) -> (felt252, felt252) { - let (resolver, parent_start) = self.domain_to_resolver(domain, 0); + let (resolver, parent_start) = self.domain_to_resolver(domain, 1); if (resolver != ContractAddressZeroable::zero()) { ( 0, IResolverDispatcher { contract_address: resolver } - .resolve(domain.slice(parent_start, domain.len() - parent_start), field) + .resolve(domain.slice(0, parent_start), field) ) } else { let hashed_domain = self.hash_domain(domain); diff --git a/src/tests/naming.cairo b/src/tests/naming.cairo index 2c35d36..d825d5b 100644 --- a/src/tests/naming.cairo +++ b/src/tests/naming.cairo @@ -1,4 +1,5 @@ mod common; mod test_abuses; +mod test_custom_resolver; mod test_usecases; mod test_features; \ No newline at end of file diff --git a/src/tests/naming/test_custom_resolver.cairo b/src/tests/naming/test_custom_resolver.cairo new file mode 100644 index 0000000..d86a04d --- /dev/null +++ b/src/tests/naming/test_custom_resolver.cairo @@ -0,0 +1,99 @@ +use array::ArrayTrait; +use array::SpanTrait; +use debug::PrintTrait; +use option::OptionTrait; +use zeroable::Zeroable; +use traits::Into; +use starknet::testing; +use starknet::ContractAddress; +use starknet::contract_address::ContractAddressZeroable; +use starknet::contract_address_const; +use starknet::testing::set_contract_address; +use super::super::utils; +use openzeppelin::token::erc20::{ + erc20::ERC20, interface::{IERC20Camel, IERC20CamelDispatcher, IERC20CamelDispatcherTrait} +}; +use identity::{ + identity::main::Identity, interface::identity::{IIdentityDispatcher, IIdentityDispatcherTrait} +}; +use naming::interface::naming::{INamingDispatcher, INamingDispatcherTrait}; +use naming::interface::pricing::{IPricingDispatcher, IPricingDispatcherTrait}; +use naming::naming::main::Naming; +use naming::pricing::Pricing; +use super::common::deploy; +use naming::naming::main::Naming::Discount; +use naming::interface::resolver::IResolver; + +#[starknet::contract] +mod CustomResolver { + use core::array::SpanTrait; + use naming::interface::resolver::IResolver; + use debug::PrintTrait; + + #[storage] + struct Storage {} + + + #[external(v0)] + impl AdditionResolveImpl of IResolver { + fn resolve(self: @ContractState, mut domain: Span, field: felt252) -> felt252 { + let mut output = 0; + loop { + match domain.pop_front() { + Option::Some(domain_part) => { output += *domain_part; }, + Option::None => { break; } + } + }; + output.print(); + output + } + } +} + + +#[test] +#[available_gas(2000000000)] +fn test_custom_resolver() { + // setup + let (eth, pricing, identity, naming) = deploy(); + let custom_resolver = IERC20CamelDispatcher { + contract_address: utils::deploy(CustomResolver::TEST_CLASS_HASH, ArrayTrait::new()) + }; + + let caller = contract_address_const::<0x123>(); + set_contract_address(caller); + let id: u128 = 1; + let th0rgal: felt252 = 33133781693; + + //we mint an id + identity.mint(id); + + // we check how much a domain costs + let (_, price) = pricing.compute_buy_price(7, 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( + id, + th0rgal, + 365, + custom_resolver.contract_address, + ContractAddressZeroable::zero(), + 0, + 0 + ); + + let domain = array![th0rgal].span(); + // by default we should have nothing written + assert(naming.resolve(domain, 'starknet') == 0, 'non empty starknet field'); + // so it should resolve to the starknetid owner + assert(naming.domain_to_address(domain) == caller, 'wrong domain target'); + + let domain = array![1, 2, 3, th0rgal].span(); + + // let's try the resolving + assert(naming.resolve(domain, 'starknet') == 1 + 2 + 3, 'wrong target'); +} From 6862a0c1ce3fedf6565edf2710345da9aa59d4a7 Mon Sep 17 00:00:00 2001 From: Thomas Marchand Date: Fri, 3 Nov 2023 16:05:43 +0700 Subject: [PATCH 3/3] fix: dependencies --- Scarb.toml | 4 ++-- src/tests/naming/common.cairo | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Scarb.toml b/Scarb.toml index 2c9c7c9..13d9c20 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -7,8 +7,8 @@ version = "0.1.0" [dependencies] starknet = "2.3.1" openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.8.0-beta.0" } -storage_read = { git = "https://github.com/starknet-id/storage_read_component", branch = "master" } -identity = { git = "https://github.com/starknet-id/identity.git", branch = "master" } +identity = { git = "https://github.com/starknet-id/identity.git", rev = "2e8fecab0d9a971710e8efb21abc25fb7825ee09" } +storage_read = { git = "https://github.com/starknet-id/storage_read_component", rev = "c6c69e15d34abfc39ac51dc21b96724e2e19ff31" } [[target.starknet-contract]] # Enable Sierra codegen. diff --git a/src/tests/naming/common.cairo b/src/tests/naming/common.cairo index 1e42475..5c55de0 100644 --- a/src/tests/naming/common.cairo +++ b/src/tests/naming/common.cairo @@ -30,7 +30,7 @@ fn deploy() -> (IERC20CamelDispatcher, IPricingDispatcher, IIdentityDispatcher, let pricing = utils::deploy(Pricing::TEST_CLASS_HASH, array![eth.into()]); // identity - let identity = utils::deploy(Identity::TEST_CLASS_HASH, ArrayTrait::::new()); + let identity = utils::deploy(Identity::TEST_CLASS_HASH, array![0]); // naming let admin = 0x123;