|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.28; |
| 3 | + |
| 4 | +import "./Ownable.sol"; |
| 5 | + |
| 6 | +/** |
| 7 | + * @title ShutterRegistry |
| 8 | + * @dev A contract for managing the registration of identities with timestamps, ensuring unique and future-dated registrations. |
| 9 | + * Inherits from OpenZeppelin's Ownable contract to enable ownership-based access control. |
| 10 | + */ |
| 11 | +contract ShutterRegistry is Ownable { |
| 12 | + // Custom error for when an identity is already registered. |
| 13 | + error AlreadyRegistered(); |
| 14 | + |
| 15 | + // Custom error for when a provided timestamp is in the past. |
| 16 | + error TimestampInThePast(); |
| 17 | + |
| 18 | + // Custom error for when a identityPrefix provided is empty. |
| 19 | + error InvalidIdentityPrefix(); |
| 20 | + |
| 21 | + // Custom error for when the registration ttl is lower than already registered. |
| 22 | + error TTLTooShort(); |
| 23 | + |
| 24 | + struct EventTriggerRegistration { |
| 25 | + uint64 eon; |
| 26 | + uint64 ttl; |
| 27 | + bytes32 triggerDefinitionHash; |
| 28 | + } |
| 29 | + /** |
| 30 | + * @dev Mapping to store registration data for each identity. |
| 31 | + * The identity is represented as a `bytes32` hash and mapped to the EventTriggerRegistration. |
| 32 | + */ |
| 33 | + |
| 34 | + mapping(bytes32 identity => EventTriggerRegistration) public registrations; |
| 35 | + |
| 36 | + /** |
| 37 | + * @dev Emitted when a new event trigger identity is successfully registered. |
| 38 | + * @param eon The eon associated with the identity. |
| 39 | + * @param identityPrefix The raw prefix input used to derive the registered identity hash. |
| 40 | + * @param sender The address of the account that performed the registration. |
| 41 | + * @param triggerDefinition The eventTriggerDefinition associated with the registered identity. |
| 42 | + * @param ttl The blockNumber after which the eventTrigger can be ignored by keypers. |
| 43 | + */ |
| 44 | + event EventTriggerRegistered( |
| 45 | + uint64 indexed eon, |
| 46 | + bytes32 identityPrefix, |
| 47 | + address sender, |
| 48 | + bytes[] triggerDefinition, |
| 49 | + uint64 ttl |
| 50 | + ); |
| 51 | + |
| 52 | + /** |
| 53 | + * @dev Initializes the contract and assigns ownership to the deployer. |
| 54 | + */ |
| 55 | + constructor() Ownable(msg.sender) {} |
| 56 | + |
| 57 | + /** |
| 58 | + * @notice Registers a new identity with a specified eventTriggerDefinition and eon. |
| 59 | + * @dev The identity is derived by hashing the provided `identityPrefix` concatenated with the sender's address. |
| 60 | + * @param eon The eon associated with the identity. |
| 61 | + * @param identityPrefix The input used to derive the identity hash. |
| 62 | + * @param triggerDefinition The eventTriggerDefinition. |
| 63 | + * @param ttl A block number in the future after which the trigger can be ignored by keypers. |
| 64 | + * @custom:requirements |
| 65 | + * - The identity must not already be registered. |
| 66 | + * - The provided ttl block number must not be in the past. |
| 67 | + */ |
| 68 | + function register( |
| 69 | + uint64 eon, |
| 70 | + bytes32 identityPrefix, |
| 71 | + bytes[] memory triggerDefinition, |
| 72 | + uint64 ttl |
| 73 | + ) external { |
| 74 | + // Ensure the timestamp is not in the past. |
| 75 | + require(ttl >= block.number, TimestampInThePast()); |
| 76 | + |
| 77 | + // Ensure identityPrefix passed in correct. |
| 78 | + require(identityPrefix != bytes32(0), InvalidIdentityPrefix()); |
| 79 | + |
| 80 | + // Generate the identity hash from the provided prefix and the sender's address. |
| 81 | + bytes32 identity = keccak256( |
| 82 | + abi.encodePacked(identityPrefix, msg.sender) |
| 83 | + ); |
| 84 | + EventTriggerRegistration storage registrationData = registrations[ |
| 85 | + identity |
| 86 | + ]; |
| 87 | + // Ensure no one is trying to decrease ttl. |
| 88 | + require(registrationData.ttl < ttl, TTLTooShort()); |
| 89 | + |
| 90 | + if (registrationData.ttl != 0) { |
| 91 | + require( |
| 92 | + keccak256(abi.encode(triggerDefinition)) == |
| 93 | + registrationData.triggerDefinitionHash, |
| 94 | + AlreadyRegistered() |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + // store the (maybe renewed) registration data; |
| 99 | + registrationData.ttl = ttl; |
| 100 | + registrationData.eon = eon; |
| 101 | + registrationData.triggerDefinitionHash = keccak256( |
| 102 | + abi.encode(triggerDefinition) |
| 103 | + ); |
| 104 | + |
| 105 | + // Emit the EventTriggerRegistered event. |
| 106 | + emit EventTriggerRegistered( |
| 107 | + eon, |
| 108 | + identityPrefix, |
| 109 | + msg.sender, |
| 110 | + triggerDefinition, |
| 111 | + ttl |
| 112 | + ); |
| 113 | + } |
| 114 | +} |
0 commit comments