|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// OpenZeppelin Contracts (last updated v4.8.0) (access/AccessControlDefaultAdminRules.sol) |
| 3 | + |
| 4 | +pragma solidity ^0.8.0; |
| 5 | + |
| 6 | +import "./AccessControl.sol"; |
| 7 | +import "./IAccessControlDefaultAdminRules.sol"; |
| 8 | +import "../utils/math/SafeCast.sol"; |
| 9 | +import "../interfaces/IERC5313.sol"; |
| 10 | + |
| 11 | +/** |
| 12 | + * @dev Extension of {AccessControl} that allows specifying special rules to manage |
| 13 | + * the `DEFAULT_ADMIN_ROLE` holder, which is a sensitive role with special permissions |
| 14 | + * over other roles that may potentially have privileged rights in the system. |
| 15 | + * |
| 16 | + * If a specific role doesn't have an admin role assigned, the holder of the |
| 17 | + * `DEFAULT_ADMIN_ROLE` will have the ability to grant it and revoke it. |
| 18 | + * |
| 19 | + * This contract implements the following risk mitigations on top of {AccessControl}: |
| 20 | + * |
| 21 | + * * Only one account holds the `DEFAULT_ADMIN_ROLE` since deployment until it's potentially renounced. |
| 22 | + * * Enforce a 2-step process to transfer the `DEFAULT_ADMIN_ROLE` to another account. |
| 23 | + * * Enforce a configurable delay between the two steps, with the ability to cancel in between. |
| 24 | + * - Even after the timer has passed to avoid locking it forever. |
| 25 | + * * It is not possible to use another role to manage the `DEFAULT_ADMIN_ROLE`. |
| 26 | + * |
| 27 | + * Example usage: |
| 28 | + * |
| 29 | + * ```solidity |
| 30 | + * contract MyToken is AccessControlDefaultAdminRules { |
| 31 | + * constructor() AccessControlDefaultAdminRules( |
| 32 | + * 3 days, |
| 33 | + * msg.sender // Explicit initial `DEFAULT_ADMIN_ROLE` holder |
| 34 | + * ) {} |
| 35 | + *} |
| 36 | + * ``` |
| 37 | + * |
| 38 | + * NOTE: The `delay` can only be set in the constructor and is fixed thereafter. |
| 39 | + * |
| 40 | + * _Available since v4.9._ |
| 41 | + */ |
| 42 | +abstract contract AccessControlDefaultAdminRules is IAccessControlDefaultAdminRules, IERC5313, AccessControl { |
| 43 | + uint48 private immutable _defaultAdminDelay; |
| 44 | + |
| 45 | + address private _currentDefaultAdmin; |
| 46 | + address private _pendingDefaultAdmin; |
| 47 | + |
| 48 | + uint48 private _defaultAdminTransferDelayedUntil; |
| 49 | + |
| 50 | + /** |
| 51 | + * @dev Sets the initial values for {defaultAdminDelay} in seconds and {defaultAdmin}. |
| 52 | + * |
| 53 | + * The `defaultAdminDelay` value is immutable. It can only be set at the constructor. |
| 54 | + */ |
| 55 | + constructor(uint48 defaultAdminDelay_, address initialDefaultAdmin) { |
| 56 | + _defaultAdminDelay = defaultAdminDelay_; |
| 57 | + _grantRole(DEFAULT_ADMIN_ROLE, initialDefaultAdmin); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @dev See {IERC5313-owner}. |
| 62 | + */ |
| 63 | + function owner() public view virtual returns (address) { |
| 64 | + return defaultAdmin(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @inheritdoc IAccessControlDefaultAdminRules |
| 69 | + */ |
| 70 | + function defaultAdminDelay() public view virtual returns (uint48) { |
| 71 | + return _defaultAdminDelay; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @inheritdoc IAccessControlDefaultAdminRules |
| 76 | + */ |
| 77 | + function defaultAdmin() public view virtual returns (address) { |
| 78 | + return _currentDefaultAdmin; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @inheritdoc IAccessControlDefaultAdminRules |
| 83 | + */ |
| 84 | + function pendingDefaultAdmin() public view virtual returns (address) { |
| 85 | + return _pendingDefaultAdmin; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @inheritdoc IAccessControlDefaultAdminRules |
| 90 | + */ |
| 91 | + function defaultAdminTransferDelayedUntil() public view virtual returns (uint48) { |
| 92 | + return _defaultAdminTransferDelayedUntil; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @dev See {IERC165-supportsInterface}. |
| 97 | + */ |
| 98 | + function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) { |
| 99 | + return interfaceId == type(IAccessControlDefaultAdminRules).interfaceId || super.supportsInterface(interfaceId); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @inheritdoc IAccessControlDefaultAdminRules |
| 104 | + */ |
| 105 | + function beginDefaultAdminTransfer(address newAdmin) public virtual onlyRole(DEFAULT_ADMIN_ROLE) { |
| 106 | + _beginDefaultAdminTransfer(newAdmin); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @inheritdoc IAccessControlDefaultAdminRules |
| 111 | + */ |
| 112 | + function acceptDefaultAdminTransfer() public virtual { |
| 113 | + require(_msgSender() == pendingDefaultAdmin(), "AccessControl: pending admin must accept"); |
| 114 | + _acceptDefaultAdminTransfer(); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @inheritdoc IAccessControlDefaultAdminRules |
| 119 | + */ |
| 120 | + function cancelDefaultAdminTransfer() public virtual onlyRole(DEFAULT_ADMIN_ROLE) { |
| 121 | + _resetDefaultAdminTransfer(); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @dev Revokes `role` from the calling account. |
| 126 | + * |
| 127 | + * For `DEFAULT_ADMIN_ROLE`, only allows renouncing in two steps, so it's required |
| 128 | + * that the {defaultAdminTransferDelayedUntil} has passed and the pending default admin is the zero address. |
| 129 | + * After its execution, it will not be possible to call `onlyRole(DEFAULT_ADMIN_ROLE)` |
| 130 | + * functions. |
| 131 | + * |
| 132 | + * For other roles, see {AccessControl-renounceRole}. |
| 133 | + * |
| 134 | + * NOTE: Renouncing `DEFAULT_ADMIN_ROLE` will leave the contract without a defaultAdmin, |
| 135 | + * thereby disabling any functionality that is only available to the default admin, and the |
| 136 | + * possibility of reassigning a non-administrated role. |
| 137 | + */ |
| 138 | + function renounceRole(bytes32 role, address account) public virtual override(AccessControl, IAccessControl) { |
| 139 | + if (role == DEFAULT_ADMIN_ROLE) { |
| 140 | + require( |
| 141 | + pendingDefaultAdmin() == address(0) && _hasDefaultAdminTransferDelayPassed(), |
| 142 | + "AccessControl: only can renounce in two delayed steps" |
| 143 | + ); |
| 144 | + } |
| 145 | + super.renounceRole(role, account); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @dev See {AccessControl-grantRole}. Reverts for `DEFAULT_ADMIN_ROLE`. |
| 150 | + */ |
| 151 | + function grantRole(bytes32 role, address account) public virtual override(AccessControl, IAccessControl) { |
| 152 | + require(role != DEFAULT_ADMIN_ROLE, "AccessControl: can't directly grant default admin role"); |
| 153 | + super.grantRole(role, account); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * @dev See {AccessControl-revokeRole}. Reverts for `DEFAULT_ADMIN_ROLE`. |
| 158 | + */ |
| 159 | + function revokeRole(bytes32 role, address account) public virtual override(AccessControl, IAccessControl) { |
| 160 | + require(role != DEFAULT_ADMIN_ROLE, "AccessControl: can't directly revoke default admin role"); |
| 161 | + super.revokeRole(role, account); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @dev See {AccessControl-_setRoleAdmin}. Reverts for `DEFAULT_ADMIN_ROLE`. |
| 166 | + */ |
| 167 | + function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual override { |
| 168 | + require(role != DEFAULT_ADMIN_ROLE, "AccessControl: can't violate default admin rules"); |
| 169 | + super._setRoleAdmin(role, adminRole); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * @dev Grants `role` to `account`. |
| 174 | + * |
| 175 | + * For `DEFAULT_ADMIN_ROLE`, it only allows granting if there isn't already a role's holder |
| 176 | + * or if the role has been previously renounced. |
| 177 | + * |
| 178 | + * For other roles, see {AccessControl-renounceRole}. |
| 179 | + * |
| 180 | + * NOTE: Exposing this function through another mechanism may make the |
| 181 | + * `DEFAULT_ADMIN_ROLE` assignable again. Make sure to guarantee this is |
| 182 | + * the expected behavior in your implementation. |
| 183 | + */ |
| 184 | + function _grantRole(bytes32 role, address account) internal virtual override { |
| 185 | + if (role == DEFAULT_ADMIN_ROLE) { |
| 186 | + require(defaultAdmin() == address(0), "AccessControl: default admin already granted"); |
| 187 | + _currentDefaultAdmin = account; |
| 188 | + } |
| 189 | + super._grantRole(role, account); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * @dev See {acceptDefaultAdminTransfer}. |
| 194 | + * |
| 195 | + * Internal function without access restriction. |
| 196 | + */ |
| 197 | + function _acceptDefaultAdminTransfer() internal virtual { |
| 198 | + require(_hasDefaultAdminTransferDelayPassed(), "AccessControl: transfer delay not passed"); |
| 199 | + _revokeRole(DEFAULT_ADMIN_ROLE, defaultAdmin()); |
| 200 | + _grantRole(DEFAULT_ADMIN_ROLE, pendingDefaultAdmin()); |
| 201 | + _resetDefaultAdminTransfer(); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * @dev See {beginDefaultAdminTransfer}. |
| 206 | + * |
| 207 | + * Internal function without access restriction. |
| 208 | + */ |
| 209 | + function _beginDefaultAdminTransfer(address newAdmin) internal virtual { |
| 210 | + _defaultAdminTransferDelayedUntil = SafeCast.toUint48(block.timestamp) + defaultAdminDelay(); |
| 211 | + _pendingDefaultAdmin = newAdmin; |
| 212 | + emit DefaultAdminRoleChangeStarted(pendingDefaultAdmin(), defaultAdminTransferDelayedUntil()); |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * @dev See {AccessControl-_revokeRole}. |
| 217 | + */ |
| 218 | + function _revokeRole(bytes32 role, address account) internal virtual override { |
| 219 | + if (role == DEFAULT_ADMIN_ROLE) { |
| 220 | + delete _currentDefaultAdmin; |
| 221 | + } |
| 222 | + super._revokeRole(role, account); |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * @dev Resets the pending default admin and delayed until. |
| 227 | + */ |
| 228 | + function _resetDefaultAdminTransfer() private { |
| 229 | + delete _pendingDefaultAdmin; |
| 230 | + delete _defaultAdminTransferDelayedUntil; |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + * @dev Checks if a {defaultAdminTransferDelayedUntil} has been set and passed. |
| 235 | + */ |
| 236 | + function _hasDefaultAdminTransferDelayPassed() private view returns (bool) { |
| 237 | + uint48 delayedUntil = defaultAdminTransferDelayedUntil(); |
| 238 | + return delayedUntil > 0 && delayedUntil < block.timestamp; |
| 239 | + } |
| 240 | +} |
0 commit comments