Skip to content

Commit bd81a40

Browse files
committed
fix : verify EIP712 digests in GSM tests
1 parent be73dce commit bd81a40

File tree

2 files changed

+216
-48
lines changed

2 files changed

+216
-48
lines changed

tests/unit/TestGhoBase.t.sol

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,4 +750,116 @@ contract TestGhoBase is Test, Constants, Events {
750750
)
751751
);
752752
}
753+
754+
function _getBuyAssetEIP712Digest(
755+
EIP712Types.BuyAssetWithSig memory params,
756+
uint256 chainId,
757+
address verifyingContract
758+
) internal view returns (bytes32) {
759+
(string memory types, string memory message) = _getBuyAssetEIP712Strings(params);
760+
return
761+
vm.eip712HashTypedData(
762+
string(
763+
abi.encodePacked(
764+
'{"types": {"EIP712Domain": [{"name": "name", "type": "string"},',
765+
'{"name": "version", "type": "string"},{"name": "chainId", "type": "uint256"},',
766+
'{"name": "verifyingContract", "type": "address"}],',
767+
types,
768+
'},',
769+
'"primaryType": "BuyAssetWithSig","domain": {"name": "GSM","version": "1","chainId": ',
770+
vm.toString(chainId),
771+
',"verifyingContract": "',
772+
vm.toString(verifyingContract),
773+
'"},',
774+
message,
775+
'}'
776+
)
777+
)
778+
);
779+
}
780+
781+
function _getSellAssetEIP712Digest(
782+
EIP712Types.SellAssetWithSig memory params,
783+
uint256 chainId,
784+
address verifyingContract
785+
) internal view returns (bytes32) {
786+
(string memory types, string memory message) = _getSellAssetEIP712Strings(params);
787+
return
788+
vm.eip712HashTypedData(
789+
string(
790+
abi.encodePacked(
791+
'{"types": {"EIP712Domain": [{"name": "name", "type": "string"},',
792+
'{"name": "version", "type": "string"},{"name": "chainId", "type": "uint256"},',
793+
'{"name": "verifyingContract", "type": "address"}],',
794+
types,
795+
'},',
796+
'"primaryType": "SellAssetWithSig","domain": {"name": "GSM","version": "1","chainId": ',
797+
vm.toString(chainId),
798+
',"verifyingContract": "',
799+
vm.toString(verifyingContract),
800+
'"},',
801+
message,
802+
'}'
803+
)
804+
)
805+
);
806+
}
807+
808+
function _getBuyAssetEIP712Strings(
809+
EIP712Types.BuyAssetWithSig memory params
810+
) internal view returns (string memory, string memory) {
811+
return (
812+
string(
813+
abi.encodePacked(
814+
'"BuyAssetWithSig": [{"name": "originator", "type": "address"},{"name": "minAmount", "type": "uint256"},',
815+
'{"name": "receiver", "type": "address"},{"name": "nonce", "type": "uint256"},',
816+
'{"name": "deadline", "type": "uint256"}]'
817+
)
818+
),
819+
string(
820+
abi.encodePacked(
821+
'"message": {"originator": "',
822+
vm.toString(params.originator),
823+
'","minAmount": ',
824+
vm.toString(params.minAmount),
825+
',"receiver": "',
826+
vm.toString(params.receiver),
827+
'","nonce": ',
828+
vm.toString(params.nonce),
829+
',"deadline": ',
830+
vm.toString(params.deadline),
831+
'}'
832+
)
833+
)
834+
);
835+
}
836+
837+
function _getSellAssetEIP712Strings(
838+
EIP712Types.SellAssetWithSig memory params
839+
) internal view returns (string memory, string memory) {
840+
return (
841+
string(
842+
abi.encodePacked(
843+
'"SellAssetWithSig": [{"name": "originator", "type": "address"},{"name": "maxAmount", "type": "uint256"},',
844+
'{"name": "receiver", "type": "address"},{"name": "nonce", "type": "uint256"},',
845+
'{"name": "deadline", "type": "uint256"}]'
846+
)
847+
),
848+
string(
849+
abi.encodePacked(
850+
'"message": {"originator": "',
851+
vm.toString(params.originator),
852+
'","maxAmount": ',
853+
vm.toString(params.maxAmount),
854+
',"receiver": "',
855+
vm.toString(params.receiver),
856+
'","nonce": ',
857+
vm.toString(params.nonce),
858+
',"deadline": ',
859+
vm.toString(params.deadline),
860+
'}'
861+
)
862+
)
863+
);
864+
}
753865
}

