AccessControlDefaultAdminRules.sol 8.7 KB

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