all files / contracts/libraries/ VerifySignature.sol

100% Statements 3/3
100% Branches 0/0
100% Functions 1/1
100% Lines 4/4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32                                      31× 31×     31×         31×      
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
 
import '@openzeppelin/contracts/utils/cryptography/ECDSA.sol';
 
// TODO: update doc
/// @title VerifySignature
contract VerifySignature {
    /// @dev Verify if the signer has a valid signature as claimed
    /// @param signer The address of attester who wants to perform an action
    /// @param epochLength The epoch length of attester
    /// @param signature The signature signed by the attester
    /// @return isValid True if the signature is valid, false otherwise
    function isValidSignature(
        address signer,
        uint256 epochLength,
        bytes memory signature
    ) internal view returns (bool) {
        // Attester signs over it's own address concatenated with this contract address
        uint256 chainId;
        assembly {
            chainId := chainid()
        }
        bytes32 messageHash = ECDSA.toEthSignedMessageHash(
            keccak256(
                abi.encodePacked(address(this), signer, epochLength, chainId)
            )
        );
        return ECDSA.recover(messageHash, signature) == signer;
    }
}