/
WalletRegistry.sol
939 lines (820 loc) · 37.9 KB
/
WalletRegistry.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
// SPDX-License-Identifier: MIT
//
// ▓▓▌ ▓▓ ▐▓▓ ▓▓▓▓▓▓▓▓▓▓▌▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▄
// ▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▌▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
// ▓▓▓▓▓▓ ▓▓▓▓▓▓▓▀ ▐▓▓▓▓▓▓ ▐▓▓▓▓▓ ▓▓▓▓▓▓ ▓▓▓▓▓ ▐▓▓▓▓▓▌ ▐▓▓▓▓▓▓
// ▓▓▓▓▓▓▄▄▓▓▓▓▓▓▓▀ ▐▓▓▓▓▓▓▄▄▄▄ ▓▓▓▓▓▓▄▄▄▄ ▐▓▓▓▓▓▌ ▐▓▓▓▓▓▓
// ▓▓▓▓▓▓▓▓▓▓▓▓▓▀ ▐▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓ ▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
// ▓▓▓▓▓▓▀▀▓▓▓▓▓▓▄ ▐▓▓▓▓▓▓▀▀▀▀ ▓▓▓▓▓▓▀▀▀▀ ▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▀
// ▓▓▓▓▓▓ ▀▓▓▓▓▓▓▄ ▐▓▓▓▓▓▓ ▓▓▓▓▓ ▓▓▓▓▓▓ ▓▓▓▓▓ ▐▓▓▓▓▓▌
// ▓▓▓▓▓▓▓▓▓▓ █▓▓▓▓▓▓▓▓▓ ▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓
// ▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓ ▐▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓
//
// Trust math, not hardware.
pragma solidity ^0.8.9;
import "./api/IWalletRegistry.sol";
import "./api/IWalletOwner.sol";
import "./libraries/EcdsaAuthorization.sol";
import "./libraries/EcdsaDkg.sol";
import "./libraries/Wallets.sol";
import "./libraries/EcdsaInactivity.sol";
import "./EcdsaDkgValidator.sol";
import "./Governable.sol";
import "@keep-network/sortition-pools/contracts/SortitionPool.sol";
import "@keep-network/random-beacon/contracts/api/IRandomBeacon.sol";
import "@keep-network/random-beacon/contracts/api/IRandomBeaconConsumer.sol";
import "@keep-network/random-beacon/contracts/Reimbursable.sol";
import "@keep-network/random-beacon/contracts/ReimbursementPool.sol";
import "@threshold-network/solidity-contracts/contracts/staking/IApplication.sol";
import "@threshold-network/solidity-contracts/contracts/staking/IStaking.sol";
// TODO: We used RC version of @openzeppelin/contracts-upgradeable to use `reinitializer`
// in upgrades. We should revisit this part before mainnet deployment and use
// a final release package if it's ready.
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
contract WalletRegistry is
IWalletRegistry,
IRandomBeaconConsumer,
IApplication,
Governable,
Reimbursable,
Initializable
{
using EcdsaAuthorization for EcdsaAuthorization.Data;
using EcdsaDkg for EcdsaDkg.Data;
using Wallets for Wallets.Data;
// Libraries data storages
EcdsaAuthorization.Data internal authorization;
EcdsaDkg.Data internal dkg;
Wallets.Data internal wallets;
// Address that is set as owner of all wallets. Only this address can request
// new wallets creation and manage their state.
IWalletOwner public walletOwner;
/// @notice Slashing amount for supporting malicious DKG result. Every
/// DKG result submitted can be challenged for the time of DKG's
/// `resultChallengePeriodLength` parameter. If the DKG result submitted
/// is challenged and proven to be malicious, each operator who
/// signed the malicious result is slashed for
/// `maliciousDkgResultSlashingAmount`.
uint96 public maliciousDkgResultSlashingAmount;
/// @notice Percentage of the staking contract malicious behavior
/// notification reward which will be transferred to the notifier
/// reporting about a malicious DKG result. Notifiers are rewarded
/// from a notifiers treasury pool. For example, if
/// notification reward is 1000 and the value of the multiplier is
/// 5, the notifier will receive: 5% of 1000 = 50 per each
/// operator affected.
uint256 public maliciousDkgResultNotificationRewardMultiplier;
/// @notice Calculated max gas cost for submitting a DKG result. This will
/// be refunded as part of the DKG approval process. It is in the
/// submitter's interest to not skip his priority turn on the approval,
/// otherwise the refund of the DKG submission will be refunded to
/// other member that will call the DKG approve function.
uint256 public dkgResultSubmissionGas;
/// @notice Gas is meant to balance the DKG result approval's overall cost.
/// It can be updated by the governance based on the current market
/// conditions.
uint256 public dkgResultApprovalGasOffset;
/// @notice Gas is meant to balance the notification of an operator inactivity.
/// It can be updated by the governace based on the current market
/// conditions.
uint256 public notifyOperatorInactivityGasOffset;
/// @notice Duration of the sortition pool rewards ban imposed on operators
/// who missed their turn for DKG result submission or who failed
/// a heartbeat.
uint256 public sortitionPoolRewardsBanDuration;
/// @notice Stores current operator inactivity claim nonce for the given
/// wallet signing group. Each claim is made with a unique nonce
/// which protects against claim replay.
mapping(bytes32 => uint256) public inactivityClaimNonce; // walletID -> nonce
// External dependencies
SortitionPool public sortitionPool;
IStaking public staking;
IRandomBeacon public randomBeacon;
// Events
event DkgStarted(uint256 indexed seed);
event DkgResultSubmitted(
bytes32 indexed resultHash,
uint256 indexed seed,
EcdsaDkg.Result result
);
event DkgTimedOut();
event DkgResultApproved(
bytes32 indexed resultHash,
address indexed approver
);
event DkgResultChallenged(
bytes32 indexed resultHash,
address indexed challenger,
string reason
);
event DkgStateLocked();
event DkgSeedTimedOut();
event WalletCreated(
bytes32 indexed walletID,
bytes32 indexed dkgResultHash
);
event WalletClosed(bytes32 indexed walletID);
event DkgMaliciousResultSlashed(
bytes32 indexed resultHash,
uint256 slashingAmount,
address maliciousSubmitter
);
event DkgMaliciousResultSlashingFailed(
bytes32 indexed resultHash,
uint256 slashingAmount,
address maliciousSubmitter
);
event AuthorizationParametersUpdated(
uint96 minimumAuthorization,
uint64 authorizationDecreaseDelay
);
event RewardParametersUpdated(
uint256 maliciousDkgResultNotificationRewardMultiplier,
uint256 sortitionPoolRewardsBanDuration
);
event SlashingParametersUpdated(uint256 maliciousDkgResultSlashingAmount);
event DkgParametersUpdated(
uint256 seedTimeout,
uint256 resultChallengePeriodLength,
uint256 resultSubmissionTimeout,
uint256 resultSubmitterPrecedencePeriodLength
);
event GasParametersUpdated(
uint256 dkgResultSubmissionGas,
uint256 dkgResultApprovalGasOffset,
uint256 notifyOperatorInactivityGasOffset
);
event RandomBeaconUpgraded(address randomBeacon);
event WalletOwnerUpdated(address walletOwner);
event OperatorRegistered(
address indexed stakingProvider,
address indexed operator
);
event AuthorizationIncreased(
address indexed stakingProvider,
address indexed operator,
uint96 fromAmount,
uint96 toAmount
);
event AuthorizationDecreaseRequested(
address indexed stakingProvider,
address indexed operator,
uint96 fromAmount,
uint96 toAmount,
uint64 decreasingAt
);
event AuthorizationDecreaseApproved(address indexed stakingProvider);
event InvoluntaryAuthorizationDecreaseFailed(
address indexed stakingProvider,
address indexed operator,
uint96 fromAmount,
uint96 toAmount
);
event OperatorJoinedSortitionPool(
address indexed stakingProvider,
address indexed operator
);
event OperatorStatusUpdated(
address indexed stakingProvider,
address indexed operator
);
event InactivityClaimed(
bytes32 indexed walletID,
uint256 nonce,
address notifier
);
modifier onlyStakingContract() {
require(
msg.sender == address(staking),
"Caller is not the staking contract"
);
_;
}
/// @notice Reverts if called not by the Wallet Owner.
modifier onlyWalletOwner() {
require(
msg.sender == address(walletOwner),
"Caller is not the Wallet Owner"
);
_;
}
modifier onlyReimbursableAdmin() override {
require(governance == msg.sender, "Caller is not the governance");
_;
}
// TODO: Could the constructor remain?
function initialize(
SortitionPool _sortitionPool,
IStaking _staking,
EcdsaDkgValidator _ecdsaDkgValidator,
IRandomBeacon _randomBeacon,
ReimbursementPool _reimbursementPool
) external initializer {
sortitionPool = _sortitionPool;
staking = _staking;
randomBeacon = _randomBeacon;
reimbursementPool = _reimbursementPool;
// TODO: revisit all initial values
dkgResultSubmissionGas = 272_000;
dkgResultApprovalGasOffset = 63_000;
notifyOperatorInactivityGasOffset = 86_000;
sortitionPoolRewardsBanDuration = 2 weeks;
// slither-disable-next-line too-many-digits
authorization.setMinimumAuthorization(400000e18); // 400k T
authorization.setAuthorizationDecreaseDelay(5184000); // 60 days
maliciousDkgResultSlashingAmount = 50000e18;
maliciousDkgResultNotificationRewardMultiplier = 100;
dkg.init(_sortitionPool, _ecdsaDkgValidator);
dkg.setSeedTimeout(1440); // ~6h assuming 15s block time
dkg.setResultChallengePeriodLength(11520); // ~48h assuming 15s block time
dkg.setResultSubmissionTimeout(100 * 20);
dkg.setSubmitterPrecedencePeriodLength(20);
_transferGovernance(msg.sender);
}
/// @notice Used by staking provider to set operator address that will
/// operate ECDSA node. The given staking provider can set operator
/// address only one time. The operator address can not be changed
/// and must be unique. Reverts if the operator is already set for
/// the staking provider or if the operator address is already in
/// use. Reverts if there is a pending authorization decrease for
/// the staking provider.
function registerOperator(address operator) external {
authorization.registerOperator(operator);
}
/// @notice Lets the operator join the sortition pool. The operator address
/// must be known - before calling this function, it has to be
/// appointed by the staking provider by calling `registerOperator`.
/// Also, the operator must have the minimum authorization required
/// by ECDSA. Function reverts if there is no minimum stake
/// authorized or if the operator is not known. If there was an
/// authorization decrease requested, it is activated by starting
/// the authorization decrease delay.
function joinSortitionPool() external {
authorization.joinSortitionPool(staking, sortitionPool);
}
/// @notice Updates status of the operator in the sortition pool. If there
/// was an authorization decrease requested, it is activated by
/// starting the authorization decrease delay.
/// Function reverts if the operator is not known.
function updateOperatorStatus(address operator) external {
authorization.updateOperatorStatus(staking, sortitionPool, operator);
}
/// @notice Used by T staking contract to inform the application that the
/// authorized stake amount for the given staking provider increased.
///
/// Reverts if the authorization amount is below the minimum.
///
/// The function is not updating the sortition pool. Sortition pool
/// state needs to be updated by the operator with a call to
/// `joinSortitionPool` or `updateOperatorStatus`.
///
/// @dev Can only be called by T staking contract.
function authorizationIncreased(
address stakingProvider,
uint96 fromAmount,
uint96 toAmount
) external onlyStakingContract {
authorization.authorizationIncreased(
stakingProvider,
fromAmount,
toAmount
);
}
/// @notice Used by T staking contract to inform the application that the
/// authorization decrease for the given staking provider has been
/// requested.
///
/// Reverts if the amount after deauthorization would be non-zero
/// and lower than the minimum authorization.
///
/// If the operator is not known (`registerOperator` was not called)
/// it lets to `approveAuthorizationDecrease` immediatelly. If the
/// operator is known (`registerOperator` was called), the operator
/// needs to update state of the sortition pool with a call to
/// `joinSortitionPool` or `updateOperatorStatus`. After the
/// sortition pool state is in sync, authorization decrease delay
/// starts.
///
/// After authorization decrease delay passes, authorization
/// decrease request needs to be approved with a call to
/// `approveAuthorizationDecrease` function.
///
/// If there is a pending authorization decrease request, it is
/// overwritten.
///
/// @dev Can only be called by T staking contract.
function authorizationDecreaseRequested(
address stakingProvider,
uint96 fromAmount,
uint96 toAmount
) external onlyStakingContract {
authorization.authorizationDecreaseRequested(
stakingProvider,
fromAmount,
toAmount
);
}
/// @notice Approves the previously registered authorization decrease
/// request. Reverts if authorization decrease delay has not passed
/// yet or if the authorization decrease was not requested for the
/// given staking provider.
function approveAuthorizationDecrease(address stakingProvider) external {
authorization.approveAuthorizationDecrease(staking, stakingProvider);
}
/// @notice Used by T staking contract to inform the application the
/// authorization has been decreased for the given staking provider
/// involuntarily, as a result of slashing.
///
/// If the operator is not known (`registerOperator` was not called)
/// the function does nothing. The operator was never in a sortition
/// pool so there is nothing to update.
///
/// If the operator is known, sortition pool is unlocked, and the
/// operator is in the sortition pool, the sortition pool state is
/// updated. If the sortition pool is locked, update needs to be
/// postponed. Every other staker is incentivized to call
/// `updateOperatorStatus` for the problematic operator to increase
/// their own rewards in the pool.
function involuntaryAuthorizationDecrease(
address stakingProvider,
uint96 fromAmount,
uint96 toAmount
) external onlyStakingContract {
authorization.involuntaryAuthorizationDecrease(
staking,
sortitionPool,
stakingProvider,
fromAmount,
toAmount
);
}
/// @notice Updates address of the Random Beacon.
/// @dev Can be called only by the contract guvnor, which should be the
/// wallet registry governance contract. The caller is responsible for
/// validating parameters.
/// @param _randomBeacon Random Beacon address.
function upgradeRandomBeacon(IRandomBeacon _randomBeacon)
external
onlyGovernance
{
randomBeacon = _randomBeacon;
emit RandomBeaconUpgraded(address(_randomBeacon));
}
/// @notice Updates the wallet owner.
/// @dev Can be called only by the contract owner, which should be the
/// wallet registry governance contract. The caller is responsible for
/// validating parameters. The wallet owner has to implement `IWalletOwner`
/// interface.
/// @param _walletOwner New wallet owner address.
function updateWalletOwner(IWalletOwner _walletOwner)
external
onlyGovernance
{
walletOwner = _walletOwner;
emit WalletOwnerUpdated(address(_walletOwner));
}
/// @notice Updates the values of authorization parameters.
/// @dev Can be called only by the contract guvnor, which should be the
/// wallet registry governance contract. The caller is responsible for
/// validating parameters.
/// @param _minimumAuthorization New minimum authorization amount
/// @param _authorizationDecreaseDelay New authorization decrease delay in
/// seconds
function updateAuthorizationParameters(
uint96 _minimumAuthorization,
uint64 _authorizationDecreaseDelay
) external onlyGovernance {
authorization.setMinimumAuthorization(_minimumAuthorization);
authorization.setAuthorizationDecreaseDelay(
_authorizationDecreaseDelay
);
emit AuthorizationParametersUpdated(
_minimumAuthorization,
_authorizationDecreaseDelay
);
}
/// @notice Updates the values of DKG parameters.
/// @dev Can be called only by the contract guvnor, which should be the
/// wallet registry governance contract. The caller is responsible for
/// validating parameters.
/// @param _seedTimeout New seed timeout.
/// @param _resultChallengePeriodLength New DKG result challenge period
/// length
/// @param _resultSubmissionTimeout New DKG result submission timeout
/// @param _submitterPrecedencePeriodLength New submitter precedence period
/// length
function updateDkgParameters(
uint256 _seedTimeout,
uint256 _resultChallengePeriodLength,
uint256 _resultSubmissionTimeout,
uint256 _submitterPrecedencePeriodLength
) external onlyGovernance {
dkg.setSeedTimeout(_seedTimeout);
dkg.setResultChallengePeriodLength(_resultChallengePeriodLength);
dkg.setResultSubmissionTimeout(_resultSubmissionTimeout);
dkg.setSubmitterPrecedencePeriodLength(
_submitterPrecedencePeriodLength
);
// slither-disable-next-line reentrancy-events
emit DkgParametersUpdated(
_seedTimeout,
_resultChallengePeriodLength,
_resultSubmissionTimeout,
_submitterPrecedencePeriodLength
);
}
/// @notice Updates the values of reward parameters.
/// @dev Can be called only by the contract guvnor, which should be the
/// wallet registry governance contract. The caller is responsible for
/// validating parameters.
/// @param _maliciousDkgResultNotificationRewardMultiplier New value of the
/// DKG malicious result notification reward multiplier.
/// @param _sortitionPoolRewardsBanDuration New sortition pool rewards
/// ban duration in seconds.
function updateRewardParameters(
uint256 _maliciousDkgResultNotificationRewardMultiplier,
uint256 _sortitionPoolRewardsBanDuration
) external onlyGovernance {
maliciousDkgResultNotificationRewardMultiplier = _maliciousDkgResultNotificationRewardMultiplier;
sortitionPoolRewardsBanDuration = _sortitionPoolRewardsBanDuration;
emit RewardParametersUpdated(
_maliciousDkgResultNotificationRewardMultiplier,
_sortitionPoolRewardsBanDuration
);
}
/// @notice Updates the values of slashing parameters.
/// @dev Can be called only by the contract guvnor, which should be the
/// wallet registry governance contract. The caller is responsible for
/// validating parameters.
/// @param _maliciousDkgResultSlashingAmount New malicious DKG result
/// slashing amount
function updateSlashingParameters(uint96 _maliciousDkgResultSlashingAmount)
external
onlyGovernance
{
maliciousDkgResultSlashingAmount = _maliciousDkgResultSlashingAmount;
emit SlashingParametersUpdated(_maliciousDkgResultSlashingAmount);
}
/// @notice Updates the values of gas-related parameters.
/// @dev Can be called only by the contract guvnor, which should be the
/// wallet registry governance contract. The caller is responsible for
/// validating parameters.
function updateGasParameters(
uint256 _dkgResultSubmissionGas,
uint256 _dkgResultApprovalGasOffset,
uint256 _notifyOperatorInactivityGasOffset
) external onlyGovernance {
dkgResultSubmissionGas = _dkgResultSubmissionGas;
dkgResultApprovalGasOffset = _dkgResultApprovalGasOffset;
notifyOperatorInactivityGasOffset = _notifyOperatorInactivityGasOffset;
emit GasParametersUpdated(
_dkgResultSubmissionGas,
_dkgResultApprovalGasOffset,
_notifyOperatorInactivityGasOffset
);
}
/// @notice Requests a new wallet creation.
/// @dev Can be called only by the owner of wallets.
/// It locks the DKG and request a new relay entry. It expects
/// that the DKG process will be started once a new relay entry
/// gets generated.
function requestNewWallet() external onlyWalletOwner {
dkg.lockState();
randomBeacon.requestRelayEntry(this);
}
/// @notice Closes an existing wallet. Reverts if wallet with the given ID
/// does not exist or if it has already been closed.
/// @param walletID ID of the wallet.
/// @dev Only a Wallet Owner can call this function.
function closeWallet(bytes32 walletID) external onlyWalletOwner {
wallets.deleteWallet(walletID);
emit WalletClosed(walletID);
}
/// @notice A callback that is executed once a new relay entry gets
/// generated. It starts the DKG process.
/// @dev Can be called only by the random beacon contract.
/// @param relayEntry Relay entry.
function __beaconCallback(uint256 relayEntry, uint256) external {
require(
msg.sender == address(randomBeacon),
"Caller is not the Random Beacon"
);
dkg.start(relayEntry);
}
/// @notice Submits result of DKG protocol.
/// The DKG result consists of result submitting member index,
/// calculated group public key, bytes array of misbehaved members,
/// concatenation of signatures from group members, indices of members
/// corresponding to each signature and the list of group members.
/// The result is registered optimistically and waits for an approval.
/// The result can be challenged when it is believed to be incorrect.
/// The challenge verifies the registered result i.a. it checks if members
/// list corresponds to the expected set of members determined
/// by the sortition pool.
/// @dev The message to be signed by each member is keccak256 hash of the
/// calculated group public key, misbehaved members indices and DKG
/// start block. The calculated hash should be prefixed with prefixed with
/// `\x19Ethereum signed message:\n` before signing, so the message to
/// sign is:
/// `\x19Ethereum signed message:\n${keccak256(groupPubKey,misbehavedIndices,startBlock)}`
/// @param dkgResult DKG result.
function submitDkgResult(EcdsaDkg.Result calldata dkgResult) external {
dkg.submitResult(dkgResult);
}
/// @notice Approves DKG result. Can be called when the challenge period for
/// the submitted result is finished. Considers the submitted result
/// as valid, pays reward to the approver, bans misbehaved group
/// members from the sortition pool rewards, and completes the group
/// creation by activating the candidate group. For the first
/// `resultSubmissionTimeout` blocks after the end of the
/// challenge period can be called only by the DKG result submitter.
/// After that time, can be called by anyone.
/// A new wallet based on the DKG result details.
/// @param dkgResult Result to approve. Must match the submitted result
/// stored during `submitDkgResult`.
function approveDkgResult(EcdsaDkg.Result calldata dkgResult) external {
uint256 gasStart = gasleft();
uint32[] memory misbehavedMembers = dkg.approveResult(dkgResult);
(bytes32 walletID, bytes32 publicKeyX, bytes32 publicKeyY) = wallets
.addWallet(dkgResult.membersHash, dkgResult.groupPubKey);
emit WalletCreated(walletID, keccak256(abi.encode(dkgResult)));
if (misbehavedMembers.length > 0) {
sortitionPool.setRewardIneligibility(
misbehavedMembers,
// solhint-disable-next-line not-rely-on-time
block.timestamp + sortitionPoolRewardsBanDuration
);
}
walletOwner.__ecdsaWalletCreatedCallback(
walletID,
publicKeyX,
publicKeyY
);
dkg.complete();
// Refunds msg.sender's ETH for dkg result submission & dkg approval
reimbursementPool.refund(
dkgResultSubmissionGas +
(gasStart - gasleft()) +
dkgResultApprovalGasOffset,
msg.sender
);
}
/// @notice Notifies about seed for DKG delivery timeout. It is expected
/// that a seed is delivered by the Random Beacon as a relay entry in a
/// callback function.
function notifySeedTimeout() external refundable(msg.sender) {
dkg.notifySeedTimeout();
dkg.complete();
}
/// @notice Notifies about DKG timeout.
function notifyDkgTimeout() external refundable(msg.sender) {
dkg.notifyDkgTimeout();
dkg.complete();
}
/// @notice Challenges DKG result. If the submitted result is proved to be
/// invalid it reverts the DKG back to the result submission phase.
/// @param dkgResult Result to challenge. Must match the submitted result
/// stored during `submitDkgResult`.
function challengeDkgResult(EcdsaDkg.Result calldata dkgResult) external {
(
bytes32 maliciousDkgResultHash,
uint32 maliciousDkgResultSubmitterId
) = dkg.challengeResult(dkgResult);
address maliciousDkgResultSubmitterAddress = sortitionPool
.getIDOperator(maliciousDkgResultSubmitterId);
address[] memory operatorWrapper = new address[](1);
operatorWrapper[0] = operatorToStakingProvider(
maliciousDkgResultSubmitterAddress
);
try
staking.seize(
maliciousDkgResultSlashingAmount,
maliciousDkgResultNotificationRewardMultiplier,
msg.sender,
operatorWrapper
)
{
// slither-disable-next-line reentrancy-events
emit DkgMaliciousResultSlashed(
maliciousDkgResultHash,
maliciousDkgResultSlashingAmount,
maliciousDkgResultSubmitterAddress
);
} catch {
// Should never happen but we want to ensure a non-critical path
// failure from an external contract does not stop the challenge
// to complete.
emit DkgMaliciousResultSlashingFailed(
maliciousDkgResultHash,
maliciousDkgResultSlashingAmount,
maliciousDkgResultSubmitterAddress
);
}
}
/// @notice Notifies about operators who are inactive. Using this function,
/// a majority of the wallet signing group can decide about
/// punishing specific group members who constantly fail doing their
/// job. If the provided claim is proved to be valid and signed by
/// sufficient number of group members, operators of members deemed
/// as inactive are banned from sortition pool rewards for the
/// duration specified by `sortitionPoolRewardsBanDuration` parameter.
/// The function allows to signal about single operators being
/// inactive as well as to signal wallet-wide heartbeat failures
/// that are propagated to the wallet owner who should begin the
/// procedure of moving responsibilities to another wallet given
/// that the wallet who failed the heartbeat may soon be not able to
/// function and provide new signatures.
/// The sender of the claim must be one of the claim signers. This
/// function can be called only for registered wallets
/// @param claim Operator inactivity claim
/// @param nonce Current inactivity claim nonce for the given wallet signing
/// group. Must be the same as the stored one
/// @param groupMembers Identifiers of the wallet signing group members
function notifyOperatorInactivity(
EcdsaInactivity.Claim calldata claim,
uint256 nonce,
uint32[] calldata groupMembers
) external {
uint256 gasStart = gasleft();
bytes32 walletID = claim.walletID;
require(nonce == inactivityClaimNonce[walletID], "Invalid nonce");
(bytes32 pubKeyX, bytes32 pubKeyY) = wallets
.getWalletPublicKeyCoordinates(walletID);
bytes32 memberIdsHash = wallets.getWalletMembersIdsHash(walletID);
require(
memberIdsHash == keccak256(abi.encode(groupMembers)),
"Invalid group members"
);
uint32[] memory ineligibleOperators = EcdsaInactivity.verifyClaim(
sortitionPool,
claim,
bytes.concat(pubKeyX, pubKeyY),
nonce,
groupMembers
);
inactivityClaimNonce[walletID]++;
emit InactivityClaimed(walletID, nonce, msg.sender);
sortitionPool.setRewardIneligibility(
ineligibleOperators,
// solhint-disable-next-line not-rely-on-time
block.timestamp + sortitionPoolRewardsBanDuration
);
if (claim.heartbeatFailed) {
walletOwner.__ecdsaWalletHeartbeatFailedCallback(
walletID,
pubKeyX,
pubKeyY
);
}
reimbursementPool.refund(
(gasStart - gasleft()) + notifyOperatorInactivityGasOffset,
msg.sender
);
}
/// @notice Checks if DKG result is valid for the current DKG.
/// @param result DKG result.
/// @return True if the result is valid. If the result is invalid it returns
/// false and an error message.
function isDkgResultValid(EcdsaDkg.Result calldata result)
external
view
returns (bool, string memory)
{
return dkg.isResultValid(result);
}
/// @notice Check current wallet creation state.
function getWalletCreationState() external view returns (EcdsaDkg.State) {
return dkg.currentState();
}
/// @notice Checks if awaiting seed timed out.
/// @return True if awaiting seed timed out, false otherwise.
function hasSeedTimedOut() external view returns (bool) {
return dkg.hasSeedTimedOut();
}
/// @notice Checks if DKG timed out. The DKG timeout period includes time required
/// for off-chain protocol execution and time for the result publication
/// for all group members. After this time result cannot be submitted
/// and DKG can be notified about the timeout.
/// @return True if DKG timed out, false otherwise.
function hasDkgTimedOut() external view returns (bool) {
return dkg.hasDkgTimedOut();
}
function getWallet(bytes32 walletID)
external
view
returns (Wallets.Wallet memory)
{
return wallets.registry[walletID];
}
/// @notice Gets public key of a wallet with a given wallet ID.
/// The public key is returned in an uncompressed format as a 64-byte
/// concatenation of X and Y coordinates.
/// @param walletID ID of the wallet.
/// @return Uncompressed public key of the wallet.
function getWalletPublicKey(bytes32 walletID)
external
view
returns (bytes memory)
{
return wallets.getWalletPublicKey(walletID);
}
/// @notice Checks if a wallet with the given ID is registered.
/// @param walletID Wallet's ID.
/// @return True if wallet is registered, false otherwise.
function isWalletRegistered(bytes32 walletID) external view returns (bool) {
return wallets.isWalletRegistered(walletID);
}
/// @notice Retrieves dkg parameters that were set in DKG library.
function dkgParameters()
external
view
returns (EcdsaDkg.Parameters memory)
{
return dkg.parameters;
}
/// @notice The minimum authorization amount required so that operator can
/// participate in ECDSA Wallet operations.
function minimumAuthorization() external view returns (uint96) {
return authorization.parameters.minimumAuthorization;
}
/// @notice Delay in seconds that needs to pass between the time
/// authorization decrease is requested and the time that request
/// can get approved.
function authorizationDecreaseDelay() external view returns (uint64) {
return authorization.parameters.authorizationDecreaseDelay;
}
/// @notice Returns the current value of the staking provider's eligible
/// stake. Eligible stake is defined as the currently authorized
/// stake minus the pending authorization decrease. Eligible stake
/// is what is used for operator's weight in the sortition pool.
/// If the authorized stake minus the pending authorization decrease
/// is below the minimum authorization, eligible stake is 0.
function eligibleStake(address stakingProvider)
external
view
returns (uint96)
{
return authorization.eligibleStake(staking, stakingProvider);
}
/// @notice Returns the amount of stake that is pending authorization
/// decrease for the given staking provider. If no authorization
/// decrease has been requested, returns zero.
function pendingAuthorizationDecrease(address stakingProvider)
external
view
returns (uint96)
{
return authorization.pendingAuthorizationDecrease(stakingProvider);
}
/// @notice Returns the remaining time in seconds that needs to pass before
/// the requested authorization decrease can be approved.
/// If the sortition pool state was not updated yet by the operator
/// after requesting the authorization decrease, returns
/// `type(uint64).max`.
function remainingAuthorizationDecreaseDelay(address stakingProvider)
external
view
returns (uint64)
{
return
authorization.remainingAuthorizationDecreaseDelay(stakingProvider);
}
/// @notice Returns operator registered for the given staking provider.
function stakingProviderToOperator(address stakingProvider)
external
view
returns (address)
{
return authorization.stakingProviderToOperator[stakingProvider];
}
/// @notice Returns staking provider of the given operator.
function operatorToStakingProvider(address operator)
public
view
returns (address)
{
return authorization.operatorToStakingProvider[operator];
}
/// @notice Checks if the operator's authorized stake is in sync with
/// operator's weight in the sortition pool.
/// If the operator is not in the sortition pool and their
/// authorized stake is non-zero, function returns false.
function isOperatorUpToDate(address operator) external view returns (bool) {
return
authorization.isOperatorUpToDate(staking, sortitionPool, operator);
}
/// @notice Returns true if the given operator is in the sortition pool.
/// Otherwise, returns false.
function isOperatorInPool(address operator) external view returns (bool) {
return sortitionPool.isOperatorInPool(operator);
}
/// @notice Selects a new group of operators based on the provided seed.
/// At least one operator has to be registered in the pool,
/// otherwise the function fails reverting the transaction.
/// @param seed Number used to select operators to the group.
/// @return IDs of selected group members.
function selectGroup(bytes32 seed) external view returns (uint32[] memory) {
// TODO: Read seed from EcdsaDkg
return sortitionPool.selectGroup(EcdsaDkg.groupSize, seed);
}
}