github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/test/mocks/FaucetHelper.sol (about) 1 // SPDX-License-Identifier: MIT 2 pragma solidity ^0.8.0; 3 4 import { ECDSAUpgradeable } from "@openzeppelin/contracts-upgradeable/utils/cryptography/ECDSAUpgradeable.sol"; 5 import { AdminFaucetAuthModule } from "src/periphery/faucet/authmodules/AdminFaucetAuthModule.sol"; 6 7 /// @notice Simple helper contract that helps with testing the Faucet contract. 8 contract FaucetHelper { 9 /// @notice EIP712 typehash for the Proof type. 10 bytes32 public constant PROOF_TYPEHASH = keccak256("Proof(address recipient,bytes32 nonce,bytes32 id)"); 11 12 /// @notice EIP712 typehash for the EIP712Domain type that is included as part of the signature. 13 bytes32 public constant EIP712_DOMAIN_TYPEHASH = 14 keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"); 15 16 /// @notice Keeps track of current nonce to generate new nonces for each drip. 17 uint256 public currentNonce; 18 19 /// @notice Returns a bytes32 nonce that should change everytime. In practice, people should use 20 /// pseudorandom nonces. 21 /// @return Nonce that should be used as part of drip parameters. 22 function consumeNonce() public returns (bytes32) { 23 return bytes32(keccak256(abi.encode(currentNonce++))); 24 } 25 26 /// @notice Returns the hash of the struct Proof. 27 /// @param _proof Proof struct to hash. 28 /// @return EIP-712 typed struct hash. 29 function getProofStructHash(AdminFaucetAuthModule.Proof memory _proof) public pure returns (bytes32) { 30 return keccak256(abi.encode(PROOF_TYPEHASH, _proof.recipient, _proof.nonce, _proof.id)); 31 } 32 33 /// @notice Computes the EIP712 digest with the given domain parameters. 34 /// Used for testing that different domain parameters fail. 35 /// @param _proof Proof struct to hash. 36 /// @param _name Contract name to use in the EIP712 domain. 37 /// @param _version Contract version to use in the EIP712 domain. 38 /// @param _chainid Chain ID to use in the EIP712 domain. 39 /// @param _verifyingContract Address to use in the EIP712 domain. 40 /// @param _verifyingContract Address to use in the EIP712 domain. 41 /// @param _verifyingContract Address to use in the EIP712 domain. 42 /// @return EIP-712 compatible digest. 43 function getDigestWithEIP712Domain( 44 AdminFaucetAuthModule.Proof memory _proof, 45 bytes memory _name, 46 bytes memory _version, 47 uint256 _chainid, 48 address _verifyingContract 49 ) 50 public 51 pure 52 returns (bytes32) 53 { 54 bytes32 domainSeparator = keccak256( 55 abi.encode(EIP712_DOMAIN_TYPEHASH, keccak256(_name), keccak256(_version), _chainid, _verifyingContract) 56 ); 57 return ECDSAUpgradeable.toTypedDataHash(domainSeparator, getProofStructHash(_proof)); 58 } 59 }