From f6ad3e7bdf1fadfde6b33fc16041a40f87f9a249 Mon Sep 17 00:00:00 2001 From: Konstantin Fastov Date: Tue, 26 Mar 2024 11:33:24 +0700 Subject: [PATCH] Use OZ account preset instead of a local copy --- src/tests.cairo | 1 - src/tests/integration/test_base.cairo | 2 +- src/tests/integration/test_metadata.cairo | 2 +- src/tests/integration/test_receiver.cairo | 2 +- .../integration/test_slot_approvable.cairo | 2 +- .../integration/test_slot_enumerable.cairo | 2 +- src/tests/mocks/account.cairo | 252 ------------------ src/tests/unit/test_metadata.cairo | 2 +- src/tests/unit/test_slot_enumerable.cairo | 2 +- src/tests/unit/test_transfer_to_address.cairo | 2 +- src/tests/unit/test_transfer_to_token.cairo | 2 +- 11 files changed, 9 insertions(+), 262 deletions(-) delete mode 100644 src/tests/mocks/account.cairo diff --git a/src/tests.cairo b/src/tests.cairo index 827d509..c439efd 100644 --- a/src/tests.cairo +++ b/src/tests.cairo @@ -13,7 +13,6 @@ mod unit { } mod mocks { - mod account; mod receiver; mod contracts; } diff --git a/src/tests/integration/test_base.cairo b/src/tests/integration/test_base.cairo index a1b554e..0dc1b95 100644 --- a/src/tests/integration/test_base.cairo +++ b/src/tests/integration/test_base.cairo @@ -11,6 +11,7 @@ use starknet::testing; // External deps use openzeppelin::introspection::interface::{ISRC5Dispatcher, ISRC5DispatcherTrait, ISRC5_ID}; use openzeppelin::token::erc721::interface::{IERC721Dispatcher, IERC721DispatcherTrait, IERC721_ID}; +use openzeppelin::presets::Account; // Local deps use cairo_erc_3525::interface::{IERC3525Dispatcher, IERC3525DispatcherTrait, IERC3525_ID}; @@ -18,7 +19,6 @@ use cairo_erc_3525::presets::erc3525_mintable_burnable::{ ERC3525MintableBurnable, IExternalDispatcher, IExternalDispatcherTrait }; use cairo_erc_3525::tests::integration::constants; -use cairo_erc_3525::tests::mocks::account::Account; #[derive(Drop)] struct Signers { diff --git a/src/tests/integration/test_metadata.cairo b/src/tests/integration/test_metadata.cairo index dcbe5c8..7a95707 100644 --- a/src/tests/integration/test_metadata.cairo +++ b/src/tests/integration/test_metadata.cairo @@ -11,6 +11,7 @@ use starknet::testing; // External deps use openzeppelin::introspection::interface::{ISRC5Dispatcher, ISRC5DispatcherTrait, ISRC5_ID}; use openzeppelin::token::erc721::interface::{IERC721Dispatcher, IERC721DispatcherTrait, IERC721_ID}; +use openzeppelin::presets::Account; // Local deps use cairo_erc_3525::interface::{IERC3525Dispatcher, IERC3525DispatcherTrait, IERC3525_ID}; @@ -21,7 +22,6 @@ use cairo_erc_3525::extensions::metadata::interface::{ IERC3525MetadataDispatcher, IERC3525MetadataDispatcherTrait, IERC3525_METADATA_ID }; use cairo_erc_3525::tests::integration::constants; -use cairo_erc_3525::tests::mocks::account::Account; #[derive(Drop)] struct Signers { diff --git a/src/tests/integration/test_receiver.cairo b/src/tests/integration/test_receiver.cairo index dba1d39..5045d40 100644 --- a/src/tests/integration/test_receiver.cairo +++ b/src/tests/integration/test_receiver.cairo @@ -11,6 +11,7 @@ use starknet::testing; // External deps use openzeppelin::introspection::interface::{ISRC5Dispatcher, ISRC5DispatcherTrait, ISRC5_ID}; use openzeppelin::token::erc721::interface::{IERC721Dispatcher, IERC721DispatcherTrait, IERC721_ID}; +use openzeppelin::presets::Account; // Local deps use cairo_erc_3525::interface::{IERC3525Dispatcher, IERC3525DispatcherTrait, IERC3525_ID}; @@ -19,7 +20,6 @@ use cairo_erc_3525::presets::erc3525_mintable_burnable::{ }; use cairo_erc_3525::tests::integration::constants; use cairo_erc_3525::tests::mocks::receiver::{IReceiverDispatcher, IReceiverDispatcherTrait}; -use cairo_erc_3525::tests::mocks::account::Account; use cairo_erc_3525::tests::mocks::receiver::Receiver; #[derive(Drop)] diff --git a/src/tests/integration/test_slot_approvable.cairo b/src/tests/integration/test_slot_approvable.cairo index 702a89e..01e6677 100644 --- a/src/tests/integration/test_slot_approvable.cairo +++ b/src/tests/integration/test_slot_approvable.cairo @@ -11,6 +11,7 @@ use starknet::testing; // External deps use openzeppelin::introspection::interface::{ISRC5Dispatcher, ISRC5DispatcherTrait, ISRC5_ID}; use openzeppelin::token::erc721::interface::{IERC721Dispatcher, IERC721DispatcherTrait, IERC721_ID}; +use openzeppelin::presets::Account; // Local deps use cairo_erc_3525::interface::{IERC3525Dispatcher, IERC3525DispatcherTrait, IERC3525_ID}; @@ -22,7 +23,6 @@ use cairo_erc_3525::extensions::slotapprovable::interface::{ IERC3525_SLOT_APPROVABLE_ID }; use cairo_erc_3525::tests::integration::constants; -use cairo_erc_3525::tests::mocks::account::Account; #[derive(Drop)] struct Signers { diff --git a/src/tests/integration/test_slot_enumerable.cairo b/src/tests/integration/test_slot_enumerable.cairo index 2130180..f110a82 100644 --- a/src/tests/integration/test_slot_enumerable.cairo +++ b/src/tests/integration/test_slot_enumerable.cairo @@ -11,6 +11,7 @@ use starknet::testing; // External deps use openzeppelin::introspection::interface::{ISRC5Dispatcher, ISRC5DispatcherTrait, ISRC5_ID}; use openzeppelin::token::erc721::interface::{IERC721Dispatcher, IERC721DispatcherTrait, IERC721_ID}; +use openzeppelin::presets::Account; // Local deps use cairo_erc_3525::interface::{IERC3525Dispatcher, IERC3525DispatcherTrait, IERC3525_ID}; @@ -22,7 +23,6 @@ use cairo_erc_3525::extensions::slotenumerable::interface::{ IERC3525_SLOT_ENUMERABLE_ID }; use cairo_erc_3525::tests::integration::constants; -use cairo_erc_3525::tests::mocks::account::Account; use debug::PrintTrait; diff --git a/src/tests/mocks/account.cairo b/src/tests/mocks/account.cairo deleted file mode 100644 index 868ce8f..0000000 --- a/src/tests/mocks/account.cairo +++ /dev/null @@ -1,252 +0,0 @@ -// SPDX-License-Identifier: MIT -// OpenZeppelin Contracts for Cairo v0.7.0 (account/account.cairo) -// TODO use imported OZ account if possible -// or upgrade it according to the upstream - -use array::ArrayTrait; -use array::SpanTrait; -use option::OptionTrait; -use serde::Serde; -use starknet::ContractAddress; -use starknet::account::Call; - -const TRANSACTION_VERSION: felt252 = 1; - -// 2**128 + TRANSACTION_VERSION -const QUERY_VERSION: felt252 = 340282366920938463463374607431768211457; - -#[starknet::interface] -trait PublicKeyTrait { - fn set_public_key(ref self: TState, new_public_key: felt252); - fn get_public_key(self: @TState) -> felt252; -} - -#[starknet::interface] -trait PublicKeyCamelTrait { - fn setPublicKey(ref self: TState, newPublicKey: felt252); - fn getPublicKey(self: @TState) -> felt252; -} - -#[starknet::contract] -mod Account { - use array::ArrayTrait; - use array::SpanTrait; - use box::BoxTrait; - use ecdsa::check_ecdsa_signature; - - use openzeppelin::account::interface; - use openzeppelin::introspection::interface::ISRC5; - use openzeppelin::introspection::interface::ISRC5Camel; - use openzeppelin::introspection::src5::SRC5Component; - use option::OptionTrait; - use starknet::get_caller_address; - use starknet::get_contract_address; - use starknet::get_tx_info; - - use super::Call; - use super::QUERY_VERSION; - use super::TRANSACTION_VERSION; - use zeroable::Zeroable; - - component!(path: SRC5Component, storage: src5, event: SRC5Event); - - impl SRC5InternalImpl = openzeppelin::introspection::src5::SRC5Component::InternalImpl; - - #[storage] - struct Storage { - #[substorage(v0)] - src5: SRC5Component::Storage, - public_key: felt252 - } - - #[event] - #[derive(Drop, starknet::Event)] - enum Event { - #[flat] - SRC5Event: SRC5Component::Event, - OwnerAdded: OwnerAdded, - OwnerRemoved: OwnerRemoved, - } - - #[derive(Drop, starknet::Event)] - struct OwnerAdded { - new_owner_guid: felt252 - } - - #[derive(Drop, starknet::Event)] - struct OwnerRemoved { - removed_owner_guid: felt252 - } - - #[constructor] - fn constructor(ref self: ContractState, _public_key: felt252) { - self.initializer(_public_key); - } - - // - // External - // - - #[abi(embed_v0)] - impl SRC6Impl of interface::ISRC6 { - fn __execute__(self: @ContractState, mut calls: Array) -> Array> { - // Avoid calls from other contracts - // https://github.com/OpenZeppelin/cairo-contracts/issues/344 - let sender = get_caller_address(); - assert(sender.is_zero(), 'Account: invalid caller'); - - // Check tx version - let tx_info = get_tx_info().unbox(); - let version = tx_info.version; - if version != TRANSACTION_VERSION { - assert(version == QUERY_VERSION, 'Account: invalid tx version'); - } - - _execute_calls(calls) - } - - fn __validate__(self: @ContractState, mut calls: Array) -> felt252 { - self.validate_transaction() - } - - fn is_valid_signature( - self: @ContractState, hash: felt252, signature: Array - ) -> felt252 { - if self._is_valid_signature(hash, signature.span()) { - starknet::VALIDATED - } else { - 0 - } - } - } - - #[abi(embed_v0)] - impl SRC6CamelOnlyImpl of interface::ISRC6CamelOnly { - fn isValidSignature( - self: @ContractState, hash: felt252, signature: Array - ) -> felt252 { - SRC6Impl::is_valid_signature(self, hash, signature) - } - } - - #[abi(embed_v0)] - impl DeclarerImpl of interface::IDeclarer { - fn __validate_declare__(self: @ContractState, class_hash: felt252) -> felt252 { - self.validate_transaction() - } - } - - #[abi(embed_v0)] - impl SRC5Impl of ISRC5 { - fn supports_interface(self: @ContractState, interface_id: felt252) -> bool { - self.src5.supports_interface(interface_id) - } - } - - #[abi(embed_v0)] - impl SRC5CamelImpl of ISRC5Camel { - fn supportsInterface(self: @ContractState, interfaceId: felt252) -> bool { - self.src5.supportsInterface(interfaceId) - } - } - - #[abi(embed_v0)] - impl PublicKeyImpl of super::PublicKeyTrait { - fn get_public_key(self: @ContractState) -> felt252 { - self.public_key.read() - } - - fn set_public_key(ref self: ContractState, new_public_key: felt252) { - assert_only_self(); - self.emit(OwnerRemoved { removed_owner_guid: self.public_key.read() }); - self._set_public_key(new_public_key); - } - } - - #[abi(embed_v0)] - impl PublicKeyCamelImpl of super::PublicKeyCamelTrait { - fn getPublicKey(self: @ContractState) -> felt252 { - self.public_key.read() - } - - fn setPublicKey(ref self: ContractState, newPublicKey: felt252) { - PublicKeyImpl::set_public_key(ref self, newPublicKey); - } - } - - #[abi(embed_v0)] - fn __validate_deploy__( - self: @ContractState, - class_hash: felt252, - contract_address_salt: felt252, - _public_key: felt252 - ) -> felt252 { - self.validate_transaction() - } - - // - // Internal - // - - #[generate_trait] - impl InternalImpl of InternalTrait { - fn initializer(ref self: ContractState, _public_key: felt252) { - self.src5.register_interface(interface::ISRC6_ID); - self._set_public_key(_public_key); - } - - fn validate_transaction(self: @ContractState) -> felt252 { - let tx_info = get_tx_info().unbox(); - let tx_hash = tx_info.transaction_hash; - let signature = tx_info.signature; - assert(self._is_valid_signature(tx_hash, signature), 'Account: invalid signature'); - starknet::VALIDATED - } - - fn _set_public_key(ref self: ContractState, new_public_key: felt252) { - self.public_key.write(new_public_key); - self.emit(OwnerAdded { new_owner_guid: new_public_key }); - } - - fn _is_valid_signature( - self: @ContractState, hash: felt252, signature: Span - ) -> bool { - let valid_length = signature.len() == 2_u32; - - if valid_length { - check_ecdsa_signature( - hash, self.public_key.read(), *signature.at(0_u32), *signature.at(1_u32) - ) - } else { - false - } - } - } - - fn assert_only_self() { - let caller = get_caller_address(); - let self = get_contract_address(); - assert(self == caller, 'Account: unauthorized'); - } - - fn _execute_calls(mut calls: Array) -> Array> { - let mut res = ArrayTrait::new(); - loop { - match calls.pop_front() { - Option::Some(call) => { - let _res = _execute_single_call(call); - res.append(_res); - }, - Option::None(_) => { - break (); - }, - }; - }; - res - } - - fn _execute_single_call(call: Call) -> Span { - let Call{to, selector, calldata } = call; - starknet::call_contract_syscall(to, selector, calldata).unwrap() - } -} diff --git a/src/tests/unit/test_metadata.cairo b/src/tests/unit/test_metadata.cairo index 1aa521b..9ee2b59 100644 --- a/src/tests/unit/test_metadata.cairo +++ b/src/tests/unit/test_metadata.cairo @@ -11,10 +11,10 @@ use starknet::testing::set_caller_address; // External imports use openzeppelin::token::erc721::erc721::ERC721Component; +use openzeppelin::presets::Account; // Local imports -use cairo_erc_3525::tests::mocks::account::Account; use cairo_erc_3525::module::ERC3525Component; use cairo_erc_3525::extensions::metadata::module::ERC3525MetadataComponent::{ ERC3525MetadataImpl, InternalImpl }; use cairo_erc_3525::extensions::metadata::module::ERC3525MetadataComponent; diff --git a/src/tests/unit/test_slot_enumerable.cairo b/src/tests/unit/test_slot_enumerable.cairo index 955ad08..bddefbc 100644 --- a/src/tests/unit/test_slot_enumerable.cairo +++ b/src/tests/unit/test_slot_enumerable.cairo @@ -12,10 +12,10 @@ use starknet::testing::set_caller_address; use openzeppelin::token::erc721::erc721::ERC721Component::{ ERC721Impl, InternalImpl as ERC721InternalImpl }; use openzeppelin::token::erc721::erc721::ERC721Component; +use openzeppelin::presets::Account; // Local imports -use cairo_erc_3525::tests::mocks::account::Account; use cairo_erc_3525::module::ERC3525Component::{ ERC3525Impl, InternalImpl }; use cairo_erc_3525::module::ERC3525Component; use cairo_erc_3525::extensions::slotenumerable::module::ERC3525SlotEnumerableComponent::{ diff --git a/src/tests/unit/test_transfer_to_address.cairo b/src/tests/unit/test_transfer_to_address.cairo index ec8d7b7..0c9a50d 100644 --- a/src/tests/unit/test_transfer_to_address.cairo +++ b/src/tests/unit/test_transfer_to_address.cairo @@ -12,10 +12,10 @@ use starknet::testing::set_caller_address; use openzeppelin::token::erc721::erc721::ERC721Component::{ ERC721Impl, InternalImpl as ERC721InternalImpl }; use openzeppelin::token::erc721::erc721::ERC721Component; +use openzeppelin::presets::Account; // Local imports -use cairo_erc_3525::tests::mocks::account::Account; use cairo_erc_3525::module::ERC3525Component::{ ERC3525Impl, InternalImpl }; use cairo_erc_3525::module::ERC3525Component; use cairo_erc_3525::tests::unit::constants::{ diff --git a/src/tests/unit/test_transfer_to_token.cairo b/src/tests/unit/test_transfer_to_token.cairo index 0be6226..c01496d 100644 --- a/src/tests/unit/test_transfer_to_token.cairo +++ b/src/tests/unit/test_transfer_to_token.cairo @@ -12,10 +12,10 @@ use starknet::testing::set_caller_address; use openzeppelin::token::erc721::erc721::ERC721Component::{ ERC721Impl, InternalImpl as ERC721InternalImpl }; use openzeppelin::token::erc721::erc721::ERC721Component; +use openzeppelin::presets::Account; // Local imports -use cairo_erc_3525::tests::mocks::account::Account; use cairo_erc_3525::module::ERC3525Component::{ ERC3525Impl, InternalImpl }; use cairo_erc_3525::module::ERC3525Component; use cairo_erc_3525::tests::unit::constants::{