FaucetHelper.sol 3.01 KB
Newer Older
tre's avatar
tre committed
1 2 3
//SPDX-License-Identifier: MIT
pragma solidity 0.8.15;

4 5 6 7 8 9
import {
    AdminFaucetAuthModule
} from "../../universal/faucet/authmodules/AdminFaucetAuthModule.sol";
import {
    ECDSAUpgradeable
} from "@openzeppelin/contracts-upgradeable/utils/cryptography/ECDSAUpgradeable.sol";
tre's avatar
tre committed
10 11 12 13 14 15

/**
 * Simple helper contract that helps with testing the Faucet contract.
 */
contract FaucetHelper {
    /**
tre's avatar
tre committed
16
     * @notice EIP712 typehash for the Proof type.
tre's avatar
tre committed
17 18
     */
    bytes32 public constant PROOF_TYPEHASH =
19
        keccak256("Proof(address recipient,bytes32 nonce,bytes32 id)");
tre's avatar
tre committed
20 21 22 23 24 25 26 27 28 29

    /**
     * @notice EIP712 typehash for the EIP712Domain type that is included as part of the signature.
     */
    bytes32 public constant EIP712_DOMAIN_TYPEHASH =
        keccak256(
            "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
        );

    /**
tre's avatar
tre committed
30
     * @notice Keeps track of current nonce to generate new nonces for each drip.
tre's avatar
tre committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44
     */
    uint256 public currentNonce;

    /**
     * @notice Returns a bytes32 nonce that should change everytime. In practice, people should use
     *         pseudorandom nonces.
     *
     * @return Nonce that should be used as part of drip parameters.
     */
    function consumeNonce() public returns (bytes32) {
        return bytes32(keccak256(abi.encode(currentNonce++)));
    }

    /**
tre's avatar
tre committed
45
     * @notice Returns the hash of the struct Proof.
tre's avatar
tre committed
46
     *
tre's avatar
tre committed
47
     * @param _proof Proof struct to hash.
tre's avatar
tre committed
48 49 50
     *
     * @return EIP-712 typed struct hash.
     */
tre's avatar
tre committed
51 52 53 54 55
    function getProofStructHash(AdminFaucetAuthModule.Proof memory _proof)
        public
        pure
        returns (bytes32)
    {
56
        return keccak256(abi.encode(PROOF_TYPEHASH, _proof.recipient, _proof.nonce, _proof.id));
tre's avatar
tre committed
57 58
    }

59
    /**
tre's avatar
tre committed
60 61 62
     * @notice Computes the EIP712 digest with the given domain parameters.
     *         Used for testing that different domain parameters fail.
     *
tre's avatar
tre committed
63
     * @param _proof             Proof struct to hash.
tre's avatar
tre committed
64 65 66 67 68 69 70 71 72 73
     * @param _name              Contract name to use in the EIP712 domain.
     * @param _version           Contract version to use in the EIP712 domain.
     * @param _chainid           Chain ID to use in the EIP712 domain.
     * @param _verifyingContract Address to use in the EIP712 domain.
     * @param _verifyingContract Address to use in the EIP712 domain.
     * @param _verifyingContract Address to use in the EIP712 domain.
     *
     * @return EIP-712 compatible digest.
     */
    function getDigestWithEIP712Domain(
74
        AdminFaucetAuthModule.Proof memory _proof,
tre's avatar
tre committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88
        bytes memory _name,
        bytes memory _version,
        uint256 _chainid,
        address _verifyingContract
    ) public pure returns (bytes32) {
        bytes32 domainSeparator = keccak256(
            abi.encode(
                EIP712_DOMAIN_TYPEHASH,
                keccak256(_name),
                keccak256(_version),
                _chainid,
                _verifyingContract
            )
        );
89
        return ECDSAUpgradeable.toTypedDataHash(domainSeparator, getProofStructHash(_proof));
tre's avatar
tre committed
90 91
    }
}