|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +/// @author thirdweb |
| 5 | + |
| 6 | +import "../../extension/interface/IAccountPermissions.sol"; |
| 7 | +import "../../openzeppelin-presets/utils/cryptography/EIP712.sol"; |
| 8 | +import "../../openzeppelin-presets/utils/structs/EnumerableSet.sol"; |
| 9 | + |
| 10 | +library AccountPermissionsStorage { |
| 11 | + bytes32 public constant ACCOUNT_PERMISSIONS_STORAGE_POSITION = keccak256("account.permissions.storage"); |
| 12 | + |
| 13 | + struct Data { |
| 14 | + /// @dev Map from address => whether the address is an admin. |
| 15 | + mapping(address => bool) isAdmin; |
| 16 | + /// @dev Map from keccak256 hash of a role => active restrictions for that role. |
| 17 | + mapping(bytes32 => IAccountPermissions.RoleStatic) roleRestrictions; |
| 18 | + /// @dev Map from address => the role held by that address. |
| 19 | + mapping(address => bytes32) roleOfAccount; |
| 20 | + /// @dev Mapping from a signed request UID => whether the request is processed. |
| 21 | + mapping(bytes32 => bool) executed; |
| 22 | + /// @dev Map from keccak256 hash of a role to its approved targets. |
| 23 | + mapping(bytes32 => EnumerableSet.AddressSet) approvedTargets; |
| 24 | + /// @dev map from keccak256 hash of a role to its members' data. See {RoleMembers}. |
| 25 | + mapping(bytes32 => EnumerableSet.AddressSet) roleMembers; |
| 26 | + } |
| 27 | + |
| 28 | + function accountPermissionsStorage() internal pure returns (Data storage accountPermissionsData) { |
| 29 | + bytes32 position = ACCOUNT_PERMISSIONS_STORAGE_POSITION; |
| 30 | + assembly { |
| 31 | + accountPermissionsData.slot := position |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +abstract contract AccountPermissions is IAccountPermissions, EIP712 { |
| 37 | + using ECDSA for bytes32; |
| 38 | + using EnumerableSet for EnumerableSet.AddressSet; |
| 39 | + |
| 40 | + bytes32 private constant TYPEHASH = |
| 41 | + keccak256( |
| 42 | + "RoleRequest(bytes32 role,address target,uint8 action,uint128 validityStartTimestamp,uint128 validityEndTimestamp,bytes32 uid)" |
| 43 | + ); |
| 44 | + |
| 45 | + modifier onlyAdmin() virtual { |
| 46 | + require(isAdmin(msg.sender), "AccountPermissions: caller is not an admin"); |
| 47 | + _; |
| 48 | + } |
| 49 | + |
| 50 | + /*/////////////////////////////////////////////////////////////// |
| 51 | + External functions |
| 52 | + //////////////////////////////////////////////////////////////*/ |
| 53 | + |
| 54 | + /// @notice Adds / removes an account as an admin. |
| 55 | + function setAdmin(address _account, bool _isAdmin) external virtual onlyAdmin { |
| 56 | + _setAdmin(_account, _isAdmin); |
| 57 | + } |
| 58 | + |
| 59 | + /// @notice Sets the restrictions for a given role. |
| 60 | + function setRoleRestrictions(RoleRestrictions calldata _restrictions) external virtual onlyAdmin { |
| 61 | + require(_restrictions.role != bytes32(0), "AccountPermissions: role cannot be empty"); |
| 62 | + |
| 63 | + AccountPermissionsStorage.Data storage data = AccountPermissionsStorage.accountPermissionsStorage(); |
| 64 | + data.roleRestrictions[_restrictions.role] = RoleStatic( |
| 65 | + _restrictions.role, |
| 66 | + _restrictions.maxValuePerTransaction, |
| 67 | + _restrictions.startTimestamp, |
| 68 | + _restrictions.endTimestamp |
| 69 | + ); |
| 70 | + |
| 71 | + uint256 len = _restrictions.approvedTargets.length; |
| 72 | + delete data.approvedTargets[_restrictions.role]; |
| 73 | + for (uint256 i = 0; i < len; i++) { |
| 74 | + data.approvedTargets[_restrictions.role].add(_restrictions.approvedTargets[i]); |
| 75 | + } |
| 76 | + |
| 77 | + emit RoleUpdated(_restrictions.role, _restrictions); |
| 78 | + } |
| 79 | + |
| 80 | + /// @notice Grant / revoke a role from a given signer. |
| 81 | + function changeRole(RoleRequest calldata _req, bytes calldata _signature) external virtual { |
| 82 | + require(_req.role != bytes32(0), "AccountPermissions: role cannot be empty"); |
| 83 | + require( |
| 84 | + _req.validityStartTimestamp < block.timestamp && block.timestamp < _req.validityEndTimestamp, |
| 85 | + "AccountPermissions: invalid validity period" |
| 86 | + ); |
| 87 | + |
| 88 | + (bool success, address signer) = verifyRoleRequest(_req, _signature); |
| 89 | + |
| 90 | + require(success, "AccountPermissions: invalid signature"); |
| 91 | + |
| 92 | + AccountPermissionsStorage.Data storage data = AccountPermissionsStorage.accountPermissionsStorage(); |
| 93 | + data.executed[_req.uid] = true; |
| 94 | + |
| 95 | + if (_req.action == RoleAction.GRANT) { |
| 96 | + data.roleOfAccount[_req.target] = _req.role; |
| 97 | + data.roleMembers[_req.role].add(_req.target); |
| 98 | + } else { |
| 99 | + delete data.roleOfAccount[_req.target]; |
| 100 | + data.roleMembers[_req.role].remove(_req.target); |
| 101 | + } |
| 102 | + |
| 103 | + emit RoleAssignment(_req.role, _req.target, signer, _req); |
| 104 | + } |
| 105 | + |
| 106 | + /*/////////////////////////////////////////////////////////////// |
| 107 | + View functions |
| 108 | + //////////////////////////////////////////////////////////////*/ |
| 109 | + |
| 110 | + /// @notice Returns whether the given account is an admin. |
| 111 | + function isAdmin(address _account) public view virtual returns (bool) { |
| 112 | + AccountPermissionsStorage.Data storage data = AccountPermissionsStorage.accountPermissionsStorage(); |
| 113 | + return data.isAdmin[_account]; |
| 114 | + } |
| 115 | + |
| 116 | + /// @notice Returns the role held by a given account along with its restrictions. |
| 117 | + function getRoleRestrictionsForAccount(address _account) external view virtual returns (RoleRestrictions memory) { |
| 118 | + AccountPermissionsStorage.Data storage data = AccountPermissionsStorage.accountPermissionsStorage(); |
| 119 | + bytes32 role = data.roleOfAccount[_account]; |
| 120 | + RoleStatic memory roleRestrictions = data.roleRestrictions[role]; |
| 121 | + |
| 122 | + return |
| 123 | + RoleRestrictions( |
| 124 | + role, |
| 125 | + data.approvedTargets[role].values(), |
| 126 | + roleRestrictions.maxValuePerTransaction, |
| 127 | + roleRestrictions.startTimestamp, |
| 128 | + roleRestrictions.endTimestamp |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + /// @notice Returns the role restrictions for a given role. |
| 133 | + function getRoleRestrictions(bytes32 _role) external view virtual returns (RoleRestrictions memory) { |
| 134 | + AccountPermissionsStorage.Data storage data = AccountPermissionsStorage.accountPermissionsStorage(); |
| 135 | + RoleStatic memory roleRestrictions = data.roleRestrictions[_role]; |
| 136 | + |
| 137 | + return |
| 138 | + RoleRestrictions( |
| 139 | + _role, |
| 140 | + data.approvedTargets[_role].values(), |
| 141 | + roleRestrictions.maxValuePerTransaction, |
| 142 | + roleRestrictions.startTimestamp, |
| 143 | + roleRestrictions.endTimestamp |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + /// @notice Returns all accounts that have a role. |
| 148 | + function getAllRoleMembers(bytes32 _role) external view virtual returns (address[] memory) { |
| 149 | + AccountPermissionsStorage.Data storage data = AccountPermissionsStorage.accountPermissionsStorage(); |
| 150 | + return data.roleMembers[_role].values(); |
| 151 | + } |
| 152 | + |
| 153 | + /// @dev Verifies that a request is signed by an authorized account. |
| 154 | + function verifyRoleRequest(RoleRequest calldata req, bytes calldata signature) |
| 155 | + public |
| 156 | + view |
| 157 | + virtual |
| 158 | + returns (bool success, address signer) |
| 159 | + { |
| 160 | + AccountPermissionsStorage.Data storage data = AccountPermissionsStorage.accountPermissionsStorage(); |
| 161 | + signer = _recoverAddress(req, signature); |
| 162 | + success = !data.executed[req.uid] && isAdmin(signer); |
| 163 | + } |
| 164 | + |
| 165 | + /*/////////////////////////////////////////////////////////////// |
| 166 | + Internal functions |
| 167 | + //////////////////////////////////////////////////////////////*/ |
| 168 | + |
| 169 | + /// @notice Runs after every `changeRole` run. |
| 170 | + function _afterChangeRole(RoleRequest calldata _req) internal virtual; |
| 171 | + |
| 172 | + /// @notice Makes the given account an admin. |
| 173 | + function _setAdmin(address _account, bool _isAdmin) internal virtual { |
| 174 | + AccountPermissionsStorage.Data storage data = AccountPermissionsStorage.accountPermissionsStorage(); |
| 175 | + data.isAdmin[_account] = _isAdmin; |
| 176 | + |
| 177 | + emit AdminUpdated(_account, _isAdmin); |
| 178 | + } |
| 179 | + |
| 180 | + /// @dev Returns the address of the signer of the request. |
| 181 | + function _recoverAddress(RoleRequest calldata _req, bytes calldata _signature) |
| 182 | + internal |
| 183 | + view |
| 184 | + virtual |
| 185 | + returns (address) |
| 186 | + { |
| 187 | + return _hashTypedDataV4(keccak256(_encodeRequest(_req))).recover(_signature); |
| 188 | + } |
| 189 | + |
| 190 | + /// @dev Encodes a request for recovery of the signer in `recoverAddress`. |
| 191 | + function _encodeRequest(RoleRequest calldata _req) internal pure virtual returns (bytes memory) { |
| 192 | + return |
| 193 | + abi.encode( |
| 194 | + TYPEHASH, |
| 195 | + _req.role, |
| 196 | + _req.target, |
| 197 | + _req.action, |
| 198 | + _req.validityStartTimestamp, |
| 199 | + _req.validityEndTimestamp, |
| 200 | + _req.uid |
| 201 | + ); |
| 202 | + } |
| 203 | +} |
0 commit comments