61 lines
1.7 KiB
Solidity
61 lines
1.7 KiB
Solidity
// SPDX-License-Identifier: MIT
|
|
pragma solidity =0.8.20;
|
|
|
|
contract SigUtils {
|
|
bytes32 internal immutable DOMAIN_SEPARATOR;
|
|
|
|
constructor(bytes32 domainSeparator) {
|
|
DOMAIN_SEPARATOR = domainSeparator;
|
|
}
|
|
|
|
// keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
|
|
// forge-lint: disable-next-line(mixed-case-function)
|
|
bytes32 public constant PERMIT_TYPEHASH =
|
|
0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9;
|
|
|
|
struct Permit {
|
|
address owner;
|
|
address spender;
|
|
uint256 value;
|
|
uint256 nonce;
|
|
uint256 deadline;
|
|
}
|
|
|
|
// computes the hash of a permit
|
|
function getStructHash(Permit memory _permit)
|
|
internal
|
|
pure
|
|
returns (bytes32)
|
|
{
|
|
// forge-lint: disable-next-item(asm-keccak256)
|
|
return
|
|
keccak256(
|
|
abi.encode(
|
|
PERMIT_TYPEHASH,
|
|
_permit.owner,
|
|
_permit.spender,
|
|
_permit.value,
|
|
_permit.nonce,
|
|
_permit.deadline
|
|
)
|
|
);
|
|
}
|
|
|
|
// computes the hash of the fully encoded EIP-712 message for the domain, which can be used to recover the signer
|
|
function getTypedDataHash(Permit memory _permit)
|
|
public
|
|
view
|
|
returns (bytes32)
|
|
{
|
|
// forge-lint: disable-next-item(asm-keccak256)
|
|
return
|
|
keccak256(
|
|
abi.encodePacked(
|
|
"\x19\x01",
|
|
DOMAIN_SEPARATOR,
|
|
getStructHash(_permit)
|
|
)
|
|
);
|
|
}
|
|
}
|