github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/src/EAS/Common.sol (about)

     1  // SPDX-License-Identifier: MIT
     2  pragma solidity ^0.8.0;
     3  
     4  // A representation of an empty/uninitialized UID.
     5  bytes32 constant EMPTY_UID = 0;
     6  
     7  // A zero expiration represents an non-expiring attestation.
     8  uint64 constant NO_EXPIRATION_TIME = 0;
     9  
    10  error AccessDenied();
    11  error DeadlineExpired();
    12  error InvalidEAS();
    13  error InvalidLength();
    14  error InvalidSignature();
    15  error NotFound();
    16  
    17  /// @dev A struct representing ECDSA signature data.
    18  struct Signature {
    19      uint8 v; // The recovery ID.
    20      bytes32 r; // The x-coordinate of the nonce R.
    21      bytes32 s; // The signature data.
    22  }
    23  
    24  /// @dev A struct representing a single attestation.
    25  struct Attestation {
    26      bytes32 uid; // A unique identifier of the attestation.
    27      bytes32 schema; // The unique identifier of the schema.
    28      uint64 time; // The time when the attestation was created (Unix timestamp).
    29      uint64 expirationTime; // The time when the attestation expires (Unix timestamp).
    30      uint64 revocationTime; // The time when the attestation was revoked (Unix timestamp).
    31      bytes32 refUID; // The UID of the related attestation.
    32      address recipient; // The recipient of the attestation.
    33      address attester; // The attester/sender of the attestation.
    34      bool revocable; // Whether the attestation is revocable.
    35      bytes data; // Custom attestation data.
    36  }
    37  
    38  // Maximum upgrade forward-compatibility storage gap.
    39  uint32 constant MAX_GAP = 50;
    40  
    41  /// @dev A helper function to work with unchecked iterators in loops.
    42  /// @param i The index to increment.
    43  /// @return j The incremented index.
    44  function uncheckedInc(uint256 i) pure returns (uint256 j) {
    45      unchecked {
    46          j = i + 1;
    47      }
    48  }
    49  
    50  /// @dev A helper function that converts a string to a bytes32.
    51  /// @param str The string to convert.
    52  /// @return The converted bytes32.
    53  function stringToBytes32(string memory str) pure returns (bytes32) {
    54      bytes32 result;
    55  
    56      assembly {
    57          result := mload(add(str, 32))
    58      }
    59  
    60      return result;
    61  }
    62  
    63  /// @dev A helper function that converts a bytes32 to a string.
    64  /// @param data The bytes32 data to convert.
    65  /// @return The converted string.
    66  function bytes32ToString(bytes32 data) pure returns (string memory) {
    67      bytes memory byteArray = new bytes(32);
    68  
    69      uint256 length = 0;
    70      for (uint256 i = 0; i < 32; i = uncheckedInc(i)) {
    71          bytes1 char = data[i];
    72          if (char == 0x00) {
    73              break;
    74          }
    75  
    76          byteArray[length] = char;
    77          length = uncheckedInc(length);
    78      }
    79  
    80      bytes memory terminatedBytes = new bytes(length);
    81      for (uint256 j = 0; j < length; j = uncheckedInc(j)) {
    82          terminatedBytes[j] = byteArray[j];
    83      }
    84  
    85      return string(terminatedBytes);
    86  }