From 24735c70ebe7915b73ab5853fedc358a643ea710 Mon Sep 17 00:00:00 2001 From: Iris Date: Thu, 11 Apr 2024 15:04:39 +0200 Subject: [PATCH 1/3] feat: add StarknetIDOffChainResolverUpdate event & update_uri function --- src/interface/resolver.cairo | 1 + src/resolver.cairo | 28 +++++++++++++++++++- src/tests.cairo | 2 +- src/tests/resolver/erc20.cairo | 2 +- src/tests/resolver/test_resolver.cairo | 36 ++++++++++++++++---------- 5 files changed, 53 insertions(+), 16 deletions(-) diff --git a/src/interface/resolver.cairo b/src/interface/resolver.cairo index ef719c6..327cbee 100644 --- a/src/interface/resolver.cairo +++ b/src/interface/resolver.cairo @@ -5,4 +5,5 @@ trait IResolver { fn resolve( self: @TContractState, domain: Span, field: felt252, hint: Span ) -> felt252; + fn update_uri(ref self: TContractState, new_uri: Span); } diff --git a/src/resolver.cairo b/src/resolver.cairo index 261dbfe..5b8ee16 100644 --- a/src/resolver.cairo +++ b/src/resolver.cairo @@ -1,5 +1,6 @@ #[starknet::contract] mod Resolver { + use core::starknet::event::EventEmitter; use core::option::OptionTrait; use core::traits::TryInto; use core::array::SpanTrait; @@ -7,11 +8,16 @@ mod Resolver { use ecdsa::check_ecdsa_signature; use resolver::interface::resolver::{IResolver, IResolverDispatcher, IResolverDispatcherTrait}; use storage_read::{main::storage_read_component, interface::IStorageRead}; + use openzeppelin::access::ownable::OwnableComponent; component!(path: storage_read_component, storage: storage_read, event: StorageReadEvent); + component!(path: OwnableComponent, storage: ownable, event: OwnableEvent); #[abi(embed_v0)] impl StorageReadImpl = storage_read_component::StorageRead; + #[abi(embed_v0)] + impl OwnableImpl = OwnableComponent::OwnableImpl; + impl OwnableInternalImpl = OwnableComponent::InternalImpl; #[storage] struct Storage { @@ -19,20 +25,34 @@ mod Resolver { uri: LegacyMap, #[substorage(v0)] storage_read: storage_read_component::Storage, + #[substorage(v0)] + ownable: OwnableComponent::Storage, } #[event] #[derive(Drop, starknet::Event)] enum Event { + StarknetIDOffChainResolverUpdate: StarknetIDOffChainResolverUpdate, #[flat] StorageReadEvent: storage_read_component::Event, + #[flat] + OwnableEvent: OwnableComponent::Event, + } + + #[derive(Drop, starknet::Event)] + struct StarknetIDOffChainResolverUpdate { + uri: Span, } #[constructor] - fn constructor(ref self: ContractState, _public_key: felt252, uri: Span) { + fn constructor( + ref self: ContractState, owner: ContractAddress, _public_key: felt252, uri: Span + ) { + self.ownable.initializer(owner); self.public_key.write(_public_key); self.store_uri(uri); + self.emit(StarknetIDOffChainResolverUpdate { uri: uri, }); } #[external(v0)] @@ -66,6 +86,12 @@ mod Resolver { return *hint.at(0); } + + fn update_uri(ref self: ContractState, new_uri: Span) { + self.ownable.assert_only_owner(); + self.store_uri(new_uri); + self.emit(StarknetIDOffChainResolverUpdate { uri: new_uri, }); + } } #[generate_trait] diff --git a/src/tests.cairo b/src/tests.cairo index 95a1975..115c495 100644 --- a/src/tests.cairo +++ b/src/tests.cairo @@ -1,2 +1,2 @@ mod utils; -mod resolver; \ No newline at end of file +mod resolver; diff --git a/src/tests/resolver/erc20.cairo b/src/tests/resolver/erc20.cairo index b22cdba..bce873b 100644 --- a/src/tests/resolver/erc20.cairo +++ b/src/tests/resolver/erc20.cairo @@ -29,4 +29,4 @@ mod ERC20 { #[flat] ERC20Event: ERC20Component::Event, } -} \ No newline at end of file +} diff --git a/src/tests/resolver/test_resolver.cairo b/src/tests/resolver/test_resolver.cairo index 5b904ce..c3707c2 100644 --- a/src/tests/resolver/test_resolver.cairo +++ b/src/tests/resolver/test_resolver.cairo @@ -10,7 +10,7 @@ use starknet::contract_address_const; use starknet::testing::{set_contract_address, set_block_timestamp}; use super::super::utils; -use openzeppelin::token::erc20::{ +use openzeppelin::token::erc20::{ interface::{IERC20Camel, IERC20CamelDispatcher, IERC20CamelDispatcherTrait} }; use super::erc20::ERC20; @@ -24,7 +24,9 @@ use naming::pricing::Pricing; use resolver::interface::resolver::{IResolver, IResolverDispatcher, IResolverDispatcherTrait}; use resolver::resolver::Resolver; -fn deploy() -> ( +fn deploy( + pub_key: felt252 +) -> ( IERC20CamelDispatcher, IPricingDispatcher, IIdentityDispatcher, @@ -47,12 +49,7 @@ fn deploy() -> ( ); let resolver = utils::deploy( - Resolver::TEST_CLASS_HASH, - array![ - 0x64018d8ea7829641419aff38ea79efd3eafedf3a5c1fe001d35339b889d48f4, - 1, - 'http://0.0.0.0:8090' - ] + Resolver::TEST_CLASS_HASH, array![admin, pub_key, 1, 'http://0.0.0.0:8090'] ); ( IERC20CamelDispatcher { contract_address: eth }, @@ -72,7 +69,9 @@ fn deploy() -> ( )] fn test_offchain_resolving_no_hint() { // setup - let (eth, pricing, identity, naming, resolver) = deploy(); + let (eth, pricing, identity, naming, resolver) = deploy( + 0x64018d8ea7829641419aff38ea79efd3eafedf3a5c1fe001d35339b889d48f4 + ); let caller = contract_address_const::<0x123>(); set_contract_address(caller); let id1: u128 = 1; @@ -101,7 +100,9 @@ fn test_offchain_resolving_no_hint() { #[available_gas(20000000000)] fn test_offchain_resolving_with_hint() { // setup - let (eth, pricing, identity, naming, resolver) = deploy(); + let (eth, pricing, identity, naming, resolver) = deploy( + 0x64018d8ea7829641419aff38ea79efd3eafedf3a5c1fe001d35339b889d48f4 + ); let caller = contract_address_const::<0x123>(); set_contract_address(caller); let id1: u128 = 1; @@ -149,7 +150,9 @@ fn test_offchain_resolving_with_hint() { #[should_panic(expected: ('Signature expired', 'ENTRYPOINT_FAILED', 'ENTRYPOINT_FAILED'))] fn test_offchain_resolving_with_hint_expired_sig() { // setup - let (eth, pricing, identity, naming, resolver) = deploy(); + let (eth, pricing, identity, naming, resolver) = deploy( + 0x64018d8ea7829641419aff38ea79efd3eafedf3a5c1fe001d35339b889d48f4 + ); let caller = contract_address_const::<0x123>(); set_contract_address(caller); let id1: u128 = 1; @@ -178,7 +181,12 @@ fn test_offchain_resolving_with_hint_expired_sig() { .resolve( array![999902, 1059716045].span(), 'starknet', - array![0x04a8173e2F008282aC9793FB929974Cc7CEd6cEb76c79A0A9e0D163e60d08b6f, 1, 2, max_validity] + array![ + 0x04a8173e2F008282aC9793FB929974Cc7CEd6cEb76c79A0A9e0D163e60d08b6f, + 1, + 2, + max_validity + ] .span() ); } @@ -188,7 +196,9 @@ fn test_offchain_resolving_with_hint_expired_sig() { #[should_panic(expected: ('Invalid signature', 'ENTRYPOINT_FAILED', 'ENTRYPOINT_FAILED'))] fn test_offchain_resolving_with_hint_invalid_sig() { // setup - let (eth, pricing, identity, naming, resolver) = deploy(); + let (eth, pricing, identity, naming, resolver) = deploy( + 0x64018d8ea7829641419aff38ea79efd3eafedf3a5c1fe001d35339b889d48f4 + ); let caller = contract_address_const::<0x123>(); set_contract_address(caller); let id1: u128 = 1; From 36a394bf8a7a832309f33342c604ac96a98eaedb Mon Sep 17 00:00:00 2001 From: Iris Date: Thu, 11 Apr 2024 15:05:44 +0200 Subject: [PATCH 2/3] fix: remove unused import --- src/resolver.cairo | 1 - 1 file changed, 1 deletion(-) diff --git a/src/resolver.cairo b/src/resolver.cairo index 5b8ee16..00ec0ea 100644 --- a/src/resolver.cairo +++ b/src/resolver.cairo @@ -1,6 +1,5 @@ #[starknet::contract] mod Resolver { - use core::starknet::event::EventEmitter; use core::option::OptionTrait; use core::traits::TryInto; use core::array::SpanTrait; From c88b2ccbfc210f1b807094ddbf1227ddcfba3020 Mon Sep 17 00:00:00 2001 From: Iris Date: Thu, 11 Apr 2024 15:06:39 +0200 Subject: [PATCH 3/3] fix: typo --- src/resolver.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/resolver.cairo b/src/resolver.cairo index 00ec0ea..b396d4f 100644 --- a/src/resolver.cairo +++ b/src/resolver.cairo @@ -51,7 +51,7 @@ mod Resolver { self.ownable.initializer(owner); self.public_key.write(_public_key); self.store_uri(uri); - self.emit(StarknetIDOffChainResolverUpdate { uri: uri, }); + self.emit(StarknetIDOffChainResolverUpdate { uri }); } #[external(v0)]