Using Audited Libraries and Access Control
Reusing well-audited code and designing permissions carefully remove whole classes of bugs. This lesson explains OpenZeppelin Contracts, the Ownable, Ownable2Step and AccessControl patterns, and how multisig wallets and timelocks protect privileged roles.
Learning outcomes
After completing this lesson, you will be able to:
- Describe what OpenZeppelin Contracts provides.
- Compare Ownable, Ownable2Step and AccessControl.
- Choose an access control model for a scenario.
- Describe how multisig wallets and timelocks protect privileged roles.
OpenZeppelin Contracts
OpenZeppelin Contracts is the most widely used library of Solidity components. It provides token standards (ERC-20, ERC-721, ERC-1155 and extensions), access control, security utilities such as ReentrancyGuard, Pausable and SafeERC20, cryptography helpers such as ECDSA and MerkleProof, and upgradeable variants. It is installed with npm install @openzeppelin/contracts in Hardhat projects, and Remix resolves its imports automatically.
Using it well means pinning a version (the current major version is 5), reading the documentation of each component, and inheriting rather than copying code. Mixing major versions, or editing library files, loses the benefit of the audits.
OpenZeppelin components
Ownable, Ownable2Step and AccessControl
The following comparison sets out the three main access control contracts.
import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol";
contract Treasury is AccessControl {
bytes32 public constant PAYER_ROLE = keccak256("PAYER_ROLE");
constructor(address admin) {
_grantRole(DEFAULT_ADMIN_ROLE, admin);
}
function pay(address to, uint256 amount) external onlyRole(PAYER_ROLE) {
// ...
}
}In OpenZeppelin 5, Ownable's constructor takes the initial owner explicitly: Ownable(initialOwner). renounceOwnership permanently leaves the contract without an owner, which disables every onlyOwner function.
Choosing an access model
Protecting privileged roles
Access control is only as strong as the keys that hold the roles. The following terms describe the standard protections.
- Multisig wallet
- A contract wallet, such as Safe, that executes a transaction only when a threshold of owners (for example 3 of 5) approve it.
- Timelock
- A contract that delays the execution of administrative actions, such as upgrades, by a set period so that users can review them and exit.
- Hardware wallet
- A device that keeps a private key isolated from the computer and asks for physical confirmation.
- Role separation
- Different roles for routine operations (such as pausing) and critical changes (such as upgrades), held by different signers.
Protecting admin keys
Choosing a model
A rewards contract needs three permissions: a back-end service mints rewards daily, the security team must be able to pause, and the board approves fee changes. Which access control design fits?
AccessControl with three roles: MINTER_ROLE for the service's key, PAUSER_ROLE for the security team, and fee changes restricted to a role held by a board multisig behind a timelock. DEFAULT_ADMIN_ROLE, which can grant and revoke roles, should also sit with the multisig. A single Ownable owner would give the service key power to change fees, violating least privilege.
A mistyped new owner
An administrator calls transferOwnership on an Ownable contract with an address that has one character wrong. What happens, and what would have prevented the loss?
Ownership moves immediately to an address that nobody controls, and every onlyOwner function becomes unusable forever. Ownable2Step would have prevented this: the transfer only completes when the new address calls acceptOwnership, which a mistyped address can never do.
What extra step does Ownable2Step add to an ownership transfer?
Show answer
The new owner must call acceptOwnership. Ownership changes only when the proposed address accepts, which prevents transfers to mistyped or uncontrolled addresses.
Exam Tips
Remember for the exam
- Inherit audited OpenZeppelin components; pin the version and do not edit library code.
- Ownable for one admin; Ownable2Step for safe transfers; AccessControl for least-privilege roles.
- Protect privileged roles with multisig wallets, timelocks and hardware wallets.
- renounceOwnership disables onlyOwner functions permanently.