AdminFaucetAuthModule.sol 2.1 KB
Newer Older
1 2 3
// SPDX-License-Identifier: MIT
pragma solidity 0.8.15;

4
// Contracts
tre's avatar
tre committed
5
import { EIP712 } from "@openzeppelin/contracts/utils/cryptography/draft-EIP712.sol";
6
import { SignatureChecker } from "@openzeppelin/contracts/utils/cryptography/SignatureChecker.sol";
7 8 9 10

// Interfaces
import { IFaucetAuthModule } from "src/periphery/faucet/authmodules/IFaucetAuthModule.sol";
import { Faucet } from "src/periphery/faucet/Faucet.sol";
11

12 13 14
/// @title  AdminFaucetAuthModule
/// @notice FaucetAuthModule that allows an admin to sign off on a given faucet drip. Takes an admin
///         as the constructor argument.
tre's avatar
tre committed
15
contract AdminFaucetAuthModule is IFaucetAuthModule, EIP712 {
16
    /// @notice Admin address that can sign off on drips.
17 18
    address public immutable ADMIN;

19
    /// @notice EIP712 typehash for the Proof type.
20
    bytes32 public immutable PROOF_TYPEHASH = keccak256("Proof(address recipient,bytes32 nonce,bytes32 id)");
21

22 23 24 25
    /// @notice Struct that represents a proof that verifies the admin.
    /// @custom:field recipient Address that will be receiving the faucet funds.
    /// @custom:field nonce     Pseudorandom nonce to prevent replay attacks.
    /// @custom:field id        id for the user requesting the faucet funds.
26 27 28
    struct Proof {
        address recipient;
        bytes32 nonce;
29
        bytes32 id;
30 31
    }

32 33 34
    /// @param _admin   Admin address that can sign off on drips.
    /// @param _name    Contract name.
    /// @param _version The current major version of the signing domain.
35
    constructor(address _admin, string memory _name, string memory _version) EIP712(_name, _version) {
tre's avatar
tre committed
36
        ADMIN = _admin;
37 38
    }

39
    /// @inheritdoc IFaucetAuthModule
40 41
    function verify(
        Faucet.DripParameters memory _params,
42
        bytes32 _id,
43
        bytes memory _proof
44 45 46 47 48
    )
        external
        view
        returns (bool valid_)
    {
49
        // Generate a EIP712 typed data hash to compare against the proof.
50 51
        valid_ = SignatureChecker.isValidSignatureNow(
            ADMIN,
52
            _hashTypedDataV4(keccak256(abi.encode(PROOF_TYPEHASH, _params.recipient, _params.nonce, _id))),
53 54
            _proof
        );
55 56
    }
}