/ test / PermitSigUtils.sol
PermitSigUtils.sol
 1  // SPDX-License-Identifier: MIT
 2  pragma solidity ^0.8.20;
 3  
 4  contract PermitSigUtils {
 5    bytes32 internal DOMAIN_SEPARATOR;
 6  
 7    constructor(bytes32 _DOMAIN_SEPARATOR) {
 8        DOMAIN_SEPARATOR = _DOMAIN_SEPARATOR;
 9    }
10  
11    // keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
12    bytes32 public constant PERMIT_TYPEHASH =
13        0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9;
14  
15    struct Permit {
16        address owner;
17        address spender;
18        uint256 value;
19        uint256 nonce;
20        uint256 deadline;
21    }
22  
23    // computes the hash of a permit
24    function getStructHash(Permit memory _permit)
25        internal
26        pure
27        returns (bytes32)
28    {
29        return
30            keccak256(
31                abi.encode(
32                    PERMIT_TYPEHASH,
33                    _permit.owner,
34                    _permit.spender,
35                    _permit.value,
36                    _permit.nonce,
37                    _permit.deadline
38                )
39            );
40    }
41  
42    // computes the hash of the fully encoded EIP-712 message for the domain, which can be used to recover the signer
43    function getTypedDataHash(Permit memory _permit)
44        public
45        view
46        returns (bytes32)
47    {
48        return
49            keccak256(
50                abi.encodePacked(
51                    "\x19\x01",
52                    DOMAIN_SEPARATOR,
53                    getStructHash(_permit)
54                )
55            );
56    }
57  }
58