|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.20; |
| 3 | + |
| 4 | +import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol"; |
| 5 | +import "./IValidator.sol"; |
| 6 | + |
| 7 | +/** |
| 8 | + * @title Validator |
| 9 | + * @dev Role-based access control for transfer validation and account management. |
| 10 | + * Uses OpenZeppelin AccessControl for secure role management. |
| 11 | + * docs: https://docs.openzeppelin.com/contracts/4.x/api/access#AccessControl |
| 12 | + * audit: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/audits/2023-05-v4.9.pdf |
| 13 | + */ |
| 14 | +contract Validator is AccessControl, IValidator { |
| 15 | + // Role identifiers |
| 16 | + bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE"); |
| 17 | + bytes32 public constant V1_BLOCKED_ROLE = keccak256("V1_BLOCKED_ROLE"); |
| 18 | + bytes32 public constant BLACKLISTED_ROLE = keccak256("BLACKLISTED_ROLE"); |
| 19 | + bytes32 public constant V1_FRONTEND_ROLE = keccak256("V1_FRONTEND_ROLE"); |
| 20 | + |
| 21 | + // Contract identifier for interface compliance |
| 22 | + bytes32 private constant ID = |
| 23 | + 0x5341d189213c4172d0c7256f80bc5f8e6350af3aaff7a029625d8dd94f0f82a5; |
| 24 | + |
| 25 | + /** |
| 26 | + * @dev Returns the contract identifier. |
| 27 | + */ |
| 28 | + function CONTRACT_ID() public pure returns (bytes32) { |
| 29 | + return ID; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @dev Sets up initial admin and role relationships. |
| 34 | + * The deployer is the default admin. |
| 35 | + * ADMIN_ROLE is the admin for blocked, blacklisted, and frontend roles. |
| 36 | + */ |
| 37 | + constructor() { |
| 38 | + _grantRole(DEFAULT_ADMIN_ROLE, msg.sender); |
| 39 | + _setRoleAdmin(V1_BLOCKED_ROLE, ADMIN_ROLE); |
| 40 | + _setRoleAdmin(BLACKLISTED_ROLE, ADMIN_ROLE); |
| 41 | + _setRoleAdmin(V1_FRONTEND_ROLE, ADMIN_ROLE); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @dev Validates a transfer between two accounts. |
| 46 | + * - If called by a V1 frontend, checks if either account is blocked. |
| 47 | + * - Always checks if either account is blacklisted. |
| 48 | + * @return valid True if transfer is allowed, false otherwise. |
| 49 | + */ |
| 50 | + function validate( |
| 51 | + address from, |
| 52 | + address to, |
| 53 | + uint256 /* amount */ |
| 54 | + ) external view override returns (bool valid) { |
| 55 | + if (isV1Frontend(msg.sender)) { |
| 56 | + valid = !(isV1Blocked(from) || isV1Blocked(to)); |
| 57 | + if (!valid) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + } |
| 61 | + if (isBlacklisted(from) || isBlacklisted(to)) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + // --- Admin role management --- |
| 68 | + |
| 69 | + /** |
| 70 | + * @dev Grants ADMIN_ROLE to an account. Only callable by an admin. |
| 71 | + */ |
| 72 | + function setAdmin(address account) external { |
| 73 | + grantRole(ADMIN_ROLE, account); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @dev Revokes ADMIN_ROLE from an account. Only callable by an admin. |
| 78 | + */ |
| 79 | + function revokeAdmin(address account) external { |
| 80 | + revokeRole(ADMIN_ROLE, account); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @dev Checks if an account has ADMIN_ROLE. |
| 85 | + */ |
| 86 | + function isAdminAccount(address account) public view returns (bool) { |
| 87 | + return hasRole(ADMIN_ROLE, account); |
| 88 | + } |
| 89 | + |
| 90 | + // --- Blocked role management --- |
| 91 | + |
| 92 | + /** |
| 93 | + * @dev Grants V1_BLOCKED_ROLE to an account. Only callable by an admin. |
| 94 | + */ |
| 95 | + function setV1Blocked(address account) external { |
| 96 | + grantRole(V1_BLOCKED_ROLE, account); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @dev Revokes V1_BLOCKED_ROLE from an account. Only callable by an admin. |
| 101 | + */ |
| 102 | + function revokeV1Blocked(address account) external { |
| 103 | + revokeRole(V1_BLOCKED_ROLE, account); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @dev Checks if an account has V1_BLOCKED_ROLE. |
| 108 | + */ |
| 109 | + function isV1Blocked(address account) public view returns (bool) { |
| 110 | + return hasRole(V1_BLOCKED_ROLE, account); |
| 111 | + } |
| 112 | + |
| 113 | + // --- Blacklisted role management --- |
| 114 | + |
| 115 | + /** |
| 116 | + * @dev Grants BLACKLISTED_ROLE to an account. Only callable by an admin. |
| 117 | + */ |
| 118 | + function setBlacklisted(address account) external { |
| 119 | + grantRole(BLACKLISTED_ROLE, account); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @dev Revokes BLACKLISTED_ROLE from an account. Only callable by an admin. |
| 124 | + */ |
| 125 | + function revokeBlacklisted(address account) external { |
| 126 | + revokeRole(BLACKLISTED_ROLE, account); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @dev Checks if an account has BLACKLISTED_ROLE. |
| 131 | + */ |
| 132 | + function isBlacklisted(address account) public view returns (bool) { |
| 133 | + return hasRole(BLACKLISTED_ROLE, account); |
| 134 | + } |
| 135 | + |
| 136 | + // --- Frontend role management --- |
| 137 | + |
| 138 | + /** |
| 139 | + * @dev Grants V1_FRONTEND_ROLE to an account. Only callable by an admin. |
| 140 | + */ |
| 141 | + function setV1Frontend(address account) external { |
| 142 | + grantRole(V1_FRONTEND_ROLE, account); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @dev Revokes V1_FRONTEND_ROLE from an account. Only callable by an admin. |
| 147 | + */ |
| 148 | + function revokeV1Frontend(address account) external { |
| 149 | + revokeRole(V1_FRONTEND_ROLE, account); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @dev Checks if an account has V1_FRONTEND_ROLE. |
| 154 | + */ |
| 155 | + function isV1Frontend(address account) public view returns (bool) { |
| 156 | + return hasRole(V1_FRONTEND_ROLE, account); |
| 157 | + } |
| 158 | +} |
0 commit comments