Created
February 5, 2025 19:04
-
-
Save Falilah/ff8942358540bcd98ab9c5e5644e256c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.20; | |
contract TWASigUtils { | |
bytes32 internal DOMAIN_SEPARATOR; | |
constructor(bytes32 _DOMAIN_SEPARATOR) { | |
DOMAIN_SEPARATOR = _DOMAIN_SEPARATOR; | |
} | |
// keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); | |
bytes32 public constant PERMIT_TYPEHASH = | |
0x72a9ba9d768e35b4ab8761b0abf8d49a4318cec0209bdc7ba8ac6136bf16ec8f; | |
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) | |
{ | |
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) | |
{ | |
return | |
keccak256( | |
abi.encodePacked( | |
"\x19\x01", | |
DOMAIN_SEPARATOR, | |
getStructHash(_permit) | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment