-
Notifications
You must be signed in to change notification settings - Fork 370
/
Accounts.t.sol
1542 lines (1285 loc) · 53.9 KB
/
Accounts.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: UNLICENSED
pragma solidity ^0.5.13;
pragma experimental ABIEncoderV2;
import "celo-foundry/Test.sol";
import "@celo-contracts/common/FixidityLib.sol";
import "@celo-contracts/common/Registry.sol";
import "@celo-contracts/common/Accounts.sol";
import "@celo-contracts/governance/test/MockValidators.sol";
import { TestConstants } from "@test-sol/constants.sol";
contract AccountsTest is Test, TestConstants {
using FixidityLib for FixidityLib.Fraction;
Registry registry;
Accounts accounts;
MockValidators validators;
string constant name = "Account";
string constant metadataURL = "https://www.celo.org";
string constant otherMetadataURL = "https://clabs.co";
bytes storageRoot = abi.encodePacked(metadataURL);
bytes otherStorageRoot = abi.encodePacked(otherMetadataURL);
bytes constant dataEncryptionKey =
hex"02f2f48ee19680706196e2e339e5da3491186e0c4c5030670656b0e01611111111";
bytes constant longDataEncryptionKey =
hex"04f2f48ee19680706196e2e339e5da3491186e0c4c5030670656b0e0161111111102f2f48ee19680706196e2e339e5da3491186e0c4c5030670656b0e01611111111";
address caller;
uint256 callerPK;
address caller2;
uint256 caller2PK;
bytes32 constant EIP712DOMAIN_TYPEHASH =
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
bytes32 public constant EIP712_AUTHORIZE_SIGNER_TYPEHASH =
keccak256("AuthorizeSigner(address account,address signer,bytes32 role)");
struct Domain {
string name;
string version;
uint256 chainId;
address verifyingContract;
}
struct AuthorizeSigner {
address account;
address signer;
bytes32 role;
}
event AttestationSignerAuthorized(address indexed account, address signer);
event VoteSignerAuthorized(address indexed account, address signer);
event ValidatorSignerAuthorized(address indexed account, address signer);
event SignerAuthorized(address indexed account, address signer, bytes32 indexed role);
event SignerAuthorizationStarted(address indexed account, address signer, bytes32 indexed role);
event SignerAuthorizationCompleted(address indexed account, address signer, bytes32 indexed role);
event AttestationSignerRemoved(address indexed account, address oldSigner);
event VoteSignerRemoved(address indexed account, address oldSigner);
event ValidatorSignerRemoved(address indexed account, address oldSigner);
event IndexedSignerSet(address indexed account, address signer, bytes32 role);
event IndexedSignerRemoved(address indexed account, address oldSigner, bytes32 role);
event DefaultSignerSet(address indexed account, address signer, bytes32 role);
event DefaultSignerRemoved(address indexed account, address oldSigner, bytes32 role);
event LegacySignerSet(address indexed account, address signer, bytes32 role);
event LegacySignerRemoved(address indexed account, address oldSigner, bytes32 role);
event SignerRemoved(address indexed account, address oldSigner, bytes32 indexed role);
event AccountDataEncryptionKeySet(address indexed account, bytes dataEncryptionKey);
event AccountNameSet(address indexed account, string name);
event AccountMetadataURLSet(address indexed account, string metadataURL);
event AccountWalletAddressSet(address indexed account, address walletAddress);
event AccountCreated(address indexed account);
event OffchainStorageRootAdded(address indexed account, bytes url);
event OffchainStorageRootRemoved(address indexed account, bytes url, uint256 index);
event PaymentDelegationSet(address indexed beneficiary, uint256 fraction);
function setUp() public {
deployCodeTo("Registry.sol", abi.encode(false), REGISTRY_ADDRESS);
accounts = new Accounts(true);
validators = new MockValidators();
registry = Registry(REGISTRY_ADDRESS);
registry.setAddressFor("Validators", address(validators));
registry.setAddressFor("Accounts", address(accounts));
accounts.initialize(address(registry));
accounts.setEip712DomainSeparator();
(caller, callerPK) = actorWithPK("caller");
(caller2, caller2PK) = actorWithPK("caller2");
}
function _whenL2() public {
deployCodeTo("Registry.sol", abi.encode(false), PROXY_ADMIN_ADDRESS);
}
function getParsedSignatureOfAddress(
address _address,
uint256 privateKey
) public pure returns (uint8, bytes32, bytes32) {
bytes32 addressHash = keccak256(abi.encodePacked(_address));
bytes32 prefixedHash = ECDSA.toEthSignedMessageHash(addressHash);
return vm.sign(privateKey, prefixedHash);
}
function assertStorageRoots(
string memory rootsHex,
uint256[] memory lengths,
string[] memory expectedRoots
) public {
assertEq(lengths.length, expectedRoots.length);
bytes memory roots = bytes(abi.encodePacked(rootsHex));
uint256 currentIndex = 0;
for (uint256 i = 0; i < expectedRoots.length; i++) {
bytes memory root = slice(roots, currentIndex, currentIndex + lengths[i]);
currentIndex += lengths[i];
assertEq(string(root), expectedRoots[i]);
}
assertEq(roots.length, currentIndex);
}
function slice(
bytes memory data,
uint256 start,
uint256 end
) internal pure returns (bytes memory) {
bytes memory part = new bytes(end - start);
for (uint256 i = 0; i < part.length; i++) {
part[i] = data[i + start];
}
return part;
}
function getAddressFromPrivateKey(uint256 privateKey) public pure returns (address) {
return vm.addr(privateKey);
}
function getSignatureForAuthorization(
address account,
bytes32 _role,
uint256 privateKeySigner, // This is used to simulate signing in the test environment
uint256 _chainId,
address verifyingContract
) public pure returns (uint8 v, bytes32 r, bytes32 s) {
bytes32 dataHash = generateTypedDataHash(
Domain("Celo Core Contracts", "1.0", _chainId, verifyingContract),
AuthorizeSigner(account, getAddressFromPrivateKey(privateKeySigner), _role)
);
// Simulate the signing of the digest with the provided private key using the EVM function
(v, r, s) = vm.sign(privateKeySigner, dataHash);
// Return the signature components
return (v, r, s);
}
function generateTypedDataHash(
Domain memory domain,
AuthorizeSigner memory authorizeSigner
) public pure returns (bytes32) {
bytes32 domainSeparator = structHashEIP712Domain(domain);
bytes32 authorizeSignerHash = getAuthorizeSigner(authorizeSigner);
return keccak256(abi.encodePacked("\x19\x01", domainSeparator, authorizeSignerHash));
}
function getAuthorizeSigner(
AuthorizeSigner memory authorizeSigner
) public pure returns (bytes32) {
return
keccak256(
abi.encode(
EIP712_AUTHORIZE_SIGNER_TYPEHASH,
authorizeSigner.account,
authorizeSigner.signer,
authorizeSigner.role
)
);
}
function structHashEIP712Domain(Domain memory domain) private pure returns (bytes32) {
return
keccak256(
abi.encode(
EIP712DOMAIN_TYPEHASH,
keccak256(bytes(domain.name)),
keccak256(bytes(domain.version)),
domain.chainId,
domain.verifyingContract
)
);
}
}
contract AccountsTest_createAccount is AccountsTest {
function setUp() public {
super.setUp();
}
function test_ShouldCreateTheAccount() public {
assertEq(accounts.isAccount(address(this)), false);
accounts.createAccount();
assertEq(accounts.isAccount(address(this)), true);
}
function test_Emits_AccountCreatedEvent() public {
vm.expectEmit(true, true, true, true);
emit AccountCreated(address(this));
accounts.createAccount();
}
}
contract AccountsTest_setAccountDataEncryptionKey is AccountsTest {
function setUp() public {
super.setUp();
}
function test_ShouldSetDataEncryptionKey() public {
accounts.setAccountDataEncryptionKey(dataEncryptionKey);
assertEq(accounts.getDataEncryptionKey(address(this)), dataEncryptionKey);
}
function test_ShouldAllowSettingAKeyWithLEadingZeros() public {
bytes
memory keyWithLeadingZeros = hex"00000000000000000000000000000000000000000000000f2f48ee19680706191111";
accounts.setAccountDataEncryptionKey(keyWithLeadingZeros);
assertEq(accounts.getDataEncryptionKey(address(this)), keyWithLeadingZeros);
}
function test_ShouldRevertWhenKeyIsInvalid() public {
bytes memory invalidKey = hex"321329312493";
vm.expectRevert(bytes("data encryption key length <= 32"));
accounts.setAccountDataEncryptionKey(invalidKey);
}
function test_ShouldAllowKeyLongerThan33Bytes() public {
accounts.setAccountDataEncryptionKey(longDataEncryptionKey);
assertEq(accounts.getDataEncryptionKey(address(this)), longDataEncryptionKey);
}
function test_Emits_AccountDataEncryptionKeySet() public {
vm.expectEmit(true, true, true, true);
emit AccountDataEncryptionKeySet(address(this), dataEncryptionKey);
accounts.setAccountDataEncryptionKey(dataEncryptionKey);
}
}
contract AccountsTest_setAccount is AccountsTest {
function setUp() public {
super.setUp();
}
function test_ShouldSetTheNameDataEncryptionKeyAndWalletAddress_WhenTheAccountHasBeenCreated()
public
{
accounts.createAccount();
accounts.setAccount(name, dataEncryptionKey, address(this), 0, 0x0, 0x0);
assertEq(accounts.getName(address(this)), name);
assertEq(accounts.getDataEncryptionKey(address(this)), dataEncryptionKey);
assertEq(accounts.getWalletAddress(address(this)), address(this));
}
function test_Emits_AccountNameSetEvent_WhenTheAccountHasBeenCreated() public {
accounts.createAccount();
vm.expectEmit(true, true, true, true);
emit AccountNameSet(address(this), name);
accounts.setAccount(name, dataEncryptionKey, address(this), 0, 0x0, 0x0);
}
function test_Emits_AccountDataEncryptionKeySetEvent_WhenTheAccountHasBeenCreated() public {
accounts.createAccount();
vm.expectEmit(true, true, true, true);
emit AccountDataEncryptionKeySet(address(this), dataEncryptionKey);
accounts.setAccount(name, dataEncryptionKey, address(this), 0, 0x0, 0x0);
}
function test_Emits_AccountWalletAddressSetEvent_WhenTheAccountHasBeenCreated() public {
accounts.createAccount();
vm.expectEmit(true, true, true, true);
emit AccountWalletAddressSet(address(this), address(this));
accounts.setAccount(name, dataEncryptionKey, address(this), 0, 0x0, 0x0);
}
function test_ShouldSetADifferentAddressWithAppropriateSignature_WhenTheAccountHasBeenCreated()
public
{
(uint8 v, bytes32 r, bytes32 s) = getParsedSignatureOfAddress(address(this), caller2PK);
accounts.setAccount(name, dataEncryptionKey, caller2, v, r, s);
assertEq(accounts.getWalletAddress(address(this)), caller2);
}
function test_ShouldSetTheNameDataEncryptionKeyAndWalletAddress_WhenTheAccountHasNotBeenCreated()
public
{
accounts.setAccount(name, dataEncryptionKey, address(this), 0, 0x0, 0x0);
assertEq(accounts.getName(address(this)), name);
assertEq(accounts.getDataEncryptionKey(address(this)), dataEncryptionKey);
assertEq(accounts.getWalletAddress(address(this)), address(this));
assertEq(accounts.isAccount(address(this)), true);
}
function test_Emits_AccountCreated_WhenTheAccountHasNotBeenCreated() public {
vm.expectEmit(true, true, true, true);
emit AccountCreated(address(this));
accounts.setAccount(name, dataEncryptionKey, address(this), 0, 0x0, 0x0);
}
function test_Emits_AccountNameSetEvent_WhenTheAccountHasNotBeenCreated() public {
vm.expectEmit(true, true, true, true);
emit AccountNameSet(address(this), name);
accounts.setAccount(name, dataEncryptionKey, address(this), 0, 0x0, 0x0);
}
function test_Emits_AccountDataEncryptionKeySetEvent_WhenTheAccountHasNotBeenCreated() public {
vm.expectEmit(true, true, true, true);
emit AccountDataEncryptionKeySet(address(this), dataEncryptionKey);
accounts.setAccount(name, dataEncryptionKey, address(this), 0, 0x0, 0x0);
}
function test_Emits_AccountWalletAddressSetEvent_WhenTheAccountHasNotBeenCreated() public {
vm.expectEmit(true, true, true, true);
emit AccountWalletAddressSet(address(this), address(this));
accounts.setAccount(name, dataEncryptionKey, address(this), 0, 0x0, 0x0);
}
function test_ShouldRevertWhenIncorrectSignature() public {
(uint8 v, bytes32 r, bytes32 s) = getParsedSignatureOfAddress(address(caller), caller2PK);
vm.prank(caller);
vm.expectRevert("Invalid signature");
accounts.setAccount(name, dataEncryptionKey, address(this), v, r, s);
}
}
contract AccountsTest_setWalletAddress is AccountsTest {
function setUp() public {
super.setUp();
}
function test_ShouldRevert_WhenAccountHasNotBeenCreated() public {
vm.expectRevert("Unknown account");
accounts.setWalletAddress(address(this), 0, 0x0, 0x0);
}
function test_ShouldSetWalletAddress_WhenAccountHasBeenCreated() public {
accounts.createAccount();
accounts.setWalletAddress(address(this), 0, 0x0, 0x0);
assertEq(accounts.getWalletAddress(address(this)), address(this));
}
function test_ShouldSetDifferentAddressWithTheAppropriateSignature_WhenAccountHasBeenCreated()
public
{
vm.startPrank(caller);
accounts.createAccount();
(uint8 v, bytes32 r, bytes32 s) = getParsedSignatureOfAddress(address(caller), caller2PK);
accounts.setWalletAddress(caller2, v, r, s);
assertEq(accounts.getWalletAddress(address(caller)), caller2);
}
function test_ShouldSetTheNULLAddress_WhenAccountHasBeenCreated() public {
accounts.createAccount();
accounts.setWalletAddress(address(0), 0, 0x0, 0x0);
assertEq(accounts.getWalletAddress(address(this)), address(0));
}
function test_Emits_TheAccountWalletAddressSetEvent_WhenAccountHasBeenCreated() public {
accounts.createAccount();
vm.expectEmit(true, true, true, true);
emit AccountWalletAddressSet(address(this), address(this));
accounts.setWalletAddress(address(this), 0, 0x0, 0x0);
}
function test_ShouldRevertWithWrongSignatureForADifferentAddress_WhenAccountHasBEenCreated()
public
{
vm.startPrank(caller);
accounts.createAccount();
(uint8 v, bytes32 r, bytes32 s) = getParsedSignatureOfAddress(address(caller), caller2PK);
vm.expectRevert("Invalid signature");
accounts.setWalletAddress(address(this), v, r, s);
}
}
contract AccountsTest_setMetadataURL is AccountsTest {
function setUp() public {
super.setUp();
}
function test_ShouldRevert_WhenAccountHasNotBeenCreated() public {
vm.expectRevert("Unknown account");
accounts.setMetadataURL(metadataURL);
}
function test_ShouldSetMetadatURL_WhenAccountHasBeenCreated() public {
accounts.createAccount();
accounts.setMetadataURL(metadataURL);
assertEq(accounts.getMetadataURL(address(this)), metadataURL);
}
function test_Emits_TheAccountMetadataURLSetEvent() public {
accounts.createAccount();
vm.expectEmit(true, true, true, true);
emit AccountMetadataURLSet(address(this), metadataURL);
accounts.setMetadataURL(metadataURL);
}
}
contract AccountsTest_batchGetMetadataURL is AccountsTest {
function setUp() public {
super.setUp();
}
function parseSolidityStringArray(
uint256[] memory stringLengths,
bytes memory data
) private pure returns (string[] memory) {
string[] memory strings = new string[](stringLengths.length);
uint256 offset = 0;
for (uint256 i = 0; i < stringLengths.length; i++) {
bytes memory stringBytes = new bytes(stringLengths[i]);
for (uint256 j = 0; j < stringLengths[i]; j++) {
stringBytes[j] = data[offset + j];
}
strings[i] = string(stringBytes);
offset += stringLengths[i];
}
return strings;
}
function test_ShouldReturnTheMetadataURLs() public {
accounts.createAccount();
accounts.setMetadataURL(metadataURL);
vm.prank(caller2);
accounts.createAccount();
vm.prank(caller2);
accounts.setMetadataURL(otherMetadataURL);
address[] memory addresses = new address[](2);
addresses[0] = address(this);
addresses[1] = address(caller2);
string[] memory urlStrings = new string[](2);
urlStrings[0] = metadataURL;
urlStrings[1] = otherMetadataURL;
(uint256[] memory sizes, bytes memory data) = accounts.batchGetMetadataURL(addresses);
string[] memory returnedStrings = parseSolidityStringArray(sizes, data);
for (uint256 i = 0; i < returnedStrings.length; i++) {
assertEq(returnedStrings[i], urlStrings[i]);
}
}
}
contract AccountsTest_addStorageRoot is AccountsTest {
function setUp() public {
super.setUp();
}
function test_ShouldRevert_WhenAccountHasNotBeenCreated() public {
vm.expectRevert("Unknown account");
accounts.addStorageRoot(storageRoot);
}
function test_ShouldAddANewStorageRoot_WhenAccountHasBeenCreated() public {
accounts.createAccount();
accounts.addStorageRoot(bytes(storageRoot));
(bytes memory concatenated, uint256[] memory length) = accounts.getOffchainStorageRoots(
address(this)
);
string[] memory urls = new string[](1);
urls[0] = metadataURL;
assertStorageRoots(string(concatenated), length, urls);
}
function test_Emits_TheOffchainStorageRootAddedEvent_WhenAccountHasBeenCreated() public {
accounts.createAccount();
vm.expectEmit(true, true, true, true);
emit OffchainStorageRootAdded(address(this), bytes(metadataURL));
accounts.addStorageRoot(bytes(metadataURL));
}
function test_ShouldAddMultipleStorageRoots_WhenAccountHasBeenCreated() public {
accounts.createAccount();
accounts.addStorageRoot(bytes(metadataURL));
accounts.addStorageRoot(bytes(otherMetadataURL));
(bytes memory concatenated, uint256[] memory length) = accounts.getOffchainStorageRoots(
address(this)
);
string[] memory urls = new string[](2);
urls[0] = metadataURL;
urls[1] = otherMetadataURL;
assertStorageRoots(string(concatenated), length, urls);
}
}
contract AccountsTest_removeStorageRoot is AccountsTest {
function setUp() public {
super.setUp();
}
function test_ShouldRevert_WhenAccountHasNotBeenCreated() public {
vm.expectRevert("Unknown account");
accounts.removeStorageRoot(0);
}
function test_ShouldRevertWithMessage_WhenThereAreNoStorageRootsAndAccountHasBeenCreated()
public
{
accounts.createAccount();
vm.expectRevert("Invalid storage root index");
accounts.removeStorageRoot(0);
}
function test_ShouldRemoveOneOfTheRoots_WhenThereAreStorageRootsAndAccountHasBeenCreated()
public
{
accounts.createAccount();
accounts.addStorageRoot(storageRoot);
accounts.addStorageRoot(otherStorageRoot);
accounts.removeStorageRoot(0);
(bytes memory concatenated, uint256[] memory length) = accounts.getOffchainStorageRoots(
address(this)
);
string[] memory urls = new string[](1);
urls[0] = otherMetadataURL;
assertStorageRoots(string(concatenated), length, urls);
}
function test_ShouldRemoveDifferentRoot_WhenThereAreStorageRootsAndAccountHasBeenCreated()
public
{
accounts.createAccount();
accounts.addStorageRoot(storageRoot);
accounts.addStorageRoot(otherStorageRoot);
accounts.removeStorageRoot(1);
(bytes memory concatenated, uint256[] memory length) = accounts.getOffchainStorageRoots(
address(this)
);
string[] memory urls = new string[](1);
urls[0] = metadataURL;
assertStorageRoots(string(concatenated), length, urls);
}
function test_Emits_OffchainStorageRootRemovedEvent_WhenThereAreStorageRootsAndAccountHasBeenCreated()
public
{
accounts.createAccount();
accounts.addStorageRoot(storageRoot);
accounts.addStorageRoot(otherStorageRoot);
vm.expectEmit(true, true, true, true);
emit OffchainStorageRootRemoved(address(this), bytes(otherMetadataURL), 1);
accounts.removeStorageRoot(1);
(bytes memory concatenated, uint256[] memory length) = accounts.getOffchainStorageRoots(
address(this)
);
string[] memory urls = new string[](1);
urls[0] = metadataURL;
assertStorageRoots(string(concatenated), length, urls);
}
}
contract AccountsTest_setPaymentDelegation is AccountsTest {
address beneficiary = actor("beneficiary");
uint256 fraction = FixidityLib.newFixedFraction(2, 10).unwrap();
uint256 badFraction = FixidityLib.newFixedFraction(12, 10).unwrap();
function setUp() public {
super.setUp();
}
function test_ShouldNotBeCallableByNonAccount() public {
vm.expectRevert("Must first register address with Account.createAccount");
accounts.setPaymentDelegation((beneficiary), fraction);
}
function test_ShouldSetAnAddressAndAFraction() public {
accounts.createAccount();
accounts.setPaymentDelegation(beneficiary, fraction);
(address realBeneficiary, uint256 realFraction) = accounts.getPaymentDelegation(address(this));
assertEq(realBeneficiary, beneficiary);
assertEq(realFraction, fraction);
}
function test_Revert_SetPaymentDelegation_WhenL2() public {
_whenL2();
accounts.createAccount();
vm.expectRevert("This method is no longer supported in L2.");
accounts.setPaymentDelegation(beneficiary, fraction);
}
function test_ShouldNotAllowFractionGreaterThan1() public {
accounts.createAccount();
vm.expectRevert("Fraction must not be greater than 1");
accounts.setPaymentDelegation(beneficiary, badFraction);
}
function test_ShouldNotAllowABeneficiaryWithNullAddress() public {
accounts.createAccount();
vm.expectRevert("Beneficiary cannot be address 0x0");
accounts.setPaymentDelegation(address(0), badFraction);
}
function test_Emits_APaymentDelegationSetEvent() public {
accounts.createAccount();
vm.expectEmit(true, true, true, true);
emit PaymentDelegationSet(beneficiary, fraction);
accounts.setPaymentDelegation(beneficiary, fraction);
}
}
contract AccountsTest_deletePaymentDelegation is AccountsTest {
address beneficiary = actor("beneficiary");
uint256 fraction = FixidityLib.newFixedFraction(2, 10).unwrap();
function setUp() public {
super.setUp();
accounts.createAccount();
accounts.setPaymentDelegation(beneficiary, fraction);
}
function test_ShouldNotBeCallableByNonAccount() public {
vm.prank(caller);
vm.expectRevert("Must first register address with Account.createAccount");
accounts.deletePaymentDelegation();
}
function test_ShouldSetTheAddressAndBeneficiaryTo0() public {
accounts.deletePaymentDelegation();
(address realBeneficiary, uint256 realFraction) = accounts.getPaymentDelegation(address(this));
assertEq(realBeneficiary, address(0));
assertEq(realFraction, 0);
}
function test_Emits_APaymentDelegationSetEvent() public {
vm.expectEmit(true, true, true, true);
emit PaymentDelegationSet(address(0), 0);
accounts.deletePaymentDelegation();
}
}
contract AccountsTest_setName is AccountsTest {
function setUp() public {
super.setUp();
}
function test_ShouldNotBeCallableByNonAccount() public {
vm.expectRevert("Register with createAccount to set account name");
accounts.setName(name);
}
function test_ShouldSetTheName() public {
accounts.createAccount();
accounts.setName(name);
assertEq(accounts.getName(address(this)), name);
}
function test_Emits_AccountNameSetEvent() public {
accounts.createAccount();
vm.expectEmit(true, true, true, true);
emit AccountNameSet(address(this), name);
accounts.setName(name);
}
}
contract AccountsTest_GenericAuthorization is AccountsTest {
address account2 = actor("account2");
address signer;
uint256 signerPK;
address signer2;
uint256 signer2PK;
bytes32 role = keccak256("Test Role");
bytes32 role2 = keccak256("Test Role 2");
uint8 v;
bytes32 r;
bytes32 s;
function setUp() public {
super.setUp();
(signer, signerPK) = actorWithPK("signer");
(signer2, signer2PK) = actorWithPK("signer2");
(v, r, s) = getSignatureForAuthorization(
address(this),
role,
signerPK,
31337,
address(accounts)
);
accounts.createAccount();
vm.prank(account2);
accounts.createAccount();
}
function test_ShouldRecoverTheCorrectSignerFromEIP712Signature() public {
address recoveredSigner = accounts.getRoleAuthorizationSigner(
address(this),
signer,
role,
v,
r,
s
);
assertEq(recoveredSigner, signer);
}
function test_ShouldNotCompleteAnAuthorizationThatHasntBeenStarted_WhenSmartContractSigner()
public
{
vm.expectRevert("Signer authorization not started");
vm.prank(signer);
accounts.completeSignerAuthorization(address(this), role);
}
function test_ShouldNotCompleteAuthorizationThatWasOnlyStarted_WhenSmartContractSigner() public {
accounts.authorizeSigner(signer, role);
assertEq(accounts.isSigner(address(this), signer, role), false);
}
function test_ShouldSetAuthorizedSignerInTwoSteps_WhenSmartContractSigner() public {
accounts.authorizeSigner(signer, role);
vm.prank(signer);
accounts.completeSignerAuthorization(address(this), role);
assertEq(accounts.isSigner(address(this), signer, role), true);
assertEq(accounts.authorizedBy(signer), address(this));
assertEq(accounts.isAuthorizedSigner(signer), true);
}
function test_Emits_TheRightEvents_WhenSmartContractSigner() public {
vm.expectEmit(true, true, true, true);
emit SignerAuthorizationStarted(address(this), signer, role);
accounts.authorizeSigner(signer, role);
vm.expectEmit(true, true, true, true);
emit SignerAuthorizationCompleted(address(this), signer, role);
vm.prank(signer);
accounts.completeSignerAuthorization(address(this), role);
}
function test_ShouldSetAuthorizedSignerInOneStep_WhenEOASigner() public {
assertEq(accounts.isSigner(address(this), signer, role), false);
accounts.authorizeSignerWithSignature(signer, role, v, r, s);
assertEq(accounts.isSigner(address(this), signer, role), true);
assertEq(accounts.authorizedBy(signer), address(this));
assertEq(accounts.isAuthorizedSigner(signer), true);
}
function test_Emits_TheRightEvents_WhenEOASigner() public {
vm.expectEmit(true, true, true, true);
emit SignerAuthorized(address(this), signer, role);
accounts.authorizeSignerWithSignature(signer, role, v, r, s);
}
function test_ShouldRemoveTheAuthorizedSigner() public {
accounts.authorizeSignerWithSignature(signer, role, v, r, s);
accounts.removeSigner(signer, role);
assertEq(accounts.isSigner(address(this), signer, role), false);
}
function test_ShouldAuthorizeMultipleSignersForARole() public {
assertEq(accounts.isSigner(address(this), signer, role), false);
assertEq(accounts.isSigner(address(this), signer2, role), false);
accounts.authorizeSignerWithSignature(signer, role, v, r, s);
(uint8 v2, bytes32 r2, bytes32 s2) = getSignatureForAuthorization(
address(this),
role,
signer2PK,
31337,
address(accounts)
);
accounts.authorizeSignerWithSignature(signer2, role, v2, r2, s2);
assertEq(accounts.isSigner(address(this), signer, role), true);
assertEq(accounts.isSigner(address(this), signer2, role), true);
assertEq(accounts.authorizedBy(signer), address(this));
assertEq(accounts.authorizedBy(signer2), address(this));
assertEq(accounts.isAuthorizedSigner(signer), true);
assertEq(accounts.isAuthorizedSigner(signer2), true);
}
function test_ShouldAuthorizeMultipleSignersForMultipleRoles() public {
assertEq(accounts.isSigner(address(this), signer, role), false);
assertEq(accounts.isSigner(address(this), signer2, role2), false);
accounts.authorizeSignerWithSignature(signer, role, v, r, s);
(uint8 v2, bytes32 r2, bytes32 s2) = getSignatureForAuthorization(
address(this),
role2,
signer2PK,
31337,
address(accounts)
);
accounts.authorizeSignerWithSignature(signer2, role2, v2, r2, s2);
assertEq(accounts.isSigner(address(this), signer, role), true);
assertEq(accounts.isSigner(address(this), signer2, role2), true);
assertEq(accounts.authorizedBy(signer), address(this));
assertEq(accounts.authorizedBy(signer2), address(this));
assertEq(accounts.isAuthorizedSigner(signer), true);
assertEq(accounts.isAuthorizedSigner(signer2), true);
}
function test_ShouldNotAllowToAuthorizeSignerForTwoAccounts() public {
accounts.authorizeSignerWithSignature(signer, role, v, r, s);
(uint8 v2, bytes32 r2, bytes32 s2) = getSignatureForAuthorization(
account2,
role,
signerPK,
31337,
address(accounts)
);
vm.expectRevert("Invalid signature");
accounts.authorizeSignerWithSignature(signer, role, v2, r2, s2);
}
function test_ShouldSetDefaultSignerForTheRole() public {
assertEq(accounts.isSigner(address(this), signer, role), false);
assertEq(accounts.hasDefaultSigner(address(this), role), false);
assertEq(accounts.getDefaultSigner(address(this), role), address(this));
vm.expectRevert("Must authorize signer before setting as default");
accounts.setIndexedSigner(signer, role);
accounts.authorizeSignerWithSignature(signer, role, v, r, s);
accounts.setIndexedSigner(signer, role);
assertEq(accounts.isSigner(address(this), signer, role), true);
assertEq(accounts.hasDefaultSigner(address(this), role), true);
assertEq(accounts.getDefaultSigner(address(this), role), signer);
}
function test_ShouldRemoveTheDefaultSignerForARole() public {
accounts.authorizeSignerWithSignature(signer, role, v, r, s);
accounts.setIndexedSigner(signer, role);
accounts.removeDefaultSigner(role);
assertEq(accounts.isSigner(address(this), signer, role), true);
assertEq(accounts.hasDefaultSigner(address(this), role), false);
assertEq(accounts.getDefaultSigner(address(this), role), address(this));
}
}
contract AccountsTest_BackwardCompatibility is AccountsTest {
address account = address(this);
address otherAccount = actor("otherAccount");
address signer;
uint256 signerPK;
address signer2;
uint256 signer2PK;
enum Role {
Attestation,
Vote,
Validator
}
function setUp() public {
super.setUp();
(signer, signerPK) = actorWithPK("signer");
(signer2, signer2PK) = actorWithPK("signer2");
accounts.createAccount();
}
function authorizeSignerFactory(
address _signer,
bytes32 _role,
uint8 _v,
bytes32 _r,
bytes32 _s,
bool expectRevert
) public {
accounts.authorizeSignerWithSignature(_signer, _role, _v, _r, _s);
if (!expectRevert) {
accounts.setIndexedSigner(_signer, _role);
}
}
function getSignature(
address _account,
bytes32 role,
uint256 _signerPK,
bool genericWrite
) public view returns (uint8, bytes32, bytes32) {
if (genericWrite) {
return getSignatureForAuthorization(_account, role, _signerPK, 31337, address(accounts));
}
return getParsedSignatureOfAddress(_account, _signerPK);
}
function getRole(Role role) public pure returns (bytes32 _role) {
if (role == Role.Attestation) {
_role = keccak256(abi.encodePacked("celo.org/core/attestation"));
} else if (role == Role.Vote) {
_role = keccak256(abi.encodePacked("celo.org/core/vote"));
} else if (role == Role.Validator) {
_role = keccak256(abi.encodePacked("celo.org/core/validator"));
}
}
function hasAuthorizedSigner(
Role role,
address _signer,
bool genericRead
) public view returns (bool) {
bytes32 _role = getRole(role);
if (genericRead) {
return accounts.hasIndexedSigner(_signer, _role);
} else {
if (role == Role.Attestation) {
return accounts.hasAuthorizedAttestationSigner(_signer);
} else if (role == Role.Vote) {
return accounts.hasAuthorizedVoteSigner(_signer);
} else if (role == Role.Validator) {
return accounts.hasAuthorizedValidatorSigner(_signer);
}
}
}
function authorize(
Role role,
bool genericWrite,
address _account,
address _signer,
uint256 _signerPK
) public {
bytes32 _role = getRole(role);
(uint8 _v, bytes32 _r, bytes32 _s) = getSignature(_account, _role, _signerPK, genericWrite);
vm.startPrank(_account);
if (genericWrite) {
authorizeSignerFactory(_signer, _role, _v, _r, _s, false);
} else {
if (role == Role.Attestation) {
accounts.authorizeAttestationSigner(_signer, _v, _r, _s);
} else if (role == Role.Vote) {
accounts.authorizeVoteSigner(_signer, _v, _r, _s);
} else if (role == Role.Validator) {
accounts.authorizeValidatorSigner(_signer, _v, _r, _s);
}
}
vm.stopPrank();
}
function removeSigner(bool genericWrite, Role role, address _account) public {
vm.startPrank(_account);
bytes32 _role = getRole(role);
if (genericWrite) {
address defaultSigner = accounts.getIndexedSigner(_account, _role);
accounts.removeSigner(defaultSigner, _role);
} else {
if (role == Role.Attestation) {
accounts.removeAttestationSigner();
} else if (role == Role.Vote) {
accounts.removeVoteSigner();
} else if (role == Role.Validator) {
accounts.removeValidatorSigner();
}
}
vm.stopPrank();
}
function getAuthorizedFromAccount(
Role role,
bool genericRead,