github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/test/mocks/OptimistInviterHelper.sol (about)

     1  // SPDX-License-Identifier: MIT
     2  pragma solidity ^0.8.0;
     3  
     4  import { OptimistInviter } from "src/periphery/op-nft/OptimistInviter.sol";
     5  import { ECDSA } from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
     6  
     7  /// @notice Simple helper contract that helps with testing flow and signature for
     8  ///         OptimistInviter contract. Made this a separate contract instead of including
     9  ///         in OptimistInviter.t.sol for reusability.
    10  contract OptimistInviterHelper {
    11      /// @notice EIP712 typehash for the ClaimableInvite type.
    12      bytes32 public constant CLAIMABLE_INVITE_TYPEHASH = keccak256("ClaimableInvite(address issuer,bytes32 nonce)");
    13  
    14      /// @notice EIP712 typehash for the EIP712Domain type that is included as part of the signature.
    15      bytes32 public constant EIP712_DOMAIN_TYPEHASH =
    16          keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
    17  
    18      /// @notice Address of OptimistInviter contract we are testing.
    19      OptimistInviter public optimistInviter;
    20  
    21      /// @notice OptimistInviter contract name. Used to construct the EIP-712 domain.
    22      string public name;
    23  
    24      /// @notice Keeps track of current nonce to generate new nonces for each invite.
    25      uint256 public currentNonce;
    26  
    27      constructor(OptimistInviter _optimistInviter, string memory _name) {
    28          optimistInviter = _optimistInviter;
    29          name = _name;
    30      }
    31  
    32      /// @notice Returns the hash of the struct ClaimableInvite.
    33      /// @param _claimableInvite ClaimableInvite struct to hash.
    34      /// @return EIP-712 typed struct hash.
    35      function getClaimableInviteStructHash(OptimistInviter.ClaimableInvite memory _claimableInvite)
    36          public
    37          pure
    38          returns (bytes32)
    39      {
    40          return keccak256(abi.encode(CLAIMABLE_INVITE_TYPEHASH, _claimableInvite.issuer, _claimableInvite.nonce));
    41      }
    42  
    43      /// @notice Returns a bytes32 nonce that should change everytime. In practice, people should use
    44      ///         pseudorandom nonces.
    45      /// @return Nonce that should be used as part of ClaimableInvite.
    46      function consumeNonce() public returns (bytes32) {
    47          return bytes32(keccak256(abi.encode(currentNonce++)));
    48      }
    49  
    50      /// @notice Returns a ClaimableInvite with the issuer and current nonce.
    51      /// @param _issuer Issuer to include in the ClaimableInvite.
    52      /// @return ClaimableInvite that can be hashed & signed.
    53      function getClaimableInviteWithNewNonce(address _issuer) public returns (OptimistInviter.ClaimableInvite memory) {
    54          return OptimistInviter.ClaimableInvite(_issuer, consumeNonce());
    55      }
    56  
    57      /// @notice Computes the EIP712 digest with default correct parameters.
    58      /// @param _claimableInvite ClaimableInvite struct to hash.
    59      /// @return EIP-712 compatible digest.
    60      function getDigest(OptimistInviter.ClaimableInvite calldata _claimableInvite) public view returns (bytes32) {
    61          return getDigestWithEIP712Domain(
    62              _claimableInvite,
    63              bytes(name),
    64              bytes(optimistInviter.EIP712_VERSION()),
    65              block.chainid,
    66              address(optimistInviter)
    67          );
    68      }
    69  
    70      /// @notice Computes the EIP712 digest with the given domain parameters.
    71      ///         Used for testing that different domain parameters fail.
    72      /// @param _claimableInvite   ClaimableInvite struct to hash.
    73      /// @param _name              Contract name to use in the EIP712 domain.
    74      /// @param _version           Contract version to use in the EIP712 domain.
    75      /// @param _chainid           Chain ID to use in the EIP712 domain.
    76      /// @param _verifyingContract Address to use in the EIP712 domain.
    77      /// @return EIP-712 compatible digest.
    78      function getDigestWithEIP712Domain(
    79          OptimistInviter.ClaimableInvite calldata _claimableInvite,
    80          bytes memory _name,
    81          bytes memory _version,
    82          uint256 _chainid,
    83          address _verifyingContract
    84      )
    85          public
    86          pure
    87          returns (bytes32)
    88      {
    89          bytes32 domainSeparator = keccak256(
    90              abi.encode(EIP712_DOMAIN_TYPEHASH, keccak256(_name), keccak256(_version), _chainid, _verifyingContract)
    91          );
    92          return ECDSA.toTypedDataHash(domainSeparator, getClaimableInviteStructHash(_claimableInvite));
    93      }
    94  }