From 0826c12c23f69c19b234f7247df565e124e449c1 Mon Sep 17 00:00:00 2001 From: saucepoint Date: Sat, 3 Aug 2024 22:15:52 -0400 Subject: [PATCH] test to validate assembly --- test/EIP712.t.sol | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 test/EIP712.t.sol diff --git a/test/EIP712.t.sol b/test/EIP712.t.sol new file mode 100644 index 00000000..badf6c9c --- /dev/null +++ b/test/EIP712.t.sol @@ -0,0 +1,47 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.24; + +import "forge-std/Test.sol"; + +import {EIP712} from "../src/base/EIP712.sol"; + +contract EIP712Test is EIP712, Test { + constructor() EIP712("EIP712Test") {} + + function setUp() public {} + + function test_domainSeparator() public view { + assertEq( + DOMAIN_SEPARATOR(), + keccak256( + abi.encode( + keccak256("EIP712Domain(string name,uint256 chainId,address verifyingContract)"), + keccak256(bytes("EIP712Test")), + block.chainid, + address(this) + ) + ) + ); + } + + function test_hashTypedData() public view { + bytes32 dataHash = keccak256(abi.encodePacked("data")); + assertEq(_hashTypedData(dataHash), keccak256(abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR(), dataHash))); + } + + function test_rebuildDomainSeparator() public { + uint256 chainId = 4444; + vm.chainId(chainId); + assertEq( + DOMAIN_SEPARATOR(), + keccak256( + abi.encode( + keccak256("EIP712Domain(string name,uint256 chainId,address verifyingContract)"), + keccak256(bytes("EIP712Test")), + chainId, + address(this) + ) + ) + ); + } +}