Skip to content

Authority

Background

OpenZeppelin is an industry standard offering low-level building blocks to help developers quickly scaffold out common smart contracts and functionality.

However, for many new developers using even the most basic OZ contracts can be confusing and setting up common functionality becomes frustrating.

While not every contract needs authority; many do. In this situations, I find it easier to start with Authority over AccessControl since it gives me a collection of common roles that I use, sets up the the constructor correctly, and with how cheap most blockchains are at this point the extra weight should be negligible in most cases.

Using Authority

AuthorityExample.sol
// SPDX-License-Identifer: MIT
pragma solidity ^0.8.24;
import "@dirtroad/sediment/contracts/authority/Authority.sol";
contract AuthorityExample is Authority {
uint256 public counter;
modifier onlyValidRole {
require(!hasRole(BLACKLIST_ROLE, _msgSender()), "Blacklist");
require(
hasRole(CONTRACT_ROLE, _msgSender()) ||
hasRole(MANAGER_ROLE, _msgSender()) ||
hasRole(SERVER_ROLE, _msgSender()) ||
hasRole(WHITEliST_ROLE, _msgSender()) ||
"Missing Valid Role"
);
}
function increment() public onlyValidRole {
counter++;
}
}

API

Solidity API

Authority

A contract that defines various roles for access control

This contract inherits from the OpenZeppelin AccessControl contract

BLACKLIST_ROLE

bytes32 BLACKLIST_ROLE

Role for contracts that need to blacklist users

CONTRACT_ROLE

bytes32 CONTRACT_ROLE

Role for contracts to call other contracts

CREATOR_ROLE

bytes32 CREATOR_ROLE

Role for creators of new assets in NFT-based contracts

MANAGER_ROLE

bytes32 MANAGER_ROLE

Role for key manual management of smart contracts

MINTER_ROLE

bytes32 MINTER_ROLE

Role for token-based contracts that have a growing supply

SERVER_ROLE

bytes32 SERVER_ROLE

Role for contracts that have server’s managing functions

WHITELIST_ROLE

bytes32 WHITELIST_ROLE

Role for contracts that need to whitelist users

constructor

constructor() public

Constructor that grants all defined roles to the deployer