Skip to content

Commit

Permalink
Feat/cairo update (#10)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Th0rgal authored Apr 18, 2024
1 parent 7abfe2d commit 21026f6
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 3 additions & 4 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/identity.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
mod main;
mod internal;
mod erc721;
42 changes: 0 additions & 42 deletions src/identity/erc721.cairo

This file was deleted.

2 changes: 1 addition & 1 deletion src/identity/internal.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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(_) => {},
Expand Down
32 changes: 6 additions & 26 deletions src/identity/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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);
Expand All @@ -39,8 +37,6 @@ mod Identity {
// allow to check what interface is supported
#[abi(embed_v0)]
impl SRC5Impl = SRC5Component::SRC5Impl<ContractState>;
#[abi(embed_v0)]
impl SRC5CamelImpl = SRC5Component::SRC5CamelImpl<ContractState>;
impl SRC5InternalImpl = SRC5Component::InternalImpl<ContractState>;
// make it a NFT
#[abi(embed_v0)]
Expand All @@ -49,8 +45,7 @@ mod Identity {
impl ERC721CamelOnlyImpl = ERC721Component::ERC721CamelOnlyImpl<ContractState>;
// allow to query name of nft collection
#[abi(embed_v0)]
impl IERC721MetadataImpl =
identity::identity::erc721::IERC721MetadataImpl<ContractState>;
impl IERC721MetadataImpl = ERC721Component::ERC721MetadataImpl<ContractState>;
// allow to query nft metadata json
#[abi(embed_v0)]
impl StorageReadImpl = storage_read_component::StorageRead<ContractState>;
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -151,23 +142,20 @@ mod Identity {
}

#[constructor]
fn constructor(
ref self: ContractState, owner: ContractAddress, token_uri_base: Span<felt252>,
) {
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<ContractState> {
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
self.ownable.assert_only_owner();
self.upgradeable._upgrade(new_class_hash);
}
}

#[external(v0)]
#[abi(embed_v0)]
impl IdentityImpl of IIdentity<ContractState> {
fn owner_from_id(self: @ContractState, id: u128) -> ContractAddress {
self.erc721.ERC721_owners.read(u256 { low: id, high: 0 })
Expand Down Expand Up @@ -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<felt252>) {
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);
}
}
}
2 changes: 0 additions & 2 deletions src/interface/identity.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,4 @@ trait IIdentity<TContractState> {
fn set_extended_verifier_data(
ref self: TContractState, id: u128, field: felt252, data: Span<felt252>, domain: u32
);

fn finalize_migration(ref self: TContractState, token_uri_base: Span<felt252>);
}
2 changes: 1 addition & 1 deletion src/tests/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ fn deploy(contract_class_hash: felt252, calldata: Array<felt252>) -> 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 }
}

0 comments on commit 21026f6

Please sign in to comment.