Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add StarknetIDOffChainResolverUpdate event & update_uri function #3

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/interface/resolver.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ trait IResolver<TContractState> {
fn resolve(
self: @TContractState, domain: Span<felt252>, field: felt252, hint: Span<felt252>
) -> felt252;
fn update_uri(ref self: TContractState, new_uri: Span<felt252>);
}
27 changes: 26 additions & 1 deletion src/resolver.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,51 @@ 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<ContractState>;
#[abi(embed_v0)]
impl OwnableImpl = OwnableComponent::OwnableImpl<ContractState>;
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;

#[storage]
struct Storage {
public_key: felt252,
uri: LegacyMap<felt252, felt252>,
#[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<felt252>,
}


#[constructor]
fn constructor(ref self: ContractState, _public_key: felt252, uri: Span<felt252>) {
fn constructor(
ref self: ContractState, owner: ContractAddress, _public_key: felt252, uri: Span<felt252>
) {
self.ownable.initializer(owner);
self.public_key.write(_public_key);
self.store_uri(uri);
self.emit(StarknetIDOffChainResolverUpdate { uri });
}

#[external(v0)]
Expand Down Expand Up @@ -66,6 +85,12 @@ mod Resolver {

return *hint.at(0);
}

fn update_uri(ref self: ContractState, new_uri: Span<felt252>) {
self.ownable.assert_only_owner();
self.store_uri(new_uri);
self.emit(StarknetIDOffChainResolverUpdate { uri: new_uri, });
}
}

#[generate_trait]
Expand Down
2 changes: 1 addition & 1 deletion src/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mod utils;
mod resolver;
mod resolver;
2 changes: 1 addition & 1 deletion src/tests/resolver/erc20.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ mod ERC20 {
#[flat]
ERC20Event: ERC20Component::Event,
}
}
}
36 changes: 23 additions & 13 deletions src/tests/resolver/test_resolver.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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 },
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
);
}
Expand All @@ -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;
Expand Down
Loading