openzeppelin_stylus::access::control

Struct AccessControl

source
pub struct AccessControl {
    pub _roles: StorageMap<FixedBytes<32>, RoleData>,
}
Expand description

State of an AccessControl contract.

Fields§

§_roles: StorageMap<FixedBytes<32>, RoleData>

Role identifier -> Role information.

Implementations§

source§

impl AccessControl

source

pub const DEFAULT_ADMIN_ROLE: [u8; 32] = _

The default admin role. [0; 32] by default.

source

pub fn has_role(&self, role: B256, account: Address) -> bool

Returns true if account has been granted role.

§Arguments
  • &self - Read access to the contract’s state.
  • role - The role identifier.
  • account - The account to check for membership.
source

pub fn only_role(&self, role: B256) -> Result<(), Error>

Checks if msg::sender has been granted role.

§Arguments
  • &self - Read access to the contract’s state.
  • role - The role identifier.
§Errors

If msg::sender has not been granted role, then the error Error::UnauthorizedAccount is returned.

source

pub fn get_role_admin(&self, role: B256) -> B256

Returns the admin role that controls role. See Self::grant_role and Self::revoke_role.

To change a role’s admin, use Self::_set_role_admin.

§Arguments
  • &self - Read access to the contract’s state.
  • role - The role identifier.
source

pub fn grant_role(&mut self, role: B256, account: Address) -> Result<(), Error>

Grants role to account.

If account had not been already granted role, emits a RoleGranted event.

§Requirements:
  • The caller must have role’s admin role.
§Arguments
  • &mut self - Write access to the contract’s state.
  • role - The role identifier.
  • account - The account which will be granted the role.
§Errors

If msg::sender has not been granted role, then the error Error::UnauthorizedAccount is returned.

§Events

May emit a RoleGranted event.

source

pub fn revoke_role(&mut self, role: B256, account: Address) -> Result<(), Error>

Revokes role from account.

If account had been granted role, emits a RoleRevoked event.

§Requirements:
  • The caller must have role’s admin role.
§Arguments
  • &mut self - Write access to the contract’s state.
  • role - The role identifier.
  • account - The account which will be revoked the role.
§Errors

If msg::sender has not been granted role, then the error Error::UnauthorizedAccount is returned.

§Events

May emit a RoleRevoked event.

source

pub fn renounce_role( &mut self, role: B256, confirmation: Address, ) -> Result<(), Error>

Revokes role from the calling account.

Roles are often managed via Self::grant_role and Self::revoke_role: this function’s purpose is to provide a mechanism for accounts to lose their privileges if they are compromised (such as when a trusted device is misplaced).

§Requirements:
  • The caller must be confirmation.
§Arguments
  • &mut self - Write access to the contract’s state.
  • role - The role identifier.
  • confirmation - The account which will be revoked the role.
§Errors

If msg::sender is not the confirmation address, then the error Error::BadConfirmation is returned.

§Events

If the calling account has its role revoked, emits a RoleRevoked event.

source§

impl AccessControl

source

pub fn _set_role_admin(&mut self, role: B256, new_admin_role: B256)

Sets admin_role as role’s admin role.

§Arguments
  • &mut self - Write access to the contract’s state.
  • role - The identifier of the role we are changing the admin to.
  • new_admin_role - The new admin role.
§Events

Emits a RoleAdminChanged event.

source

pub fn _check_role(&self, role: B256, account: Address) -> Result<(), Error>

Checks if account has been granted role.

§Arguments
  • &self - Read access to the contract’s state.
  • role - The role identifier.
  • account - The account to check for membership.
§Errors

If msg::sender has not been granted role, then the error Error::UnauthorizedAccount is returned.

source

pub fn _grant_role(&mut self, role: B256, account: Address) -> bool

Attempts to grant role to account and returns a boolean indicating if role was granted.

Internal function without access restriction.

§Arguments
  • &mut self - Write access to the contract’s state.
  • role - The role identifier.
  • account - The account which will be granted the role.
§Events

May emit a RoleGranted event.

source

pub fn _revoke_role(&mut self, role: B256, account: Address) -> bool

Attempts to revoke role from account and returns a boolean indicating if role was revoked.

Internal function without access restriction.

§Arguments
  • &mut self - Write access to the contract’s state.
  • role - The role identifier.
  • account - The account which will be granted the role.
§Events

May emit a RoleRevoked event.

Trait Implementations§

source§

impl<S> Router<S> for AccessControl
where S: TopLevelStorage + BorrowMut<Self>,

source§

type Storage = AccessControl

The type the TopLevelStorage borrows into. Usually just Self.
source§

fn route(storage: &mut S, selector: u32, input: &[u8]) -> Option<ArbResult>

Tries to find and execute a method for the given selector, returning None if none is found. Routes add via #[inherit] will only execute if no match is found among Self. This means that it is possible to override a method by redefining it in Self.
source§

impl StorageType for AccessControl

source§

const SLOT_BYTES: usize = 32usize

The number of bytes in a slot needed to represent the type. Must not exceed 32. For types larger than 32 bytes that are stored inline with a struct’s fields, set this to 32 and return the full size in StorageType::new. Read more
source§

const REQUIRED_SLOTS: usize = 1usize

The number of words this type must fill. For primitives this is always 0. For complex types requiring more than one inline word, set this to the total size.
source§

type Wraps<'a> = StorageGuard<'a, AccessControl> where Self: 'a

For primitive types, this is the type being stored. For collections, this is the StorageType being collected.
source§

type WrapsMut<'a> = StorageGuardMut<'a, AccessControl> where Self: 'a

Mutable accessor to the type being stored.
source§

unsafe fn new(root: U256, offset: u8) -> Self

Where in persistent storage the type should live. Although useful for framework designers creating new storage types, most user programs shouldn’t call this. Note: implementations will have to be const once generic_const_exprs stabilizes. Read more
source§

fn load<'s>(self) -> Self::Wraps<'s>

Load the wrapped type, consuming the accessor. Note: most types have a get and/or getter, which don’t consume Self.
source§

fn load_mut<'s>(self) -> Self::WrapsMut<'s>

Load the wrapped mutable type, consuming the accessor. Note: most types have a set and/or setter, which don’t consume Self.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.