|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.25; |
| 3 | + |
| 4 | +import {ERC165} from "@openzeppelin/contracts/utils/introspection/ERC165.sol"; |
| 5 | +import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol"; |
| 6 | +import {AccessRequirement, IAccessPredicate, RequirementLogic} from "../src/interfaces/IAccessPredicate.sol"; |
| 7 | +import {IERC7496Trait} from "../src/interfaces/IRequirementTypes.sol"; |
| 8 | +import {IToolRegistry} from "../src/interfaces/IToolRegistry.sol"; |
| 9 | + |
| 10 | +/// @notice Minimal interface for ERC-7496 Dynamic Traits. |
| 11 | +interface IERC7496 { |
| 12 | + function getTraitValue(uint256 tokenId, bytes32 traitKey) external view returns (bytes32); |
| 13 | +} |
| 14 | + |
| 15 | +/// @title TraitGatedPredicate |
| 16 | +/// @notice IAccessPredicate that gates tool access on ERC-721 ownership plus |
| 17 | +/// an ERC-7496 dynamic trait value. The traits contract may differ |
| 18 | +/// from the NFT contract (e.g. a separate renderer). |
| 19 | +/// @dev The caller must pass `abi.encode(uint256 tokenId)` as the `data` |
| 20 | +/// argument to `hasAccess`. |
| 21 | +contract TraitGatedPredicate is IAccessPredicate, ERC165 { |
| 22 | + uint256 public constant MAX_ALLOWED_VALUES = 32; |
| 23 | + |
| 24 | + struct ToolTraitConfig { |
| 25 | + address collection; |
| 26 | + address traitsContract; |
| 27 | + bytes32 traitKey; |
| 28 | + bytes32[] allowedValues; |
| 29 | + } |
| 30 | + |
| 31 | + IToolRegistry public immutable REGISTRY; |
| 32 | + |
| 33 | + mapping(uint256 => ToolTraitConfig) private _configs; |
| 34 | + |
| 35 | + event ToolTraitConfigured( |
| 36 | + uint256 indexed toolId, |
| 37 | + address indexed collection, |
| 38 | + address traitsContract, |
| 39 | + bytes32 traitKey, |
| 40 | + bytes32[] allowedValues |
| 41 | + ); |
| 42 | + |
| 43 | + error CallerIsNotToolCreator(uint256 toolId, address caller); |
| 44 | + error ZeroCollection(); |
| 45 | + error ZeroTraitsContract(); |
| 46 | + error CollectionNoCode(address collection); |
| 47 | + error TraitsContractNoCode(address traitsContract); |
| 48 | + error EmptyAllowedValues(); |
| 49 | + error TooManyAllowedValues(uint256 count, uint256 max); |
| 50 | + error ZeroValueInAllowedValues(uint256 index); |
| 51 | + |
| 52 | + constructor(address _registry) { |
| 53 | + require(_registry != address(0), "TraitGatedPredicate: zero registry"); |
| 54 | + REGISTRY = IToolRegistry(_registry); |
| 55 | + } |
| 56 | + |
| 57 | + /// @notice Configure which collection, traits contract, trait key, and |
| 58 | + /// allowed values gate a tool. Replaces any previous configuration. |
| 59 | + /// @dev Only the tool's creator (as recorded in the registry) may call this. |
| 60 | + /// @param toolId The tool to configure. |
| 61 | + /// @param collection The ERC-721 collection (for `ownerOf` checks). |
| 62 | + /// @param traitsContract The contract implementing ERC-7496 `getTraitValue`. |
| 63 | + /// May be the same as `collection` or a separate renderer. |
| 64 | + /// @param traitKey The trait key to query (e.g. `bytes32("tier")`). |
| 65 | + /// @param allowedValues The set of trait values that grant access. |
| 66 | + function configureToolTrait( |
| 67 | + uint256 toolId, |
| 68 | + address collection, |
| 69 | + address traitsContract, |
| 70 | + bytes32 traitKey, |
| 71 | + bytes32[] calldata allowedValues |
| 72 | + ) external { |
| 73 | + if (REGISTRY.getToolConfig(toolId).creator != msg.sender) { |
| 74 | + revert CallerIsNotToolCreator(toolId, msg.sender); |
| 75 | + } |
| 76 | + |
| 77 | + if (collection == address(0)) revert ZeroCollection(); |
| 78 | + if (traitsContract == address(0)) revert ZeroTraitsContract(); |
| 79 | + if (collection.code.length == 0) revert CollectionNoCode(collection); |
| 80 | + if (traitsContract.code.length == 0) revert TraitsContractNoCode(traitsContract); |
| 81 | + if (allowedValues.length == 0) revert EmptyAllowedValues(); |
| 82 | + if (allowedValues.length > MAX_ALLOWED_VALUES) { |
| 83 | + revert TooManyAllowedValues(allowedValues.length, MAX_ALLOWED_VALUES); |
| 84 | + } |
| 85 | + for (uint256 i; i < allowedValues.length; ++i) { |
| 86 | + if (allowedValues[i] == bytes32(0)) revert ZeroValueInAllowedValues(i); |
| 87 | + } |
| 88 | + |
| 89 | + _configs[toolId] = ToolTraitConfig({ |
| 90 | + collection: collection, |
| 91 | + traitsContract: traitsContract, |
| 92 | + traitKey: traitKey, |
| 93 | + allowedValues: allowedValues |
| 94 | + }); |
| 95 | + emit ToolTraitConfigured(toolId, collection, traitsContract, traitKey, allowedValues); |
| 96 | + } |
| 97 | + |
| 98 | + function getToolTraitConfig(uint256 toolId) external view returns (ToolTraitConfig memory) { |
| 99 | + return _configs[toolId]; |
| 100 | + } |
| 101 | + |
| 102 | + /// @param data Must be `abi.encode(uint256 tokenId)`. |
| 103 | + function hasAccess(uint256 toolId, address account, bytes calldata data) external view override returns (bool) { |
| 104 | + ToolTraitConfig storage config = _configs[toolId]; |
| 105 | + if (config.collection == address(0)) return false; |
| 106 | + if (config.collection.code.length == 0) return false; |
| 107 | + if (data.length != 32) return false; |
| 108 | + |
| 109 | + uint256 tokenId = abi.decode(data, (uint256)); |
| 110 | + |
| 111 | + try IERC721(config.collection).ownerOf(tokenId) returns (address owner) { |
| 112 | + if (owner != account) return false; |
| 113 | + } catch { |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + address traits = config.traitsContract; |
| 118 | + if (traits.code.length == 0) return false; |
| 119 | + |
| 120 | + try IERC7496(traits).getTraitValue(tokenId, config.traitKey) returns (bytes32 value) { |
| 121 | + bytes32[] storage allowed = config.allowedValues; |
| 122 | + uint256 len = allowed.length; |
| 123 | + for (uint256 i; i < len; ++i) { |
| 124 | + if (value == allowed[i]) return true; |
| 125 | + } |
| 126 | + return false; |
| 127 | + } catch { |
| 128 | + return false; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /// @inheritdoc IAccessPredicate |
| 133 | + function getRequirements(uint256 toolId) |
| 134 | + external |
| 135 | + view |
| 136 | + override |
| 137 | + returns (AccessRequirement[] memory requirements, RequirementLogic logic) |
| 138 | + { |
| 139 | + ToolTraitConfig storage config = _configs[toolId]; |
| 140 | + if (config.collection == address(0)) { |
| 141 | + requirements = new AccessRequirement[](0); |
| 142 | + } else { |
| 143 | + requirements = new AccessRequirement[](1); |
| 144 | + requirements[0] = AccessRequirement({ |
| 145 | + kind: type(IERC7496Trait).interfaceId, |
| 146 | + data: abi.encode(config.collection, config.traitsContract, config.traitKey, config.allowedValues), |
| 147 | + label: "" |
| 148 | + }); |
| 149 | + } |
| 150 | + logic = RequirementLogic.AND; |
| 151 | + } |
| 152 | + |
| 153 | + function supportsInterface(bytes4 interfaceId) public view override returns (bool) { |
| 154 | + return interfaceId == type(IAccessPredicate).interfaceId || super.supportsInterface(interfaceId); |
| 155 | + } |
| 156 | + |
| 157 | + /// @inheritdoc IAccessPredicate |
| 158 | + function name() external pure returns (string memory) { |
| 159 | + return "TraitGatedPredicate"; |
| 160 | + } |
| 161 | + |
| 162 | + function version() external pure returns (string memory) { |
| 163 | + return "0.1"; |
| 164 | + } |
| 165 | +} |
0 commit comments