Skip to content

Commit 78d5ae1

Browse files
0xClandestineypatil12davidironblocks
committed
fix: SignatureUtils construction (#990)
* fix: integration test initialization params (#978) * fix: initialization params * fix: roll blocks usage * fix: `SignatureUtils` construction --------- Co-authored-by: Yash Patil <[email protected]> Co-authored-by: davidironblocks <[email protected]>
1 parent 975b078 commit 78d5ae1

File tree

2 files changed

+86
-8
lines changed

2 files changed

+86
-8
lines changed

src/contracts/mixins/SignatureUtils.sol

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,21 @@ abstract contract SignatureUtils is ISignatureUtils {
4040
* for more detailed information please read EIP-712.
4141
* @dev Use `_calculateDomainSeparator` rather than using this function.
4242
*/
43-
function domainSeparator() external view virtual returns (bytes32) {
44-
return _calculateDomainSeparator();
43+
function domainSeparator() public view virtual returns (bytes32) {
44+
/// forgefmt: disable-next-item
45+
return block.chainid == _INITIAL_CHAIN_ID
46+
// If the chain ID is the same, return the original domain separator.
47+
? _INITIAL_DOMAIN_SEPARATOR
48+
// If the chain ID is different, return the new domain separator.
49+
: _calculateDomainSeparator();
4550
}
4651

4752
/// INTERNAL HELPERS
4853

49-
/// @dev Helper for calculating the contract's current domain separator.
54+
/// @dev Helper for calculating the contract's domain separator.
5055
function _calculateDomainSeparator() internal view returns (bytes32) {
5156
/// forgefmt: disable-next-item
52-
return block.chainid == _INITIAL_CHAIN_ID ?
53-
// If the chain ID is the same, return the original domain separator.
54-
_INITIAL_DOMAIN_SEPARATOR :
55-
// If the chain ID is different, return the new domain separator.
57+
return
5658
keccak256(
5759
abi.encode(
5860
EIP712_DOMAIN_TYPEHASH,
@@ -67,7 +69,7 @@ abstract contract SignatureUtils is ISignatureUtils {
6769
function _calculateSignableDigest(
6870
bytes32 hash
6971
) internal view returns (bytes32) {
70-
return keccak256(abi.encodePacked("\x19\x01", _calculateDomainSeparator(), hash));
72+
return keccak256(abi.encodePacked("\x19\x01", domainSeparator(), hash));
7173
}
7274

7375
/// @dev Helper for checking if a signature is valid, reverts if not valid.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// SPDX-License-Identifier: BUSL-1.1
2+
pragma solidity ^0.8.27;
3+
4+
import "forge-std/Test.sol";
5+
import "src/contracts/mixins/SignatureUtils.sol";
6+
7+
contract MockSigner {
8+
mapping(bytes32 => mapping(bytes => bool)) public validSignatures;
9+
10+
function setValidSignature(bytes32 digest, bytes memory signature, bool valid) public {
11+
validSignatures[digest][signature] = valid;
12+
}
13+
14+
function isValidSignatureNow(bytes32 digest, bytes memory signature) public view returns (bool) {
15+
return validSignatures[digest][signature];
16+
}
17+
}
18+
19+
contract SignatureUtilsUnit is Test, SignatureUtils {
20+
uint256 signerPk;
21+
address signer;
22+
bytes32 hash;
23+
bytes32 digest;
24+
bytes32 expectedDomainSeparator;
25+
26+
function setUp() public {
27+
vm.chainId(1);
28+
29+
signerPk = 1;
30+
signer = vm.addr(signerPk);
31+
32+
hash = keccak256("");
33+
digest = _calculateSignableDigest(hash);
34+
35+
expectedDomainSeparator = keccak256(
36+
abi.encode(
37+
keccak256("EIP712Domain(string name,uint256 chainId,address verifyingContract)"),
38+
keccak256(bytes("EigenLayer")),
39+
block.chainid,
40+
address(this)
41+
)
42+
);
43+
}
44+
45+
function test_domainSeparator_NonZero() public {
46+
assertTrue(_INITIAL_DOMAIN_SEPARATOR != 0, "The initial domain separator should be non-zero");
47+
assertTrue(domainSeparator() != 0, "The domain separator should be non-zero");
48+
assertTrue(domainSeparator() == expectedDomainSeparator, "The domain separator should be as expected");
49+
}
50+
51+
function test_domainSeparator_NewChainId() public {
52+
bytes32 initialDomainSeparator = domainSeparator();
53+
54+
// Change the chain ID
55+
vm.chainId(9999);
56+
57+
bytes32 newDomainSeparator = domainSeparator();
58+
59+
assertTrue(newDomainSeparator != 0, "The new domain separator should be non-zero");
60+
assertTrue(
61+
initialDomainSeparator != newDomainSeparator,
62+
"The domain separator should change when the chain ID changes"
63+
);
64+
}
65+
66+
function test_checkIsValidSignatureNow_Expired() public {
67+
(uint8 v, bytes32 r, bytes32 s) = vm.sign(signerPk, digest);
68+
69+
vm.expectRevert(ISignatureUtils.SignatureExpired.selector);
70+
_checkIsValidSignatureNow(signer, digest, abi.encode(r, s, v), block.timestamp - 1);
71+
}
72+
73+
function testFail_checkIsValidSignatureNow_InvalidSignature() public {
74+
_checkIsValidSignatureNow(signer, digest, "", block.timestamp);
75+
}
76+
}

0 commit comments

Comments
 (0)