Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Migrate AccessControl to component #770

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
142 commits
Select commit Hold shift + click to select a range
331844d
fix: link (#545)
ericnordelo Jan 20, 2023
b9ec20b
add submodule
andrew-fleming Feb 18, 2023
bb40124
update cairo
andrew-fleming Feb 18, 2023
a0c3157
add Cargo and Makefile
andrew-fleming Feb 18, 2023
3a6e20d
update cairo
andrew-fleming Feb 18, 2023
96431de
add deps in cairo_project, create lib
andrew-fleming Feb 18, 2023
7b38706
add presets
andrew-fleming Feb 18, 2023
2fa00dd
add base lib
andrew-fleming Feb 18, 2023
f0036e7
add tests
andrew-fleming Feb 18, 2023
8d781a8
remove old cairo lib and interface
andrew-fleming Feb 18, 2023
ae37b1c
remove unused import
andrew-fleming Feb 18, 2023
da57a91
fix vars
andrew-fleming Feb 18, 2023
fd42fdd
change external funcs to snake case
andrew-fleming Feb 18, 2023
1cb1252
remove unused import
andrew-fleming Feb 18, 2023
f0330cd
update cairo
andrew-fleming Feb 21, 2023
abc737a
add tests for externals
andrew-fleming Feb 21, 2023
4ca2620
add bool assertions
andrew-fleming Feb 21, 2023
8dd8ed3
update cairo
andrew-fleming Feb 22, 2023
091a29c
remove preset mods
andrew-fleming Feb 22, 2023
1c866f2
add IERC20 trait
andrew-fleming Feb 22, 2023
3405063
update cairo
andrew-fleming Feb 22, 2023
8fe3c82
clean up test
andrew-fleming Feb 22, 2023
ad928bc
remove assertion
andrew-fleming Feb 23, 2023
d883eb5
update cairo
andrew-fleming Feb 23, 2023
543501b
simplify max_u256
andrew-fleming Feb 23, 2023
2ecba91
clarify error msg
andrew-fleming Feb 23, 2023
6c3978b
add erc165 + tests
martriay Mar 3, 2023
60365c8
kickstart account module
martriay Mar 3, 2023
be1e9df
clean up python project. ready for rust/cairo1
martriay Mar 7, 2023
c20fd02
rebase to cairo-1
martriay Mar 7, 2023
bce2506
re-structure project
martriay Mar 7, 2023
35ede09
re-structure project
martriay Mar 8, 2023
e55889a
re-structure project
martriay Mar 8, 2023
9d5079c
update makefile
martriay Mar 8, 2023
ae89fa5
bump submodule
martriay Mar 8, 2023
a54e98c
Update erc20 migration branch (#586)
andrew-fleming Mar 28, 2023
91dcad3
Migrate security/initializable (#592)
andrew-fleming Mar 31, 2023
f48da2c
Use zeroable::zero() instead of contract_address_const::<0>() (#598)
Amxx Mar 31, 2023
d671d4c
Migrate security/pausable (#593)
andrew-fleming Mar 31, 2023
e86bcd2
add utils and constants
andrew-fleming Apr 18, 2023
42a160f
Revert "add utils and constants"
andrew-fleming Apr 18, 2023
598c232
Migrate ERC165 (#582)
martriay Apr 26, 2023
b2624ee
Migrate constants (#611)
andrew-fleming Apr 26, 2023
4a4bca9
Set up new CI (#599)
andrew-fleming Apr 27, 2023
0962051
fix formatting (#613)
andrew-fleming Apr 28, 2023
a1950ee
Normalize error message style (#606)
andrew-fleming Apr 28, 2023
c29b45c
Add `BoundedInt` and internal macros, update cairo (#600)
andrew-fleming Apr 28, 2023
e48a682
Migrate ownable (#604)
andrew-fleming May 11, 2023
5f86f65
Migrate security/reentrancyguard (#590)
andrew-fleming May 19, 2023
897facb
Migrate access control (#605)
andrew-fleming May 19, 2023
372de37
Migrate account (#620)
andrew-fleming May 23, 2023
d86eb73
Migrate ERC721 (#619)
martriay May 30, 2023
62688e7
Dual interface dispatcher for ERC721 (#623)
martriay Jul 2, 2023
ba69328
Replace ERC-165 with SRC-5 (Update Interface Ids) (#637)
ericnordelo Jul 2, 2023
8534ec6
Fix conflicts from the dual721 and src5 merge (#641)
andrew-fleming Jul 3, 2023
bc90bd5
Add camel support for ownable (#625)
andrew-fleming Jul 3, 2023
c8fa9f9
Add camel support for access control (#626)
andrew-fleming Jul 5, 2023
3a8e3e7
erc20 dual dispatcher (#622)
andrew-fleming Jul 5, 2023
6a1f729
Add `UnwrapAndCast` trait (#644)
andrew-fleming Jul 6, 2023
bbcb875
add test_dual721 mod (#648)
andrew-fleming Jul 8, 2023
5a27baa
Update account interface (#646)
ericnordelo Jul 10, 2023
ba29133
Add camel support for Account (#647)
ericnordelo Jul 12, 2023
b44f7dc
Add src5 dual dispatcher (#659)
andrew-fleming Jul 12, 2023
33dbd56
Add dual case erc721 receiver (#649)
andrew-fleming Jul 13, 2023
65d03ae
Add owner param to ownable initializer (#660)
ericnordelo Jul 13, 2023
a863859
Migrate SRC5 to Cairo 2 (#664)
ericnordelo Jul 18, 2023
c4590b9
Migrate initializable to cairo2 (#661)
andrew-fleming Jul 18, 2023
f18fcdb
Migrate Account to Cairo 2 (#666)
ericnordelo Jul 20, 2023
dc4a233
Migrate pausable to cairo2 (#662)
andrew-fleming Jul 20, 2023
27514b1
Migrate reentrancyguard to cairo2 (#663)
andrew-fleming Jul 20, 2023
8e10821
Migrate ERC721 to Cairo2 (#667)
ericnordelo Jul 21, 2023
a00a9d3
Migrate ownable to cairo2 (#665)
andrew-fleming Jul 21, 2023
0b40485
Migrate ERC20 to Cairo 2 (#669)
ericnordelo Jul 22, 2023
4e64434
Migrate access control to cairo2 (#668)
andrew-fleming Jul 22, 2023
79fe2db
Migrate to Scarb for cairo-2 (#671)
maciektr Aug 4, 2023
177ad63
Add spdx (#684)
andrew-fleming Aug 4, 2023
495ed8a
feat: sort imports (#679)
ericnordelo Aug 4, 2023
c28c758
Migrate upgrades (#603)
andrew-fleming Aug 10, 2023
f9c30da
Add testing for Ownable events (#675)
ericnordelo Aug 11, 2023
457b98b
Add account events (#687)
andrew-fleming Aug 11, 2023
544ddac
Add testing for AccessControl events (#674)
ericnordelo Aug 15, 2023
4473243
Add testing for Pausable events (#676)
ericnordelo Aug 15, 2023
e95def8
Add testing for ERC20 events (#677)
ericnordelo Aug 15, 2023
77089c1
Add mint to erc721 preset constructor (#700)
andrew-fleming Aug 15, 2023
3e08fa6
Add testing for ERC721 events (#678)
ericnordelo Aug 16, 2023
8307644
Update README and fix scarb package (#688)
martriay Aug 18, 2023
cece32b
Bump scarb and fix dual721 test context (#703)
andrew-fleming Aug 23, 2023
32f9974
add rc version (#708)
martriay Aug 23, 2023
b40c35b
remove unused imports (#707)
andrew-fleming Aug 23, 2023
fbdd759
Upgrade Cairo version and Scarb version to latest releases (#712)
bal7hazar Aug 24, 2023
05429e4
bump antora (#715)
martriay Aug 28, 2023
adac09f
Add selector inline macro/fix `tokenURI` (#724)
andrew-fleming Sep 8, 2023
90be39f
fix: naming convention (#732)
ericnordelo Sep 14, 2023
fcdd6d1
feat: add Errors modules (#691)
ericnordelo Sep 14, 2023
7373daa
Update account docs (#709)
ericnordelo Sep 15, 2023
cbb5bf5
Add Interface & Dispatchers docs (#730)
martriay Sep 20, 2023
4e388f5
Update overview docs (#735)
ericnordelo Sep 20, 2023
a1b559c
remove underscore from name_ and symbol_ (#738)
andrew-fleming Sep 21, 2023
74eb4e8
Add prefixes to storage members (#743)
ericnordelo Sep 26, 2023
420f320
Sanitizing for release. (#736)
ericnordelo Sep 27, 2023
4f973b6
feat: migrate logic
ericnordelo Sep 27, 2023
2f4b1f0
feat: update workflow
ericnordelo Sep 27, 2023
715e473
feat: update logic
ericnordelo Sep 28, 2023
61fec1d
Merge branch 'feat/migrate-src5-to-component' into feat/migrate-acces…
ericnordelo Sep 28, 2023
f1241b5
Merge branch 'main' of github.com:OpenZeppelin/cairo-contracts into f…
ericnordelo Oct 2, 2023
b425e2c
Merge branch 'main' of github.com:OpenZeppelin/cairo-contracts into f…
ericnordelo Oct 2, 2023
3ac8eb8
Merge branch 'feat/migrate-src5-to-component' into feat/migrate-acces…
ericnordelo Oct 2, 2023
e139553
Merge branch 'feat/migrate-ownable-to-components' into feat/migrate-a…
ericnordelo Oct 2, 2023
354a2cd
fix: docs
ericnordelo Oct 2, 2023
ea0a956
feat: add main logic with dependencies
ericnordelo Oct 2, 2023
9a274be
feat: apply review updates
ericnordelo Oct 5, 2023
4d116ad
Merge branch 'main' of github.com:OpenZeppelin/cairo-contracts into f…
ericnordelo Oct 5, 2023
f0d8c6f
feat: remove unnecesary imports
ericnordelo Oct 5, 2023
cb5b828
feat: add in-code comments
ericnordelo Oct 5, 2023
09c47ad
feat: apply review updates
ericnordelo Oct 6, 2023
cce8fc9
Merge branch 'feat/migrate-src5-to-component' into feat/migrate-acces…
ericnordelo Oct 6, 2023
f090a83
refactor: remove unnecessary trait
ericnordelo Oct 6, 2023
95706b1
feat: apply update reviews
ericnordelo Oct 6, 2023
d2c538b
Merge branch 'main' of github.com:OpenZeppelin/cairo-contracts into f…
ericnordelo Oct 6, 2023
fb330b1
Merge branch 'feat/migrate-ownable-to-components' into feat/migrate-a…
ericnordelo Oct 6, 2023
83c1203
refactor: remove imports
ericnordelo Oct 6, 2023
e4d332e
Merge branch 'feat/migrate-ownable-to-components' into feat/migrate-a…
ericnordelo Oct 6, 2023
2ae8139
Update src/access/ownable/ownable.cairo
ericnordelo Oct 6, 2023
fb514e1
feat: apply review updates
ericnordelo Oct 6, 2023
25fcab8
Merge branch 'feat/migrate-ownable-to-components' of github.com:ericn…
ericnordelo Oct 6, 2023
54396a6
feat: add comments to camelCase functions
ericnordelo Oct 6, 2023
3fc22bc
fix: comment
ericnordelo Oct 9, 2023
4f24b82
Update docs/modules/ROOT/pages/api/access.adoc
ericnordelo Oct 10, 2023
e112ba1
feat: apply review updates
ericnordelo Oct 11, 2023
95f2f76
Merge branch 'main' of github.com:OpenZeppelin/cairo-contracts into f…
ericnordelo Oct 11, 2023
9d8b0aa
Merge branch 'feat/migrate-ownable-to-components' of github.com:ericn…
ericnordelo Oct 11, 2023
d13b688
feat: update docs
ericnordelo Oct 11, 2023
ccaa90e
Merge branch 'feat/migrate-ownable-to-components' into feat/migrate-a…
ericnordelo Oct 11, 2023
7ec2ce8
docs: add in-code documentation
ericnordelo Oct 11, 2023
c823416
feat: update comments
ericnordelo Oct 11, 2023
dd8d3f4
Merge branch 'feat/migrate-ownable-to-components' into feat/migrate-a…
ericnordelo Oct 11, 2023
34dea75
feat: apply review
ericnordelo Oct 12, 2023
91ee940
Merge branch 'feat/migrate-ownable-to-components' into feat/migrate-a…
ericnordelo Oct 12, 2023
1693147
docs: update access page
ericnordelo Oct 12, 2023
83c7c2a
docs: update api
ericnordelo Oct 12, 2023
fa71208
Merge branch 'main' of github.com:OpenZeppelin/cairo-contracts into f…
ericnordelo Oct 12, 2023
c8b49f1
feat: apply review updates
ericnordelo Oct 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 93 additions & 74 deletions docs/modules/ROOT/pages/access.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,40 @@ and sets a 'minter' role:

[,javascript]
----
const MINTER_ROLE: felt252 = selector!('MINTER_ROLE');
const MINTER_ROLE: felt252 = selector!("MINTER_ROLE");

#[starknet::contract]
mod MyContract {
use openzeppelin::access::accesscontrol::AccessControl::InternalImpl::assert_only_role;
use openzeppelin::access::accesscontrol::AccessControl;
use openzeppelin::access::accesscontrol::AccessControl as accesscontrol_component;
use openzeppelin::introspection::src5::SRC5 as src5_component;
use openzeppelin::token::erc20::ERC20;
use starknet::ContractAddress;
use super::MINTER_ROLE;

component!(path: accesscontrol_component, storage: accesscontrol, event: AccessControlEvent);
component!(path: src5_component, storage: src5, event: SRC5Event);

#[abi(embed_v0)]
impl AccessControlImpl =
accesscontrol_component::AccessControlImpl<ContractState>;
#[abi(embed_v0)]
impl SRC5Impl = src5_component::SRC5Impl<ContractState>;
impl AccessControlInternalImpl = accesscontrol_component::InternalImpl<ContractState>;

#[storage]
struct Storage {}
struct Storage {
#[substorage(v0)]
accesscontrol: accesscontrol_component::Storage,
#[substorage(v0)]
src5: src5_component::Storage,
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
AccessControlEvent: accesscontrol_component::Event,
SRC5Event: src5_component::Event
}

#[constructor]
fn constructor(
Expand All @@ -160,22 +182,16 @@ mod MyContract {
ERC20::InternalImpl::_mint(ref erc20_state, recipient, initial_supply);

// AccessControl related initialization
let mut access_state = AccessControl::unsafe_new_contract_state();
AccessControl::InternalImpl::initializer(ref access_state);
AccessControl::InternalImpl::_grant_role(
ref access_state,
MINTER_ROLE,
minter
);
self.accesscontrol.initializer();
self.accesscontrol._grant_role(MINTER_ROLE, minter);
}

// This function can only be called by a minter
/// This function can only be called by a minter.
#[external(v0)]
fn mint(ref self: ContractState, recipient: ContractAddress, amount: u256) {
let access_state = AccessControl::unsafe_new_contract_state();
assert_only_role(@access_state, MINTER_ROLE);
self.accesscontrol.assert_only_role(MINTER_ROLE);

let mut erc20_state = AccessControl::unsafe_new_contract_state();
let mut erc20_state = ERC20::unsafe_new_contract_state();
ERC20::InternalImpl::_mint(ref erc20_state, recipient, amount);
}
}
Expand All @@ -192,19 +208,41 @@ Let's augment our ERC20 token example by also defining a 'burner' role, which le

[,javascript]
----
const MINTER_ROLE: felt252 = selector!('MINTER_ROLE');
const BURNER_ROLE: felt252 = selector!('BURNER_ROLE');
const MINTER_ROLE: felt252 = selector!("MINTER_ROLE");
const BURNER_ROLE: felt252 = selector!("BURNER_ROLE");

#[starknet::contract]
mod MyContract {
use openzeppelin::access::accesscontrol::AccessControl::InternalImpl::assert_only_role;
use openzeppelin::access::accesscontrol::AccessControl;
use openzeppelin::access::accesscontrol::AccessControl as accesscontrol_component;
use openzeppelin::introspection::src5::SRC5 as src5_component;
use openzeppelin::token::erc20::ERC20;
use starknet::ContractAddress;
use super::{MINTER_ROLE, BURNER_ROLE};

component!(path: accesscontrol_component, storage: accesscontrol, event: AccessControlEvent);
component!(path: src5_component, storage: src5, event: SRC5Event);

#[abi(embed_v0)]
impl AccessControlImpl =
accesscontrol_component::AccessControlImpl<ContractState>;
#[abi(embed_v0)]
impl SRC5Impl = src5_component::SRC5Impl<ContractState>;
impl AccessControlInternalImpl = accesscontrol_component::InternalImpl<ContractState>;

#[storage]
struct Storage {}
struct Storage {
#[substorage(v0)]
accesscontrol: accesscontrol_component::Storage,
#[substorage(v0)]
src5: src5_component::Storage,
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
AccessControlEvent: accesscontrol_component::Event,
SRC5Event: src5_component::Event
}

#[constructor]
fn constructor(
Expand All @@ -222,38 +260,26 @@ mod MyContract {
ERC20::InternalImpl::_mint(ref erc20_state, recipient, initial_supply);

// AccessControl related initialization
let mut access_state = AccessControl::unsafe_new_contract_state();
AccessControl::InternalImpl::initializer(ref access_state);
AccessControl::InternalImpl::_grant_role(
ref access_state,
MINTER_ROLE,
minter
);
AccessControl::InternalImpl::_grant_role(
ref access_state,
BURNER_ROLE,
burner
);
self.accesscontrol.initializer();
self.accesscontrol._grant_role(MINTER_ROLE, minter);
self.accesscontrol._grant_role(BURNER_ROLE, burner);
}


// This function can only be called by a minter
/// This function can only be called by a minter.
#[external(v0)]
fn mint(ref self: ContractState, recipient: ContractAddress, amount: u256) {
let access_state = AccessControl::unsafe_new_contract_state();
assert_only_role(@access_state, MINTER_ROLE);
self.accesscontrol.assert_only_role(MINTER_ROLE);

let mut erc20_state = AccessControl::unsafe_new_contract_state();
let mut erc20_state = ERC20::unsafe_new_contract_state();
ERC20::InternalImpl::_mint(ref erc20_state, recipient, amount);
}

// This function can only be called by a burner
/// This function can only be called by a burner.
#[external(v0)]
fn burn(ref self: ContractState, account: ContractAddress, amount: u256) {
let access_state = AccessControl::unsafe_new_contract_state();
assert_only_role(@access_state, BURNER_ROLE);
self.accesscontrol.assert_only_role(BURNER_ROLE);

let mut erc20_state = AccessControl::unsafe_new_contract_state();
let mut erc20_state = ERC20::unsafe_new_contract_state();
ERC20::InternalImpl::_burn(ref erc20_state, account, amount);
}
}
Expand Down Expand Up @@ -292,20 +318,29 @@ Let's take a look at the ERC20 token example, this time taking advantage of the

[,javascript]
----
const MINTER_ROLE: felt252 = selector!('MINTER_ROLE');
const BURNER_ROLE: felt252 = selector!('BURNER_ROLE');
const MINTER_ROLE: felt252 = selector!("MINTER_ROLE");
const BURNER_ROLE: felt252 = selector!("BURNER_ROLE");

#[starknet::contract]
mod MyContract {
use openzeppelin::access::accesscontrol::AccessControl::InternalImpl::assert_only_role;
use openzeppelin::access::accesscontrol::AccessControl;
use openzeppelin::access::accesscontrol::AccessControl as accesscontrol_component;
use openzeppelin::access::accesscontrol::DEFAULT_ADMIN_ROLE;
use openzeppelin::introspection::src5::SRC5 as src5_component;
use openzeppelin::token::erc20::ERC20;
use starknet::ContractAddress;
use super::{MINTER_ROLE, BURNER_ROLE};

#[storage]
struct Storage {}
component!(path: accesscontrol_component, storage: accesscontrol, event: AccessControlEvent);
component!(path: src5_component, storage: src5, event: SRC5Event);

#[abi(embed_v0)]
impl AccessControlImpl =
accesscontrol_component::AccessControlImpl<ContractState>;
#[abi(embed_v0)]
impl SRC5Impl = src5_component::SRC5Impl<ContractState>;
impl AccessControlInternalImpl = accesscontrol_component::InternalImpl<ContractState>;

(...)

#[constructor]
fn constructor(
Expand All @@ -322,49 +357,33 @@ mod MyContract {
ERC20::InternalImpl::_mint(ref erc20_state, recipient, initial_supply);

// AccessControl related initialization
let mut access_state = AccessControl::unsafe_new_contract_state();
AccessControl::InternalImpl::initializer(ref access_state);
AccessControl::InternalImpl::_grant_role(
ref access_state,
DEFAULT_ADMIN_ROLE,
admin
);
self.accesscontrol.initializer();
self.accesscontrol._grant_role(DEFAULT_ADMIN_ROLE, admin);
}

// This function can only be called by a minter
/// This function can only be called by a minter.
#[external(v0)]
fn mint(ref self: ContractState, recipient: ContractAddress, amount: u256) {
let access_state = AccessControl::unsafe_new_contract_state();
assert_only_role(@access_state, MINTER_ROLE);
self.accesscontrol.assert_only_role(MINTER_ROLE);

let mut erc20_state = AccessControl::unsafe_new_contract_state();
let mut erc20_state = ERC20::unsafe_new_contract_state();
ERC20::InternalImpl::_mint(ref erc20_state, recipient, amount);
}

// This function can only be called by a burner
/// This function can only be called by a burner.
#[external(v0)]
fn burn(ref self: ContractState, account: ContractAddress, amount: u256) {
let access_state = AccessControl::unsafe_new_contract_state();
assert_only_role(@access_state, BURNER_ROLE);
self.accesscontrol.assert_only_role(BURNER_ROLE);

let mut erc20_state = AccessControl::unsafe_new_contract_state();
let mut erc20_state = ERC20::unsafe_new_contract_state();
ERC20::InternalImpl::_burn(ref erc20_state, account, amount);
}

// These function can only be called by the roles' admin
#[external(v0)]
fn grant_role(ref self: ContractState, role: felt252, account: ContractAddress) {
let mut unsafe_state = AccessControl::unsafe_new_contract_state();
AccessControl::AccessControlImpl::grant_role(ref unsafe_state, role, account);
}
#[external(v0)]
fn revoke_role(ref self: ContractState, role: felt252, account: ContractAddress) {
let mut unsafe_state = AccessControl::unsafe_new_contract_state();
AccessControl::AccessControlImpl::revoke_role(ref unsafe_state, role, account);
}
}
----

TIP: The `grant_role` and `revoke_role` functions are automatically exposed as `external` functions
from the `AccessControlImpl` by leveraging the `#[abi(embed_v0)]` annotation.

Note that, unlike the previous examples, no accounts are granted the 'minter' or 'burner' roles.
However, because those roles' admin role is the default admin role, and that role was granted to the 'admin', that
same account can call `grant_role` to give minting or burning permission, and `revoke_role` to remove it.
Expand Down
Loading