From b08d0e04b7e021fcd17d42246f7c16b7e244e1e2 Mon Sep 17 00:00:00 2001 From: Eric Nordelo Date: Thu, 12 Oct 2023 13:22:56 -0400 Subject: [PATCH] Migrate Ownable to component (#768) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: link (#545) * add submodule * update cairo * add Cargo and Makefile * update cairo * add deps in cairo_project, create lib * add presets * add base lib * add tests * remove old cairo lib and interface * remove unused import * fix vars * change external funcs to snake case * remove unused import * update cairo * add tests for externals * add bool assertions * update cairo * remove preset mods * add IERC20 trait * update cairo * clean up test * remove assertion * update cairo * simplify max_u256 * clarify error msg * add erc165 + tests * kickstart account module * clean up python project. ready for rust/cairo1 * re-structure project * re-structure project * re-structure project * update makefile * bump submodule * Update erc20 migration branch (#586) * Change the spelling of "StarkNet" to "Starknet" (#557) * fix: link (#545) * change StarkNet to Starknet --------- Co-authored-by: Eric Nordelo * add Cargo and Makefile * add deps in cairo_project, create lib * add presets * add base lib * add tests * remove old cairo lib and interface * remove unused import * fix vars * change external funcs to snake case * remove unused import * add tests for externals * add bool assertions * remove preset mods * add IERC20 trait * clean up test * remove assertion * simplify max_u256 * clarify error msg * clean up python project. ready for rust/cairo1 * add erc165 + tests * kickstart account module * re-structure project * re-structure project * re-structure project * update makefile * re-structure project * update to felt252, add use ContractAddress * formatting * remove colons from storage, remove _mint from initializer * add constructor test * add comments * fix format * check error msg in tests * set max_u256 as func * update cairo * update cargo * revert doc commit * remove __init__ * refix link --------- Co-authored-by: kongtaoxing <69096526+kongtaoxing@users.noreply.github.com> Co-authored-by: Eric Nordelo Co-authored-by: Martín Triay * Migrate security/initializable (#592) * update cairo * fix path * add security mod * add initializable * add tests * Update src/openzeppelin/security/initializable.cairo Co-authored-by: Martín Triay * remove underscore * add internal macros --------- Co-authored-by: Martín Triay * Use zeroable::zero() instead of contract_address_const::<0>() (#598) * use zeroable for address * cleanup import * Migrate security/pausable (#593) * fix path * add security mod * add pausable * add mock * add tests * add security mod * update cargo * update cairo * Apply suggestions from code review Co-authored-by: Martín Triay * remove underscore from _paused * remove unused imports * add test assertion for is_paused * move mocks inside tests crate * remove underscore * add blank line --------- Co-authored-by: Martín Triay * add utils and constants * Revert "add utils and constants" This reverts commit e86bcd2107b052e13c58a9af7907e1f82e427a60. * Migrate ERC165 (#582) * Apply suggestions from code review Co-authored-by: Andrew Fleming Co-authored-by: Hadrien Croubois * apply review suggestions * remove unused import * add internal macro * add deregister --------- Co-authored-by: Andrew Fleming Co-authored-by: Hadrien Croubois * Migrate constants (#611) * add utils and constants * simplify role val * add context comments for interface ids * Set up new CI (#599) * add lint and test * fix name * add md linter * add header * remove extra line * add blank line * remove env * change name * fix on condition * change name * fix formatting * remove trailing comma in storage * fix formatting (#613) * Normalize error message style (#606) * normalize error msg * update cairo * update Cargo * fix test command * update should_panic syntax * use super for interface import * Add `BoundedInt` and internal macros, update cairo (#600) * update cairo * update Cargo * remove path flag * refactor _spend_allowance with BoundedInt * add BoundedInt for u256_max * add internal macros * update cairo * update cairo * update Cargo * remove import * update expected syntax * remove redundant maxu256 fn * update expected syntax * Update src/openzeppelin/tests/test_erc20.cairo Co-authored-by: Hadrien Croubois * use into trait * use address zeroable in mod * set cairo to alpha7 * update Cargo --------- Co-authored-by: Hadrien Croubois * Migrate ownable (#604) * add ownable * add ownable tests * simplify renounce * fix formatting * add internal macros * Apply suggestions from code review Co-authored-by: Martín Triay * add test_transfer_ownership_from_zero * update cairo to alpha7 * update Cargo * fix expected syntax * update cairo to rc0 * update Cargo * fix import * simplify imports * Apply suggestions from code review Co-authored-by: Eric Nordelo * Update src/openzeppelin/token/erc20.cairo Co-authored-by: Eric Nordelo * fix set_caller_address --------- Co-authored-by: Martín Triay Co-authored-by: Eric Nordelo * Migrate security/reentrancyguard (#590) * add reentrancyguard * update Cargo * remove mytest * add reentrancy mocks * add utils * finish tests * remove unused func * add comments * clean up code * clean up code * Apply suggestions from code review Co-authored-by: Martín Triay * remove underscore * remove constructor in mock * update interface name * change count_this_recursive to count_external_recursive * move mocks inside tests dir * remove underscore * update cairo to alpha7 * update Cargo * add withdraw_gas * fix formatting * update cairo to rc0 * update Cargo * fix import * simplify imports * remove duplicate import * rename mod * add mock interface * finish tests * update cairo * add new line * Update src/openzeppelin/tests/mocks/reentrancy_mock.cairo Co-authored-by: Eric Nordelo * import dispatchers * fix formatting * Apply suggestions from code review Co-authored-by: Martín Triay * add deploy fn to utils * use deploy from utils --------- Co-authored-by: Martín Triay Co-authored-by: Eric Nordelo * Migrate access control (#605) * add accesscontrol * add accesscontrol tests * remove mock, add impl to contract * fix formatting * remove accesscontrol mock * formatting, remove unused imports * integrate erc165 * fix typo * update cairo to alpha7 * update Cargo * update expected syntax * update expected syntax * update cairo to rc0 * update Cargo * fix import * simplify imports * Apply suggestions from code review Co-authored-by: Martín Triay * fix storage mapping name * rename impl * add warning * fix comment * remove constructor, setup test constructor in setup * fix format * fix import format * add test for granting already granted role * add clarity to revoke role test * add test for revoking already revoked role * add test for renouncing already renounced role * add test for revoked admin role losing privileges * fix test_role_admin_cycle * add target func as comment * add default admin tests * refactor tests, rename actors * add tests for has_role and set_role_admin * tidy up tests * add general doc for events * fix assert_only_role test * remove redundant test * remove unnecessary assertion * remove duplicate test * Update src/openzeppelin/access/accesscontrol.cairo Co-authored-by: Martín Triay * fix imports, define constants locally * Apply suggestions from code review Co-authored-by: Martín Triay * fix test name * bump cairo * fix formatting --------- Co-authored-by: Martín Triay * Migrate account (#620) * continue account implementation * add missing account interface functions * tidy up module * fix validate * bump cairo + account changes * fix __execute__, add serde, rename felt>felt252 * tidy up code * add account tests * complete account implementation * Apply suggestions from code review Co-authored-by: Andrew Fleming Co-authored-by: Hadrien Croubois * apply review suggestions * remove unused import * clarify __execute__ guard * add account tests * add internals * tidy up * add internal macro * add internal macro * add deregister * update erc165 * feat: refactor account and add validate_transaction test * feat: work on span serde * feat: add test for validate * refactor: remove unnecessary imports * refactor: tests * feat: implement test_execute * Update src/openzeppelin/tests/test_account.cairo Co-authored-by: Martín Triay * Update src/openzeppelin/tests/test_account.cairo Co-authored-by: Martín Triay * feat: update from review * feat: format files * feat: SpanSerde compiling version * feat: update tests and interface * feat: format file * feat: add test for multicall * feat: add more tests * test: remove unused function * refactor: update ERC20 interface and module * feat: add impl again * refactor: apply review comments * test: execute out of gas * refactor: is valid signature because of lack of short circuit condicionals * refactor: apply updates from reviews * feat: add SpanSerde implementation * refactor: remove span_to_array * Update src/openzeppelin/tests/test_account.cairo Co-authored-by: Andrew Fleming * Update src/openzeppelin/account.cairo Co-authored-by: Andrew Fleming * test: update from review * feat: splitting account interface and abi * feat: add new account files * remove underscore on mod api methods * remove comment * remove underscore * move erc1271 id to interface * remove unused import * integrate deploy util * add comments for validate dummy params * add initializer * change error msg * add empty sig tests * move test * replace dummy args, fix comments * fix formatting * tidy up code * add erc20 transfer call in execute test * tidy up imports * remove unused const --------- Co-authored-by: Martín Triay Co-authored-by: Hadrien Croubois Co-authored-by: Eric Nordelo * Migrate ERC721 (#619) * continue account implementation * add missing account interface functions * tidy up module * fix validate * bump cairo + account changes * fix __execute__, add serde, rename felt>felt252 * tidy up code * WIP ERC721 * make format * fix dispatcher call * clean * working on tests * replace match with `is_some()` and `is_none()` for readeability * erc721 tests * use Option.expect * add account tests * check panic reason * rename _owner() to _owner_of() * spacing * complete account implementation * apply recommandation for PR * Apply suggestions from code review Co-authored-by: Andrew Fleming * check low level ownership int * prefix test function with test_ * Apply suggestions from code review Co-authored-by: Andrew Fleming Co-authored-by: Hadrien Croubois * apply review suggestions * remove unused import * clarify __execute__ guard * add account tests * add internals * tidy up * update ERC165 ids to u32 * apply sugestions from code review * Apply suggestions from code review Co-authored-by: Martín Triay * update & expand tests * update lock * add internal macro * add internal macro * add deregister * update erc165 * wip (dispatched issue) * fix abi * start array→span transition * minimise account dependency * add SpanSerde in utils * add dual interfaces * update test message. fix linter * split interfaces from preset module * fix linter * rename metadata id var * add camelCase to traits * fully implement dual interface traits * simplify _owner_of * add constructor and getter tests * add token_uri tests * remove conflictive test * add erc721receiver. add IERC721ABI * add safe_transfer_from tests * add safe_mint tests * Update src/openzeppelin/token/erc721/interface.cairo Co-authored-by: Eric Nordelo * move erc721 abi next to module * address review comments --------- Co-authored-by: Hadrien Croubois Co-authored-by: Andrew Fleming Co-authored-by: Andrew Fleming Co-authored-by: Eric Nordelo * Dual interface dispatcher for ERC721 (#623) * add dual interface dispatcher draft * complete dual721 implementation * fix format * add felt252 <> bool casts * simplify Felt252IntoBool * make felt252 into -> try_into * add base test suite * implement most tests * add mocks * add tests for panic cases * fix some tests * fix format * fix more tests * fix tests * apply review feedback * normalize mock names * add token_uri tests * rename non implementing mock * move comment * upgrade to cairo 1.1.1 * Update src/openzeppelin/utils.cairo Co-authored-by: Eric Nordelo * address review comments --------- Co-authored-by: Eric Nordelo * Replace ERC-165 with SRC-5 (Update Interface Ids) (#637) * feat: add src5 module * refactor: interface ids and account standard interface * refactor: naming convention * refactor: separate interfaces * feat: apply last SNIP update * fix: account interface signature * feat: apply updates from review * fix: accesscontrol interface id * Update src/openzeppelin/account/account.cairo Co-authored-by: Andrew Fleming * feat: apply update from review --------- Co-authored-by: Andrew Fleming * Fix conflicts from the dual721 and src5 merge (#641) * add abi to interfaces * change u32 to felt * Add camel support for ownable (#625) * add camel support * add camel tests * nest ownable into dir * move interface * add dual ownable * add const selectors * add try_selector_with_fallback and bool impl * nest access tests in test dirs * move accesscontrol and ownable test mods * move tests * move tests * add mocks * add dual ownable tests * add panic traits * tidy up tests * fix formatting * remove comments * fix hierarchy * remove import comments * remove comment * Apply suggestions from code review Co-authored-by: Martín Triay * remove unused import * fix camel dispatcher * normalize assert error msg * fix formatting * remove redundant assertion * change selector casing * remove unused import * fix comments, add ref for ignored tests * fix comments * remove panic trait * bump to cairo v1.1.1 * update Cargo * remove ignore from tests * Apply suggestions from code review Co-authored-by: Eric Nordelo * remove selectors from constants * add selectors to selectors.cairo * remove selector bindings * fix comment * remove/fix comments * fix conflicts * Update src/openzeppelin/utils/selectors.cairo Co-authored-by: Eric Nordelo --------- Co-authored-by: Martín Triay Co-authored-by: Eric Nordelo * Add camel support for access control (#626) * add camel support * add camel tests * move accesscontrol into dir * separate interface * add try_selector_with_fallback and bool impl * add const selectors * add dual accesscontrol * move access tests * add panic trait * add accesscontrol mocks * add dual accesscontrol tests * fix formatting * Apply suggestions from code review Co-authored-by: Eric Nordelo * add comment to supportsInterface * add tests for supports_interface * remove comments * alphabetize use clauses * remove util panic trait * remove unused imports * remove unused imports * remove unused import * update selector casings * Apply suggestions from code review Co-authored-by: Martín Triay * fix casing on target calls * add space to comments * remove comments, add ref with ignored tests * remove unused imports * remove selectors from constants * add selectors to selectors mod * fix formatting * reorder selectors * fix comment * update interface id * replace erc165 with src5 * fix conflicts * replace u32 with felt * add abi to interfaces * remove ignore from tests * fix formatting * fix comment * remove camel supportsInterface * fix comment consistency * Update src/openzeppelin/tests/access/test_dual_accesscontrol.cairo Co-authored-by: Eric Nordelo * add append_serde * change append to append_serde * remove unused import * remove bindings --------- Co-authored-by: Eric Nordelo Co-authored-by: Martín Triay * erc20 dual dispatcher (#622) * add camel methods * update erc20 dispatcher path * refactor tests, add tests for camel methods * fix formatting * add try_selector_with_fallback and bool impl * add erc20 selectors * add abi to traits * add dual20 mocks * add panic trait * add dual20 mocks * add dual20 * add dual20 * add dual20 tests * tidy up tests * move tests to token dir * fix formatting * clean up tests * restructure token tests * remove panic trait * reorder imports * remove unused imports * fix comments * update const selector casings * remove comments * Apply suggestions from code review Co-authored-by: Eric Nordelo * change target member to contract_address * fix formatting * fix dispatcher calls * change dispatcher to dual_dispatcher * reorder use clauses * move selectors to selectors mod * fix formatting * reorder selectors * remove duplicate selectors * remove ignore * simplify dispatcher vars * fix comment * Apply suggestions from code review Co-authored-by: Eric Nordelo * fix test constants * add SerializedAppend trait * integrate append_serde * integrate append_serde and utils selector * change u256 fns to constants * change u256 fns to constants * move dual721 test to token/, apply append_serde, change u256 fn to constant * fix formatting * add append_serde * move append_serde to serde * update append_serde path * change dispatcher to IERC20 * remove unnecessary into() * remove bindings * add binding to empty arrays * fix formatting --------- Co-authored-by: Eric Nordelo * Add `UnwrapAndCast` trait (#644) * add unwrap_and_cast trait and impls * use unwrap_and_cast * tidy up code * add UnwrapAndCastU8 impl * reorder use clauses * use unwrap_and_cast on view fns * add test_dual721 mod (#648) * Update account interface (#646) * feat: update interface and ids * refactor: format files * feat: update from reviews * Update src/openzeppelin/account/account.cairo Co-authored-by: Andrew Fleming --------- Co-authored-by: Andrew Fleming * Add camel support for Account (#647) * feat: update interface and ids * refactor: format files * feat: update from reviews * feat: add dual interface to account * fix: tests * feat: add tests * feat: add more tests * feat: apply review suggestions * feat: apply review updates * Update src/openzeppelin/tests/account/test_dual_account.cairo Co-authored-by: Andrew Fleming --------- Co-authored-by: Andrew Fleming * Add src5 dual dispatcher (#659) * add dual src5 * add dual src5 tests and mocks * add felt impl * remove unused imports, integrate unwrap_and_cast for felts * add supports_interface to interface * add supports_interface to erc721 interface * add camelCase supportsInterface test * add dual supports_interface to mocks * add dual supports_interface tests * add dual supports_interface to tests * add comment * fix formatting * clean up test * add comments * remove unused imports * fix interface * fix src5 impl * apply review suggestion * Add dual case erc721 receiver (#649) * add 721 holder * add 721 receiver selectors * add abis * add dual receiver * add dual receiver mocks * add dual 721 receiver and holder mods * add dual 721 receiver tests * integrate dual case receiver * remove camel supportsInterface * add isrc5 impl * add isrc5 impl * fix formatting * change receiver import * remove unused mod * add isrc5 impl * remove mod * fix formatting * remove conflict * add felt impl to UnwrapAndCast * clean up mocks * add camelCase ISRC5 impl * add camelCase tests for safe methods * fix test * remove unused imports * remove unused imports * remove unused imports * Apply suggestions from code review Co-authored-by: Eric Nordelo * add transferFrom and safeTransferFrom tests * fix comments * fix comments --------- Co-authored-by: Eric Nordelo * Add owner param to ownable initializer (#660) * feat: add owner param to ownable initializer * feat: apply review updates * fix: remove unnecessary mut * Update src/openzeppelin/tests/access/test_dual_ownable.cairo Co-authored-by: Andrew Fleming --------- Co-authored-by: Andrew Fleming * Migrate SRC5 to Cairo 2 (#664) * feat: migrate files * feat: add interface.cairo * feat: apply review updates * refactor: improve readability * Migrate initializable to cairo2 (#661) * bump to cairo v2.0.2 * update Cargo * comment out mods * update initializable syntax * move security tests * update initializable tests * fix formatting * update cairo * update Cargo * simplify and clarify tests * fix formatting * Apply suggestions from code review Co-authored-by: Eric Nordelo * fix tests --------- Co-authored-by: Eric Nordelo * Migrate Account to Cairo 2 (#666) * feat: migrate files * feat: add interface.cairo * feat: apply review updates * refactor: improve readability * fix: dual account tests * fix: account tests * feat: apply review updates * Migrate pausable to cairo2 (#662) * bump to cairo v2.0.2 * update Cargo * comment out mods * update initializable syntax * move security tests * update initializable tests * fix formatting * update pausable syntax * update pausable tests * update cairo * update Cargo * simplify and clarify tests * fix formatting * simplify and clarify tests * Apply suggestions from code review Co-authored-by: Eric Nordelo * fix tests * change StorageTrait to InternalTrait * use InternalImpl * add external pausable impl * Apply suggestions from code review Co-authored-by: Eric Nordelo --------- Co-authored-by: Eric Nordelo * Migrate reentrancyguard to cairo2 (#663) * bump to cairo v2.0.2 * update Cargo * comment out mods * update initializable syntax * move security tests * update initializable tests * fix formatting * remove SpanSerde * update reentrancyguard syntax * add reentrancyguard mocks * update reentrancyguard tests * update cairo * update Cargo * simplify and clarify tests * fix formatting * simplify and clarify tests * fix formatting * Apply suggestions from code review Co-authored-by: Eric Nordelo * fix tests * change StorageTrait to InternalTrait * remove internal is_entered * use internal contract state * Apply suggestions from code review Co-authored-by: Eric Nordelo * fix import order * change generic state to TState --------- Co-authored-by: Eric Nordelo * Migrate ERC721 to Cairo2 (#667) * feat: migrate files * feat: add interface.cairo * feat: apply review updates * refactor: improve readability * fix: dual account tests * fix: account tests * fix: mocks * fix: dual721 tests * fix: erc721 tests * feat: apply review updates * feat: apply review suggestions * Migrate ownable to cairo2 (#665) * bump to cairo v2.0.2 * update Cargo * comment out mods * update initializable syntax * move security tests * update initializable tests * fix formatting * remove SpanSerde * update ownable syntax * add ownable_camel impl * update syntax in ownable mocks * update ownable tests * fix formatting * update cairo * update Cargo * simplify and clarify tests * fix formatting * simplify internal fns * add initializer * fix constructor, tidy up code * fix tests * Apply suggestions from code review Co-authored-by: Eric Nordelo * fix tests * change StorageTrait to InternalTrait * start refactor * change impls to external fns * use IOwnableCamelOnly * fix tests * remove unneeded mut * remove unneeded mut * Apply suggestions from code review Co-authored-by: Eric Nordelo * change to TState * fix formatting * remove Ownable prefix in fn calls * remove internal owner fn * use internal state trait to read owner --------- Co-authored-by: Eric Nordelo * Migrate ERC20 to Cairo 2 (#669) * feat: fix tests * feat: apply review updates * Migrate access control to cairo2 (#668) * bump to cairo v2.0.2 * update Cargo * comment out mods * update initializable syntax * move security tests * update initializable tests * fix formatting * remove SpanSerde * update ownable syntax * add ownable_camel impl * update syntax in ownable mocks * update ownable tests * fix formatting * update cairo * update Cargo * simplify and clarify tests * fix formatting * simplify internal fns * add initializer * fix constructor, tidy up code * fix tests * Apply suggestions from code review Co-authored-by: Eric Nordelo * fix tests * change StorageTrait to InternalTrait * update syntax * update mocks * start updating tests * start refactor * change impls to external fns * use IOwnableCamelOnly * fix tests * remove unneeded mut * remove unneeded mut * fix mocks * simplify events * change array syntax * update syntax in tests * fix formatting * Apply suggestions from code review Co-authored-by: Eric Nordelo * add space between events * fix generic state * add space between events * fix formatting --------- Co-authored-by: Eric Nordelo * Migrate to Scarb for cairo-2 (#671) * Migrate to Scarb * Hide tests module behind cfg * Add spdx (#684) * add spdx * add spdx * feat: sort imports (#679) * Migrate upgrades (#603) * add upgrades to lib * add proxy mod * update cairo * update Cargo * fix test command * add upgrades * fix spelling * add upgrade test * fix spelling * add upgrade_and_call * update cairo * update Cargo * add events * tidy up code * fix format * update cairo to alpha7 * update Cargo * fix expected syntax * fix conflict * update expected syntax * formatting * update cairo to rc0 * update Cargo * fix import * refactor mocks * start test refactor * simplify upgrades, add upgrade_and_call * refactor mocks * refactor upgrades * refactor tests * fix imports, add impl * fix imports * fix formatting * add abis to mocks * refactor tests with dispatchers * fix formatting * start migration * update syntax * update syntax in mocks * fix tests * add line * remove line * add upgradeable trait * replace interface impl with trait impl * fix formatting * add assertion * add tests for events * Apply suggestions from code review Co-authored-by: Eric Nordelo * move logic to internal impl * add interface * update paths * fix formatting * simplify imports * Apply suggestions from code review Co-authored-by: Eric Nordelo * Apply suggestions from code review Co-authored-by: Martín Triay * remove upgrade_and_call, test event in separate test * remove upgrade_and_call * remove unused import * reorder imports, tidy up code --------- Co-authored-by: Eric Nordelo Co-authored-by: Martín Triay * Add testing for Ownable events (#675) * feat: add tests for ownable events * feat: improve tests * Update src/tests/utils.cairo Co-authored-by: Martín Triay --------- Co-authored-by: Martín Triay * Add account events (#687) * feat: add tests for ownable events * define account events * add event to set_public_key * add event test * change param name * fix event emit param * fix formatting * add tests for events * add default checks in tests * feat: improve tests * Apply suggestions from code review Co-authored-by: Eric Nordelo * reorder imports, remove Account prefix * fix events * fix formatting * Update src/tests/utils.cairo Co-authored-by: Martín Triay * add event checks, use ZERO from constants * fix formatting --------- Co-authored-by: Eric Nordelo Co-authored-by: Martín Triay * Add testing for AccessControl events (#674) * feat: add testing for events * feat: add tests for ownable events * feat: improve tests * feat: apply review updates * Add testing for Pausable events (#676) * feat: add testing for events * feat: add tests for ownable events * feat: add tests for pausable events * Update src/openzeppelin/tests/security/test_pausable.cairo Co-authored-by: Andrew Fleming * feat: improve tests * feat: apply review updates * feat: apply review updates --------- Co-authored-by: Andrew Fleming Co-authored-by: Martín Triay * Add testing for ERC20 events (#677) * feat: add testing for events * feat: add tests for ownable events * feat: add tests for pausable events * feat: add tests for erc20 events * Update src/openzeppelin/tests/security/test_pausable.cairo Co-authored-by: Andrew Fleming * feat: apply review updates * feat: improve tests * feat: apply review updates * feat: apply review updates * feat: apply review updates --------- Co-authored-by: Andrew Fleming Co-authored-by: Martín Triay * Add mint to erc721 preset constructor (#700) * add _mint to constructor * add constructor assertions * fix formatting * Add testing for ERC721 events (#678) * feat: add testing for events * feat: add tests for ownable events * feat: add tests for pausable events * feat: add tests for erc20 events * feat: add tests for erc721 events * Update src/openzeppelin/tests/security/test_pausable.cairo Co-authored-by: Andrew Fleming * feat: apply review updates * feat: add test helper for events * feat: improve tests * feat: apply review updates * feat: apply review updates * feat: apply review updates * feat: apply review updates * feat: apply review updates --------- Co-authored-by: Andrew Fleming * Update README and fix scarb package (#688) * update README to cairo-2 and scarb usage. bump scarb package version * update readme fix linter issues fix linter issues fix linter issues update readme * add main to CI * update readme * update readme * rename starknet * fix linter * update readme * Update README.md Co-authored-by: Andrew Fleming * Update README.md Co-authored-by: Andrew Fleming * update CONTRIBUTING * fix linting * Update CONTRIBUTING.md Co-authored-by: Andrew Fleming * Update CONTRIBUTING.md Co-authored-by: Andrew Fleming * apply review changes * fix linter * fix typo --------- Co-authored-by: Andrew Fleming * Bump scarb and fix dual721 test context (#703) * bump scarb and cairo version * fix testing context --------- Co-authored-by: Martín Triay * add rc version (#708) * remove unused imports (#707) * Upgrade Cairo version and Scarb version to latest releases (#712) * 👽️ Update code due to breaking changes * ⬆️ Upgrade scarb and cairo versions * 💄 Code format * bump antora (#715) * Add selector inline macro/fix `tokenURI` (#724) * update selectors * use inline selector * remove selectors file * remove import * fix camel tokenURI * fix formatting * re-add selectors mod * import from selectors mod * fix formatting * import selector * fix: naming convention (#732) * feat: add Errors modules (#691) * Update account docs (#709) * feat: update format and add api * fix: typo * feat: add counterfactual deployment doc * feat: add API entries * feat: add events * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/guides/deployment.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/guides/deployment.adoc Co-authored-by: Andrew Fleming * feat: update from reviews * feat: apply review updates * feat: update docs * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/guides/deployment.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/guides/deployment.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/guides/deployment.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/guides/deployment.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * feat: apply review updates * fix: account casing * feat: add headers * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * feat: add link * feat: move API * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * refactor: update wording * Update docs/antora.yml Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * feat: apply update reviews * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * refactor: UI * fix: UI * feat: focus on SRC6 * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * feat: apply review updates --------- Co-authored-by: Andrew Fleming Co-authored-by: Martín Triay * Add Interface & Dispatchers docs (#730) * feat: update format and add api * fix: typo * feat: add counterfactual deployment doc * feat: add API entries * feat: add events * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/guides/deployment.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/guides/deployment.adoc Co-authored-by: Andrew Fleming * feat: update from reviews * feat: apply review updates * feat: update docs * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/guides/deployment.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/guides/deployment.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/guides/deployment.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/guides/deployment.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * feat: apply review updates * fix: account casing * feat: add headers * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * feat: add link * feat: move API * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * refactor: update wording * Update docs/antora.yml Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * feat: apply update reviews * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * refactor: UI * fix: UI * feat: focus on SRC6 * add interface & dispatchers docs * apply review feedback * address feedback comments --------- Co-authored-by: Eric Nordelo Co-authored-by: Andrew Fleming * Update overview docs (#735) * feat: update format and add api * fix: typo * feat: add counterfactual deployment doc * feat: add API entries * feat: add events * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/guides/deployment.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/guides/deployment.adoc Co-authored-by: Andrew Fleming * feat: update from reviews * feat: apply review updates * feat: update docs * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/guides/deployment.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/guides/deployment.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/guides/deployment.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/guides/deployment.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * feat: apply review updates * fix: account casing * feat: add headers * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * feat: add link * feat: move API * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Andrew Fleming * refactor: update wording * Update docs/antora.yml Co-authored-by: Andrew Fleming * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * feat: apply update reviews * Update docs/modules/ROOT/pages/api/account.adoc Co-authored-by: Martín Triay * refactor: UI * fix: UI * feat: focus on SRC6 * Update docs/modules/ROOT/pages/accounts.adoc Co-authored-by: Martín Triay * feat: update overview * feat: apply review updates * Update docs/modules/ROOT/pages/index.adoc Co-authored-by: Martín Triay * feat: apply review updates * Update docs/modules/ROOT/pages/index.adoc Co-authored-by: Andrew Fleming * feat: apply review updates * Update docs/modules/ROOT/pages/index.adoc Co-authored-by: Andrew Fleming --------- Co-authored-by: Andrew Fleming Co-authored-by: Martín Triay * remove underscore from name_ and symbol_ (#738) * Add prefixes to storage members (#743) * feat: add prefixes * Update src/access/accesscontrol/accesscontrol.cairo Co-authored-by: Andrew Fleming --------- Co-authored-by: Andrew Fleming * Sanitizing for release. (#736) * refactor: sanitizing * feat: sanitizing * feat: apply review updates * feat: apply review updates * feat: migrate logic * feat: update workflow * fix: docs * feat: apply update reviews * refactor: remove imports * Update src/access/ownable/ownable.cairo Co-authored-by: Andrew Fleming * feat: apply review updates * feat: add comments to camelCase functions * fix: comment * Update docs/modules/ROOT/pages/api/access.adoc Co-authored-by: Andrew Fleming * feat: apply review updates * feat: update docs * feat: update comments * Update src/tests/utils.cairo Co-authored-by: Andrew Fleming --------- Co-authored-by: Andrew Co-authored-by: Martín Triay Co-authored-by: kongtaoxing <69096526+kongtaoxing@users.noreply.github.com> Co-authored-by: Hadrien Croubois Co-authored-by: Andrew Fleming Co-authored-by: maciektr Co-authored-by: Bal7hazar @ Carbonable --- docs/modules/ROOT/pages/access.adoc | 39 ++++-- docs/modules/ROOT/pages/api/access.adoc | 32 ++++- src/access/ownable/dual_ownable.cairo | 8 +- src/access/ownable/ownable.cairo | 99 ++++++++++------ src/tests/access/test_dual_ownable.cairo | 21 ++-- src/tests/access/test_ownable.cairo | 72 ++++++------ src/tests/mocks.cairo | 2 +- src/tests/mocks/dual_ownable_mocks.cairo | 116 ------------------ src/tests/mocks/ownable_mocks.cairo | 144 +++++++++++++++++++++++ 9 files changed, 311 insertions(+), 222 deletions(-) delete mode 100644 src/tests/mocks/dual_ownable_mocks.cairo create mode 100644 src/tests/mocks/ownable_mocks.cairo diff --git a/docs/modules/ROOT/pages/access.adoc b/docs/modules/ROOT/pages/access.adoc index b469e8810..6c63f53eb 100644 --- a/docs/modules/ROOT/pages/access.adoc +++ b/docs/modules/ROOT/pages/access.adoc @@ -19,26 +19,42 @@ OpenZeppelin Contracts for Cairo provides {ownable-cairo} for implementing owner === Usage -Integrating this module into a contract first requires assigning an owner. +Integrating this component into a contract first requires assigning an owner. The implementing contract's constructor should set the initial owner by passing the owner's address to Ownable's xref:/api/access.adoc#AccessControl-initializer[`initializer`] like this: [,javascript] ---- -use openzeppelin::access::ownable::Ownable; - #[starknet::contract] mod MyContract { + use openzeppelin::access::ownable::Ownable as ownable_component; use starknet::ContractAddress; - use super::Ownable; + + component!(path: ownable_component, storage: ownable, event: OwnableEvent); + + #[abi(embed_v0)] + impl OwnableImpl = ownable_component::OwnableImpl; + #[abi(embed_v0)] + impl OwnableCamelOnlyImpl = + ownable_component::OwnableCamelOnlyImpl; + impl InternalImpl = ownable_component::InternalImpl; #[storage] - struct Storage {} + struct Storage { + #[substorage(v0)] + ownable: ownable_component::Storage + } + + #[event] + #[derive(Drop, starknet::Event)] + enum Event { + OwnableEvent: ownable_component::Event + } #[constructor] - fn constructor(self: @ContractState, owner: ContractAddress) { - let mut unsafe_state = Ownable::unsafe_new_contract_state(); - Ownable::InternalImpl::initializer(ref unsafe_state, owner); + fn constructor(ref self: ContractState, owner: ContractAddress) { + // Set the initial owner of the contract + self.ownable.initializer(owner); } (...) @@ -51,15 +67,12 @@ To restrict a function's access to the owner only, add in the `assert_only_owner ---- #[starknet::contract] mod MyContract { - use openzeppelin::access::ownable::Ownable; - (...) #[external(v0)] - fn foo(ref self: ContractState) { + fn only_owner_allowed(ref self: ContractState) { // This function can only be called by the owner - let unsafe_state = Ownable::unsafe_new_contract_state(); - Ownable::InternalImpl::assert_only_owner(@unsafe_state); + self.ownable.assert_only_owner(); (...) } diff --git a/docs/modules/ROOT/pages/api/access.adoc b/docs/modules/ROOT/pages/api/access.adoc index 7e1562804..71b017914 100644 --- a/docs/modules/ROOT/pages/api/access.adoc +++ b/docs/modules/ROOT/pages/api/access.adoc @@ -27,10 +27,10 @@ use openzeppelin::access::ownable::Ownable; `Ownable` provides a basic access control mechanism where an account (an owner) can be granted exclusive access to specific functions. -This module includes the `assert_only_owner` internal to restrict a function to be used only by the owner. +This module includes the internal `assert_only_owner` to restrict a function to be used only by the owner. [.contract-index] -.External Functions +.Embeddable Functions -- .OwnableImpl @@ -39,6 +39,15 @@ This module includes the `assert_only_owner` internal to restrict a function to * xref:Ownable-renounce_ownership[`++renounce_ownership(self)++`] -- +[.contract-index] +.camelCase Support +-- +.OwnableCamelOnlyImpl + +* xref:Ownable-transferOwnership[`++transferOwnership(self, newOwner)++`] +* xref:Ownable-renounceOwnership[`++renounceOwnership(self)++`] +-- + [.contract-index] .Internal Functions -- @@ -55,8 +64,8 @@ This module includes the `assert_only_owner` internal to restrict a function to * xref:Ownable-OwnershipTransferred[`++OwnershipTransferred(previous_owner, new_owner)++`] -- -[#Ownable-External-Functions] -==== External Functions +[#Ownable-Embeddable-Functions] +==== Embeddable Functions [.contract-item] [[Ownable-owner]] @@ -83,6 +92,21 @@ Leaves the contract without owner. It will not be possible to call NOTE: Renouncing ownership will leave the contract without an owner, thereby removing any functionality that is only available to the owner. +[#Ownable-camelCase-Support] +==== camelCase Support + +[.contract-item] +[[Ownable-transferOwnership]] +==== `[.contract-item-name]#++transferOwnership++#++(ref self: ContractState, newOwner: ContractAddress)++` [.item-kind]#external# + +See xref:Ownable-transfer_ownership[transfer_ownership]. + +[.contract-item] +[[Ownable-renounceOwnership]] +==== `[.contract-item-name]#++renounceOwnership++#++(ref self: ContractState)++` [.item-kind]#external# + +See xref:Ownable-renounce_ownership[renounce_ownership]. + [#Ownable-Internal-Functions] ==== Internal Functions diff --git a/src/access/ownable/dual_ownable.cairo b/src/access/ownable/dual_ownable.cairo index 923f97c5b..c53e1ce06 100644 --- a/src/access/ownable/dual_ownable.cairo +++ b/src/access/ownable/dual_ownable.cairo @@ -1,13 +1,11 @@ // SPDX-License-Identifier: MIT // OpenZeppelin Contracts for Cairo v0.7.0 (access/ownable/dual_ownable.cairo) -use openzeppelin::utils::Felt252TryIntoBool; use openzeppelin::utils::UnwrapAndCast; use openzeppelin::utils::selectors; use openzeppelin::utils::serde::SerializedAppend; use openzeppelin::utils::try_selector_with_fallback; use starknet::ContractAddress; -use starknet::Felt252TryIntoContractAddress; use starknet::SyscallResultTrait; use starknet::call_contract_syscall; @@ -24,14 +22,14 @@ trait DualCaseOwnableTrait { impl DualCaseOwnableImpl of DualCaseOwnableTrait { fn owner(self: @DualCaseOwnable) -> ContractAddress { - let args = ArrayTrait::new(); + let args = array![]; call_contract_syscall(*self.contract_address, selectors::owner, args.span()) .unwrap_and_cast() } fn transfer_ownership(self: @DualCaseOwnable, new_owner: ContractAddress) { - let mut args = ArrayTrait::new(); + let mut args = array![]; args.append_serde(new_owner); try_selector_with_fallback( @@ -44,7 +42,7 @@ impl DualCaseOwnableImpl of DualCaseOwnableTrait { } fn renounce_ownership(self: @DualCaseOwnable) { - let mut args = ArrayTrait::new(); + let mut args = array![]; try_selector_with_fallback( *self.contract_address, diff --git a/src/access/ownable/ownable.cairo b/src/access/ownable/ownable.cairo index 9fac9da4a..d85e27f35 100644 --- a/src/access/ownable/ownable.cairo +++ b/src/access/ownable/ownable.cairo @@ -1,7 +1,15 @@ // SPDX-License-Identifier: MIT // OpenZeppelin Contracts for Cairo v0.7.0 (access/ownable/ownable.cairo) -#[starknet::contract] +/// # Ownable Component +/// +/// The Ownable component provides a basic access control mechanism, where +/// there is an account (an owner) that can be granted exclusive access to +/// specific functions. +/// +/// The initial owner can be set by using the `initializer` function in +/// construction time. This can later be changed with `transfer_ownership`. +#[starknet::component] mod Ownable { use openzeppelin::access::ownable::interface; use starknet::ContractAddress; @@ -30,20 +38,72 @@ mod Ownable { const ZERO_ADDRESS_OWNER: felt252 = 'New owner is the zero address'; } + #[embeddable_as(OwnableImpl)] + impl Ownable< + TContractState, +HasComponent + > of interface::IOwnable> { + /// Returns the address of the current owner. + fn owner(self: @ComponentState) -> ContractAddress { + self.Ownable_owner.read() + } + + /// Transfers ownership of the contract to a new address. + fn transfer_ownership( + ref self: ComponentState, new_owner: ContractAddress + ) { + assert(!new_owner.is_zero(), Errors::ZERO_ADDRESS_OWNER); + self.assert_only_owner(); + self._transfer_ownership(new_owner); + } + + /// Leaves the contract without owner. It will not be possible to call `assert_only_owner` + /// functions anymore. Can only be called by the current owner. + fn renounce_ownership(ref self: ComponentState) { + self.assert_only_owner(); + self._transfer_ownership(Zeroable::zero()); + } + } + + /// Adds camelCase support for `IOwnable`. + #[embeddable_as(OwnableCamelOnlyImpl)] + impl OwnableCamelOnly< + TContractState, +HasComponent + > of interface::IOwnableCamelOnly> { + fn transferOwnership(ref self: ComponentState, newOwner: ContractAddress) { + self.transfer_ownership(newOwner); + } + + fn renounceOwnership(ref self: ComponentState) { + self.renounce_ownership(); + } + } + #[generate_trait] - impl InternalImpl of InternalTrait { - fn initializer(ref self: ContractState, owner: ContractAddress) { + impl InternalImpl< + TContractState, +HasComponent + > of InternalTrait { + /// Sets the contract's initial owner. + /// + /// This function should be called at construction time. + fn initializer(ref self: ComponentState, owner: ContractAddress) { self._transfer_ownership(owner); } - fn assert_only_owner(self: @ContractState) { + /// Panics if called by any account other than the owner. Use this + /// to restrict access to certain functions to the owner. + fn assert_only_owner(self: @ComponentState) { let owner: ContractAddress = self.Ownable_owner.read(); let caller: ContractAddress = get_caller_address(); assert(!caller.is_zero(), Errors::ZERO_ADDRESS_CALLER); assert(caller == owner, Errors::NOT_OWNER); } - fn _transfer_ownership(ref self: ContractState, new_owner: ContractAddress) { + /// Transfers ownership of the contract to a new address. + /// + /// Internal function without access restriction. + fn _transfer_ownership( + ref self: ComponentState, new_owner: ContractAddress + ) { let previous_owner: ContractAddress = self.Ownable_owner.read(); self.Ownable_owner.write(new_owner); self @@ -52,33 +112,4 @@ mod Ownable { ); } } - - #[external(v0)] - impl OwnableImpl of interface::IOwnable { - fn owner(self: @ContractState) -> ContractAddress { - self.Ownable_owner.read() - } - - fn transfer_ownership(ref self: ContractState, new_owner: ContractAddress) { - assert(!new_owner.is_zero(), Errors::ZERO_ADDRESS_OWNER); - self.assert_only_owner(); - self._transfer_ownership(new_owner); - } - - fn renounce_ownership(ref self: ContractState) { - self.assert_only_owner(); - self._transfer_ownership(Zeroable::zero()); - } - } - - #[external(v0)] - impl OwnableCamelOnlyImpl of interface::IOwnableCamelOnly { - fn transferOwnership(ref self: ContractState, newOwner: ContractAddress) { - OwnableImpl::transfer_ownership(ref self, newOwner); - } - - fn renounceOwnership(ref self: ContractState) { - OwnableImpl::renounce_ownership(ref self); - } - } } diff --git a/src/tests/access/test_dual_ownable.cairo b/src/tests/access/test_dual_ownable.cairo index 40f736bf5..1338c32b5 100644 --- a/src/tests/access/test_dual_ownable.cairo +++ b/src/tests/access/test_dual_ownable.cairo @@ -1,18 +1,16 @@ use openzeppelin::access::ownable::dual_ownable::DualCaseOwnable; use openzeppelin::access::ownable::dual_ownable::DualCaseOwnableTrait; use openzeppelin::access::ownable::interface::IOwnableCamelOnlyDispatcher; -use openzeppelin::access::ownable::interface::IOwnableCamelOnlyDispatcherTrait; use openzeppelin::access::ownable::interface::IOwnableDispatcher; use openzeppelin::access::ownable::interface::IOwnableDispatcherTrait; -use openzeppelin::tests::mocks::dual_ownable_mocks::CamelOwnableMock; -use openzeppelin::tests::mocks::dual_ownable_mocks::CamelOwnablePanicMock; -use openzeppelin::tests::mocks::dual_ownable_mocks::SnakeOwnableMock; -use openzeppelin::tests::mocks::dual_ownable_mocks::SnakeOwnablePanicMock; use openzeppelin::tests::mocks::non_implementing_mock::NonImplementingMock; +use openzeppelin::tests::mocks::ownable_mocks::CamelOwnableMock; +use openzeppelin::tests::mocks::ownable_mocks::CamelOwnablePanicMock; +use openzeppelin::tests::mocks::ownable_mocks::SnakeOwnableMock; +use openzeppelin::tests::mocks::ownable_mocks::SnakeOwnablePanicMock; use openzeppelin::tests::utils::constants::{OWNER, NEW_OWNER}; use openzeppelin::tests::utils; use openzeppelin::utils::serde::SerializedAppend; -use starknet::testing::set_caller_address; use starknet::testing::set_contract_address; // @@ -20,14 +18,14 @@ use starknet::testing::set_contract_address; // fn setup_snake() -> (DualCaseOwnable, IOwnableDispatcher) { - let mut calldata = ArrayTrait::new(); + let mut calldata = array![]; calldata.append_serde(OWNER()); let target = utils::deploy(SnakeOwnableMock::TEST_CLASS_HASH, calldata); (DualCaseOwnable { contract_address: target }, IOwnableDispatcher { contract_address: target }) } fn setup_camel() -> (DualCaseOwnable, IOwnableCamelOnlyDispatcher) { - let mut calldata = ArrayTrait::new(); + let mut calldata = array![]; calldata.append_serde(OWNER()); let target = utils::deploy(CamelOwnableMock::TEST_CLASS_HASH, calldata); ( @@ -37,14 +35,14 @@ fn setup_camel() -> (DualCaseOwnable, IOwnableCamelOnlyDispatcher) { } fn setup_non_ownable() -> DualCaseOwnable { - let calldata = ArrayTrait::new(); + let calldata = array![]; let target = utils::deploy(NonImplementingMock::TEST_CLASS_HASH, calldata); DualCaseOwnable { contract_address: target } } fn setup_ownable_panic() -> (DualCaseOwnable, DualCaseOwnable) { - let snake_target = utils::deploy(SnakeOwnablePanicMock::TEST_CLASS_HASH, ArrayTrait::new()); - let camel_target = utils::deploy(CamelOwnablePanicMock::TEST_CLASS_HASH, ArrayTrait::new()); + let snake_target = utils::deploy(SnakeOwnablePanicMock::TEST_CLASS_HASH, array![]); + let camel_target = utils::deploy(CamelOwnablePanicMock::TEST_CLASS_HASH, array![]); ( DualCaseOwnable { contract_address: snake_target }, DualCaseOwnable { contract_address: camel_target } @@ -118,7 +116,6 @@ fn test_dual_renounce_ownership() { assert(target.owner().is_zero(), 'Should be zero'); } - #[test] #[available_gas(2000000)] #[should_panic(expected: ('ENTRYPOINT_NOT_FOUND',))] diff --git a/src/tests/access/test_ownable.cairo b/src/tests/access/test_ownable.cairo index 292bda3de..9a76e35f5 100644 --- a/src/tests/access/test_ownable.cairo +++ b/src/tests/access/test_ownable.cairo @@ -1,26 +1,24 @@ -use openzeppelin::access::ownable::Ownable::InternalImpl; -use openzeppelin::access::ownable::Ownable::OwnableCamelOnlyImpl; -use openzeppelin::access::ownable::Ownable::OwnableImpl; -use openzeppelin::access::ownable::Ownable::Ownable_owner::InternalContractMemberStateTrait; +use openzeppelin::access::ownable::Ownable::InternalTrait; use openzeppelin::access::ownable::Ownable::OwnershipTransferred; -use openzeppelin::access::ownable::Ownable; +use openzeppelin::access::ownable::interface::{IOwnable, IOwnableCamelOnly}; +use openzeppelin::tests::mocks::ownable_mocks::DualCaseOwnableMock; use openzeppelin::tests::utils::constants::{ZERO, OTHER, OWNER}; use openzeppelin::tests::utils; use starknet::ContractAddress; -use starknet::contract_address_const; +use starknet::storage::StorageMemberAccessTrait; use starknet::testing; // // Setup // -fn STATE() -> Ownable::ContractState { - Ownable::contract_state_for_testing() +fn STATE() -> DualCaseOwnableMock::ContractState { + DualCaseOwnableMock::contract_state_for_testing() } -fn setup() -> Ownable::ContractState { +fn setup() -> DualCaseOwnableMock::ContractState { let mut state = STATE(); - InternalImpl::initializer(ref state, OWNER()); + state.ownable.initializer(OWNER()); utils::drop_event(ZERO()); state } @@ -31,14 +29,14 @@ fn setup() -> Ownable::ContractState { #[test] #[available_gas(2000000)] -fn test_initializer() { +fn test_initializer_owner() { let mut state = STATE(); - assert(state.Ownable_owner.read().is_zero(), 'Should be zero'); - InternalImpl::initializer(ref state, OWNER()); + assert(state.ownable.Ownable_owner.read().is_zero(), 'Should be zero'); + state.ownable.initializer(OWNER()); assert_event_ownership_transferred(ZERO(), OWNER()); - assert(state.Ownable_owner.read() == OWNER(), 'Owner should be set'); + assert(state.ownable.Ownable_owner.read() == OWNER(), 'Owner should be set'); } // @@ -50,7 +48,7 @@ fn test_initializer() { fn test_assert_only_owner() { let state = setup(); testing::set_caller_address(OWNER()); - InternalImpl::assert_only_owner(@state); + state.ownable.assert_only_owner(); } #[test] @@ -59,7 +57,7 @@ fn test_assert_only_owner() { fn test_assert_only_owner_when_not_owner() { let state = setup(); testing::set_caller_address(OTHER()); - InternalImpl::assert_only_owner(@state); + state.ownable.assert_only_owner(); } #[test] @@ -67,7 +65,7 @@ fn test_assert_only_owner_when_not_owner() { #[should_panic(expected: ('Caller is the zero address',))] fn test_assert_only_owner_when_caller_zero() { let state = setup(); - InternalImpl::assert_only_owner(@state); + state.ownable.assert_only_owner(); } // @@ -78,11 +76,11 @@ fn test_assert_only_owner_when_caller_zero() { #[available_gas(2000000)] fn test__transfer_ownership() { let mut state = setup(); - InternalImpl::_transfer_ownership(ref state, OTHER()); + state.ownable._transfer_ownership(OTHER()); assert_event_ownership_transferred(OWNER(), OTHER()); - assert(state.Ownable_owner.read() == OTHER(), 'Owner should be OTHER'); + assert(state.ownable.Ownable_owner.read() == OTHER(), 'Owner should be OTHER'); } // @@ -94,11 +92,11 @@ fn test__transfer_ownership() { fn test_transfer_ownership() { let mut state = setup(); testing::set_caller_address(OWNER()); - OwnableImpl::transfer_ownership(ref state, OTHER()); + state.ownable.transfer_ownership(OTHER()); assert_event_ownership_transferred(OWNER(), OTHER()); - assert(OwnableImpl::owner(@state) == OTHER(), 'Should transfer ownership'); + assert(state.ownable.owner() == OTHER(), 'Should transfer ownership'); } #[test] @@ -107,7 +105,7 @@ fn test_transfer_ownership() { fn test_transfer_ownership_to_zero() { let mut state = setup(); testing::set_caller_address(OWNER()); - OwnableImpl::transfer_ownership(ref state, ZERO()); + state.ownable.transfer_ownership(ZERO()); } #[test] @@ -115,7 +113,7 @@ fn test_transfer_ownership_to_zero() { #[should_panic(expected: ('Caller is the zero address',))] fn test_transfer_ownership_from_zero() { let mut state = setup(); - OwnableImpl::transfer_ownership(ref state, OTHER()); + state.ownable.transfer_ownership(OTHER()); } #[test] @@ -124,7 +122,7 @@ fn test_transfer_ownership_from_zero() { fn test_transfer_ownership_from_nonowner() { let mut state = setup(); testing::set_caller_address(OTHER()); - OwnableImpl::transfer_ownership(ref state, OTHER()); + state.ownable.transfer_ownership(OTHER()); } #[test] @@ -132,11 +130,11 @@ fn test_transfer_ownership_from_nonowner() { fn test_transferOwnership() { let mut state = setup(); testing::set_caller_address(OWNER()); - OwnableCamelOnlyImpl::transferOwnership(ref state, OTHER()); + state.ownable.transferOwnership(OTHER()); assert_event_ownership_transferred(OWNER(), OTHER()); - assert(OwnableImpl::owner(@state) == OTHER(), 'Should transfer ownership'); + assert(state.ownable.owner() == OTHER(), 'Should transfer ownership'); } #[test] @@ -145,7 +143,7 @@ fn test_transferOwnership() { fn test_transferOwnership_to_zero() { let mut state = setup(); testing::set_caller_address(OWNER()); - OwnableCamelOnlyImpl::transferOwnership(ref state, ZERO()); + state.ownable.transferOwnership(ZERO()); } #[test] @@ -153,7 +151,7 @@ fn test_transferOwnership_to_zero() { #[should_panic(expected: ('Caller is the zero address',))] fn test_transferOwnership_from_zero() { let mut state = setup(); - OwnableCamelOnlyImpl::transferOwnership(ref state, OTHER()); + state.ownable.transferOwnership(OTHER()); } #[test] @@ -162,7 +160,7 @@ fn test_transferOwnership_from_zero() { fn test_transferOwnership_from_nonowner() { let mut state = setup(); testing::set_caller_address(OTHER()); - OwnableCamelOnlyImpl::transferOwnership(ref state, OTHER()); + state.ownable.transferOwnership(OTHER()); } // @@ -174,11 +172,11 @@ fn test_transferOwnership_from_nonowner() { fn test_renounce_ownership() { let mut state = setup(); testing::set_caller_address(OWNER()); - OwnableImpl::renounce_ownership(ref state); + state.ownable.renounce_ownership(); assert_event_ownership_transferred(OWNER(), ZERO()); - assert(OwnableImpl::owner(@state) == ZERO(), 'Should renounce ownership'); + assert(state.ownable.owner() == ZERO(), 'Should renounce ownership'); } #[test] @@ -186,7 +184,7 @@ fn test_renounce_ownership() { #[should_panic(expected: ('Caller is the zero address',))] fn test_renounce_ownership_from_zero_address() { let mut state = setup(); - OwnableImpl::renounce_ownership(ref state); + state.ownable.renounce_ownership(); } #[test] @@ -195,7 +193,7 @@ fn test_renounce_ownership_from_zero_address() { fn test_renounce_ownership_from_nonowner() { let mut state = setup(); testing::set_caller_address(OTHER()); - OwnableImpl::renounce_ownership(ref state); + state.ownable.renounce_ownership(); } #[test] @@ -203,11 +201,11 @@ fn test_renounce_ownership_from_nonowner() { fn test_renounceOwnership() { let mut state = setup(); testing::set_caller_address(OWNER()); - OwnableCamelOnlyImpl::renounceOwnership(ref state); + state.ownable.renounceOwnership(); assert_event_ownership_transferred(OWNER(), ZERO()); - assert(OwnableImpl::owner(@state) == ZERO(), 'Should renounce ownership'); + assert(state.ownable.owner() == ZERO(), 'Should renounce ownership'); } #[test] @@ -215,7 +213,7 @@ fn test_renounceOwnership() { #[should_panic(expected: ('Caller is the zero address',))] fn test_renounceOwnership_from_zero_address() { let mut state = setup(); - OwnableCamelOnlyImpl::renounceOwnership(ref state); + state.ownable.renounceOwnership(); } #[test] @@ -224,7 +222,7 @@ fn test_renounceOwnership_from_zero_address() { fn test_renounceOwnership_from_nonowner() { let mut state = setup(); testing::set_caller_address(OTHER()); - OwnableCamelOnlyImpl::renounceOwnership(ref state); + state.ownable.renounceOwnership(); } // diff --git a/src/tests/mocks.cairo b/src/tests/mocks.cairo index 11a30de47..b5ce9b524 100644 --- a/src/tests/mocks.cairo +++ b/src/tests/mocks.cairo @@ -5,11 +5,11 @@ mod camel721_mock; mod camel_accesscontrol_mock; mod camel_account_mock; mod dual721_receiver_mocks; -mod dual_ownable_mocks; mod erc20_panic; mod erc721_panic_mock; mod erc721_receiver; mod non_implementing_mock; +mod ownable_mocks; mod reentrancy_attacker_mock; mod reentrancy_mock; mod snake20_mock; diff --git a/src/tests/mocks/dual_ownable_mocks.cairo b/src/tests/mocks/dual_ownable_mocks.cairo deleted file mode 100644 index 7826e859e..000000000 --- a/src/tests/mocks/dual_ownable_mocks.cairo +++ /dev/null @@ -1,116 +0,0 @@ -use openzeppelin::access::ownable::Ownable; - -#[starknet::contract] -mod SnakeOwnableMock { - use starknet::ContractAddress; - use super::Ownable; - - #[storage] - struct Storage {} - - #[constructor] - fn constructor(self: @ContractState, owner: ContractAddress) { - let mut unsafe_state = Ownable::unsafe_new_contract_state(); - Ownable::InternalImpl::initializer(ref unsafe_state, owner); - } - - #[external(v0)] - fn owner(self: @ContractState) -> ContractAddress { - let unsafe_state = Ownable::unsafe_new_contract_state(); - Ownable::OwnableImpl::owner(@unsafe_state) - } - - #[external(v0)] - fn transfer_ownership(ref self: ContractState, new_owner: ContractAddress) { - let mut unsafe_state = Ownable::unsafe_new_contract_state(); - Ownable::OwnableImpl::transfer_ownership(ref unsafe_state, new_owner); - } - - #[external(v0)] - fn renounce_ownership(ref self: ContractState) { - let mut unsafe_state = Ownable::unsafe_new_contract_state(); - Ownable::OwnableImpl::renounce_ownership(ref unsafe_state); - } -} - -#[starknet::contract] -mod CamelOwnableMock { - use starknet::ContractAddress; - use super::Ownable; - - #[storage] - struct Storage {} - - #[constructor] - fn constructor(self: @ContractState, owner: ContractAddress) { - let mut unsafe_state = Ownable::unsafe_new_contract_state(); - Ownable::InternalImpl::initializer(ref unsafe_state, owner); - } - - #[external(v0)] - fn owner(self: @ContractState) -> ContractAddress { - let unsafe_state = Ownable::unsafe_new_contract_state(); - Ownable::OwnableImpl::owner(@unsafe_state) - } - - #[external(v0)] - fn transferOwnership(ref self: ContractState, newOwner: ContractAddress) { - let mut unsafe_state = Ownable::unsafe_new_contract_state(); - Ownable::OwnableCamelOnlyImpl::transferOwnership(ref unsafe_state, newOwner); - } - - #[external(v0)] - fn renounceOwnership(ref self: ContractState) { - let mut unsafe_state = Ownable::unsafe_new_contract_state(); - Ownable::OwnableCamelOnlyImpl::renounceOwnership(ref unsafe_state); - } -} - -#[starknet::contract] -mod SnakeOwnablePanicMock { - use starknet::ContractAddress; - use zeroable::Zeroable; - - #[storage] - struct Storage {} - - #[external(v0)] - fn owner(self: @ContractState) -> ContractAddress { - panic_with_felt252('Some error'); - Zeroable::zero() - } - - #[external(v0)] - fn transfer_ownership(ref self: ContractState, new_owner: ContractAddress) { - panic_with_felt252('Some error'); - } - - #[external(v0)] - fn renounce_ownership(ref self: ContractState) { - panic_with_felt252('Some error'); - } -} - -#[starknet::contract] -mod CamelOwnablePanicMock { - use starknet::ContractAddress; - - #[storage] - struct Storage {} - - #[external(v0)] - fn owner(self: @ContractState) -> ContractAddress { - panic_with_felt252('Some error'); - Zeroable::zero() - } - - #[external(v0)] - fn transferOwnership(ref self: ContractState, newOwner: ContractAddress) { - panic_with_felt252('Some error'); - } - - #[external(v0)] - fn renounceOwnership(ref self: ContractState) { - panic_with_felt252('Some error'); - } -} diff --git a/src/tests/mocks/ownable_mocks.cairo b/src/tests/mocks/ownable_mocks.cairo new file mode 100644 index 000000000..81433d063 --- /dev/null +++ b/src/tests/mocks/ownable_mocks.cairo @@ -0,0 +1,144 @@ +#[starknet::contract] +mod DualCaseOwnableMock { + use openzeppelin::access::ownable::Ownable as ownable_component; + use starknet::ContractAddress; + + component!(path: ownable_component, storage: ownable, event: OwnableEvent); + + #[abi(embed_v0)] + impl OwnableImpl = ownable_component::OwnableImpl; + #[abi(embed_v0)] + impl OwnableCamelOnlyImpl = + ownable_component::OwnableCamelOnlyImpl; + impl InternalImpl = ownable_component::InternalImpl; + + #[storage] + struct Storage { + #[substorage(v0)] + ownable: ownable_component::Storage + } + + #[event] + #[derive(Drop, starknet::Event)] + enum Event { + OwnableEvent: ownable_component::Event + } + + #[constructor] + fn constructor(ref self: ContractState, owner: ContractAddress) { + self.ownable.initializer(owner); + } +} + +#[starknet::contract] +mod SnakeOwnableMock { + use openzeppelin::access::ownable::Ownable as ownable_component; + use starknet::ContractAddress; + + component!(path: ownable_component, storage: ownable, event: OwnableEvent); + + #[abi(embed_v0)] + impl OwnableImpl = ownable_component::OwnableImpl; + impl InternalImpl = ownable_component::InternalImpl; + + #[storage] + struct Storage { + #[substorage(v0)] + ownable: ownable_component::Storage + } + + #[event] + #[derive(Drop, starknet::Event)] + enum Event { + OwnableEvent: ownable_component::Event + } + + #[constructor] + fn constructor(ref self: ContractState, owner: ContractAddress) { + self.ownable.initializer(owner); + } +} + +#[starknet::contract] +mod CamelOwnableMock { + use openzeppelin::access::ownable::Ownable as ownable_component; + use starknet::ContractAddress; + + component!(path: ownable_component, storage: ownable, event: OwnableEvent); + + #[abi(embed_v0)] + impl OwnableCamelOnlyImpl = + ownable_component::OwnableCamelOnlyImpl; + impl InternalImpl = ownable_component::InternalImpl; + + #[storage] + struct Storage { + #[substorage(v0)] + ownable: ownable_component::Storage + } + + #[event] + #[derive(Drop, starknet::Event)] + enum Event { + OwnableEvent: ownable_component::Event + } + + #[constructor] + fn constructor(ref self: ContractState, owner: ContractAddress) { + self.ownable.initializer(owner); + } + + #[external(v0)] + fn owner(self: @ContractState) -> ContractAddress { + self.ownable.Ownable_owner.read() + } +} + +#[starknet::contract] +mod SnakeOwnablePanicMock { + use starknet::ContractAddress; + use zeroable::Zeroable; + + #[storage] + struct Storage {} + + #[external(v0)] + fn owner(self: @ContractState) -> ContractAddress { + panic_with_felt252('Some error'); + Zeroable::zero() + } + + #[external(v0)] + fn transfer_ownership(ref self: ContractState, new_owner: ContractAddress) { + panic_with_felt252('Some error'); + } + + #[external(v0)] + fn renounce_ownership(ref self: ContractState) { + panic_with_felt252('Some error'); + } +} + +#[starknet::contract] +mod CamelOwnablePanicMock { + use starknet::ContractAddress; + + #[storage] + struct Storage {} + + #[external(v0)] + fn owner(self: @ContractState) -> ContractAddress { + panic_with_felt252('Some error'); + Zeroable::zero() + } + + #[external(v0)] + fn transferOwnership(ref self: ContractState, newOwner: ContractAddress) { + panic_with_felt252('Some error'); + } + + #[external(v0)] + fn renounceOwnership(ref self: ContractState) { + panic_with_felt252('Some error'); + } +}