tests/unit/TestGsm.t.sol

Lines changed: 104 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,24 @@ contract TestGsm is TestGhoBase {
196196

197197
assertEq(GHO_GSM.nonces(gsmSignerAddr), 0, 'Unexpected before gsmSignerAddr nonce');
198198

199-
bytes32 digest = _getSellAssetTypedDataHash(
200-
EIP712Types.SellAssetWithSig({
199+
bytes memory signature;
200+
{
201+
EIP712Types.SellAssetWithSig memory params = EIP712Types.SellAssetWithSig({
201202
originator: gsmSignerAddr,
202203
maxAmount: DEFAULT_GSM_USDX_AMOUNT,
203204
receiver: gsmSignerAddr,
204205
nonce: GHO_GSM.nonces(gsmSignerAddr),
205206
deadline: deadline
206-
})
207-
);
208-
(uint8 v, bytes32 r, bytes32 s) = vm.sign(gsmSignerKey, digest);
209-
bytes memory signature = abi.encodePacked(r, s, v);
207+
});
208+
bytes32 digest = _getSellAssetTypedDataHash(params);
209+
assertEq(
210+
digest,
211+
_getSellAssetEIP712Digest(params, block.chainid, address(GHO_GSM)),
212+
'EIP712 digest not similar'
213+
);
214+
(uint8 v, bytes32 r, bytes32 s) = vm.sign(gsmSignerKey, digest);
215+
signature = abi.encodePacked(r, s, v);
216+
}
210217

211218
assertTrue(gsmSignerAddr != ALICE, 'Signer is the same as Alice');
212219

@@ -249,17 +256,24 @@ contract TestGsm is TestGhoBase {
249256

250257
assertEq(GHO_GSM.nonces(gsmSignerAddr), 0, 'Unexpected before gsmSignerAddr nonce');
251258

252-
bytes32 digest = _getSellAssetTypedDataHash(
253-
EIP712Types.SellAssetWithSig({
259+
bytes memory signature;
260+
{
261+
EIP712Types.SellAssetWithSig memory params = EIP712Types.SellAssetWithSig({
254262
originator: gsmSignerAddr,
255263
maxAmount: DEFAULT_GSM_USDX_AMOUNT,
256264
receiver: gsmSignerAddr,
257265
nonce: GHO_GSM.nonces(gsmSignerAddr),
258266
deadline: deadline
259-
})
260-
);
261-
(uint8 v, bytes32 r, bytes32 s) = vm.sign(gsmSignerKey, digest);
262-
bytes memory signature = abi.encodePacked(r, s, v);
267+
});
268+
bytes32 digest = _getSellAssetTypedDataHash(params);
269+
assertEq(
270+
digest,
271+
_getSellAssetEIP712Digest(params, block.chainid, address(GHO_GSM)),
272+
'EIP712 digest not similar'
273+
);
274+
(uint8 v, bytes32 r, bytes32 s) = vm.sign(gsmSignerKey, digest);
275+
signature = abi.encodePacked(r, s, v);
276+
}
263277

264278
assertTrue(gsmSignerAddr != ALICE, 'Signer is the same as Alice');
265279

@@ -291,17 +305,24 @@ contract TestGsm is TestGhoBase {
291305
function testRevertSellAssetWithSigExpiredSignature() public {
292306
uint256 deadline = block.timestamp - 1;
293307

294-
bytes32 digest = _getSellAssetTypedDataHash(
295-
EIP712Types.SellAssetWithSig({
308+
bytes memory signature;
309+
{
310+
EIP712Types.SellAssetWithSig memory params = EIP712Types.SellAssetWithSig({
296311
originator: gsmSignerAddr,
297312
maxAmount: DEFAULT_GSM_USDX_AMOUNT,
298313
receiver: gsmSignerAddr,
299314
nonce: GHO_GSM.nonces(gsmSignerAddr),
300315
deadline: deadline
301-
})
302-
);
303-
(uint8 v, bytes32 r, bytes32 s) = vm.sign(gsmSignerKey, digest);
304-
bytes memory signature = abi.encodePacked(r, s, v);
316+
});
317+
bytes32 digest = _getSellAssetTypedDataHash(params);
318+
assertEq(
319+
digest,
320+
_getSellAssetEIP712Digest(params, block.chainid, address(GHO_GSM)),
321+
'EIP712 digest not similar'
322+
);
323+
(uint8 v, bytes32 r, bytes32 s) = vm.sign(gsmSignerKey, digest);
324+
signature = abi.encodePacked(r, s, v);
325+
}
305326

306327
assertTrue(gsmSignerAddr != ALICE, 'Signer is the same as Alice');
307328

@@ -320,17 +341,24 @@ contract TestGsm is TestGhoBase {
320341
function testRevertSellAssetWithSigInvalidSignature() public {
321342
uint256 deadline = block.timestamp + 1 hours;
322343

323-
bytes32 digest = _getSellAssetTypedDataHash(
324-
EIP712Types.SellAssetWithSig({
344+
bytes memory signature;
345+
{
346+
EIP712Types.SellAssetWithSig memory params = EIP712Types.SellAssetWithSig({
325347
originator: gsmSignerAddr,
326348
maxAmount: DEFAULT_GSM_USDX_AMOUNT,
327349
receiver: gsmSignerAddr,
328350
nonce: GHO_GSM.nonces(gsmSignerAddr),
329351
deadline: deadline
330-
})
331-
);
332-
(uint8 v, bytes32 r, bytes32 s) = vm.sign(gsmSignerKey, digest);
333-
bytes memory signature = abi.encodePacked(r, s, v);
352+
});
353+
bytes32 digest = _getSellAssetTypedDataHash(params);
354+
assertEq(
355+
digest,
356+
_getSellAssetEIP712Digest(params, block.chainid, address(GHO_GSM)),
357+
'EIP712 digest not similar'
358+
);
359+
(uint8 v, bytes32 r, bytes32 s) = vm.sign(gsmSignerKey, digest);
360+
signature = abi.encodePacked(r, s, v);
361+
}
334362

335363
assertTrue(gsmSignerAddr != ALICE, 'Signer is the same as Alice');
336364

@@ -597,17 +625,24 @@ contract TestGsm is TestGhoBase {
597625

598626
assertEq(GHO_GSM.nonces(gsmSignerAddr), 0, 'Unexpected before gsmSignerAddr nonce');
599627

600-
bytes32 digest = _getBuyAssetTypedDataHash(
601-
EIP712Types.BuyAssetWithSig({
628+
bytes memory signature;
629+
{
630+
EIP712Types.BuyAssetWithSig memory params = EIP712Types.BuyAssetWithSig({
602631
originator: gsmSignerAddr,
603632
minAmount: DEFAULT_GSM_USDX_AMOUNT,
604633
receiver: gsmSignerAddr,
605634
nonce: GHO_GSM.nonces(gsmSignerAddr),
606635
deadline: deadline
607-
})
608-
);
609-
(uint8 v, bytes32 r, bytes32 s) = vm.sign(gsmSignerKey, digest);
610-
bytes memory signature = abi.encodePacked(r, s, v);
636+
});
637+
bytes32 digest = _getBuyAssetTypedDataHash(params);
638+
assertEq(
639+
digest,
640+
_getBuyAssetEIP712Digest(params, block.chainid, address(GHO_GSM)),
641+
'EIP712 digest not similar'
642+
);
643+
(uint8 v, bytes32 r, bytes32 s) = vm.sign(gsmSignerKey, digest);
644+
signature = abi.encodePacked(r, s, v);
645+
}
611646

612647
assertTrue(gsmSignerAddr != BOB, 'Signer is the same as Bob');
613648

@@ -665,17 +700,24 @@ contract TestGsm is TestGhoBase {
665700

666701
assertEq(GHO_GSM.nonces(gsmSignerAddr), 0, 'Unexpected before gsmSignerAddr nonce');
667702

668-
bytes32 digest = _getBuyAssetTypedDataHash(
669-
EIP712Types.BuyAssetWithSig({
703+
bytes memory signature;
704+
{
705+
EIP712Types.BuyAssetWithSig memory params = EIP712Types.BuyAssetWithSig({
670706
originator: gsmSignerAddr,
671707
minAmount: DEFAULT_GSM_USDX_AMOUNT,
672708
receiver: gsmSignerAddr,
673709
nonce: GHO_GSM.nonces(gsmSignerAddr),
674710
deadline: deadline
675-
})
676-
);
677-
(uint8 v, bytes32 r, bytes32 s) = vm.sign(gsmSignerKey, digest);
678-
bytes memory signature = abi.encodePacked(r, s, v);
711+
});
712+
bytes32 digest = _getBuyAssetTypedDataHash(params);
713+
assertEq(
714+
digest,
715+
_getBuyAssetEIP712Digest(params, block.chainid, address(GHO_GSM)),
716+
'EIP712 digest not similar'
717+
);
718+
(uint8 v, bytes32 r, bytes32 s) = vm.sign(gsmSignerKey, digest);
719+
signature = abi.encodePacked(r, s, v);
720+
}
679721

680722
assertTrue(gsmSignerAddr != BOB, 'Signer is the same as Bob');
681723

@@ -758,17 +800,24 @@ contract TestGsm is TestGhoBase {
758800
function testRevertBuyAssetWithSigExpiredSignature() public {
759801
uint256 deadline = block.timestamp - 1;
760802

761-
bytes32 digest = _getBuyAssetTypedDataHash(
762-
EIP712Types.BuyAssetWithSig({
803+
bytes memory signature;
804+
{
805+
EIP712Types.BuyAssetWithSig memory params = EIP712Types.BuyAssetWithSig({
763806
originator: gsmSignerAddr,
764807
minAmount: DEFAULT_GSM_USDX_AMOUNT,
765808
receiver: gsmSignerAddr,
766809
nonce: GHO_GSM.nonces(gsmSignerAddr),
767810
deadline: deadline
768-
})
769-
);
770-
(uint8 v, bytes32 r, bytes32 s) = vm.sign(gsmSignerKey, digest);
771-
bytes memory signature = abi.encodePacked(r, s, v);
811+
});
812+
bytes32 digest = _getBuyAssetTypedDataHash(params);
813+
assertEq(
814+
digest,
815+
_getBuyAssetEIP712Digest(params, block.chainid, address(GHO_GSM)),
816+
'EIP712 digest not similar'
817+
);
818+
(uint8 v, bytes32 r, bytes32 s) = vm.sign(gsmSignerKey, digest);
819+
signature = abi.encodePacked(r, s, v);
820+
}
772821

773822
assertTrue(gsmSignerAddr != BOB, 'Signer is the same as Bob');
774823

@@ -786,17 +835,24 @@ contract TestGsm is TestGhoBase {
786835
function testRevertBuyAssetWithSigInvalidSignature() public {
787836
uint256 deadline = block.timestamp + 1 hours;
788837

789-
bytes32 digest = _getBuyAssetTypedDataHash(
790-
EIP712Types.BuyAssetWithSig({
838+
bytes memory signature;
839+
{
840+
EIP712Types.BuyAssetWithSig memory params = EIP712Types.BuyAssetWithSig({
791841
originator: gsmSignerAddr,
792842
minAmount: DEFAULT_GSM_USDX_AMOUNT,
793843
receiver: gsmSignerAddr,
794844
nonce: GHO_GSM.nonces(gsmSignerAddr),
795845
deadline: deadline
796-
})
797-
);
798-
(uint8 v, bytes32 r, bytes32 s) = vm.sign(gsmSignerKey, digest);
799-
bytes memory signature = abi.encodePacked(r, s, v);
846+
});
847+
bytes32 digest = _getBuyAssetTypedDataHash(params);
848+
assertEq(
849+
digest,
850+
_getBuyAssetEIP712Digest(params, block.chainid, address(GHO_GSM)),
851+
'EIP712 digest not similar'
852+
);
853+
(uint8 v, bytes32 r, bytes32 s) = vm.sign(gsmSignerKey, digest);
854+
signature = abi.encodePacked(r, s, v);
855+
}
800856

801857
assertTrue(gsmSignerAddr != BOB, 'Signer is the same as Bob');
802858

0 commit comments

Comments
 (0)