Skip to content

Commit 1ecea74

Browse files
committed
fix : revert back to simpler tests for EIP712 digests
1 parent ea0a8b8 commit 1ecea74

File tree

2 files changed

+48
-216
lines changed

2 files changed

+48
-216
lines changed

tests/unit/TestGhoBase.t.sol

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -750,116 +750,4 @@ 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-
}
865753
}

tests/unit/TestGsm.t.sol

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

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

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

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

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

257250
assertEq(GHO_GSM.nonces(gsmSignerAddr), 0, 'Unexpected before gsmSignerAddr nonce');
258251

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

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

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

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

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

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

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

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

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

626598
assertEq(GHO_GSM.nonces(gsmSignerAddr), 0, 'Unexpected before gsmSignerAddr nonce');
627599

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

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

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

701666
assertEq(GHO_GSM.nonces(gsmSignerAddr), 0, 'Unexpected before gsmSignerAddr nonce');
702667

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)