From 21026f628034d3b3659529eb37dfc1f771f24da6 Mon Sep 17 00:00:00 2001 From: Thomas Marchand Date: Thu, 18 Apr 2024 13:46:20 +0100 Subject: [PATCH] Feat/cairo update (#10) * feat: update dependencies * feat: update scarb version in workflow * update: use #[abi(embed_v0)] for externals * fix: warning for unused storage_write_syscall * feat: migrate out of custom_uri component in favor of standard erc721 * fix: deployment of identity in tests, encoding of ByteArray --- .github/workflows/test.yml | 2 +- Scarb.toml | 7 +++--- src/identity.cairo | 1 - src/identity/erc721.cairo | 42 ------------------------------------ src/identity/internal.cairo | 2 +- src/identity/main.cairo | 32 ++++++--------------------- src/interface/identity.cairo | 2 -- src/tests/utils.cairo | 2 +- 8 files changed, 12 insertions(+), 78 deletions(-) delete mode 100644 src/identity/erc721.cairo diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 50d5781..46a696c 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.1 + SCARB_VERSION: 2.6.3 jobs: scarb-tests: diff --git a/Scarb.toml b/Scarb.toml index eb86abf..4f99135 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -8,10 +8,9 @@ repository = "https://github.com/starknet-id/identity" # See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest [dependencies] -starknet = "2.3.1" -openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", rev = "f3e2a5f0547a429c716f32471b06df729cbdfb9f" } -storage_read = { git = "https://github.com/starknet-id/storage_read_component.git", rev = "c6c69e15d34abfc39ac51dc21b96724e2e19ff31" } -custom_uri = { git = "https://github.com/starknet-id/custom_uri_component.git", rev = "abb2f3d43c7be56dd5cd9f93c33af40b272c2245" } +starknet = "2.6.3" +openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", rev = "a83f36b23f1af6e160288962be4a2701c3ecbcda" } +storage_read = { git = "https://github.com/starknet-id/storage_read_component.git", rev = "c7a640371d0336cc09b907154cbfeec743739ff9" } [[target.starknet-contract]] sierra = true diff --git a/src/identity.cairo b/src/identity.cairo index 1ba44d0..fe3285d 100644 --- a/src/identity.cairo +++ b/src/identity.cairo @@ -1,3 +1,2 @@ mod main; mod internal; -mod erc721; \ No newline at end of file diff --git a/src/identity/erc721.cairo b/src/identity/erc721.cairo deleted file mode 100644 index 6aff887..0000000 --- a/src/identity/erc721.cairo +++ /dev/null @@ -1,42 +0,0 @@ -use openzeppelin::{ - token::erc721::{ERC721Component::{ERC721Metadata, HasComponent}}, - introspection::src5::SRC5Component, -}; -use custom_uri::{main::custom_uri_component::InternalImpl, main::custom_uri_component}; - - -#[starknet::interface] -trait IERC721Metadata { - fn name(self: @TState) -> felt252; - fn symbol(self: @TState) -> felt252; - fn token_uri(self: @TState, tokenId: u256) -> Array; - fn tokenURI(self: @TState, tokenId: u256) -> Array; -} - -#[starknet::embeddable] -impl IERC721MetadataImpl< - TContractState, - +HasComponent, - +SRC5Component::HasComponent, - +custom_uri_component::HasComponent, - +Drop -> of IERC721Metadata { - fn name(self: @TContractState) -> felt252 { - let component = HasComponent::get_component(self); - ERC721Metadata::name(component) - } - - fn symbol(self: @TContractState) -> felt252 { - let component = HasComponent::get_component(self); - ERC721Metadata::symbol(component) - } - - fn token_uri(self: @TContractState, tokenId: u256) -> Array { - let component = custom_uri_component::HasComponent::get_component(self); - component.get_uri(tokenId) - } - - fn tokenURI(self: @TContractState, tokenId: u256) -> Array { - self.token_uri(tokenId) - } -} diff --git a/src/identity/internal.cairo b/src/identity/internal.cairo index 9ada9c0..683481b 100644 --- a/src/identity/internal.cairo +++ b/src/identity/internal.cairo @@ -70,7 +70,7 @@ impl InternalImpl of InternalTrait { let addr = storage_base_address_from_felt252(base + values.len().into()); starknet::storage_write_syscall( domain, starknet::storage_address_from_base_and_offset(addr, 0), *value - ); + ).unwrap(); self._set(domain, base, values); }, Option::None(_) => {}, diff --git a/src/identity/main.cairo b/src/identity/main.cairo index 954af2a..dad3de6 100644 --- a/src/identity/main.cairo +++ b/src/identity/main.cairo @@ -12,14 +12,13 @@ mod Identity { use integer::{u256_safe_divmod, u256_as_non_zero}; use core::pedersen; use storage_read::{main::storage_read_component, interface::IStorageRead}; - use custom_uri::{interface::IInternalCustomURI, main::custom_uri_component}; use openzeppelin::{ account, access::ownable::OwnableComponent, upgrades::{UpgradeableComponent, interface::IUpgradeable}, token::erc721::{ ERC721Component, erc721::ERC721Component::InternalTrait as ERC721InternalTrait }, - introspection::{src5::SRC5Component, dual_src5::{DualCaseSRC5, DualCaseSRC5Trait}} + introspection::{src5::SRC5Component} }; use identity::identity::{internal::InternalTrait}; @@ -28,7 +27,6 @@ mod Identity { const VERIFIER_DATA_ADDR: felt252 = 304878986635684253299743444353489138340069571156984851619649640349195152192; - component!(path: custom_uri_component, storage: custom_uri, event: CustomUriEvent); component!(path: storage_read_component, storage: storage_read, event: StorageReadEvent); component!(path: SRC5Component, storage: src5, event: SRC5Event); component!(path: ERC721Component, storage: erc721, event: ERC721Event); @@ -39,8 +37,6 @@ mod Identity { // allow to check what interface is supported #[abi(embed_v0)] impl SRC5Impl = SRC5Component::SRC5Impl; - #[abi(embed_v0)] - impl SRC5CamelImpl = SRC5Component::SRC5CamelImpl; impl SRC5InternalImpl = SRC5Component::InternalImpl; // make it a NFT #[abi(embed_v0)] @@ -49,8 +45,7 @@ mod Identity { impl ERC721CamelOnlyImpl = ERC721Component::ERC721CamelOnlyImpl; // allow to query name of nft collection #[abi(embed_v0)] - impl IERC721MetadataImpl = - identity::identity::erc721::IERC721MetadataImpl; + impl IERC721MetadataImpl = ERC721Component::ERC721MetadataImpl; // allow to query nft metadata json #[abi(embed_v0)] impl StorageReadImpl = storage_read_component::StorageRead; @@ -69,8 +64,6 @@ mod Identity { // legacy owner Proxy_admin: felt252, #[substorage(v0)] - custom_uri: custom_uri_component::Storage, - #[substorage(v0)] storage_read: storage_read_component::Storage, #[substorage(v0)] src5: SRC5Component::Storage, @@ -96,8 +89,6 @@ mod Identity { MainIdUpdate: MainIdUpdate, // components #[flat] - CustomUriEvent: custom_uri_component::Event, - #[flat] StorageReadEvent: storage_read_component::Event, #[flat] SRC5Event: SRC5Component::Event, @@ -151,15 +142,12 @@ mod Identity { } #[constructor] - fn constructor( - ref self: ContractState, owner: ContractAddress, token_uri_base: Span, - ) { + fn constructor(ref self: ContractState, owner: ContractAddress, token_uri_base: ByteArray,) { self.ownable.initializer(owner); - self.erc721.initializer('Starknet.id', 'ID'); - self.custom_uri.set_base_uri(token_uri_base); + self.erc721.initializer("Starknet.id", "ID", token_uri_base); } - #[external(v0)] + #[abi(embed_v0)] impl UpgradeableImpl of IUpgradeable { fn upgrade(ref self: ContractState, new_class_hash: ClassHash) { self.ownable.assert_only_owner(); @@ -167,7 +155,7 @@ mod Identity { } } - #[external(v0)] + #[abi(embed_v0)] impl IdentityImpl of IIdentity { fn owner_from_id(self: @ContractState, id: u128) -> ContractAddress { self.erc721.ERC721_owners.read(u256 { low: id, high: 0 }) @@ -324,13 +312,5 @@ mod Identity { ) ); } - - // this function should be called after upgrading from Cairo 0 contract - fn finalize_migration(ref self: ContractState, token_uri_base: Span) { - let caller = get_caller_address(); - assert(caller.into() == self.Proxy_admin.read(), 'only proxy admin can migrate'); - self.ownable.initializer(caller); - self.custom_uri.set_base_uri(token_uri_base); - } } } diff --git a/src/interface/identity.cairo b/src/interface/identity.cairo index 7a17559..1327d02 100644 --- a/src/interface/identity.cairo +++ b/src/interface/identity.cairo @@ -60,6 +60,4 @@ trait IIdentity { fn set_extended_verifier_data( ref self: TContractState, id: u128, field: felt252, data: Span, domain: u32 ); - - fn finalize_migration(ref self: TContractState, token_uri_base: Span); } diff --git a/src/tests/utils.cairo b/src/tests/utils.cairo index eaecf27..34ac4bd 100644 --- a/src/tests/utils.cairo +++ b/src/tests/utils.cairo @@ -17,6 +17,6 @@ fn deploy(contract_class_hash: felt252, calldata: Array) -> ContractAdd } fn deploy_identity() -> IIdentityDispatcher { - let address = deploy(Identity::TEST_CLASS_HASH, array![ADMIN, 0]); + let address = deploy(Identity::TEST_CLASS_HASH, array![ADMIN, 0, 0, 0]); IIdentityDispatcher { contract_address: address } }