-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathOpenfortPaymasterV2Test.t.sol
1685 lines (1383 loc) · 72.2 KB
/
OpenfortPaymasterV2Test.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-3.0
pragma solidity =0.8.19;
import {console} from "lib/forge-std/src/Test.sol";
import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import {EntryPoint, UserOperation, IEntryPoint} from "account-abstraction/core/EntryPoint.sol";
import {TestCounter} from "account-abstraction/test/TestCounter.sol";
import {IPaymaster} from "account-abstraction/interfaces/IPaymaster.sol";
import {MockERC20} from "contracts/mock/MockERC20.sol";
import {UpgradeableOpenfortFactory} from "contracts/core/upgradeable/UpgradeableOpenfortFactory.sol";
import {UpgradeableOpenfortAccount} from "contracts/core/upgradeable/UpgradeableOpenfortAccount.sol";
import {OpenfortPaymasterV2} from "contracts/paymaster/OpenfortPaymasterV2.sol";
import {OpenfortBaseTest} from "../core/OpenfortBaseTest.t.sol";
import {OpenfortErrorsAndEvents} from "contracts/interfaces/OpenfortErrorsAndEvents.sol";
import {UpgradeableOpenfortDeploy} from "script/deployUpgradeableAccounts.s.sol";
contract OpenfortPaymasterV2Test is OpenfortBaseTest {
using ECDSA for bytes32;
OpenfortPaymasterV2 public openfortPaymaster;
address private paymasterAdmin;
uint256 private paymasterAdminPKey;
UpgradeableOpenfortAccount public upgradeableOpenfortAccountImpl;
UpgradeableOpenfortFactory public openfortFactory;
uint48 internal constant VALIDUNTIL = 2 ** 48 - 1;
uint48 internal constant VALIDAFTER = 0;
uint256 internal constant EXCHANGERATE = 10 ** 3;
uint256 internal constant MOCKSIG = 2 ** 256 - 1;
uint256 internal TESTTOKEN_ACCOUNT_PREFUND = 100 * 10 ** 18;
error InvalidTokenRecipient();
event PostOpGasUpdated(uint256 oldPostOpGas, uint256 _newPostOpGas);
/*
* Auxiliary function to generate a userOP
*/
function _setupUserOp(
address sender,
uint256 _signerPKey,
bytes memory _initCode,
bytes memory _callDataForEntrypoint,
bytes memory paymasterAndData
) internal returns (UserOperation[] memory ops) {
// Get user op fields
UserOperation memory op = UserOperation({
sender: sender,
nonce: entryPoint.getNonce(sender, 0),
initCode: _initCode,
callData: _callDataForEntrypoint,
callGasLimit: 500_000,
verificationGasLimit: 500_000,
preVerificationGas: 500_000,
maxFeePerGas: 1500000000,
maxPriorityFeePerGas: 1500000000,
paymasterAndData: paymasterAndData,
signature: bytes("")
});
// Sign UserOp
bytes32 opHash = entryPoint.getUserOpHash(op);
bytes32 msgHash = ECDSA.toEthSignedMessageHash(opHash);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(_signerPKey, msgHash);
bytes memory userOpSignature = abi.encodePacked(r, s, v);
// Verifications below commented to avoid "Stack too deep" error
// address recoveredSigner = ECDSA.recover(msgHash, v, r, s);
// address expectedSigner = vm.addr(_signerPKey);
assertEq(ECDSA.recover(msgHash, v, r, s), vm.addr(_signerPKey));
op.signature = userOpSignature;
// Store UserOp
ops = new UserOperation[](1);
ops[0] = op;
}
/*
* Auxiliary function to generate a userOP using the execute()
* from the account
*/
function _setupUserOpExecute(
address sender,
uint256 _signerPKey,
bytes memory _initCode,
address _target,
uint256 _value,
bytes memory _callData,
bytes memory paymasterAndData
) internal returns (UserOperation[] memory) {
bytes memory callDataForEntrypoint =
abi.encodeWithSignature("execute(address,uint256,bytes)", _target, _value, _callData);
return _setupUserOp(sender, _signerPKey, _initCode, callDataForEntrypoint, paymasterAndData);
}
/*
* Auxiliary function to generate a userOP using the executeBatch()
* from the account
*/
function _setupUserOpExecuteBatch(
address sender,
uint256 _signerPKey,
bytes memory _initCode,
address[] memory _target,
uint256[] memory _value,
bytes[] memory _callData,
bytes memory paymasterAndData
) internal returns (UserOperation[] memory) {
bytes memory callDataForEntrypoint =
abi.encodeWithSignature("executeBatch(address[],uint256[],bytes[])", _target, _value, _callData);
return _setupUserOp(sender, _signerPKey, _initCode, callDataForEntrypoint, paymasterAndData);
}
function mockPaymasterDataNative(address _depositor) internal pure returns (bytes memory dataEncoded) {
OpenfortPaymasterV2.PolicyStrategy memory strategy;
strategy.paymasterMode = OpenfortPaymasterV2.Mode.PayForUser;
strategy.depositor = _depositor;
strategy.erc20Token = address(0);
strategy.exchangeRate = 0;
// Looking at the source code, I've found this part was not Packed (filled with 0s)
dataEncoded = abi.encode(VALIDUNTIL, VALIDAFTER, strategy);
}
function mockPaymasterDataERC20Dynamic(address _depositor) internal view returns (bytes memory dataEncoded) {
OpenfortPaymasterV2.PolicyStrategy memory strategy;
strategy.paymasterMode = OpenfortPaymasterV2.Mode.DynamicRate;
strategy.depositor = _depositor;
strategy.erc20Token = address(mockERC20);
strategy.exchangeRate = EXCHANGERATE;
// Looking at the source code, I've found this part was not Packed (filled with 0s)
dataEncoded = abi.encode(VALIDUNTIL, VALIDAFTER, strategy);
}
function mockPaymasterDataERC20Fixed(address _depositor, uint256 _pricePerTransaction)
internal
view
returns (bytes memory dataEncoded)
{
OpenfortPaymasterV2.PolicyStrategy memory strategy;
strategy.paymasterMode = OpenfortPaymasterV2.Mode.FixedRate;
strategy.depositor = _depositor;
strategy.erc20Token = address(mockERC20);
strategy.exchangeRate = _pricePerTransaction;
// Looking at the source code, I've found this part was not Packed (filled with 0s)
dataEncoded = abi.encode(VALIDUNTIL, VALIDAFTER, strategy);
}
/**
* @notice Initialize the UpgradeableOpenfortAccount testing contract.
* Scenario:
* - openfortAdmin is the deployer (and owner) of the UpgradeableOpenfortFactory
* - paymasterAdmin is the deployer (and owner) of the OpenfortPaymaster
* - openfortAdmin is the account used to deploy new static accounts
* - entryPoint is the singleton EntryPoint
* - testCounter is the counter used to test userOps
*/
function setUp() public override {
super.setUp();
(paymasterAdmin, paymasterAdminPKey) = makeAddrAndKey("paymasterAdmin");
vm.deal(paymasterAdmin, 100 ether);
vm.prank(paymasterAdmin);
openfortPaymaster =
new OpenfortPaymasterV2{salt: versionSalt}(IEntryPoint(payable(address(entryPoint))), paymasterAdmin);
// Paymaster deposits 50 ETH to EntryPoint
vm.prank(paymasterAdmin);
openfortPaymaster.deposit{value: 50 ether}();
// Paymaster stakes 25 ETH
vm.prank(paymasterAdmin);
openfortPaymaster.addStake{value: 25 ether}(1);
// deploy account factory
UpgradeableOpenfortDeploy upgradeableOpenfortDeploy = new UpgradeableOpenfortDeploy();
(upgradeableOpenfortAccountImpl, openfortFactory) = upgradeableOpenfortDeploy.run();
// deploy a new TestCounter
testCounter = new TestCounter();
// mint 1000 mockERC20
mockERC20.mint(address(this), 1_000 * 10 ** 18);
// Create an Openfort account and get its address
vm.prank(openfortAdmin);
accountAddress = openfortFactory.createAccountWithNonce(openfortAdmin, "1", true);
}
/*
* Test initial parameters
*
*/
function testInitialParameters() public {
assertEq(address(openfortPaymaster.entryPoint()), vm.envAddress("ENTRY_POINT_ADDRESS"));
assertEq(address(openfortPaymaster.owner()), paymasterAdmin);
}
/**
* Deposit should fail if not the owner
*/
function testFailDeposit() public {
vm.prank(openfortAdmin);
openfortPaymaster.deposit{value: 50 ether}();
}
/*
* Test parsePaymasterAndData() when using the native token
*
*/
function testParsePaymasterDataNative() public {
// Encode the paymaster data
bytes memory dataEncoded = mockPaymasterDataNative(paymasterAdmin);
// Get the related paymaster data signature
bytes32 hash = keccak256(dataEncoded);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(paymasterAdminPKey, hash);
bytes memory signature = abi.encodePacked(r, s, v);
// Create the paymasterAndData info
bytes memory paymasterAndData = abi.encodePacked(address(openfortPaymaster), dataEncoded, signature);
(
uint48 returnedValidUntil,
uint48 returnedValidAfter,
OpenfortPaymasterV2.PolicyStrategy memory strategy,
bytes memory returnedSignature
) = openfortPaymaster.parsePaymasterAndData(paymasterAndData);
assertEq(returnedValidUntil, VALIDUNTIL);
assertEq(returnedValidAfter, VALIDAFTER);
assertEq(strategy.erc20Token, address(0));
assertEq(strategy.exchangeRate, 0);
assertEq(signature, returnedSignature);
}
/*
* Test parsePaymasterAndData() with an ERC20 dynamic
*
*/
function testParsePaymasterDataERC20() public {
// Encode the paymaster data
bytes memory dataEncoded = mockPaymasterDataERC20Dynamic(paymasterAdmin);
// Get the related paymaster data signature
bytes32 hash = keccak256(dataEncoded);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(paymasterAdminPKey, hash);
bytes memory signature = abi.encodePacked(r, s, v);
// Create the paymasterAndData info
bytes memory paymasterAndData = abi.encodePacked(address(openfortPaymaster), dataEncoded, signature);
console.logBytes(paymasterAndData);
(
uint48 returnedValidUntil,
uint48 returnedValidAfter,
OpenfortPaymasterV2.PolicyStrategy memory strategy,
bytes memory returnedSignature
) = openfortPaymaster.parsePaymasterAndData(paymasterAndData);
assertEq(returnedValidUntil, VALIDUNTIL);
assertEq(returnedValidAfter, VALIDAFTER);
assertEq(strategy.erc20Token, address(mockERC20));
assertEq(strategy.exchangeRate, EXCHANGERATE);
assertEq(signature, returnedSignature);
}
/*
* The owner (paymasterAdmin) can add and withdraw stake.
* Others cannot.
*/
function testPaymasterStake() public {
assertEq(paymasterAdmin.balance, 25 ether);
// The owner can add stake
vm.prank(paymasterAdmin);
openfortPaymaster.addStake{value: 2 ether}(10);
assertEq(paymasterAdmin.balance, 23 ether);
// Others cannot add stake
vm.expectRevert("Ownable: caller is not the owner");
openfortPaymaster.addStake{value: 2}(10);
// The owner trying to withdraw stake fails because it has not unlocked
// The owner can withdraw stake
vm.prank(paymasterAdmin);
vm.expectRevert();
openfortPaymaster.withdrawStake(payable(paymasterAdmin));
// The owner unlocks the stake
vm.prank(paymasterAdmin);
openfortPaymaster.unlockStake();
// The owner trying to unlock fails because it has not passed enough time
vm.prank(paymasterAdmin);
vm.expectRevert();
openfortPaymaster.withdrawStake(payable(paymasterAdmin));
// Passes 20 blocks...
skip(20);
// The owner can now withdraw stake (the 2 ethers recently staked + the 25 from the SetUp)
vm.prank(paymasterAdmin);
openfortPaymaster.withdrawStake(payable(paymasterAdmin));
assertEq(paymasterAdmin.balance, 50 ether);
}
/*
* Complete deposit walkthrough test
*/
function testDepositsToPaymaster() public {
// Initially, the Paymaster has 50 ether deposited
assertEq(entryPoint.balanceOf(address(openfortPaymaster)), 50 ether);
// Directly deposit 1 ETH to EntryPoint on behalf of the Paymaster
entryPoint.depositTo{value: 1 ether}(address(openfortPaymaster));
assertEq(entryPoint.balanceOf(address(openfortPaymaster)), 51 ether);
// Cannot deposit to address 0
vm.expectRevert();
openfortPaymaster.depositFor{value: 0 ether}(address(0));
// Cannot deposit 0 ether
vm.expectRevert();
openfortPaymaster.depositFor{value: 0 ether}(openfortAdmin);
// Cannot depositFor using owner
vm.prank(paymasterAdmin);
vm.expectRevert();
openfortPaymaster.depositFor{value: 1 ether}(paymasterAdmin);
assertEq(openfortPaymaster.getDepositFor(openfortAdmin), 0);
// Paymaster deposits 1 ETH to EntryPoint
openfortPaymaster.depositFor{value: 1 ether}(openfortAdmin);
assertEq(openfortPaymaster.getDepositFor(openfortAdmin), 1 ether);
// Get the WHOLE deposited amount so far
assertEq(openfortPaymaster.getDeposit(), 52 ether);
// Notice that, even though the deposit was made to the EntryPoint for the openfortPaymaster, the deposit is 0:
assertEq(openfortPaymaster.getDepositFor(address(openfortPaymaster)), 0 ether);
// All deposit not made using "depositFor" goes to owner (paymasterAdmin)
assertEq(openfortPaymaster.getDepositFor(paymasterAdmin), 51 ether);
vm.expectRevert("Not Owner: use depositFor() instead");
openfortPaymaster.deposit{value: 1 ether}();
// Get the WHOLE deposited amount so far
assertEq(openfortPaymaster.getDeposit(), 52 ether);
vm.prank(paymasterAdmin);
openfortPaymaster.deposit{value: 1 ether}();
// Get the WHOLE deposited amount so far
assertEq(openfortPaymaster.getDeposit(), 53 ether);
vm.expectRevert();
openfortPaymaster.withdrawTo(payable(paymasterAdmin), 1 ether);
assertEq(openfortPaymaster.getDeposit(), 53 ether);
vm.prank(paymasterAdmin);
openfortPaymaster.withdrawTo(payable(paymasterAdmin), 1 ether);
// Get the WHOLE deposited amount so far
assertEq(openfortPaymaster.getDeposit(), 52 ether);
vm.expectRevert();
vm.prank(paymasterAdmin);
openfortPaymaster.withdrawTo(payable(paymasterAdmin), 10000 ether);
// Get the WHOLE deposited amount so far
assertEq(openfortPaymaster.getDeposit(), 52 ether);
// Let's now use withdrawDepositorTo
// Owner cannot call it
vm.expectRevert();
vm.prank(paymasterAdmin);
openfortPaymaster.withdrawDepositorTo(payable(paymasterAdmin), 1 ether);
// openfortAdmin can call it
assertEq(openfortPaymaster.getDepositFor(openfortAdmin), 1 ether);
assertEq(openfortAdmin.balance, 100 ether);
// but not too much!
vm.expectRevert();
vm.prank(openfortAdmin);
openfortPaymaster.withdrawDepositorTo(payable(openfortAdmin), 1000 ether);
// not using address 0!
vm.expectRevert();
vm.prank(openfortAdmin);
openfortPaymaster.withdrawDepositorTo(payable(address(0)), 1 ether);
vm.prank(openfortAdmin);
openfortPaymaster.withdrawDepositorTo(payable(openfortAdmin), 1 ether);
assertEq(openfortPaymaster.getDepositFor(openfortAdmin), 0 ether);
assertEq(openfortAdmin.balance, 101 ether);
// Deposit of the owner is still 51
assertEq(openfortPaymaster.getDepositFor(paymasterAdmin), 51 ether);
// Finally, let's test withdrawFromDepositor
// deposit again using openfortAdmin
openfortPaymaster.depositFor{value: 1 ether}(openfortAdmin);
assertEq(openfortPaymaster.getDepositFor(openfortAdmin), 1 ether);
// Only owner cannot call it
vm.expectRevert("Ownable: caller is not the owner");
openfortPaymaster.withdrawFromDepositor(openfortAdmin, payable(openfortAdmin), 1 ether);
assertEq(openfortPaymaster.getDepositFor(openfortAdmin), 1 ether);
vm.expectRevert();
vm.prank(paymasterAdmin);
openfortPaymaster.withdrawFromDepositor(openfortAdmin, payable(openfortAdmin), 100 ether);
assertEq(openfortPaymaster.getDepositFor(openfortAdmin), 1 ether);
vm.expectRevert(OpenfortErrorsAndEvents.ZeroValueNotAllowed.selector);
vm.prank(paymasterAdmin);
openfortPaymaster.withdrawFromDepositor(address(0), payable(openfortAdmin), 1 ether);
assertEq(openfortPaymaster.getDepositFor(openfortAdmin), 1 ether);
vm.prank(paymasterAdmin);
openfortPaymaster.withdrawFromDepositor(openfortAdmin, payable(openfortAdmin), 1 ether);
assertEq(openfortPaymaster.getDepositFor(openfortAdmin), 0 ether);
}
/*
* Test sending a userOp with an invalid paymasterAndData (valid paymaster, but invalid sig length)
* Should revert
*/
function testPaymasterUserOpWrongSigLength() public {
bytes memory dataEncoded = mockPaymasterDataERC20Dynamic(paymasterAdmin);
bytes memory paymasterAndData = abi.encodePacked(address(openfortPaymaster), dataEncoded, "0x1234");
UserOperation[] memory userOp = _setupUserOpExecute(
accountAddress,
openfortAdminPKey,
bytes(""),
address(testCounter),
0,
abi.encodeWithSignature("count()"),
paymasterAndData
);
// "ECDSA: invalid signature length"
vm.expectRevert();
entryPoint.simulateValidation(userOp[0]);
// Verify that the counter has not increased
assertEq(testCounter.counters(accountAddress), 0);
}
/*
* Test sending a userOp with an invalid paymasterAndData (valid paymaster, but invalid sig)
* Should revert
*/
function testPaymasterUserOpWrongSig() public {
bytes memory dataEncoded = mockPaymasterDataERC20Dynamic(paymasterAdmin);
bytes memory paymasterAndData = abi.encodePacked(address(openfortPaymaster), dataEncoded, MOCKSIG, "1", MOCKSIG); // MOCKSIG, "1", MOCKSIG to make sure we send 65 bytes as sig
UserOperation[] memory userOp = _setupUserOpExecute(
accountAddress,
openfortAdminPKey,
bytes(""),
address(testCounter),
0,
abi.encodeWithSignature("count()"),
paymasterAndData
);
// "AA33 reverted: ECDSA: invalid signature"
vm.expectRevert();
entryPoint.simulateValidation(userOp[0]);
// Verify that the counter has not increased
assertEq(testCounter.counters(accountAddress), 0);
}
/*
* Test sending a userOp with an valid paymasterAndData (valid paymaster, valid sig)
* Should work
*/
function testPaymasterUserOpNativeValidSig() public {
bytes memory dataEncoded = mockPaymasterDataNative(paymasterAdmin);
bytes memory paymasterAndData = abi.encodePacked(address(openfortPaymaster), dataEncoded, MOCKSIG, "1", MOCKSIG);
UserOperation[] memory userOps = _setupUserOpExecute(
accountAddress,
openfortAdminPKey,
bytes(""),
address(testCounter),
0,
abi.encodeWithSignature("count()"),
paymasterAndData
);
OpenfortPaymasterV2.PolicyStrategy memory strategy;
strategy.paymasterMode = OpenfortPaymasterV2.Mode.PayForUser;
strategy.depositor = paymasterAdmin;
strategy.erc20Token = address(0);
strategy.exchangeRate = 0;
bytes32 hash;
{
// Simulating that the Paymaster gets the userOp and signs it
hash = ECDSA.toEthSignedMessageHash(openfortPaymaster.getHash(userOps[0], VALIDUNTIL, VALIDAFTER, strategy));
(uint8 v, bytes32 r, bytes32 s) = vm.sign(paymasterAdminPKey, hash);
bytes memory signature = abi.encodePacked(r, s, v);
bytes memory paymasterAndDataSigned = abi.encodePacked(address(openfortPaymaster), dataEncoded, signature);
assertEq(openfortPaymaster.owner(), ECDSA.recover(hash, signature));
userOps[0].paymasterAndData = paymasterAndDataSigned;
}
// Back to the user. Sign the userOp
bytes memory userOpSignature;
bytes32 hash2;
{
bytes32 opHash = entryPoint.getUserOpHash(userOps[0]);
bytes32 msgHash = ECDSA.toEthSignedMessageHash(opHash);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(openfortAdminPKey, msgHash);
userOpSignature = abi.encodePacked(r, s, v);
// Verifications below commented to avoid "Stack too deep" error
assertEq(ECDSA.recover(msgHash, v, r, s), vm.addr(openfortAdminPKey));
// Should return account admin
hash2 =
ECDSA.toEthSignedMessageHash(openfortPaymaster.getHash(userOps[0], VALIDUNTIL, VALIDAFTER, strategy));
}
// The hash of the userOp should not have changed after the inclusion of the sig
assertEq(hash, hash2);
userOps[0].signature = userOpSignature;
// Get the paymaster deposit before handling the userOp
uint256 paymasterDepositBefore = openfortPaymaster.getDeposit();
entryPoint.handleOps(userOps, beneficiary);
// entryPoint.simulateValidation(userOp);
// Verify that the paymaster has less deposit now
assert(paymasterDepositBefore > openfortPaymaster.getDeposit());
//Verify that the counter has increased
assertEq(testCounter.counters(accountAddress), 1);
}
/*
* Test sending a userOp with signature from a wrong address
* Should not work
*/
function testPaymasterUserOpNativeWrongUserSig() public {
bytes memory dataEncoded = mockPaymasterDataNative(paymasterAdmin);
bytes memory paymasterAndData = abi.encodePacked(address(openfortPaymaster), dataEncoded, MOCKSIG, "1", MOCKSIG);
UserOperation[] memory userOps = _setupUserOpExecute(
accountAddress,
openfortAdminPKey,
bytes(""),
address(testCounter),
0,
abi.encodeWithSignature("count()"),
paymasterAndData
);
OpenfortPaymasterV2.PolicyStrategy memory strategy;
strategy.paymasterMode = OpenfortPaymasterV2.Mode.PayForUser;
strategy.erc20Token = address(0);
strategy.exchangeRate = EXCHANGERATE;
bytes32 hash;
{
// Simulating that the factory admin gets the userOp and tries to sign it
hash = ECDSA.toEthSignedMessageHash(openfortPaymaster.getHash(userOps[0], VALIDUNTIL, VALIDAFTER, strategy));
(uint8 v, bytes32 r, bytes32 s) = vm.sign(openfortAdminPKey, hash);
bytes memory signature = abi.encodePacked(r, s, v);
bytes memory paymasterAndDataSigned = abi.encodePacked(address(openfortPaymaster), dataEncoded, signature);
assertEq(openfortAdmin, ECDSA.recover(hash, signature));
userOps[0].paymasterAndData = paymasterAndDataSigned;
}
// Back to the user. Sign the userOp
bytes memory userOpSignature;
bytes32 hash2;
{
bytes32 opHash = entryPoint.getUserOpHash(userOps[0]);
bytes32 msgHash = ECDSA.toEthSignedMessageHash(opHash);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(openfortAdminPKey, msgHash);
userOpSignature = abi.encodePacked(r, s, v);
// Verifications below commented to avoid "Stack too deep" error
assertEq(ECDSA.recover(msgHash, v, r, s), vm.addr(openfortAdminPKey));
// Should return account admin
hash2 =
ECDSA.toEthSignedMessageHash(openfortPaymaster.getHash(userOps[0], VALIDUNTIL, VALIDAFTER, strategy));
}
// The hash of the userOp should not have changed after the inclusion of the sig
assertEq(hash, hash2);
userOps[0].signature = userOpSignature;
// Get the paymaster deposit before handling the userOp
uint256 paymasterDepositBefore = openfortPaymaster.getDeposit();
vm.expectRevert();
entryPoint.handleOps(userOps, beneficiary);
// entryPoint.simulateValidation(userOp);
// Verify that the paymaster has less deposit now
assert(paymasterDepositBefore == openfortPaymaster.getDeposit());
//Verify that the counter has not increased
assertEq(testCounter.counters(accountAddress), 0);
}
/*
* Test sending a userOp with an valid paymasterAndData (valid paymaster, valid sig)
* Using ERC20. Should work
*/
function testPaymasterUserOpERC20ValidSigDiffMaxPriorityFeePerGas() public {
assertEq(mockERC20.balanceOf(accountAddress), 0);
mockERC20.mint(accountAddress, TESTTOKEN_ACCOUNT_PREFUND);
assertEq(mockERC20.balanceOf(accountAddress), TESTTOKEN_ACCOUNT_PREFUND);
bytes memory dataEncoded = mockPaymasterDataERC20Dynamic(paymasterAdmin);
bytes memory paymasterAndData = abi.encodePacked(address(openfortPaymaster), dataEncoded, MOCKSIG, "1", MOCKSIG);
// Create a userOp to let the paymaster use our mockERC20s
UserOperation[] memory userOps = _setupUserOpExecute(
accountAddress,
openfortAdminPKey,
bytes(""),
address(mockERC20),
0,
abi.encodeWithSignature("approve(address,uint256)", address(openfortPaymaster), 2 ** 256 - 1),
paymasterAndData
);
userOps[0].maxPriorityFeePerGas += 1;
OpenfortPaymasterV2.PolicyStrategy memory strategy;
strategy.paymasterMode = OpenfortPaymasterV2.Mode.DynamicRate;
strategy.depositor = paymasterAdmin;
strategy.erc20Token = address(mockERC20);
strategy.exchangeRate = EXCHANGERATE;
// Simulating that the Paymaster gets the userOp and signs it
bytes32 hash;
{
hash = ECDSA.toEthSignedMessageHash(openfortPaymaster.getHash(userOps[0], VALIDUNTIL, VALIDAFTER, strategy));
(uint8 v, bytes32 r, bytes32 s) = vm.sign(paymasterAdminPKey, hash);
bytes memory signature = abi.encodePacked(r, s, v);
bytes memory paymasterAndDataSigned = abi.encodePacked(address(openfortPaymaster), dataEncoded, signature);
assertEq(openfortPaymaster.owner(), ECDSA.recover(hash, signature));
userOps[0].paymasterAndData = paymasterAndDataSigned;
}
// Back to the user. Sign the userOp
bytes memory userOpSignature;
bytes32 hash2;
{
bytes32 opHash = entryPoint.getUserOpHash(userOps[0]);
bytes32 msgHash = ECDSA.toEthSignedMessageHash(opHash);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(openfortAdminPKey, msgHash);
userOpSignature = abi.encodePacked(r, s, v);
// Verifications below commented to avoid "Stack too deep" error
assertEq(ECDSA.recover(msgHash, v, r, s), vm.addr(openfortAdminPKey));
// Should return account admin
hash2 =
ECDSA.toEthSignedMessageHash(openfortPaymaster.getHash(userOps[0], VALIDUNTIL, VALIDAFTER, strategy));
}
// The hash of the userOp should not have changed after the inclusion of the sig
assertEq(hash, hash2);
userOps[0].signature = userOpSignature;
// Get the paymaster deposit before handling the userOp
uint256 paymasterDepositBefore = openfortPaymaster.getDeposit();
entryPoint.handleOps(userOps, beneficiary);
// Verify that the paymaster has less deposit now
assert(paymasterDepositBefore > openfortPaymaster.getDeposit());
// Verify that the balance of the smart account has decreased
assert(mockERC20.balanceOf(accountAddress) < TESTTOKEN_ACCOUNT_PREFUND);
}
/*
* Test sending a userOp with a valid paymasterAndData (valid paymaster, valid sig)
* Using ERC20. Should work
*/
function testPaymasterUserOpERC20ValidSig() public {
assertEq(mockERC20.balanceOf(accountAddress), 0);
mockERC20.mint(accountAddress, TESTTOKEN_ACCOUNT_PREFUND);
assertEq(mockERC20.balanceOf(accountAddress), TESTTOKEN_ACCOUNT_PREFUND);
bytes memory dataEncoded = mockPaymasterDataERC20Dynamic(paymasterAdmin);
bytes memory paymasterAndData = abi.encodePacked(address(openfortPaymaster), dataEncoded, MOCKSIG, "1", MOCKSIG);
// Create a userOp to let the paymaster use our mockERC20s
UserOperation[] memory userOps = _setupUserOpExecute(
accountAddress,
openfortAdminPKey,
bytes(""),
address(mockERC20),
0,
abi.encodeWithSignature("approve(address,uint256)", address(openfortPaymaster), 2 ** 256 - 1),
paymasterAndData
);
OpenfortPaymasterV2.PolicyStrategy memory strategy;
strategy.paymasterMode = OpenfortPaymasterV2.Mode.DynamicRate;
strategy.depositor = paymasterAdmin;
strategy.erc20Token = address(mockERC20);
strategy.exchangeRate = EXCHANGERATE;
// Simulating that the Paymaster gets the userOp and signs it
bytes32 hash;
{
hash = ECDSA.toEthSignedMessageHash(openfortPaymaster.getHash(userOps[0], VALIDUNTIL, VALIDAFTER, strategy));
(uint8 v, bytes32 r, bytes32 s) = vm.sign(paymasterAdminPKey, hash);
bytes memory signature = abi.encodePacked(r, s, v);
bytes memory paymasterAndDataSigned = abi.encodePacked(address(openfortPaymaster), dataEncoded, signature);
assertEq(openfortPaymaster.owner(), ECDSA.recover(hash, signature));
userOps[0].paymasterAndData = paymasterAndDataSigned;
}
// Back to the user. Sign the userOp
bytes memory userOpSignature;
bytes32 hash2;
{
bytes32 opHash = entryPoint.getUserOpHash(userOps[0]);
bytes32 msgHash = ECDSA.toEthSignedMessageHash(opHash);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(openfortAdminPKey, msgHash);
userOpSignature = abi.encodePacked(r, s, v);
// Verifications below commented to avoid "Stack too deep" error
assertEq(ECDSA.recover(msgHash, v, r, s), vm.addr(openfortAdminPKey));
// Should return account admin
hash2 =
ECDSA.toEthSignedMessageHash(openfortPaymaster.getHash(userOps[0], VALIDUNTIL, VALIDAFTER, strategy));
}
// The hash of the userOp should not have changed after the inclusion of the sig
assertEq(hash, hash2);
userOps[0].signature = userOpSignature;
// Get the paymaster deposit before handling the userOp
uint256 paymasterDepositBefore = openfortPaymaster.getDeposit();
entryPoint.handleOps(userOps, beneficiary);
// Verify that the paymaster has less deposit now
assert(paymasterDepositBefore > openfortPaymaster.getDeposit());
// Verify that the balance of the smart account has decreased
assert(mockERC20.balanceOf(accountAddress) < TESTTOKEN_ACCOUNT_PREFUND);
}
/*
* Test sending a userOp with an valid paymasterAndData (valid paymaster, valid sig)
* Using FIXED ERC20. Should work
*/
function testPaymasterUserOpERC20FixedValidSig() public {
assertEq(mockERC20.balanceOf(accountAddress), 0);
mockERC20.mint(accountAddress, TESTTOKEN_ACCOUNT_PREFUND);
assertEq(mockERC20.balanceOf(accountAddress), TESTTOKEN_ACCOUNT_PREFUND);
uint256 pricePerTransaction = 10 ** 18;
bytes memory dataEncoded = mockPaymasterDataERC20Fixed(paymasterAdmin, pricePerTransaction);
bytes memory paymasterAndData = abi.encodePacked(address(openfortPaymaster), dataEncoded, MOCKSIG, "1", MOCKSIG);
// Create a userOp to let the paymaster use our mockERC20s
UserOperation[] memory userOps = _setupUserOpExecute(
accountAddress,
openfortAdminPKey,
bytes(""),
address(mockERC20),
0,
abi.encodeWithSignature("approve(address,uint256)", address(openfortPaymaster), 2 ** 256 - 1),
paymasterAndData
);
OpenfortPaymasterV2.PolicyStrategy memory strategy;
strategy.paymasterMode = OpenfortPaymasterV2.Mode.FixedRate;
strategy.depositor = paymasterAdmin;
strategy.erc20Token = address(mockERC20);
strategy.exchangeRate = pricePerTransaction;
// Simulating that the Paymaster gets the userOp and signs it
bytes32 hash;
{
hash = ECDSA.toEthSignedMessageHash(openfortPaymaster.getHash(userOps[0], VALIDUNTIL, VALIDAFTER, strategy));
(uint8 v, bytes32 r, bytes32 s) = vm.sign(paymasterAdminPKey, hash);
bytes memory signature = abi.encodePacked(r, s, v);
bytes memory paymasterAndDataSigned = abi.encodePacked(address(openfortPaymaster), dataEncoded, signature);
assertEq(openfortPaymaster.owner(), ECDSA.recover(hash, signature));
userOps[0].paymasterAndData = paymasterAndDataSigned;
}
// Back to the user. Sign the userOp
bytes memory userOpSignature;
bytes32 hash2;
{
bytes32 opHash = entryPoint.getUserOpHash(userOps[0]);
bytes32 msgHash = ECDSA.toEthSignedMessageHash(opHash);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(openfortAdminPKey, msgHash);
userOpSignature = abi.encodePacked(r, s, v);
// Verifications below commented to avoid "Stack too deep" error
assertEq(ECDSA.recover(msgHash, v, r, s), vm.addr(openfortAdminPKey));
// Should return account admin
hash2 =
ECDSA.toEthSignedMessageHash(openfortPaymaster.getHash(userOps[0], VALIDUNTIL, VALIDAFTER, strategy));
}
// The hash of the userOp should not have changed after the inclusion of the sig
assertEq(hash, hash2);
userOps[0].signature = userOpSignature;
// Get the paymaster deposit before handling the userOp
uint256 paymasterDepositBefore = openfortPaymaster.getDeposit();
entryPoint.handleOps(userOps, beneficiary);
// Verify that the paymaster has less deposit now
assert(paymasterDepositBefore > openfortPaymaster.getDeposit());
// Verify that the balance of the smart account has decreased
assert(mockERC20.balanceOf(accountAddress) == TESTTOKEN_ACCOUNT_PREFUND - pricePerTransaction);
}
/*
* Test sending a userOp with an valid paymasterAndData (valid paymaster, valid sig)
* ExecBatch. Using dynamic ERC20. Should work
*/
function testPaymasterUserOpERC20ValidSigExecBatch() public {
assertEq(mockERC20.balanceOf(accountAddress), 0);
mockERC20.mint(accountAddress, TESTTOKEN_ACCOUNT_PREFUND);
assertEq(mockERC20.balanceOf(accountAddress), TESTTOKEN_ACCOUNT_PREFUND);
assertEq(testCounter.counters(accountAddress), 0);
bytes memory dataEncoded = mockPaymasterDataERC20Dynamic(paymasterAdmin);
bytes memory paymasterAndData = abi.encodePacked(address(openfortPaymaster), dataEncoded, MOCKSIG, "1", MOCKSIG);
uint256 count = 2;
address[] memory targets = new address[](count);
uint256[] memory values = new uint256[](count);
bytes[] memory callData = new bytes[](count);
targets[0] = address(mockERC20);
values[0] = 0;
callData[0] = abi.encodeWithSignature("approve(address,uint256)", address(openfortPaymaster), 2 ** 256 - 1);
targets[1] = address(testCounter);
values[1] = 0;
callData[1] = abi.encodeWithSignature("count()");
// Create a userOp to let the paymaster use our mockERC20s
UserOperation[] memory userOps = _setupUserOpExecuteBatch(
accountAddress, openfortAdminPKey, bytes(""), targets, values, callData, paymasterAndData
);
OpenfortPaymasterV2.PolicyStrategy memory strategy;
strategy.paymasterMode = OpenfortPaymasterV2.Mode.DynamicRate;
strategy.depositor = paymasterAdmin;
strategy.erc20Token = address(mockERC20);
strategy.exchangeRate = EXCHANGERATE;
// Simulating that the Paymaster gets the userOp and signs it
bytes32 hash;
{
hash = ECDSA.toEthSignedMessageHash(openfortPaymaster.getHash(userOps[0], VALIDUNTIL, VALIDAFTER, strategy));
(uint8 v, bytes32 r, bytes32 s) = vm.sign(paymasterAdminPKey, hash);
bytes memory signature = abi.encodePacked(r, s, v);
bytes memory paymasterAndDataSigned = abi.encodePacked(address(openfortPaymaster), dataEncoded, signature);
assertEq(openfortPaymaster.owner(), ECDSA.recover(hash, signature));
userOps[0].paymasterAndData = paymasterAndDataSigned;
}
// Back to the user. Sign the userOp
bytes memory userOpSignature;
bytes32 hash2;
{
bytes32 opHash = entryPoint.getUserOpHash(userOps[0]);
bytes32 msgHash = ECDSA.toEthSignedMessageHash(opHash);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(openfortAdminPKey, msgHash);
userOpSignature = abi.encodePacked(r, s, v);
// Verifications below commented to avoid "Stack too deep" error
assertEq(ECDSA.recover(msgHash, v, r, s), vm.addr(openfortAdminPKey));
// Should return account admin
hash2 =
ECDSA.toEthSignedMessageHash(openfortPaymaster.getHash(userOps[0], VALIDUNTIL, VALIDAFTER, strategy));
}
// The hash of the userOp should not have changed after the inclusion of the sig
assertEq(hash, hash2);
userOps[0].signature = userOpSignature;
// Get the paymaster deposit before handling the userOp
uint256 paymasterDepositBefore = openfortPaymaster.getDeposit();
entryPoint.handleOps(userOps, beneficiary);
// Verify that the paymaster has less deposit now
assert(paymasterDepositBefore > openfortPaymaster.getDeposit());
// Verify that the balance of the smart account has decreased
assert(mockERC20.balanceOf(accountAddress) < TESTTOKEN_ACCOUNT_PREFUND);
assertEq(testCounter.counters(accountAddress), 1);
}
/*
* Test sending a userOp with an valid paymasterAndData (valid paymaster, valid sig)
* ExecBatch. Using fixed ERC20. Should work
*/
function testPaymasterUserOpERC20FixedValidSigExecBatch() public {
assertEq(mockERC20.balanceOf(accountAddress), 0);
mockERC20.mint(accountAddress, TESTTOKEN_ACCOUNT_PREFUND);
assertEq(mockERC20.balanceOf(accountAddress), TESTTOKEN_ACCOUNT_PREFUND);
assertEq(testCounter.counters(accountAddress), 0);
uint256 pricePerTransaction = 10 ** 18;
bytes memory dataEncoded = mockPaymasterDataERC20Fixed(paymasterAdmin, pricePerTransaction);
bytes memory paymasterAndData = abi.encodePacked(address(openfortPaymaster), dataEncoded, MOCKSIG, "1", MOCKSIG);
uint256 count = 2;
address[] memory targets = new address[](count);
uint256[] memory values = new uint256[](count);
bytes[] memory callData = new bytes[](count);
targets[0] = address(mockERC20);
values[0] = 0;
callData[0] = abi.encodeWithSignature("approve(address,uint256)", address(openfortPaymaster), 2 ** 256 - 1);
targets[1] = address(testCounter);
values[1] = 0;
callData[1] = abi.encodeWithSignature("count()");
// Create a userOp to let the paymaster use our mockERC20s
UserOperation[] memory userOps = _setupUserOpExecuteBatch(
accountAddress, openfortAdminPKey, bytes(""), targets, values, callData, paymasterAndData
);
OpenfortPaymasterV2.PolicyStrategy memory strategy;
strategy.paymasterMode = OpenfortPaymasterV2.Mode.FixedRate;
strategy.depositor = paymasterAdmin;
strategy.erc20Token = address(mockERC20);
strategy.exchangeRate = pricePerTransaction;
// Simulating that the Paymaster gets the userOp and signs it
bytes32 hash;
{
hash = ECDSA.toEthSignedMessageHash(openfortPaymaster.getHash(userOps[0], VALIDUNTIL, VALIDAFTER, strategy));
(uint8 v, bytes32 r, bytes32 s) = vm.sign(paymasterAdminPKey, hash);
bytes memory signature = abi.encodePacked(r, s, v);
bytes memory paymasterAndDataSigned = abi.encodePacked(address(openfortPaymaster), dataEncoded, signature);
assertEq(openfortPaymaster.owner(), ECDSA.recover(hash, signature));
userOps[0].paymasterAndData = paymasterAndDataSigned;
}
// Back to the user. Sign the userOp
bytes memory userOpSignature;
bytes32 hash2;
{
bytes32 opHash = entryPoint.getUserOpHash(userOps[0]);
bytes32 msgHash = ECDSA.toEthSignedMessageHash(opHash);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(openfortAdminPKey, msgHash);
userOpSignature = abi.encodePacked(r, s, v);
// Verifications below commented to avoid "Stack too deep" error
assertEq(ECDSA.recover(msgHash, v, r, s), vm.addr(openfortAdminPKey));
// Should return account admin
hash2 =
ECDSA.toEthSignedMessageHash(openfortPaymaster.getHash(userOps[0], VALIDUNTIL, VALIDAFTER, strategy));
}
// The hash of the userOp should not have changed after the inclusion of the sig
assertEq(hash, hash2);
userOps[0].signature = userOpSignature;
// Get the paymaster deposit before handling the userOp
uint256 paymasterDepositBefore = openfortPaymaster.getDeposit();
entryPoint.handleOps(userOps, beneficiary);
// Verify that the paymaster has less deposit now
assert(paymasterDepositBefore > openfortPaymaster.getDeposit());
// Verify that the balance of the smart account has decreased
assert(mockERC20.balanceOf(accountAddress) == TESTTOKEN_ACCOUNT_PREFUND - pricePerTransaction);
assertEq(testCounter.counters(accountAddress), 1);
}
/*
* Test sending a userOp with an valid paymasterAndData (valid paymaster, valid sig)
* ExecBatch. Using fixed ERC20 expensive. Should work