@lukso/lsp-smart-contracts 0.8.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/CONTRIBUTING.md +8 -0
  2. package/README.md +21 -21
  3. package/artifacts/LSP0ERC725Account.json +2 -2
  4. package/artifacts/LSP0ERC725AccountInit.json +2 -2
  5. package/artifacts/LSP11BasicSocialRecovery.json +2 -2
  6. package/artifacts/LSP11BasicSocialRecoveryInit.json +2 -2
  7. package/artifacts/LSP1UniversalReceiverDelegateUP.json +2 -2
  8. package/artifacts/LSP1UniversalReceiverDelegateVault.json +2 -2
  9. package/artifacts/LSP6KeyManager.json +2 -2
  10. package/artifacts/LSP6KeyManagerInit.json +2 -2
  11. package/artifacts/LSP7CappedSupply.json +5 -0
  12. package/artifacts/LSP7CappedSupplyInitAbstract.json +5 -0
  13. package/artifacts/LSP7CompatibleERC20.json +5 -0
  14. package/artifacts/LSP7CompatibleERC20InitAbstract.json +5 -0
  15. package/artifacts/LSP7CompatibleERC20Mintable.json +7 -2
  16. package/artifacts/LSP7CompatibleERC20MintableInit.json +7 -2
  17. package/artifacts/LSP7DigitalAsset.json +5 -0
  18. package/artifacts/LSP7DigitalAssetInitAbstract.json +5 -0
  19. package/artifacts/LSP7Mintable.json +7 -2
  20. package/artifacts/LSP7MintableInit.json +7 -2
  21. package/artifacts/LSP8CappedSupply.json +5 -0
  22. package/artifacts/LSP8CappedSupplyInitAbstract.json +5 -0
  23. package/artifacts/LSP8CompatibleERC721.json +5 -0
  24. package/artifacts/LSP8CompatibleERC721InitAbstract.json +5 -0
  25. package/artifacts/LSP8CompatibleERC721Mintable.json +7 -2
  26. package/artifacts/LSP8CompatibleERC721MintableInit.json +7 -2
  27. package/artifacts/LSP8IdentifiableDigitalAsset.json +5 -0
  28. package/artifacts/LSP8IdentifiableDigitalAssetInitAbstract.json +5 -0
  29. package/artifacts/LSP8Mintable.json +7 -2
  30. package/artifacts/LSP8MintableInit.json +7 -2
  31. package/artifacts/LSP9Vault.json +2 -2
  32. package/artifacts/LSP9VaultInit.json +2 -2
  33. package/artifacts/UniversalProfile.json +2 -2
  34. package/artifacts/UniversalProfileInit.json +2 -2
  35. package/common/constants.d.ts +738 -0
  36. package/common/constants.js +785 -0
  37. package/contracts/LSP0ERC725Account/LSP0ERC725AccountCore.sol +87 -28
  38. package/contracts/LSP0ERC725Account/LSP0Utils.sol +50 -0
  39. package/contracts/LSP10ReceivedVaults/LSP10Utils.sol +3 -5
  40. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateUP/LSP1UniversalReceiverDelegateUP.sol +2 -4
  41. package/contracts/LSP2ERC725YJSONSchema/LSP2Utils.sol +7 -7
  42. package/contracts/LSP6KeyManager/LSP6KeyManagerCore.sol +44 -734
  43. package/contracts/LSP6KeyManager/LSP6Modules/LSP6ExecuteModule.sol +229 -0
  44. package/contracts/LSP6KeyManager/LSP6Modules/LSP6OwnershipModule.sol +25 -0
  45. package/contracts/LSP6KeyManager/LSP6Modules/LSP6SetDataModule.sol +606 -0
  46. package/contracts/LSP6KeyManager/LSP6Utils.sol +31 -1
  47. package/contracts/LSP7DigitalAsset/LSP7DigitalAssetCore.sol +2 -6
  48. package/contracts/LSP7DigitalAsset/LSP7Errors.sol +5 -0
  49. package/contracts/LSP8IdentifiableDigitalAsset/LSP8Errors.sol +5 -0
  50. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAssetCore.sol +7 -13
  51. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721.sol +1 -3
  52. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721InitAbstract.sol +1 -3
  53. package/contracts/LSP9Vault/LSP9VaultCore.sol +85 -25
  54. package/module/constants.d.ts +738 -0
  55. package/module/constants.js +782 -0
  56. package/package.json +26 -4
  57. package/constants.js +0 -856
@@ -3,33 +3,39 @@ pragma solidity ^0.8.5;
3
3
 
4
4
  // interfaces
5
5
  import {IERC1271} from "@openzeppelin/contracts/interfaces/IERC1271.sol";
6
- import {IERC725X} from "@erc725/smart-contracts/contracts/interfaces/IERC725X.sol";
7
6
  import {ILSP6KeyManager} from "./ILSP6KeyManager.sol";
8
7
 
9
8
  // modules
10
9
  import {ILSP14Ownable2Step} from "../LSP14Ownable2Step/ILSP14Ownable2Step.sol";
11
10
  import {ERC725Y} from "@erc725/smart-contracts/contracts/ERC725Y.sol";
12
11
  import {ERC165} from "@openzeppelin/contracts/utils/introspection/ERC165.sol";
12
+ import {LSP6SetDataModule} from "./LSP6Modules/LSP6SetDataModule.sol";
13
+ import {LSP6ExecuteModule} from "./LSP6Modules/LSP6ExecuteModule.sol";
14
+ import {LSP6OwnershipModule} from "./LSP6Modules/LSP6OwnershipModule.sol";
13
15
 
14
16
  // libraries
15
17
  import {GasLib} from "../Utils/GasLib.sol";
