-
Notifications
You must be signed in to change notification settings - Fork 1
/
MultiSigWalletBase.sol
566 lines (489 loc) · 17.2 KB
/
MultiSigWalletBase.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import { IMultiSigWallet } from "./IMultiSigWallet.sol";
import { MultiSigWalletStorage } from "./MultiSigWalletStorage.sol";
/**
* @title MultiSigWalletBase contract
* @author CloudWalk Inc.
* @dev The base of the multi-signature wallet contract.
*/
abstract contract MultiSigWalletBase is MultiSigWalletStorage, IMultiSigWallet {
// --------------------------- Constants ---------------------------
/// @dev The minimal transaction expiration time.
uint256 public constant MINIMUM_EXPIRATION_TIME = 60 minutes;
// --------------------------- Errors ---------------------------
/// @dev An unauthorized account called a function.
error UnauthorizedCaller();
/// @dev A transaction with the specified ID does not exist.
error TransactionNotExist();
/// @dev A transaction with the specified ID is already executed.
error TransactionAlreadyExecuted();
/// @dev A transaction with the specified ID must be approved by the caller.
error TransactionNotApproved();
/// @dev A transaction with the specified ID is already approved by the caller.
error TransactionAlreadyApproved();
/// @dev An empty array of addresses was passed when configuring the wallet owners.
error EmptyOwnersArray();
/// @dev The zero address was passed within the owners array when configuring the wallet owners.
error ZeroOwnerAddress();
/// @dev A duplicate address was passed within the owners array when configuring the wallet owners.
error DuplicateOwnerAddress();
/// @dev An invalid number of required approvals was passed when configuring the wallet owners.
error InvalidRequiredApprovals();
/// @dev The number of approvals for a given transaction is less than the required minimum.
error NotEnoughApprovals();
/// @dev A low level call/transaction to the transaction receiver failed.
error InternalTransactionFailed(bytes data);
/// @dev A transaction with the specified ID has already expired.
error TransactionExpired();
/// @dev A transaction with the specified ID is on cooldown.
error CooldownNotEnded();
/// @dev The invalid amount of time was passed when configuring the expiration time.
error InvalidExpirationTime();
// ------------------------- Modifiers --------------------------
/**
* @dev Throws if called by any account other than a wallet owner.
*/
modifier onlyOwner() {
if (!_isOwner[msg.sender]) {
revert UnauthorizedCaller();
}
_;
}
/**
* @dev Throws if called by any account other than the contract itself.
*/
modifier onlySelfCall() {
if (msg.sender != address(this)) {
revert UnauthorizedCaller();
}
_;
}
// ------------------------- Functions --------------------------
/**
* @dev Called when native tokens are sent to the contract.
*
* Emits a {Deposit} event.
*/
receive() external payable {
emit Deposit(msg.sender, msg.value);
}
/**
* @dev See {IMultiSigWallet-submit}.
*
* Requirements:
*
* - The caller must be a wallet owner.
*/
function submit(
address to,
uint256 value,
bytes calldata data
) external onlyOwner {
_submit(to, value, data);
}
/**
* @dev See {IMultiSigWallet-submitAndApprove}.
*
* Requirements:
*
* - The caller must be a wallet owner.
*/
function submitAndApprove(
address to,
uint256 value,
bytes calldata data
) external onlyOwner {
_approve(_submit(to, value, data));
}
/**
* @inheritdoc IMultiSigWallet
*
* @dev Requirements:
*
* - The caller must be a wallet owner.
* - The transaction with the given ID must exist.
* - The transaction with the given ID must not be expired.
* - The transaction with the given ID must not be executed.
* - The transaction with the given ID must not be already approved by the caller.
*/
function approve(uint256 txId) external onlyOwner {
_approve(txId);
}
/**
* @inheritdoc IMultiSigWallet
*
* @dev Requirements:
*
* - The caller must be a wallet owner.
* - The transactions with the given IDs must exist.
* - The transactions with the given IDs must not be expired.
* - The transactions with the given IDs must not be executed.
* - The transactions with the given IDs must not be already approved by the caller.
*/
function approveBatch(uint256[] calldata txIds) external onlyOwner {
uint256 count = txIds.length;
for (uint256 i = 0; i < count; ++i) {
_approve(txIds[i]);
}
}
/**
* @inheritdoc IMultiSigWallet
*
* @dev Requirements:
*
* - The caller must be a wallet owner.
* - The transaction with the given ID must exist.
* - The transaction with the given ID must not be expired.
* - The transaction with the given ID must not be executed.
* - The transaction with the given ID must not be on cooldown.
* - The transaction with the given ID must not be already approved by the caller.
* - The transaction with the given ID must have at least the required number of approvals minus one.
*/
function approveAndExecute(uint256 txId) external onlyOwner {
_approve(txId);
_execute(txId);
}
/**
* @inheritdoc IMultiSigWallet
*
* @dev Requirements:
*
* - The caller must be a wallet owner.
* - The transactions with the given IDs must exist.
* - The transactions with the given IDs must not be expired.
* - The transactions with the given IDs must not be executed.
* - The transactions with the given IDs must not be on cooldown.
* - The transactions with the given IDs must not be already approved by the caller.
* - The transactions with the given IDs must have at least the required number of approvals minus one.
*/
function approveAndExecuteBatch(uint256[] calldata txIds) external onlyOwner {
uint256 count = txIds.length;
for (uint256 i = 0; i < count; ++i) {
uint256 txId = txIds[i];
_approve(txId);
_execute(txId);
}
}
/**
* @inheritdoc IMultiSigWallet
*
* @dev Requirements:
*
* - The caller must be a wallet owner.
* - The transaction with the given ID must exist.
* - The transaction with the given ID must not be expired.
* - The transaction with the given ID must not be executed.
* - The transaction with the given ID must not be on cooldown.
* - The transaction with the given ID must have at least the required number of approvals.
*/
function execute(uint256 txId) external onlyOwner {
_execute(txId);
}
/**
* @inheritdoc IMultiSigWallet
*
* @dev Requirements:
*
* - The caller must be a wallet owner.
* - The transactions with the given IDs must exist.
* - The transactions with the given IDs must not be expired.
* - The transactions with the given IDs must not be executed.
* - The transactions with the given IDs must not be on cooldown.
* - The transactions with the given IDs must have at least the required number of approvals.
*/
function executeBatch(uint256[] calldata txIds) external onlyOwner {
uint256 count = txIds.length;
for (uint256 i = 0; i < count; ++i) {
_execute(txIds[i]);
}
}
/**
* @inheritdoc IMultiSigWallet
*
* @dev Requirements:
*
* - The caller must be a wallet owner.
* - The transaction with the given ID must exist.
* - The transaction with the given ID must not be expired.
* - The transaction with the given ID must not be executed.
* - The transaction with the given ID must be approved by the caller.
*/
function revoke(uint256 txId) external onlyOwner {
_revoke(txId);
}
/**
* @inheritdoc IMultiSigWallet
*
* @dev Requirements:
*
* - The caller must be a wallet owner.
* - The transactions with the given IDs must exist.
* - The transactions with the given IDs must not be expired.
* - The transactions with the given IDs must not be executed.
* - The transactions with the given IDs must be approved by the caller.
*/
function revokeBatch(uint256[] calldata txIds) external onlyOwner {
uint256 count = txIds.length;
for (uint256 i = 0; i < count; ++i) {
_revoke(txIds[i]);
}
}
/**
* @inheritdoc IMultiSigWallet
*
* @dev Requirements:
*
* - The array of wallet owners must not be empty.
* - The number of required approvals must not be zero and must not exceed the length of the wallet owners array.
*/
function configureOwners(address[] memory newOwners, uint16 newRequiredApprovals) external onlySelfCall {
_configureOwners(newOwners, newRequiredApprovals);
}
/**
* @inheritdoc IMultiSigWallet
*/
function configureExpirationTime(uint120 newExpirationTime) external onlySelfCall {
_configureExpirationTime(newExpirationTime);
}
/**
* @inheritdoc IMultiSigWallet
*/
function configureCooldownTime(uint120 newCooldownTime) external onlySelfCall {
_configureCooldownTime(newCooldownTime);
}
/**
* @inheritdoc IMultiSigWallet
*/
function getApprovalCount(uint256 txId) external view returns (uint256) {
return _approvalCount[txId];
}
/**
* @inheritdoc IMultiSigWallet
*/
function getApprovalStatus(uint256 txId, address owner) external view returns (bool) {
return _approvalStatus[txId][owner];
}
/**
* @inheritdoc IMultiSigWallet
*/
function getTransaction(uint256 txId) external view returns (Transaction memory) {
if (txId >= _transactions.length) {
revert TransactionNotExist();
}
return _transactions[txId];
}
/**
* @inheritdoc IMultiSigWallet
*
* @dev Notes:
* - The total number of returned transactions will not exceed the provided limit, but may be less.
* - The function will return an empty array if there is no transaction with the given ID or if the limit is zero.
* - The empty transaction array will be returned if the transaction with the provided ID does not exist or the
* provided limit is zero.
*/
function getTransactions(uint256 txId, uint256 limit) external view returns (Transaction[] memory txs) {
uint256 len = _transactions.length;
if (len <= txId || limit == 0) {
txs = new Transaction[](0);
} else {
len -= txId;
if (len > limit) {
len = limit;
}
txs = new Transaction[](len);
for (uint256 i = 0; i < len; i++) {
txs[i] = _transactions[txId];
txId++;
}
}
}
/**
* @inheritdoc IMultiSigWallet
*/
function owners() external view returns (address[] memory) {
return _owners;
}
/**
* @inheritdoc IMultiSigWallet
*/
function isOwner(address account) external view returns (bool) {
return _isOwner[account];
}
/**
* @inheritdoc IMultiSigWallet
*/
function requiredApprovals() external view returns (uint256) {
return _requiredApprovals;
}
/**
* @inheritdoc IMultiSigWallet
*/
function transactionCount() external view returns (uint256) {
return _transactions.length;
}
/**
* @inheritdoc IMultiSigWallet
*/
function expirationTime() external view returns (uint120) {
return _expirationTime;
}
/**
* @inheritdoc IMultiSigWallet
*/
function cooldownTime() external view returns (uint120) {
return _cooldownTime;
}
/**
* @dev Submits a transaction internally. See {MultiSigWallet-submit}.
*/
function _submit(
address to,
uint256 value,
bytes calldata data
) internal returns (uint256 txId) {
uint128 blockTimestamp = toUint128(block.timestamp);
_transactions.push(
Transaction({
to: to,
executed: false,
cooldown: blockTimestamp + _cooldownTime,
expiration: blockTimestamp + _cooldownTime + _expirationTime,
value: value,
data: data
})
);
txId = _transactions.length - 1;
emit Submit(msg.sender, txId);
}
/**
* @dev Approves a transaction internally. See {MultiSigWallet-approve}.
*/
function _approve(uint256 txId) internal {
if (txId >= _transactions.length) {
revert TransactionNotExist();
}
if (_approvalStatus[txId][msg.sender]) {
revert TransactionAlreadyApproved();
}
Transaction memory transaction = _transactions[txId];
if (transaction.executed) {
revert TransactionAlreadyExecuted();
}
if (transaction.expiration < block.timestamp) {
revert TransactionExpired();
}
_approvalCount[txId] += 1;
_approvalStatus[txId][msg.sender] = true;
emit Approve(msg.sender, txId);
}
/**
* @dev Executes a transaction internally. {MultiSigWallet-execute}.
*/
function _execute(uint256 txId) internal {
if (txId >= _transactions.length) {
revert TransactionNotExist();
}
Transaction storage transaction = _transactions[txId];
if (transaction.executed) {
revert TransactionAlreadyExecuted();
}
if (transaction.cooldown > block.timestamp) {
revert CooldownNotEnded();
}
if (transaction.expiration < block.timestamp) {
revert TransactionExpired();
}
if (_approvalCount[txId] < _requiredApprovals) {
revert NotEnoughApprovals();
}
transaction.executed = true;
emit Execute(msg.sender, txId);
(bool success, bytes memory data) = transaction.to.call{ value: transaction.value }(transaction.data);
if (!success) {
revert InternalTransactionFailed(data);
}
}
/**
* @dev Revokes a transaction internally. {MultiSigWallet-revoke}.
*/
function _revoke(uint256 txId) internal {
if (txId >= _transactions.length) {
revert TransactionNotExist();
}
if (!_approvalStatus[txId][msg.sender]) {
revert TransactionNotApproved();
}
Transaction storage transaction = _transactions[txId];
if (transaction.executed) {
revert TransactionAlreadyExecuted();
}
if (transaction.expiration < block.timestamp) {
revert TransactionExpired();
}
_approvalCount[txId] -= 1;
_approvalStatus[txId][msg.sender] = false;
emit Revoke(msg.sender, txId);
}
/**
* @dev Configures owners internally. See {MultiSigWallet-configureOwners}.
*/
function _configureOwners(address[] memory newOwners, uint16 newRequiredApprovals) internal {
if (newOwners.length == 0) {
revert EmptyOwnersArray();
}
if (newRequiredApprovals == 0) {
revert InvalidRequiredApprovals();
}
if (newRequiredApprovals > newOwners.length) {
revert InvalidRequiredApprovals();
}
uint256 len;
if (_owners.length != 0) {
len = _owners.length;
for (uint256 i = 0; i < len; i++) {
_isOwner[_owners[i]] = false;
}
}
address owner;
len = newOwners.length;
for (uint256 i = 0; i < len; i++) {
owner = newOwners[i];
if (owner == address(0)) {
revert ZeroOwnerAddress();
}
if (_isOwner[owner]) {
revert DuplicateOwnerAddress();
}
_isOwner[owner] = true;
}
_owners = newOwners;
_requiredApprovals = newRequiredApprovals;
emit ConfigureOwners(newOwners, newRequiredApprovals);
}
/**
* @dev Configures expiration time of transactions internally. See {MultiSigWallet-configureExpirationTime}.
*/
function _configureExpirationTime(uint120 newExpirationTime) internal {
if (newExpirationTime < MINIMUM_EXPIRATION_TIME) {
revert InvalidExpirationTime();
}
_expirationTime = newExpirationTime;
emit ConfigureExpirationTime(newExpirationTime);
}
/**
* @dev Configures cooldown time of transactions internally. See {MultiSigWallet-configureCooldownTime}.
*/
function _configureCooldownTime(uint120 newCooldownTime) internal {
_cooldownTime = newCooldownTime;
emit ConfigureCooldownTime(newCooldownTime);
}
/**
* @dev Returns the downcasted uint128 from uint256, reverting on
* overflow (when the input is greater than largest uint128).
*/
function toUint128(uint256 value) internal pure returns (uint128) {
require(value <= type(uint128).max, "SafeCast: value doesn't fit in 128 bits");
return uint128(value);
}
}