16
18
  import {BytesLib} from "solidity-bytes-utils/contracts/BytesLib.sol";
17
19
  import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
18
20
  import {Address} from "@openzeppelin/contracts/utils/Address.sol";
19
- import {ERC165Checker} from "@openzeppelin/contracts/utils/introspection/ERC165Checker.sol";
20
21
  import {LSP6Utils} from "./LSP6Utils.sol";
21
22
  import {EIP191Signer} from "../Custom/EIP191Signer.sol";
22
23
 
23
24
  // errors
24
- import "./LSP6Errors.sol";
25
+ import {
26
+ BatchExecuteParamsLengthMismatch,
27
+ BatchExecuteRelayCallParamsLengthMismatch,
28
+ LSP6BatchExcessiveValueSent,
29
+ LSP6BatchInsufficientValueSent,
30
+ InvalidPayload,
31
+ InvalidRelayNonce,
32
+ NoPermissionsSet,
33
+ InvalidERC725Function,
34
+ NotAuthorised
35
+ } from "./LSP6Errors.sol";
25
36
 
26
37
  // constants
27
38
  import {
28
- OPERATION_0_CALL,
29
- OPERATION_1_CREATE,
30
- OPERATION_2_CREATE2,
31
- OPERATION_3_STATICCALL,
32
- OPERATION_4_DELEGATECALL,
33
39
  SETDATA_SELECTOR,
34
40
  SETDATA_ARRAY_SELECTOR,
35
41
  EXECUTE_SELECTOR
@@ -39,15 +45,12 @@ import {
39
45
  _ERC1271_MAGICVALUE,
40
46
  _ERC1271_FAILVALUE
41
47
  } from "../LSP0ERC725Account/LSP0Constants.sol";
42
-
43
48
  import {
44
- _LSP1_UNIVERSAL_RECEIVER_DELEGATE_PREFIX,
45
- _LSP1_UNIVERSAL_RECEIVER_DELEGATE_KEY
46
- } from "../LSP1UniversalReceiver/LSP1Constants.sol";
47
-
48
- import "./LSP6Constants.sol";
49
-
50
- import {_LSP17_EXTENSION_PREFIX} from "../LSP17ContractExtension/LSP17Constants.sol";
49
+ LSP6_VERSION,
50
+ _INTERFACEID_LSP6,
51
+ _PERMISSION_SIGN,
52
+ _PERMISSION_REENTRANCY
53
+ } from "./LSP6Constants.sol";
51
54
 
52
55
  /**
53
56
  * @title Core implementation of the LSP6 Key Manager standard.
@@ -55,11 +58,15 @@ import {_LSP17_EXTENSION_PREFIX} from "../LSP17ContractExtension/LSP17Constants.
55
58
  * @dev This contract acts as a controller for an ERC725 Account.
56
59
  * Permissions for controllers are stored in the ERC725Y storage of the ERC725 Account and can be updated using `setData(...)`.
57
60
  */
58
- abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
61
+ abstract contract LSP6KeyManagerCore is
62
+ ERC165,
63
+ ILSP6KeyManager,
64
+ LSP6SetDataModule,
65
+ LSP6ExecuteModule,
66
+ LSP6OwnershipModule
67
+ {
59
68
  using LSP6Utils for *;
60
- using Address for address;
61
69
  using ECDSA for bytes32;
62
- using ERC165Checker for address;
63
70
  using EIP191Signer for address;
64
71
  using BytesLib for bytes;
65
72
 
@@ -304,7 +311,7 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
304
311
  if (erc725Function == SETDATA_SELECTOR) {
305
312
  (bytes32 inputKey, bytes memory inputValue) = abi.decode(payload[4:], (bytes32, bytes));
306
313
 
307
- _verifyCanSetData(from, permissions, inputKey, inputValue);
314
+ LSP6SetDataModule._verifyCanSetData(_target, from, permissions, inputKey, inputValue);
308
315
 
309
316
  // ERC725Y.setData(bytes32[],bytes[])
310
317
  } else if (erc725Function == SETDATA_ARRAY_SELECTOR) {
@@ -313,732 +320,21 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
313
320
  (bytes32[], bytes[])
314
321
  );
315
322
 
316
- _verifyCanSetData(from, permissions, inputKeys, inputValues);
323
+ LSP6SetDataModule._verifyCanSetData(_target, from, permissions, inputKeys, inputValues);
317
324
 
318
325
  // ERC725X.execute(uint256,address,uint256,bytes)
319
326
  } else if (erc725Function == EXECUTE_SELECTOR) {
320
- _verifyCanExecute(from, permissions, payload);
327
+ LSP6ExecuteModule._verifyCanExecute(_target, from, permissions, payload);
321
328
  } else if (
322
329
  erc725Function == ILSP14Ownable2Step.transferOwnership.selector ||
323
330
  erc725Function == ILSP14Ownable2Step.acceptOwnership.selector
324
331
  ) {
325
- _requirePermissions(from, permissions, _PERMISSION_CHANGEOWNER);
332
+ LSP6OwnershipModule._verifyOwnershipPermissions(from, permissions);
326
333
  } else {
327
334
  revert InvalidERC725Function(erc725Function);
328
335
  }
329
336
  }
330
337
 
331
- /**
332
- * @dev verify if the `controllerAddress` has the permissions required to set a data key on the ERC725Y storage of the `target`.
333
- * @param controllerAddress the address who want to set the data key.
334
- * @param controllerPermissions the permissions to be checked against.
335
- * @param inputDataKey the data key to set on the `target`.
336
- * @param inputDataValue the data value to set for the `inputDataKey`.
337
- */
338
- function _verifyCanSetData(
339
- address controllerAddress,
340
- bytes32 controllerPermissions,
341
- bytes32 inputDataKey,
342
- bytes memory inputDataValue
343
- ) internal view virtual {
344
- bytes32 requiredPermission = _getPermissionRequiredToSetDataKey(
345
- inputDataKey,
346
- inputDataValue
347
- );
348
-
349
- // CHECK if allowed to set an ERC725Y Data Key
350
- if (requiredPermission == _PERMISSION_SETDATA) {
351
- // Skip if caller has SUPER permissions
352
- if (controllerPermissions.hasPermission(_PERMISSION_SUPER_SETDATA)) return;
353
-
354
- _requirePermissions(controllerAddress, controllerPermissions, _PERMISSION_SETDATA);
355
-
356
- _verifyAllowedERC725YSingleKey(
357
- controllerAddress,
358
- inputDataKey,
359
- ERC725Y(_target).getAllowedERC725YDataKeysFor(controllerAddress)
360
- );
361
- } else {
362
- // Otherwise CHECK the required permission if setting LSP6 permissions, LSP1 Delegate or LSP17 Extensions.
363
- _requirePermissions(controllerAddress, controllerPermissions, requiredPermission);
364
- }
365
- }
366
-
367
- /**
368
- * @dev verify if the `controllerAddress` has the permissions required to set an array of data keys on the ERC725Y storage of the `target`.
369
- * @param controllerAddress the address who want to set the data keys.
370
- * @param controllerPermissions the permissions to be checked against.
371
- * @param inputDataKeys an array of data keys to set on the `target`.
372
- * @param inputDataValues an array of data values to set for the `inputDataKeys`.
373
- */
374
- function _verifyCanSetData(
375
- address controllerAddress,
376
- bytes32 controllerPermissions,
377
- bytes32[] memory inputDataKeys,
378
- bytes[] memory inputDataValues
379
- ) internal view virtual {
380
- bool isSettingERC725YKeys;
381
- bool[] memory validatedInputDataKeys = new bool[](inputDataKeys.length);
382
-
383
- bytes32 requiredPermission;
384
-
385
- uint256 ii;
386
- do {
387
- requiredPermission = _getPermissionRequiredToSetDataKey(
388
- inputDataKeys[ii],
389
- inputDataValues[ii]
390
- );
391
-
392
- if (requiredPermission == _PERMISSION_SETDATA) {
393
- isSettingERC725YKeys = true;
394
- } else {
395
- // CHECK the required permissions if setting LSP6 permissions, LSP1 Delegate or LSP17 Extensions.
396
- _requirePermissions(controllerAddress, controllerPermissions, requiredPermission);
397
- validatedInputDataKeys[ii] = true;
398
- }
399
-
400
- ii = GasLib.uncheckedIncrement(ii);
401
- } while (ii < inputDataKeys.length);
402
-
403
- // CHECK if allowed to set one (or multiple) ERC725Y Data Keys
404
- if (isSettingERC725YKeys) {
405
- // Skip if caller has SUPER permissions
406
- if (controllerPermissions.hasPermission(_PERMISSION_SUPER_SETDATA)) return;
407
-
408
- _requirePermissions(controllerAddress, controllerPermissions, _PERMISSION_SETDATA);
409
-
410
- _verifyAllowedERC725YDataKeys(
411
- controllerAddress,
412
- inputDataKeys,
413
- ERC725Y(_target).getAllowedERC725YDataKeysFor(controllerAddress),
414
- validatedInputDataKeys
415
- );
416
- }
417
- }
418
-
419
- /**
420
- * @dev retrieve the permission required based on the data key to be set on the `target`.
421
- * @param inputDataKey the data key to set on the `target`. Can be related to LSP6 Permissions, LSP1 Delegate or LSP17 Extensions.
422
- * @param inputDataValue the data value to set for the `inputDataKey`.
423
- * @return the permission required to set the `inputDataKey` on the `target`.
424
- */
425
- function _getPermissionRequiredToSetDataKey(bytes32 inputDataKey, bytes memory inputDataValue)
426
- internal
427
- view
428
- virtual
429
- returns (bytes32)
430
- {
431
- // AddressPermissions[] or AddressPermissions[index]
432
- if (bytes16(inputDataKey) == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY_PREFIX) {
433
- return _getPermissionToSetPermissionsArray(inputDataKey, inputDataValue);
434
-
435
- // AddressPermissions:...
436
- } else if (bytes6(inputDataKey) == _LSP6KEY_ADDRESSPERMISSIONS_PREFIX) {
437
- // AddressPermissions:Permissions:<address>
438
- if (bytes12(inputDataKey) == _LSP6KEY_ADDRESSPERMISSIONS_PERMISSIONS_PREFIX) {
439
- return _getPermissionToSetControllerPermissions(inputDataKey);
440
-
441
- // AddressPermissions:AllowedCalls:<address>
442
- } else if (bytes12(inputDataKey) == _LSP6KEY_ADDRESSPERMISSIONS_ALLOWEDCALLS_PREFIX) {
443
- return _getPermissionToSetAllowedCalls(inputDataKey, inputDataValue);
444
-
445
- // AddressPermissions:AllowedERC725YKeys:<address>
446
- } else if (
447
- bytes12(inputDataKey) == _LSP6KEY_ADDRESSPERMISSIONS_AllowedERC725YDataKeys_PREFIX
448
- ) {
449
- return _getPermissionToSetAllowedERC725YDataKeys(inputDataKey, inputDataValue);
450
-
451
- // if the first 6 bytes of the input data key are "AddressPermissions:..." but did not match
452
- // with anything above, this is not a standard LSP6 permission data key so we revert.
453
- } else {
454
- /**
455
- * @dev more permissions types starting with `AddressPermissions:...` can be implemented by overriding this function.
456
- *
457
- * // AddressPermissions:MyCustomPermissions:<address>
458
- * bytes12 CUSTOM_PERMISSION_PREFIX = 0x4b80742de2bf9e659ba40000
459
- *
460
- * if (bytes12(dataKey) == CUSTOM_PERMISSION_PREFIX) {
461
- * // custom logic
462
- * }
463
- *
464
- * super._getPermissionRequiredToSetDataKey(...)
465
- */
466
- revert NotRecognisedPermissionKey(inputDataKey);
467
- }
468
-
469
- // LSP1UniversalReceiverDelegate or LSP1UniversalReceiverDelegate:<typeId>
470
- } else if (
471
- inputDataKey == _LSP1_UNIVERSAL_RECEIVER_DELEGATE_KEY ||
472
- bytes12(inputDataKey) == _LSP1_UNIVERSAL_RECEIVER_DELEGATE_PREFIX
473
- ) {
474
- return _getPermissionToSetLSP1Delegate(inputDataKey);
475
-
476
- // LSP17Extension:<bytes4>
477
- } else if (bytes12(inputDataKey) == _LSP17_EXTENSION_PREFIX) {
478
- return _getPermissionToSetLSP17Extension(inputDataKey);
479
- } else {
480
- return _PERMISSION_SETDATA;
481
- }
482
- }
483
-
484
- /**
485
- * @dev retrieve the permission required to update the `AddressPermissions[]` array data key defined in LSP6.
486
- * @param inputDataKey either `AddressPermissions[]` (array length) or `AddressPermissions[index]` (array index)
487
- * @param inputDataValue the updated value for the `inputDataKey`. MUST be:
488
- * - a `uint256` for `AddressPermissions[]` (array length)
489
- * - an `address` or `0x` for `AddressPermissions[index]` (array entry).
490
- *
491
- * @return either ADD or CHANGE PERMISSIONS.
492
- */
493
- function _getPermissionToSetPermissionsArray(bytes32 inputDataKey, bytes memory inputDataValue)
494
- internal
495
- view
496
- virtual
497
- returns (bytes32)
498
- {
499
- bytes memory currentValue = ERC725Y(_target).getData(inputDataKey);
500
-
501
- // AddressPermissions[] -> array length
502
- if (inputDataKey == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY) {
503
- uint256 newLength = uint256(bytes32(inputDataValue));
504
-
505
- return
506
- newLength > uint256(bytes32(currentValue))
507
- ? _PERMISSION_ADDCONTROLLER
508
- : _PERMISSION_CHANGEPERMISSIONS;
509
- }
510
-
511
- // AddressPermissions[index] -> array index
512
-
513
- // CHECK that we either ADD an address (20 bytes long) or REMOVE an address (0x)
514
- if (inputDataValue.length != 0 && inputDataValue.length != 20) {
515
- revert AddressPermissionArrayIndexValueNotAnAddress(inputDataKey, inputDataValue);
516
- }
517
-
518
- return currentValue.length == 0 ? _PERMISSION_ADDCONTROLLER : _PERMISSION_CHANGEPERMISSIONS;
519
- }
520
-
521
- /**
522
- * @dev retrieve the permission required to set permissions for a controller address.
523
- * @param inputPermissionDataKey `AddressPermissions:Permissions:<controller-address>`.
524
- * @return either ADD or CHANGE PERMISSIONS.
525
- */
526
- function _getPermissionToSetControllerPermissions(bytes32 inputPermissionDataKey)
527
- internal
528
- view
529
- virtual
530
- returns (bytes32)
531
- {
532
- return
533
- // if there is nothing stored under the data key, we are trying to ADD a new controller.
534
- // if there are already some permissions set under the data key, we are trying to CHANGE the permissions of a controller.
535
- bytes32(ERC725Y(_target).getData(inputPermissionDataKey)) == bytes32(0)
536
- ? _PERMISSION_ADDCONTROLLER
537
- : _PERMISSION_CHANGEPERMISSIONS;
538
- }
539
-
540
- /**
541
- * @dev retrieve the permission required to set some AllowedCalls for a controller.
542
- * @param dataKey `AddressPermissions:AllowedCalls:<controller-address>`.
543
- * @param dataValue the updated value for the `dataKey`. MUST be a bytes28[CompactBytesArray] of Allowed Calls.
544
- * @return either ADD or CHANGE PERMISSIONS.
545
- */
546
- function _getPermissionToSetAllowedCalls(bytes32 dataKey, bytes memory dataValue)
547
- internal
548
- view
549
- virtual
550
- returns (bytes32)
551
- {
552
- if (!LSP6Utils.isCompactBytesArrayOfAllowedCalls(dataValue)) {
553
- revert InvalidEncodedAllowedCalls(dataValue);
554
- }
555
-
556
- // if there is nothing stored under the Allowed Calls of the controller,
557
- // we are trying to ADD a list of restricted calls (standards + address + function selector)
558
- //
559
- // if there are already some data set under the Allowed Calls of the controller,
560
- // we are trying to CHANGE (= edit) these restrictions.
561
- return
562
- ERC725Y(_target).getData(dataKey).length == 0
563
- ? _PERMISSION_ADDCONTROLLER
564
- : _PERMISSION_CHANGEPERMISSIONS;
565
- }
566
-
567
- /**
568
- * @dev retrieve the permission required to set some AllowedCalls for a controller.
569
- * @param dataKey or `AddressPermissions:AllowedERC725YDataKeys:<controller-address>`.
570
- * @param dataValue the updated value for the `dataKey`. MUST be a bytes[CompactBytesArray] of Allowed ERC725Y Data Keys.
571
- * @return either ADD or CHANGE PERMISSIONS.
572
- */
573
- function _getPermissionToSetAllowedERC725YDataKeys(bytes32 dataKey, bytes memory dataValue)
574
- internal
575
- view
576
- returns (bytes32)
577
- {
578
- if (!LSP6Utils.isCompactBytesArrayOfAllowedERC725YDataKeys(dataValue)) {
579
- revert InvalidEncodedAllowedERC725YDataKeys(dataValue);
580
- }
581
-
582
- // if there is nothing stored under the Allowed ERC725Y Data Keys of the controller,
583
- // we are trying to ADD a list of restricted ERC725Y Data Keys.
584
- //
585
- // if there are already some data set under the Allowed ERC725Y Data Keys of the controller,
586
- // we are trying to CHANGE (= edit) these restricted ERC725Y data keys.
587
- return
588
- ERC725Y(_target).getData(dataKey).length == 0
589
- ? _PERMISSION_ADDCONTROLLER
590
- : _PERMISSION_CHANGEPERMISSIONS;
591
- }
592
-
593
- /**
594
- * @dev retrieve the permission required to either add or change the address
595
- * of a LSP1 Universal Receiver Delegate stored under a specific LSP1 data key.
596
- * @param lsp1DelegateDataKey either the data key for the default `LSP1UniversalReceiverDelegate`,
597
- * or a data key for a specific `LSP1UniversalReceiverDelegate:<typeId>`, starting with `_LSP1_UNIVERSAL_RECEIVER_DELEGATE_PREFIX`.
598
- * @return either ADD or CHANGE UNIVERSALRECEIVERDELEGATE.
599
- */
600
- function _getPermissionToSetLSP1Delegate(bytes32 lsp1DelegateDataKey)
601
- internal
602
- view
603
- virtual
604
- returns (bytes32)
605
- {
606
- return
607
- ERC725Y(_target).getData(lsp1DelegateDataKey).length == 0
608
- ? _PERMISSION_ADDUNIVERSALRECEIVERDELEGATE
609
- : _PERMISSION_CHANGEUNIVERSALRECEIVERDELEGATE;
610
- }
611
-
612
- /**
613
- * @dev Verify if `from` has the required permissions to either add or change the address
614
- * of an LSP0 Extension stored under a specific LSP17Extension data key
615
- * @param lsp17ExtensionDataKey the dataKey to set with `_LSP17_EXTENSION_PREFIX` as prefix.
616
- */
617
- function _getPermissionToSetLSP17Extension(bytes32 lsp17ExtensionDataKey)
618
- internal
619
- view
620
- virtual
621
- returns (bytes32)
622
- {
623
- return
624
- ERC725Y(_target).getData(lsp17ExtensionDataKey).length == 0
625
- ? _PERMISSION_ADDEXTENSIONS
626
- : _PERMISSION_CHANGEEXTENSIONS;
627
- }
628
-
629
- /**
630
- * @dev Verify if the `inputKey` is present in the list of `allowedERC725KeysCompacted` for the `controllerAddress`.
631
- * @param controllerAddress the address of the controller.
632
- * @param inputDataKey the data key to verify against the allowed ERC725Y Data Keys for the `controllerAddress`.
633
- * @param allowedERC725YDataKeysCompacted a CompactBytesArray of allowed ERC725Y Data Keys for the `controllerAddress`.
634
- */
635
- function _verifyAllowedERC725YSingleKey(
636
- address controllerAddress,
637
- bytes32 inputDataKey,
638
- bytes memory allowedERC725YDataKeysCompacted
639
- ) internal pure virtual {
640
- if (allowedERC725YDataKeysCompacted.length == 0)
641
- revert NoERC725YDataKeysAllowed(controllerAddress);
642
-
643
- /**
644
- * The pointer will always land on the length of each bytes value:
645
- *
646
- * ↓↓
647
- * 03 a00000
648
- * 05 fff83a0011
649
- * 20 aa0000000000000000000000000000000000000000000000000000000000cafe
650
- * 12 bb000000000000000000000000000000beef
651
- * 19 cc00000000000000000000000000000000000000000000deed
652
- * ↑↑
653
- *
654
- */
655
- uint256 pointer;
656
-
657
- // information extracted from each Allowed ERC725Y Data Key.
658
- uint256 length;
659
- bytes32 allowedKey;
660
- bytes32 mask;
661
-
662
- /**
663
- * iterate over each data key and update the `pointer` variable with the index where to find the length of each data key.
664
- *
665
- * 0x 03 a00000 03 fff83a 20 aa00...00cafe
666
- * ↑↑ ↑↑ ↑↑
667
- * first | second | third
668
- * length | length | length
669
- */
670
- while (pointer < allowedERC725YDataKeysCompacted.length) {
671
- // save the length of the allowed data key to calculate the `mask`.
672
- length = uint16(
673
- bytes2(
674
- abi.encodePacked(
675
- allowedERC725YDataKeysCompacted[pointer],
676
- allowedERC725YDataKeysCompacted[pointer + 1]
677
- )
678
- )
679
- );
680
-
681
- /**
682
- * The bitmask discard the last `32 - length` bytes of the input data key via ANDing &
683
- * It is used to compare only the relevant parts of each input data key against dynamic allowed data keys.
684
- *
685
- * E.g.:
686
- *
687
- * allowed data key = 0xa00000
688
- *
689
- * compare this part
690
- * vvvvvv
691
- * input data key = 0xa00000cafecafecafecafecafecafecafe000000000000000000000011223344
692
- *
693
- * & discard this part
694
- * vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
695
- * mask = 0xffffff0000000000000000000000000000000000000000000000000000000000
696
- */
697
- mask =
698
- bytes32(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) <<
699
- (8 * (32 - length));
700
-
701
- /*
702
- * transform the allowed data key situated from `pointer + 1` until `pointer + 1 + length` to a bytes32 value.
703
- * E.g. 0xfff83a -> 0xfff83a0000000000000000000000000000000000000000000000000000000000
704
- */
705
- // solhint-disable-next-line no-inline-assembly
706
- assembly {
707
- // the first 32 bytes word in memory (where allowedERC725YDataKeysCompacted is stored)
708
- // correspond to the total number of bytes in `allowedERC725YDataKeysCompacted`
709
- let offset := add(add(pointer, 2), 32)
710
- let memoryAt := mload(add(allowedERC725YDataKeysCompacted, offset))
711
- // MLOAD loads 32 bytes word, so we need to keep only the `length` number of bytes that makes up the allowed data key.
712
- allowedKey := and(memoryAt, mask)
713
- }
714
-
715
- // voila you found the key ;)
716
- if (allowedKey == (inputDataKey & mask)) return;
717
-
718
- // move the pointer to the index of the next allowed data key
719
- unchecked {
720
- pointer = pointer + (length + 2);
721
- }
722
- }
723
-
724
- revert NotAllowedERC725YDataKey(controllerAddress, inputDataKey);
725
- }
726
-
727
- /**
728
- * @dev Verify if all the `inputDataKeys` are present in the list of `allowedERC725KeysCompacted` of the `controllerAddress`.
729
- * @param controllerAddress the address of the controller.
730
- * @param inputDataKeys the data keys to verify against the allowed ERC725Y Data Keys of the `controllerAddress`.
731
- * @param allowedERC725YDataKeysCompacted a CompactBytesArray of allowed ERC725Y Data Keys of the `controllerAddress`.
732
- * @param validatedInputKeys an array of booleans to store the result of the verification of each data keys checked.
733
- */
734
- function _verifyAllowedERC725YDataKeys(
735
- address controllerAddress,
736
- bytes32[] memory inputDataKeys,
737
- bytes memory allowedERC725YDataKeysCompacted,
738
- bool[] memory validatedInputKeys
739
- ) internal pure virtual {
740
- if (allowedERC725YDataKeysCompacted.length == 0)
741
- revert NoERC725YDataKeysAllowed(controllerAddress);
742
-
743
- uint256 allowedKeysFound;
744
-
745
- // cache the input data keys from the start
746
- uint256 inputKeysLength = inputDataKeys.length;
747
-
748
- /**
749
- * The pointer will always land on the length of each bytes value:
750
- *
751
- * ↓↓
752
- * 03 a00000
753
- * 05 fff83a0011
754
- * 20 aa0000000000000000000000000000000000000000000000000000000000cafe
755
- * 12 bb000000000000000000000000000000beef
756
- * 19 cc00000000000000000000000000000000000000000000deed
757
- * ↑↑
758
- *
759
- */
760
- uint256 pointer;
761
-
762
- // information extracted from each Allowed ERC725Y Data Key.
763
- uint256 length;
764
- bytes32 allowedKey;
765
- bytes32 mask;
766
-
767
- /**
768
- * iterate over each data key and update the `pointer` variable with the index where to find the length of each data key.
769
- *
770
- * 0x 03 a00000 03 fff83a 20 aa00...00cafe
771
- * ↑↑ ↑↑ ↑↑
772
- * first | second | third
773
- * length | length | length
774
- */
775
- while (pointer < allowedERC725YDataKeysCompacted.length) {
776
- // save the length of the allowed data key to calculate the `mask`.
777
- length = uint16(
778
- bytes2(
779
- abi.encodePacked(
780
- allowedERC725YDataKeysCompacted[pointer],
781
- allowedERC725YDataKeysCompacted[pointer + 1]
782
- )
783
- )
784
- );
785
-
786
- /**
787
- * The bitmask discard the last `32 - length` bytes of the input data key via ANDing &
788
- * It is used to compare only the relevant parts of each input data key against dynamic allowed data keys.
789
- *
790
- * E.g.:
791
- *
792
- * allowed data key = 0xa00000
793
- *
794
- * compare this part
795
- * vvvvvv
796
- * input data key = 0xa00000cafecafecafecafecafecafecafe000000000000000000000011223344
797
- *
798
- * & discard this part
799
- * vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
800
- * mask = 0xffffff0000000000000000000000000000000000000000000000000000000000
801
- */
802
- mask =
803
- bytes32(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) <<
804
- (8 * (32 - length));
805
-
806
- /*
807
- * transform the allowed data key situated from `pointer + 1` until `pointer + 1 + length` to a bytes32 value.
808
- * E.g. 0xfff83a -> 0xfff83a0000000000000000000000000000000000000000000000000000000000
809
- */
810
- // solhint-disable-next-line no-inline-assembly
811
- assembly {
812
- // the first 32 bytes word in memory (where allowedERC725YDataKeysCompacted is stored)
813
- // correspond to the length of allowedERC725YDataKeysCompacted (= total number of bytes)
814
- let offset := add(add(pointer, 2), 32)
815
- let memoryAt := mload(add(allowedERC725YDataKeysCompacted, offset))
816
- allowedKey := and(memoryAt, mask)
817
- }
818
-
819
- /**
820
- * Iterate over the `inputDataKeys` to check them against the allowed data keys.
821
- * This until we have validated them all.
822
- */
823
- for (uint256 ii; ii < inputKeysLength; ii = GasLib.uncheckedIncrement(ii)) {
824
- // if the input data key has been marked as allowed previously,
825
- // SKIP it and move to the next input data key.
826
- if (validatedInputKeys[ii]) continue;
827
-
828
- // CHECK if the input data key is allowed.
829
- if ((inputDataKeys[ii] & mask) == allowedKey) {
830
- // if the input data key is allowed, mark it as allowed
831
- // and increment the number of allowed keys found.
832
- validatedInputKeys[ii] = true;
833
- allowedKeysFound = GasLib.uncheckedIncrement(allowedKeysFound);
834
-
835
- // Continue checking until all the inputKeys` have been found.
836
- if (allowedKeysFound == inputKeysLength) return;
837
- }
838
- }
839
-
840
- // Move the pointer to the next AllowedERC725YKey
841
- unchecked {
842
- pointer = pointer + (length + 2);
843
- }
844
- }
845
-
846
- // if we did not find all the input data keys, search for the first not allowed data key to revert.
847
- for (uint256 jj; jj < inputKeysLength; jj = GasLib.uncheckedIncrement(jj)) {
848
- if (!validatedInputKeys[jj]) {
849
- revert NotAllowedERC725YDataKey(controllerAddress, inputDataKeys[jj]);
850
- }
851
- }
852
- }
853
-
854
- /**
855
- * @dev verify if `from` has the required permissions to interact with other addresses using the target.
856
- * @param from the address who want to run the execute function on the ERC725Account
857
- * @param permissions the permissions of the caller
858
- * @param payload the ABI encoded payload `target.execute(...)`
859
- */
860
- function _verifyCanExecute(
861
- address from,
862
- bytes32 permissions,
863
- bytes calldata payload
864
- ) internal view virtual {
865
- // MUST be one of the ERC725X operation types.
866
- uint256 operationType = uint256(bytes32(payload[4:36]));
867
-
868
- // DELEGATECALL is disallowed by default on the LSP6 Key Manager.
869
- if (operationType == OPERATION_4_DELEGATECALL) {
870
- revert DelegateCallDisallowedViaKeyManager();
871
- }
872
-
873
- uint256 value = uint256(bytes32(payload[68:100]));
874
-
875
- // prettier-ignore
876
- bool isContractCreation = operationType == OPERATION_1_CREATE || operationType == OPERATION_2_CREATE2;
877
- bool isCallDataPresent = payload.length > 164;
878
-
879
- // SUPER operation only applies to contract call, not contract creation
880
- bool hasSuperOperation = isContractCreation
881
- ? false
882
- : permissions.hasPermission(_extractSuperPermissionFromOperation(operationType));
883
-
884
- // CHECK if we are doing an empty call, as the receive() or fallback() function
885
- // of the target contract could run some code.
886
- if (!hasSuperOperation && !isCallDataPresent && value == 0) {
887
- _requirePermissions(from, permissions, _extractPermissionFromOperation(operationType));
888
- }
889
-
890
- if (isCallDataPresent && !hasSuperOperation) {
891
- _requirePermissions(from, permissions, _extractPermissionFromOperation(operationType));
892
- }
893
-
894
- bool hasSuperTransferValue = permissions.hasPermission(_PERMISSION_SUPER_TRANSFERVALUE);
895
-
896
- if (value != 0 && !hasSuperTransferValue) {
897
- _requirePermissions(from, permissions, _PERMISSION_TRANSFERVALUE);
898
- }
899
-
900
- // Skip on contract creation (CREATE or CREATE2)
901
- if (isContractCreation) return;
902
-
903
- // Skip if caller has SUPER permissions for external calls, with or without calldata (empty calls)
904
- if (hasSuperOperation && value == 0) return;
905
-
906
- // Skip if caller has SUPER permission for value transfers
907
- if (hasSuperTransferValue && !isCallDataPresent && value != 0) return;
908
-
909
- // Skip if both SUPER permissions are present
910
- if (hasSuperOperation && hasSuperTransferValue) return;
911
-
912
- _verifyAllowedCall(from, payload);
913
- }
914
-
915
- function _verifyAllowedCall(address from, bytes calldata payload) internal view virtual {
916
- // CHECK for ALLOWED CALLS
917
- address to = address(bytes20(payload[48:68]));
918
-
919
- bool containsFunctionCall = payload.length >= 168;
920
- bytes4 selector;
921
- if (containsFunctionCall) selector = bytes4(payload[164:168]);
922
-
923
- bytes memory allowedCalls = ERC725Y(_target).getAllowedCallsFor(from);
924
- uint256 allowedCallsLength = allowedCalls.length;
925
-
926
- if (allowedCallsLength == 0) {
927
- revert NoCallsAllowed(from);
928
- }
929
-
930
- bool isAllowedStandard;
931
- bool isAllowedAddress;
932
- bool isAllowedFunction;
933
-
934
- for (uint256 ii; ii < allowedCallsLength; ii += 30) {
935
- if (ii + 30 > allowedCallsLength) {
936
- revert InvalidEncodedAllowedCalls(allowedCalls);
937
- }
938
- bytes memory chunk = BytesLib.slice(allowedCalls, ii + 2, 28);
939
-
940
- if (bytes28(chunk) == 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffff) {
941
- revert InvalidWhitelistedCall(from);
942
- }
943
-
944
- bytes4 allowedStandard = bytes4(chunk);
945
- address allowedAddress = address(bytes20(bytes28(chunk) << 32));
946
- bytes4 allowedFunction = bytes4(bytes28(chunk) << 192);
947
-
948
- isAllowedStandard =
949
- allowedStandard == 0xffffffff ||
950
- to.supportsERC165InterfaceUnchecked(allowedStandard);
951
- isAllowedAddress =
952
- allowedAddress == 0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF ||
953
- to == allowedAddress;
954
- isAllowedFunction =
955
- allowedFunction == 0xffffffff ||
956
- (containsFunctionCall && (selector == allowedFunction));
957
-
958
- if (isAllowedStandard && isAllowedAddress && isAllowedFunction) return;
959
- }
960
-
961
- revert NotAllowedCall(from, to, selector);
962
- }
963
-
964
- /**
965
- * @dev extract the required permission + a descriptive string, based on the `_operationType`
966
- * being run via ERC725Account.execute(...)
967
- * @param operationType 0 = CALL, 1 = CREATE, 2 = CREATE2, etc... See ERC725X docs for more infos.
968
- * @return permissionsRequired (bytes32) the permission associated with the `_operationType`
969
- */
970
- function _extractPermissionFromOperation(uint256 operationType)
971
- internal
972
- pure
973
- virtual
974
- returns (bytes32 permissionsRequired)
975
- {
976
- if (operationType == OPERATION_0_CALL) return _PERMISSION_CALL;
977
- else if (operationType == OPERATION_1_CREATE) return _PERMISSION_DEPLOY;
978
- else if (operationType == OPERATION_2_CREATE2) return _PERMISSION_DEPLOY;
979
- else if (operationType == OPERATION_3_STATICCALL) return _PERMISSION_STATICCALL;
980
- else if (operationType == OPERATION_4_DELEGATECALL) return _PERMISSION_DELEGATECALL;
981
- }
982
-
983
- /**
984
- * @dev returns the `superPermission` needed for a specific `operationType` of the `execute(..)`
985
- */
986
- function _extractSuperPermissionFromOperation(uint256 operationType)
987
- internal
988
- pure
989
- virtual
990
- returns (bytes32 superPermission)
991
- {
992
- if (operationType == OPERATION_0_CALL) return _PERMISSION_SUPER_CALL;
993
- else if (operationType == OPERATION_3_STATICCALL) return _PERMISSION_SUPER_STATICCALL;
994
- else if (operationType == OPERATION_4_DELEGATECALL) return _PERMISSION_SUPER_DELEGATECALL;
995
- }
996
-
997
- /**
998
- * @dev revert if `from`'s `addressPermissions` doesn't contain `permissionsRequired`
999
- * @param from the caller address
1000
- * @param addressPermissions the caller's permissions BitArray
1001
- * @param permissionRequired the required permission
1002
- */
1003
- function _requirePermissions(
1004
- address from,
1005
- bytes32 addressPermissions,
1006
- bytes32 permissionRequired
1007
- ) internal pure virtual {
1008
- if (!addressPermissions.hasPermission(permissionRequired)) {
1009
- string memory permissionErrorString = _getPermissionName(permissionRequired);
1010
- revert NotAuthorised(from, permissionErrorString);
1011
- }
1012
- }
1013
-
1014
- /**
1015
- * @dev returns the name of the permission as a string
1016
- */
1017
- function _getPermissionName(bytes32 permission)
1018
- internal
1019
- pure
1020
- virtual
1021
- returns (string memory errorMessage)
1022
- {
1023
- if (permission == _PERMISSION_CHANGEOWNER) return "TRANSFEROWNERSHIP";
1024
- if (permission == _PERMISSION_CHANGEPERMISSIONS) return "CHANGEPERMISSIONS";
1025
- if (permission == _PERMISSION_ADDCONTROLLER) return "ADDCONTROLLER";
1026
- if (permission == _PERMISSION_ADDEXTENSIONS) return "ADDEXTENSIONS";
1027
- if (permission == _PERMISSION_CHANGEEXTENSIONS) return "CHANGEEXTENSIONS";
1028
- if (permission == _PERMISSION_ADDUNIVERSALRECEIVERDELEGATE)
1029
- return "ADDUNIVERSALRECEIVERDELEGATE";
1030
- if (permission == _PERMISSION_CHANGEUNIVERSALRECEIVERDELEGATE)
1031
- return "CHANGEUNIVERSALRECEIVERDELEGATE";
1032
- if (permission == _PERMISSION_REENTRANCY) return "REENTRANCY";
1033
- if (permission == _PERMISSION_SETDATA) return "SETDATA";
1034
- if (permission == _PERMISSION_CALL) return "CALL";
1035
- if (permission == _PERMISSION_STATICCALL) return "STATICCALL";
1036
- if (permission == _PERMISSION_DELEGATECALL) return "DELEGATECALL";
1037
- if (permission == _PERMISSION_DEPLOY) return "DEPLOY";
1038
- if (permission == _PERMISSION_TRANSFERVALUE) return "TRANSFERVALUE";
1039
- if (permission == _PERMISSION_SIGN) return "SIGN";
1040
- }
1041
-
1042
338
  /**
1043
339
  * @dev Initialise _reentrancyStatus to _NOT_ENTERED.
1044
340
  */
@@ -1073,4 +369,18 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
1073
369
  // https://eips.ethereum.org/EIPS/eip-2200)
1074
370
  _reentrancyStatus = false;
1075
371
  }
372
+
373
+ /**
374
+ * @dev revert if `controller`'s `addressPermissions` doesn't contain `permissionsRequired`
375
+ * @param controller the caller address
376
+ * @param addressPermissions the caller's permissions BitArray
377
+ * @param permissionRequired the required permission
378
+ */
379
+ function _requirePermissions(
380
+ address controller,
381
+ bytes32 addressPermissions,
382
+ bytes32 permissionRequired
383
+ ) internal pure override(LSP6ExecuteModule, LSP6SetDataModule) {
384
+ LSP6ExecuteModule._requirePermissions(controller, addressPermissions, permissionRequired);
385
+ }
1076
386
  }