@lukso/lsp-smart-contracts 0.6.2 → 0.8.0

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 (175) hide show
  1. package/CONTRIBUTING.md +31 -0
  2. package/README.md +73 -28
  3. package/artifacts/Create2Factory.json +2 -2
  4. package/artifacts/LSP0ERC725Account.json +227 -15
  5. package/artifacts/LSP0ERC725AccountInit.json +227 -41
  6. package/artifacts/LSP11BasicSocialRecovery.json +493 -0
  7. package/artifacts/LSP11BasicSocialRecoveryInit.json +513 -0
  8. package/artifacts/LSP16UniversalFactory.json +236 -0
  9. package/artifacts/LSP1UniversalReceiverDelegateUP.json +134 -15
  10. package/artifacts/LSP1UniversalReceiverDelegateVault.json +80 -15
  11. package/artifacts/LSP4Compatibility.json +22 -0
  12. package/artifacts/LSP4DigitalAssetMetadata.json +22 -0
  13. package/artifacts/{LSP4DigitalAssetMetadataInit.json → LSP4DigitalAssetMetadataInitAbstract.json} +24 -51
  14. package/artifacts/LSP6KeyManager.json +188 -33
  15. package/artifacts/LSP6KeyManagerInit.json +188 -33
  16. package/artifacts/LSP7CappedSupply.json +58 -31
  17. package/artifacts/{LSP7CappedSupplyInit.json → LSP7CappedSupplyInitAbstract.json} +60 -72
  18. package/artifacts/LSP7CompatibleERC20.json +52 -46
  19. package/artifacts/LSP7CompatibleERC20InitAbstract.json +733 -0
  20. package/artifacts/LSP7CompatibleERC20Mintable.json +61 -34
  21. package/artifacts/LSP7CompatibleERC20MintableInit.json +66 -60
  22. package/artifacts/LSP7DigitalAsset.json +52 -51
  23. package/artifacts/LSP7DigitalAssetInitAbstract.json +556 -0
  24. package/artifacts/LSP7Mintable.json +62 -35
  25. package/artifacts/LSP7MintableInit.json +62 -61
  26. package/artifacts/LSP8CappedSupply.json +64 -5
  27. package/artifacts/LSP8CappedSupplyInitAbstract.json +667 -0
  28. package/artifacts/LSP8CompatibleERC721.json +102 -21
  29. package/artifacts/LSP8CompatibleERC721InitAbstract.json +936 -0
  30. package/artifacts/LSP8CompatibleERC721Mintable.json +111 -9
  31. package/artifacts/LSP8CompatibleERC721MintableInit.json +116 -35
  32. package/artifacts/LSP8IdentifiableDigitalAsset.json +58 -20
  33. package/artifacts/{LSP8CappedSupplyInit.json → LSP8IdentifiableDigitalAssetInitAbstract.json} +62 -65
  34. package/artifacts/LSP8Mintable.json +67 -8
  35. package/artifacts/LSP8MintableInit.json +67 -34
  36. package/artifacts/LSP9Vault.json +235 -17
  37. package/artifacts/LSP9VaultInit.json +235 -43
  38. package/artifacts/UniversalProfile.json +227 -15
  39. package/artifacts/UniversalProfileInit.json +227 -41
  40. package/artifacts/UniversalReceiverAddressStore.json +3 -3
  41. package/constants.js +416 -95
  42. package/contracts/Custom/EIP191Signer.sol +12 -0
  43. package/contracts/Factories/Create2Factory.sol +1 -3
  44. package/contracts/LSP0ERC725Account/LSP0Constants.sol +13 -2
  45. package/contracts/LSP0ERC725Account/LSP0ERC725Account.sol +2 -1
  46. package/contracts/LSP0ERC725Account/LSP0ERC725AccountCore.sol +274 -74
  47. package/contracts/LSP0ERC725Account/LSP0ERC725AccountInit.sol +2 -2
  48. package/contracts/LSP0ERC725Account/LSP0ERC725AccountInitAbstract.sol +3 -2
  49. package/contracts/LSP10ReceivedVaults/LSP10Constants.sol +4 -4
  50. package/contracts/LSP10ReceivedVaults/LSP10Utils.sol +214 -0
  51. package/contracts/LSP11BasicSocialRecovery/ILSP11BasicSocialRecovery.sol +177 -0
  52. package/contracts/LSP11BasicSocialRecovery/LSP11BasicSocialRecovery.sol +23 -0
  53. package/contracts/LSP11BasicSocialRecovery/LSP11BasicSocialRecoveryCore.sol +258 -0
  54. package/contracts/LSP11BasicSocialRecovery/LSP11BasicSocialRecoveryInit.sol +26 -0
  55. package/contracts/LSP11BasicSocialRecovery/LSP11BasicSocialRecoveryInitAbstract.sol +19 -0
  56. package/contracts/LSP11BasicSocialRecovery/LSP11Constants.sol +6 -0
  57. package/contracts/LSP11BasicSocialRecovery/LSP11Errors.sol +60 -0
  58. package/contracts/LSP14Ownable2Step/ILSP14Ownable2Step.sol +53 -0
  59. package/contracts/LSP14Ownable2Step/LSP14Constants.sol +15 -0
  60. package/contracts/LSP14Ownable2Step/LSP14Errors.sol +12 -0
  61. package/contracts/LSP14Ownable2Step/LSP14Ownable2Step.sol +212 -0
  62. package/contracts/LSP16UniversalFactory/LSP16UniversalFactory.sol +246 -0
  63. package/contracts/LSP17ContractExtension/LSP17Constants.sol +17 -0
  64. package/contracts/LSP17ContractExtension/LSP17Errors.sol +7 -0
  65. package/contracts/LSP17ContractExtension/LSP17Extendable.sol +104 -0
  66. package/contracts/LSP17ContractExtension/LSP17Extension.sol +45 -0
  67. package/contracts/LSP17ContractExtension/LSP17Utils.sol +20 -0
  68. package/contracts/LSP1UniversalReceiver/ILSP1UniversalReceiver.sol +5 -5
  69. package/contracts/LSP1UniversalReceiver/LSP1Constants.sol +5 -3
  70. package/contracts/LSP1UniversalReceiver/LSP1Errors.sol +21 -0
  71. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateUP/LSP1UniversalReceiverDelegateUP.sol +156 -42
  72. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateVault/LSP1UniversalReceiverDelegateVault.sol +64 -31
  73. package/contracts/LSP1UniversalReceiver/LSP1Utils.sol +54 -41
  74. package/contracts/LSP2ERC725YJSONSchema/LSP2Utils.sol +136 -28
  75. package/contracts/LSP3UniversalProfile/LSP3Constants.sol +1 -1
  76. package/contracts/LSP4DigitalAssetMetadata/ILSP4Compatibility.sol +1 -1
  77. package/contracts/LSP4DigitalAssetMetadata/LSP4Compatibility.sol +1 -1
  78. package/contracts/LSP4DigitalAssetMetadata/LSP4Constants.sol +1 -1
  79. package/contracts/LSP4DigitalAssetMetadata/LSP4DigitalAssetMetadata.sol +16 -7
  80. package/contracts/LSP4DigitalAssetMetadata/LSP4DigitalAssetMetadataInitAbstract.sol +16 -7
  81. package/contracts/LSP4DigitalAssetMetadata/LSP4Errors.sol +3 -3
  82. package/contracts/LSP5ReceivedAssets/LSP5Constants.sol +4 -4
  83. package/contracts/LSP5ReceivedAssets/LSP5Utils.sol +103 -115
  84. package/contracts/LSP6KeyManager/ILSP6KeyManager.sol +26 -8
  85. package/contracts/LSP6KeyManager/LSP6Constants.sol +45 -45
  86. package/contracts/LSP6KeyManager/LSP6Errors.sol +97 -20
  87. package/contracts/LSP6KeyManager/LSP6KeyManager.sol +3 -2
  88. package/contracts/LSP6KeyManager/LSP6KeyManagerCore.sol +786 -359
  89. package/contracts/LSP6KeyManager/LSP6KeyManagerInit.sol +2 -2
  90. package/contracts/LSP6KeyManager/LSP6KeyManagerInitAbstract.sol +4 -3
  91. package/contracts/LSP6KeyManager/LSP6Utils.sol +127 -35
  92. package/contracts/LSP7DigitalAsset/ILSP7DigitalAsset.sol +26 -13
  93. package/contracts/LSP7DigitalAsset/LSP7Constants.sol +6 -6
  94. package/contracts/LSP7DigitalAsset/LSP7DigitalAsset.sol +5 -5
  95. package/contracts/LSP7DigitalAsset/LSP7DigitalAssetCore.sol +65 -52
  96. package/contracts/LSP7DigitalAsset/LSP7DigitalAssetInitAbstract.sol +3 -3
  97. package/contracts/LSP7DigitalAsset/LSP7Errors.sol +30 -1
  98. package/contracts/LSP7DigitalAsset/extensions/ILSP7CompatibleERC20.sol +1 -1
  99. package/contracts/LSP7DigitalAsset/extensions/LSP7Burnable.sol +24 -0
  100. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupply.sol +28 -8
  101. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupplyInitAbstract.sol +28 -11
  102. package/contracts/LSP7DigitalAsset/extensions/LSP7CompatibleERC20.sol +87 -20
  103. package/contracts/LSP7DigitalAsset/extensions/LSP7CompatibleERC20InitAbstract.sol +97 -19
  104. package/contracts/LSP7DigitalAsset/presets/ILSP7Mintable.sol +3 -3
  105. package/contracts/LSP7DigitalAsset/presets/LSP7CompatibleERC20Mintable.sol +3 -4
  106. package/contracts/LSP7DigitalAsset/presets/LSP7CompatibleERC20MintableInit.sol +9 -2
  107. package/contracts/LSP7DigitalAsset/presets/LSP7CompatibleERC20MintableInitAbstract.sol +3 -3
  108. package/contracts/LSP7DigitalAsset/presets/LSP7Mintable.sol +12 -12
  109. package/contracts/LSP7DigitalAsset/presets/LSP7MintableInit.sol +5 -5
  110. package/contracts/LSP7DigitalAsset/presets/LSP7MintableInitAbstract.sol +11 -10
  111. package/contracts/LSP8IdentifiableDigitalAsset/ILSP8IdentifiableDigitalAsset.sol +10 -8
  112. package/contracts/LSP8IdentifiableDigitalAsset/LSP8Constants.sol +11 -11
  113. package/contracts/LSP8IdentifiableDigitalAsset/LSP8Errors.sol +45 -1
  114. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAsset.sol +3 -3
  115. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAssetCore.sol +75 -71
  116. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAssetInitAbstract.sol +1 -1
  117. package/contracts/LSP8IdentifiableDigitalAsset/extensions/ILSP8CompatibleERC721.sol +20 -3
  118. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8Burnable.sol +20 -0
  119. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupply.sol +27 -7
  120. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupplyInitAbstract.sol +27 -10
  121. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721.sol +236 -30
  122. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721InitAbstract.sol +240 -30
  123. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8Enumerable.sol +49 -0
  124. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8EnumerableInitAbstract.sol +51 -0
  125. package/contracts/LSP8IdentifiableDigitalAsset/presets/ILSP8Mintable.sol +3 -3
  126. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8CompatibleERC721Mintable.sol +3 -4
  127. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8CompatibleERC721MintableInit.sol +9 -2
  128. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8CompatibleERC721MintableInitAbstract.sol +3 -3
  129. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8Mintable.sol +9 -8
  130. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8MintableInit.sol +2 -2
  131. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8MintableInitAbstract.sol +8 -7
  132. package/contracts/LSP9Vault/LSP9Constants.sol +11 -8
  133. package/contracts/LSP9Vault/LSP9Errors.sol +8 -0
  134. package/contracts/LSP9Vault/LSP9Vault.sol +6 -4
  135. package/contracts/LSP9Vault/LSP9VaultCore.sol +324 -90
  136. package/contracts/LSP9Vault/LSP9VaultInit.sol +2 -2
  137. package/contracts/LSP9Vault/LSP9VaultInitAbstract.sol +5 -4
  138. package/contracts/Legacy/Registries/AddressRegistry.sol +1 -1
  139. package/contracts/Legacy/Registries/AddressRegistryRequiresERC725.sol +1 -1
  140. package/contracts/Legacy/UniversalReceiverAddressStore.sol +4 -13
  141. package/contracts/UniversalProfile.sol +2 -2
  142. package/contracts/UniversalProfileInit.sol +2 -2
  143. package/contracts/UniversalProfileInitAbstract.sol +1 -1
  144. package/contracts/Utils/GasLib.sol +17 -0
  145. package/package.json +37 -32
  146. package/artifacts/LSP7CompatibleERC20Init.json +0 -760
  147. package/artifacts/LSP7DigitalAssetInit.json +0 -588
  148. package/artifacts/LSP8CompatibleERC721Init.json +0 -883
  149. package/artifacts/LSP8IdentifiableDigitalAssetInit.json +0 -639
  150. package/artifacts/UniversalFactory.json +0 -127
  151. package/contracts/Custom/ClaimOwnership.sol +0 -34
  152. package/contracts/Custom/ERC165Checker.sol +0 -130
  153. package/contracts/Custom/IClaimOwnership.sol +0 -10
  154. package/contracts/Factories/UniversalFactory.sol +0 -164
  155. package/contracts/LSP1UniversalReceiver/ILSP1UniversalReceiverDelegate.sol +0 -24
  156. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateUP/Handling/TokenAndVaultHandling.sol +0 -85
  157. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateVault/Handling/TokenHandling.sol +0 -76
  158. package/contracts/LSP2ERC725YJSONSchema/LSP2Errors.sol +0 -9
  159. package/contracts/LSP4DigitalAssetMetadata/LSP4DigitalAssetMetadataInit.sol +0 -26
  160. package/contracts/LSP7DigitalAsset/LSP7DigitalAssetInit.sol +0 -39
  161. package/contracts/LSP7DigitalAsset/extensions/ILSP7CappedSupply.sol +0 -17
  162. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupplyCore.sol +0 -57
  163. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupplyInit.sol +0 -19
  164. package/contracts/LSP7DigitalAsset/extensions/LSP7CompatibleERC20Core.sol +0 -112
  165. package/contracts/LSP7DigitalAsset/extensions/LSP7CompatibleERC20Init.sol +0 -29
  166. package/contracts/LSP7DigitalAsset/presets/LSP7MintableCore.sol +0 -26
  167. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAssetInit.sol +0 -35
  168. package/contracts/LSP8IdentifiableDigitalAsset/extensions/ILSP8CappedSupply.sol +0 -17
  169. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupplyCore.sol +0 -58
  170. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupplyInit.sol +0 -19
  171. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleConstants.sol +0 -6
  172. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721Core.sol +0 -187
  173. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721Init.sol +0 -25
  174. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8MintableCore.sol +0 -26
  175. package/contracts/Utils/UtilsLib.sol +0 -31
@@ -1,5 +1,5 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.6;
2
+ pragma solidity ^0.8.5;
3
3
 
4
4
  // interfaces
5
5
  import {IERC1271} from "@openzeppelin/contracts/interfaces/IERC1271.sol";
@@ -7,53 +7,74 @@ import {IERC725X} from "@erc725/smart-contracts/contracts/interfaces/IERC725X.so
7
7
  import {ILSP6KeyManager} from "./ILSP6KeyManager.sol";
8
8
 
9
9
  // modules
10
- import {OwnableUnset} from "@erc725/smart-contracts/contracts/custom/OwnableUnset.sol";
11
- import {IClaimOwnership} from "../Custom/IClaimOwnership.sol";
10
+ import {ILSP14Ownable2Step} from "../LSP14Ownable2Step/ILSP14Ownable2Step.sol";
12
11
  import {ERC725Y} from "@erc725/smart-contracts/contracts/ERC725Y.sol";
13
12
  import {ERC165} from "@openzeppelin/contracts/utils/introspection/ERC165.sol";
14
13
 
15
14
  // libraries
15
+ import {GasLib} from "../Utils/GasLib.sol";
16
16
  import {BytesLib} from "solidity-bytes-utils/contracts/BytesLib.sol";
17
17
  import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
18
18
  import {Address} from "@openzeppelin/contracts/utils/Address.sol";
19
- import {ERC165Checker} from "../Custom/ERC165Checker.sol";
20
- import {LSP2Utils} from "../LSP2ERC725YJSONSchema/LSP2Utils.sol";
19
+ import {ERC165Checker} from "@openzeppelin/contracts/utils/introspection/ERC165Checker.sol";
21
20
  import {LSP6Utils} from "./LSP6Utils.sol";
21
+ import {EIP191Signer} from "../Custom/EIP191Signer.sol";
22
22
 
23
23
  // errors
24
24
  import "./LSP6Errors.sol";
25
- import {InvalidABIEncodedArray} from "../LSP2ERC725YJSONSchema/LSP2Errors.sol";
26
25
 
27
26
  // constants
28
27
  import {
29
- OPERATION_CALL,
30
- OPERATION_CREATE,
31
- OPERATION_CREATE2,
32
- OPERATION_STATICCALL,
33
- OPERATION_DELEGATECALL
28
+ OPERATION_0_CALL,
29
+ OPERATION_1_CREATE,
30
+ OPERATION_2_CREATE2,
31
+ OPERATION_3_STATICCALL,
32
+ OPERATION_4_DELEGATECALL,
33
+ SETDATA_SELECTOR,
34
+ SETDATA_ARRAY_SELECTOR,
35
+ EXECUTE_SELECTOR
34
36
  } from "@erc725/smart-contracts/contracts/constants.sol";
35
37
  import {
36
38
  _INTERFACEID_ERC1271,
37
39
  _ERC1271_MAGICVALUE,
38
40
  _ERC1271_FAILVALUE
39
41
  } from "../LSP0ERC725Account/LSP0Constants.sol";
42
+
43
+ import {
44
+ _LSP1_UNIVERSAL_RECEIVER_DELEGATE_PREFIX,
45
+ _LSP1_UNIVERSAL_RECEIVER_DELEGATE_KEY
46
+ } from "../LSP1UniversalReceiver/LSP1Constants.sol";
47
+
40
48
  import "./LSP6Constants.sol";
41
49
 
50
+ import {_LSP17_EXTENSION_PREFIX} from "../LSP17ContractExtension/LSP17Constants.sol";
51
+
42
52
  /**
43
- * @title Core implementation of a contract acting as a controller of an ERC725 Account, using permissions stored in the ERC725Y storage
53
+ * @title Core implementation of the LSP6 Key Manager standard.
44
54
  * @author Fabian Vogelsteller <frozeman>, Jean Cavallera (CJ42), Yamen Merhi (YamenMerhi)
45
- * @dev all the permissions can be set on the ERC725 Account using `setData(...)` with the keys constants below
55
+ * @dev This contract acts as a controller for an ERC725 Account.
56
+ * Permissions for controllers are stored in the ERC725Y storage of the ERC725 Account and can be updated using `setData(...)`.
46
57
  */
47
58
  abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
48
- using LSP2Utils for *;
49
59
  using LSP6Utils for *;
50
60
  using Address for address;
51
61
  using ECDSA for bytes32;
52
62
  using ERC165Checker for address;
63
+ using EIP191Signer for address;
64
+ using BytesLib for bytes;
65
+
66
+ address internal _target;
67
+
68
+ // Variables, methods and modifier used for ReentrancyGuard are taken from the link below and modified accordingly.
69
+ // https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v4.8/contracts/security/ReentrancyGuard.sol
70
+ bool private _reentrancyStatus;
53
71
 
54
- address public override target;
55
72
  mapping(address => mapping(uint256 => uint256)) internal _nonceStore;
56
73
 
74
+ function target() public view returns (address) {
75
+ return _target;
76
+ }
77
+
57
78
  /**
58
79
  * @dev See {IERC165-supportsInterface}.
59
80
  */
@@ -67,9 +88,9 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
67
88
  /**
68
89
  * @inheritdoc ILSP6KeyManager
69
90
  */
70
- function getNonce(address from, uint256 channelId) public view override returns (uint256) {
71
- uint128 nonceId = uint128(_nonceStore[from][channelId]);
72
- return (uint256(channelId) << 128) | nonceId;
91
+ function getNonce(address from, uint128 channelId) public view returns (uint256) {
92
+ uint256 nonceInChannel = _nonceStore[from][channelId];
93
+ return (uint256(channelId) << 128) | nonceInChannel;
73
94
  }
74
95
 
75
96
  /**
@@ -78,13 +99,12 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
78
99
  function isValidSignature(bytes32 dataHash, bytes memory signature)
79
100
  public
80
101
  view
81
- override
82
102
  returns (bytes4 magicValue)
83
103
  {
84
104
  address recoveredAddress = dataHash.recover(signature);
85
105
 
86
106
  return (
87
- ERC725Y(target).getPermissionsFor(recoveredAddress).hasPermission(_PERMISSION_SIGN)
107
+ ERC725Y(_target).getPermissionsFor(recoveredAddress).hasPermission(_PERMISSION_SIGN)
88
108
  ? _ERC1271_MAGICVALUE
89
109
  : _ERC1271_FAILVALUE
90
110
  );
@@ -93,40 +113,126 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
93
113
  /**
94
114
  * @inheritdoc ILSP6KeyManager
95
115
  */
96
- function execute(bytes calldata payload) public payable override returns (bytes memory) {
97
- _verifyPermissions(msg.sender, payload);
116
+ function execute(bytes calldata payload) public payable virtual returns (bytes memory) {
117
+ return _execute(msg.value, payload);
118
+ }
98
119
 
99
- // solhint-disable avoid-low-level-calls
100
- (bool success, bytes memory returnData) = target.call{value: msg.value, gas: gasleft()}(
101
- payload
102
- );
120
+ /**
121
+ * @inheritdoc ILSP6KeyManager
122
+ */
123
+ function execute(uint256[] calldata values, bytes[] calldata payloads)
124
+ public
125
+ payable
126
+ virtual
127
+ returns (bytes[] memory)
128
+ {
129
+ if (values.length != payloads.length) {
130
+ revert BatchExecuteParamsLengthMismatch();
131
+ }
103
132
 
104
- bytes memory result = Address.verifyCallResult(
105
- success,
106
- returnData,
107
- "LSP6: Unknow Error occured when calling the linked target contract"
108
- );
133
+ bytes[] memory results = new bytes[](payloads.length);
134
+ uint256 totalValues;
109
135
 
110
- emit Executed(msg.value, bytes4(payload));
111
- return result.length != 0 ? abi.decode(result, (bytes)) : result;
136
+ for (uint256 ii; ii < payloads.length; ii = GasLib.uncheckedIncrement(ii)) {
137
+ if ((totalValues += values[ii]) > msg.value) {
138
+ revert LSP6BatchInsufficientValueSent(totalValues, msg.value);
139
+ }
140
+
141
+ results[ii] = _execute(values[ii], payloads[ii]);
142
+ }
143
+
144
+ if (totalValues < msg.value) {
145
+ revert LSP6BatchExcessiveValueSent(totalValues, msg.value);
146
+ }
147
+
148
+ return results;
149
+ }
150
+
151
+ /**
152
+ * @inheritdoc ILSP6KeyManager
153
+ */
154
+ function executeRelayCall(
155
+ bytes memory signature,
156
+ uint256 nonce,
157
+ bytes calldata payload
158
+ ) public payable virtual returns (bytes memory) {
159
+ return _executeRelayCall(signature, nonce, msg.value, payload);
112
160
  }
113
161
 
114
162
  /**
115
163
  * @inheritdoc ILSP6KeyManager
116
164
  */
117
165
  function executeRelayCall(
166
+ bytes[] memory signatures,
167
+ uint256[] calldata nonces,
168
+ uint256[] calldata values,
169
+ bytes[] calldata payloads
170
+ ) public payable virtual returns (bytes[] memory) {
171
+ if (
172
+ signatures.length != nonces.length ||
173
+ nonces.length != values.length ||
174
+ values.length != payloads.length
175
+ ) {
176
+ revert BatchExecuteRelayCallParamsLengthMismatch();
177
+ }
178
+
179
+ bytes[] memory results = new bytes[](payloads.length);
180
+ uint256 totalValues;
181
+
182
+ for (uint256 ii; ii < payloads.length; ii = GasLib.uncheckedIncrement(ii)) {
183
+ if ((totalValues += values[ii]) > msg.value) {
184
+ revert LSP6BatchInsufficientValueSent(totalValues, msg.value);
185
+ }
186
+
187
+ results[ii] = _executeRelayCall(signatures[ii], nonces[ii], values[ii], payloads[ii]);
188
+ }
189
+
190
+ if (totalValues < msg.value) {
191
+ revert LSP6BatchExcessiveValueSent(totalValues, msg.value);
192
+ }
193
+
194
+ return results;
195
+ }
196
+
197
+ function _execute(uint256 msgValue, bytes calldata payload)
198
+ internal
199
+ virtual
200
+ returns (bytes memory)
201
+ {
202
+ if (payload.length < 4) {
203
+ revert InvalidPayload(payload);
204
+ }
205
+
206
+ _nonReentrantBefore(msg.sender);
207
+ _verifyPermissions(msg.sender, payload);
208
+ bytes memory result = _executePayload(msgValue, payload);
209
+ _nonReentrantAfter();
210
+ return result;
211
+ }
212
+
213
+ function _executeRelayCall(
118
214
  bytes memory signature,
119
215
  uint256 nonce,
216
+ uint256 msgValue,
120
217
  bytes calldata payload
121
- ) public payable override returns (bytes memory) {
122
- bytes memory blob = abi.encodePacked(
218
+ ) internal virtual returns (bytes memory) {
219
+ if (payload.length < 4) {
220
+ revert InvalidPayload(payload);
221
+ }
222
+
223
+ bytes memory encodedMessage = abi.encodePacked(
224
+ LSP6_VERSION,
123
225
  block.chainid,
124
- address(this), // needs to be signed for this keyManager
125
226
  nonce,
227
+ msgValue,
126
228
  payload
127
229
  );
128
230
 
129
- address signer = keccak256(blob).toEthSignedMessageHash().recover(signature);
231
+ address signer = address(this).toDataWithIntendedValidator(encodedMessage).recover(
232
+ signature
233
+ );
234
+
235
+ _nonReentrantBefore(signer);
130
236
 
131
237
  if (!_isValidNonce(signer, nonce)) {
132
238
  revert InvalidRelayNonce(signer, nonce, signature);
@@ -137,18 +243,34 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
137
243
 
138
244
  _verifyPermissions(signer, payload);
139
245
 
140
- // solhint-disable avoid-low-level-calls
141
- (bool success, bytes memory returnData) = target.call{value: msg.value, gas: gasleft()}(
246
+ bytes memory result = _executePayload(msgValue, payload);
247
+
248
+ _nonReentrantAfter();
249
+
250
+ return result;
251
+ }
252
+
253
+ /**
254
+ * @notice execute the `payload` passed to `execute(...)` or `executeRelayCall(...)`
255
+ * @param payload the abi-encoded function call to execute on the target.
256
+ * @return bytes the result from calling the target with `payload`
257
+ */
258
+ function _executePayload(uint256 msgValue, bytes calldata payload)
259
+ internal
260
+ virtual
261
+ returns (bytes memory)
262
+ {
263
+ emit Executed(bytes4(payload), msgValue);
264
+
265
+ (bool success, bytes memory returnData) = _target.call{value: msgValue, gas: gasleft()}(
142
266
  payload
143
267
  );
144
-
145
268
  bytes memory result = Address.verifyCallResult(
146
269
  success,
147
270
  returnData,
148
- "LSP6: Unknow Error occured when calling the linked target contract"
271
+ "LSP6: failed executing payload"
149
272
  );
150
273
 
151
- emit Executed(msg.value, bytes4(payload));
152
274
  return result.length != 0 ? abi.decode(result, (bytes)) : result;
153
275
  }
154
276
 
@@ -160,7 +282,7 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
160
282
  * @param from caller address
161
283
  * @param idx (channel id + nonce within the channel)
162
284
  */
163
- function _isValidNonce(address from, uint256 idx) internal view returns (bool) {
285
+ function _isValidNonce(address from, uint256 idx) internal view virtual returns (bool) {
164
286
  // idx % (1 << 128) = nonce
165
287
  // (idx >> 128) = channel
166
288
  // equivalent to: return (nonce == _nonceStore[_from][channel]
@@ -168,70 +290,37 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
168
290
  }
169
291
 
170
292
  /**
171
- * @dev verify the permissions of the _from address that want to interact with the `target`
172
- * @param from the address making the request
173
- * @param payload the payload that will be run on `target`
293
+ * @dev verify if the `from` address is allowed to execute the `payload` on the `target`.
294
+ * @param from either the caller of `execute(...)` or the signer of `executeRelayCall(...)`.
295
+ * @param payload the payload to execute on the `target`.
174
296
  */
175
- function _verifyPermissions(address from, bytes calldata payload) internal view {
176
- bytes4 erc725Function = bytes4(payload[:4]);
177
-
178
- // get the permissions of the caller
179
- bytes32 permissions = ERC725Y(target).getPermissionsFor(from);
180
-
297
+ function _verifyPermissions(address from, bytes calldata payload) internal view virtual {
298
+ bytes32 permissions = ERC725Y(_target).getPermissionsFor(from);
181
299
  if (permissions == bytes32(0)) revert NoPermissionsSet(from);
182
300
 
183
- if (erc725Function == setDataSingleSelector) {
301
+ bytes4 erc725Function = bytes4(payload);
302
+
303
+ // ERC725Y.setData(bytes32,bytes)
304
+ if (erc725Function == SETDATA_SELECTOR) {
184
305
  (bytes32 inputKey, bytes memory inputValue) = abi.decode(payload[4:], (bytes32, bytes));
185
306
 
186
- if (
187
- // CHECK for permission keys
188
- bytes6(inputKey) == _LSP6KEY_ADDRESSPERMISSIONS_PREFIX ||
189
- bytes16(inputKey) == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY_PREFIX
190
- ) {
191
- _verifyCanSetPermissions(inputKey, inputValue, from, permissions);
192
- } else {
193
- bytes32[] memory wrappedInputKey = new bytes32[](1);
194
- wrappedInputKey[0] = inputKey;
307
+ _verifyCanSetData(from, permissions, inputKey, inputValue);
195
308
 
196
- _verifyCanSetData(from, permissions, wrappedInputKey);
197
- }
198
- } else if (erc725Function == setDataMultipleSelector) {
309
+ // ERC725Y.setData(bytes32[],bytes[])
310
+ } else if (erc725Function == SETDATA_ARRAY_SELECTOR) {
199
311
  (bytes32[] memory inputKeys, bytes[] memory inputValues) = abi.decode(
200
312
  payload[4:],
201
313
  (bytes32[], bytes[])
202
314
  );
203
315
 
204
- bool isSettingERC725YKeys = false;
205
-
206
- // loop through each ERC725Y data keys
207
- for (uint256 ii = 0; ii < inputKeys.length; ii++) {
208
- bytes32 key = inputKeys[ii];
209
- bytes memory value = inputValues[ii];
210
-
211
- if (
212
- // CHECK for permission keys
213
- bytes6(key) == _LSP6KEY_ADDRESSPERMISSIONS_PREFIX ||
214
- bytes16(key) == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY_PREFIX
215
- ) {
216
- _verifyCanSetPermissions(key, value, from, permissions);
217
-
218
- // "nullify" permission keys
219
- // to not check them against allowed ERC725Y keys
220
- inputKeys[ii] = bytes32(0);
221
- } else {
222
- // if the key is any other bytes32 key
223
- isSettingERC725YKeys = true;
224
- }
225
- }
316
+ _verifyCanSetData(from, permissions, inputKeys, inputValues);
226
317
 
227
- if (isSettingERC725YKeys) {
228
- _verifyCanSetData(from, permissions, inputKeys);
229
- }
230
- } else if (erc725Function == IERC725X.execute.selector) {
318
+ // ERC725X.execute(uint256,address,uint256,bytes)
319
+ } else if (erc725Function == EXECUTE_SELECTOR) {
231
320
  _verifyCanExecute(from, permissions, payload);
232
321
  } else if (
233
- erc725Function == OwnableUnset.transferOwnership.selector ||
234
- erc725Function == IClaimOwnership.claimOwnership.selector
322
+ erc725Function == ILSP14Ownable2Step.transferOwnership.selector ||
323
+ erc725Function == ILSP14Ownable2Step.acceptOwnership.selector
235
324
  ) {
236
325
  _requirePermissions(from, permissions, _PERMISSION_CHANGEOWNER);
237
326
  } else {
@@ -240,204 +329,530 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
240
329
  }
241
330
 
242
331
  /**
243
- * @dev verify if `_from` has the required permissions to set some keys
244
- * on the linked ERC725Account
245
- * @param from the address who want to set the keys
246
- * @param permissions the permissions
247
- * @param inputKeys the data keys being set
248
- * containing a list of keys-value pairs
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`.
249
337
  */
250
338
  function _verifyCanSetData(
251
- address from,
252
- bytes32 permissions,
253
- bytes32[] memory inputKeys
254
- ) internal view {
255
- // Skip if caller has SUPER permissions
256
- if (permissions.hasPermission(_PERMISSION_SUPER_SETDATA)) return;
257
-
258
- _requirePermissions(from, permissions, _PERMISSION_SETDATA);
259
-
260
- _verifyAllowedERC725YKeys(from, inputKeys);
261
- }
262
-
263
- function _verifyCanSetPermissions(
264
- bytes32 key,
265
- bytes memory value,
266
- address from,
267
- bytes32 permissions
268
- ) internal view {
269
- // prettier-ignore
270
- if (bytes12(key) == _LSP6KEY_ADDRESSPERMISSIONS_PERMISSIONS_PREFIX) {
271
-
272
- // key = AddressPermissions:Permissions:<address>
273
- _verifyCanSetBytes32Permissions(key, from, permissions);
274
-
275
- } else if (key == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY) {
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
+ );
276
348
 
277
- // key = AddressPermissions[]
278
- _verifyCanSetPermissionsArray(key, value, from, permissions);
279
-
280
- } else if (bytes16(key) == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY_PREFIX) {
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;
281
353
 
282
- // key = AddressPermissions[index]
283
- _requirePermissions(from, permissions, _PERMISSION_CHANGEPERMISSIONS);
354
+ _requirePermissions(controllerAddress, controllerPermissions, _PERMISSION_SETDATA);
284
355
 
285
- } else if (bytes12(key) == _LSP6KEY_ADDRESSPERMISSIONS_ALLOWEDADDRESSES_PREFIX) {
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
+ }
286
366
 
287
- bool isClearingArray = value.length == 0;
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
+ );
288
391
 
289
- // AddressPermissions:AllowedAddresses:<address>
290
- if (!isClearingArray && !LSP2Utils.isEncodedArrayOfAddresses(value)) {
291
- revert InvalidABIEncodedArray(value, "address");
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;
292
398
  }
293
399
 
294
- bytes memory storedAllowedAddresses = ERC725Y(target).getData(key);
400
+ ii = GasLib.uncheckedIncrement(ii);
401
+ } while (ii < inputDataKeys.length);
295
402
 
296
- if (storedAllowedAddresses.length == 0) {
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;
297
407
 
298
- _requirePermissions(from, permissions, _PERMISSION_ADDPERMISSIONS);
408
+ _requirePermissions(controllerAddress, controllerPermissions, _PERMISSION_SETDATA);
299
409
 
300
- } else {
410
+ _verifyAllowedERC725YDataKeys(
411
+ controllerAddress,
412
+ inputDataKeys,
413
+ ERC725Y(_target).getAllowedERC725YDataKeysFor(controllerAddress),
414
+ validatedInputDataKeys
415
+ );
416
+ }
417
+ }
301
418
 
302
- _requirePermissions(from, permissions, _PERMISSION_CHANGEPERMISSIONS);
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);
303
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);
304
467
  }
305
468
 
469
+ // LSP1UniversalReceiverDelegate or LSP1UniversalReceiverDelegate:<typeId>
306
470
  } else if (
307
- bytes12(key) == _LSP6KEY_ADDRESSPERMISSIONS_ALLOWEDSTANDARDS_PREFIX ||
308
- bytes12(key) == _LSP6KEY_ADDRESSPERMISSIONS_ALLOWEDFUNCTIONS_PREFIX
471
+ inputDataKey == _LSP1_UNIVERSAL_RECEIVER_DELEGATE_KEY ||
472
+ bytes12(inputDataKey) == _LSP1_UNIVERSAL_RECEIVER_DELEGATE_PREFIX
309
473
  ) {
310
- bool isClearingArray = value.length == 0;
474
+ return _getPermissionToSetLSP1Delegate(inputDataKey);
311
475
 
312
- // AddressPermissions:AllowedFunctions:<address>
313
- // AddressPermissions:AllowedStandards:<address>
314
- if (!isClearingArray && !LSP2Utils.isBytes4EncodedArray(value)) {
315
- revert InvalidABIEncodedArray(value, "bytes4");
316
- }
476
+ // LSP17Extension:<bytes4>
477
+ } else if (bytes12(inputDataKey) == _LSP17_EXTENSION_PREFIX) {
478
+ return _getPermissionToSetLSP17Extension(inputDataKey);
479
+ } else {
480
+ return _PERMISSION_SETDATA;
481
+ }
482
+ }
317
483
 
318
- bytes memory storedAllowedBytes4 = ERC725Y(target).getData(key);
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);
319
500
 
320
- if (storedAllowedBytes4.length == 0) {
501
+ // AddressPermissions[] -> array length
502
+ if (inputDataKey == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY) {
503
+ uint256 newLength = uint256(bytes32(inputDataValue));
321
504
 
322
- _requirePermissions(from, permissions, _PERMISSION_ADDPERMISSIONS);
505
+ return
506
+ newLength > uint256(bytes32(currentValue))
507
+ ? _PERMISSION_ADDCONTROLLER
508
+ : _PERMISSION_CHANGEPERMISSIONS;
509
+ }
323
510
 
324
- } else {
511
+ // AddressPermissions[index] -> array index
325
512
 
326
- _requirePermissions(from, permissions, _PERMISSION_CHANGEPERMISSIONS);
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
+ }
327
517
 
328
- }
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
+ }
329
539
 
330
- } else if (bytes12(key) == _LSP6KEY_ADDRESSPERMISSIONS_ALLOWEDERC725YKEYS_PREFIX) {
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
+ }
331
555
 
332
- bool isClearingArray = value.length == 0;
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
+ }
333
566
 
334
- // AddressPermissions:AllowedERC725YKeys:<address>
335
- if (!isClearingArray && !LSP2Utils.isEncodedArray(value)) {
336
- revert InvalidABIEncodedArray(value, "bytes32");
337
- }
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
+ }
338
581
 
339
- bytes memory storedAllowedERC725YKeys = ERC725Y(target).getData(key);
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
+ }
340
592
 
341
- if (storedAllowedERC725YKeys.length == 0) {
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
+ }
342
611
 
343
- _requirePermissions(from, permissions, _PERMISSION_ADDPERMISSIONS);
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
+ }
344
628
 
345
- } else {
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;
346
661
 
347
- _requirePermissions(from, permissions, _PERMISSION_CHANGEPERMISSIONS);
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
+ );
348
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)
349
713
  }
350
714
 
351
- }
352
- }
715
+ // voila you found the key ;)
716
+ if (allowedKey == (inputDataKey & mask)) return;
353
717
 
354
- function _verifyCanSetBytes32Permissions(
355
- bytes32 key,
356
- address from,
357
- bytes32 callerPermissions
358
- ) internal view {
359
- if (bytes32(ERC725Y(target).getData(key)) == bytes32(0)) {
360
- // if there is nothing stored under this data key,
361
- // we are trying to ADD permissions for a NEW address
362
- _requirePermissions(from, callerPermissions, _PERMISSION_ADDPERMISSIONS);
363
- } else {
364
- // if there are already some permissions stored under this data key,
365
- // we are trying to CHANGE the permissions of an address
366
- // (that has already some EXISTING permissions set)
367
- _requirePermissions(from, callerPermissions, _PERMISSION_CHANGEPERMISSIONS);
718
+ // move the pointer to the index of the next allowed data key
719
+ unchecked {
720
+ pointer = pointer + (length + 2);
721
+ }
368
722
  }
369
- }
370
-
371
- function _verifyCanSetPermissionsArray(
372
- bytes32 key,
373
- bytes memory value,
374
- address from,
375
- bytes32 permissions
376
- ) internal view {
377
- uint256 arrayLength = uint256(bytes32(ERC725Y(target).getData(key)));
378
- uint256 newLength = uint256(bytes32(value));
379
723
 
380
- if (newLength > arrayLength) {
381
- _requirePermissions(from, permissions, _PERMISSION_ADDPERMISSIONS);
382
- } else {
383
- _requirePermissions(from, permissions, _PERMISSION_CHANGEPERMISSIONS);
384
- }
724
+ revert NotAllowedERC725YDataKey(controllerAddress, inputDataKey);
385
725
  }
386
726
 
387
- function _verifyAllowedERC725YKeys(address from, bytes32[] memory inputKeys) internal view {
388
- bytes memory allowedERC725YKeysEncoded = ERC725Y(target).getAllowedERC725YKeysFor(from);
389
-
390
- // whitelist any ERC725Y key
391
- if (
392
- // if nothing in the list
393
- allowedERC725YKeysEncoded.length == 0 ||
394
- // if not correctly abi-encoded array
395
- !LSP2Utils.isEncodedArray(allowedERC725YKeysEncoded)
396
- ) return;
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;
397
766
 
398
- bytes32[] memory allowedERC725YKeys = abi.decode(allowedERC725YKeysEncoded, (bytes32[]));
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
+ );
399
785
 
400
- uint256 zeroBytesCount;
401
- bytes32 mask;
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
+ }
402
818
 
403
- // loop through each allowed ERC725Y key retrieved from storage
404
- for (uint256 ii = 0; ii < allowedERC725YKeys.length; ii++) {
405
- // required to know which part of the input key to compare against the allowed key
406
- zeroBytesCount = _countTrailingZeroBytes(allowedERC725YKeys[ii]);
407
-
408
- // loop through each keys given as input
409
- for (uint256 jj = 0; jj < inputKeys.length; jj++) {
410
- // skip permissions keys that have been previously checked and "nulled"
411
- if (inputKeys[jj] == bytes32(0)) continue;
412
-
413
- // use a bitmask to discard the last `n` bytes of the input key (where `n` = `zeroBytesCount`)
414
- // and compare only the relevant parts of each ERC725Y keys
415
- //
416
- // for an allowed key = 0xcafecafecafecafecafecafecafecafe00000000000000000000000000000000
417
- //
418
- // |------compare this part-------|------discard this part--------|
419
- // v v v
420
- // mask = 0xffffffffffffffffffffffffffffffff00000000000000000000000000000000
421
- // & input key = 0xcafecafecafecafecafecafecafecafe00000000000000000000000011223344
422
- //
423
- mask = bytes32(type(uint256).max) << (8 * zeroBytesCount);
424
-
425
- if (allowedERC725YKeys[ii] == (inputKeys[jj] & mask)) {
426
- // if the input key matches the allowed key
427
- // make it null to mark it as allowed
428
- inputKeys[jj] = bytes32(0);
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;
429
837
  }
430
838
  }
839
+
840
+ // Move the pointer to the next AllowedERC725YKey
841
+ unchecked {
842
+ pointer = pointer + (length + 2);
843
+ }
431
844
  }
432
845
 
433
- for (uint256 ii = 0; ii < inputKeys.length; ii++) {
434
- if (inputKeys[ii] != bytes32(0)) revert NotAllowedERC725YKey(from, inputKeys[ii]);
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
+ }
435
851
  }
436
852
  }
437
853
 
438
854
  /**
439
- * @dev verify if `from` has the required permissions to make an external call
440
- * via the linked ERC725Account
855
+ * @dev verify if `from` has the required permissions to interact with other addresses using the target.
441
856
  * @param from the address who want to run the execute function on the ERC725Account
442
857
  * @param permissions the permissions of the caller
443
858
  * @param payload the ABI encoded payload `target.execute(...)`
@@ -446,20 +861,19 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
446
861
  address from,
447
862
  bytes32 permissions,
448
863
  bytes calldata payload
449
- ) internal view {
864
+ ) internal view virtual {
865
+ // MUST be one of the ERC725X operation types.
450
866
  uint256 operationType = uint256(bytes32(payload[4:36]));
451
- require(operationType < 5, "LSP6KeyManager: invalid operation type");
452
867
 
453
- // TODO: if re-enable delegatecall, add check to ensure owner() + initialized() are not overriden after delegatecall
454
- require(
455
- operationType != OPERATION_DELEGATECALL,
456
- "LSP6KeyManager: operation DELEGATECALL is currently disallowed"
457
- );
868
+ // DELEGATECALL is disallowed by default on the LSP6 Key Manager.
869
+ if (operationType == OPERATION_4_DELEGATECALL) {
870
+ revert DelegateCallDisallowedViaKeyManager();
871
+ }
458
872
 
459
873
  uint256 value = uint256(bytes32(payload[68:100]));
460
874
 
461
875
  // prettier-ignore
462
- bool isContractCreation = operationType == OPERATION_CREATE || operationType == OPERATION_CREATE2;
876
+ bool isContractCreation = operationType == OPERATION_1_CREATE || operationType == OPERATION_2_CREATE2;
463
877
  bool isCallDataPresent = payload.length > 164;
464
878
 
465
879
  // SUPER operation only applies to contract call, not contract creation
@@ -467,42 +881,84 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
467
881
  ? false
468
882
  : permissions.hasPermission(_extractSuperPermissionFromOperation(operationType));
469
883
 
470
- if (isCallDataPresent) {
471
- // prettier-ignore
472
- hasSuperOperation || _requirePermissions(from, permissions, _extractPermissionFromOperation(operationType));
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));
473
892
  }
474
893
 
475
894
  bool hasSuperTransferValue = permissions.hasPermission(_PERMISSION_SUPER_TRANSFERVALUE);
476
895
 
477
- if (value > 0) {
478
- // prettier-ignore
479
- hasSuperTransferValue || _requirePermissions(from, permissions, _PERMISSION_TRANSFERVALUE);
896
+ if (value != 0 && !hasSuperTransferValue) {
897
+ _requirePermissions(from, permissions, _PERMISSION_TRANSFERVALUE);
480
898
  }
481
899
 
482
900
  // Skip on contract creation (CREATE or CREATE2)
483
901
  if (isContractCreation) return;
484
902
 
485
- // Skip if caller has SUPER permissions for operations
486
- if (hasSuperOperation && isCallDataPresent && value == 0) return;
903
+ // Skip if caller has SUPER permissions for external calls, with or without calldata (empty calls)
904
+ if (hasSuperOperation && value == 0) return;
487
905
 
488
906
  // Skip if caller has SUPER permission for value transfers
489
- if (hasSuperTransferValue && !isCallDataPresent && value > 0) return;
907
+ if (hasSuperTransferValue && !isCallDataPresent && value != 0) return;
490
908
 
491
909
  // Skip if both SUPER permissions are present
492
910
  if (hasSuperOperation && hasSuperTransferValue) return;
493
911
 
494
- // CHECK for ALLOWED ADDRESSES
912
+ _verifyAllowedCall(from, payload);
913
+ }
914
+
915
+ function _verifyAllowedCall(address from, bytes calldata payload) internal view virtual {
916
+ // CHECK for ALLOWED CALLS
495
917
  address to = address(bytes20(payload[48:68]));
496
- _verifyAllowedAddress(from, to);
497
918
 
498
- if (to.code.length > 0) {
499
- // CHECK for ALLOWED STANDARDS
500
- _verifyAllowedStandard(from, to);
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;
501
933
 
502
- // CHECK for ALLOWED FUNCTIONS
503
- // extract bytes4 function selector from payload passed to ERC725X.execute(...)
504
- if (payload.length >= 168) _verifyAllowedFunction(from, bytes4(payload[164:168]));
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;
505
959
  }
960
+
961
+ revert NotAllowedCall(from, to, selector);
506
962
  }
507
963
 
508
964
  /**
@@ -514,136 +970,107 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
514
970
  function _extractPermissionFromOperation(uint256 operationType)
515
971
  internal
516
972
  pure
973
+ virtual
517
974
  returns (bytes32 permissionsRequired)
518
975
  {
519
- if (operationType == OPERATION_CALL) return _PERMISSION_CALL;
520
- else if (operationType == OPERATION_CREATE) return _PERMISSION_DEPLOY;
521
- else if (operationType == OPERATION_CREATE2) return _PERMISSION_DEPLOY;
522
- else if (operationType == OPERATION_STATICCALL) return _PERMISSION_STATICCALL;
523
- else if (operationType == OPERATION_DELEGATECALL) return _PERMISSION_DELEGATECALL;
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;
524
981
  }
525
982
 
983
+ /**
984
+ * @dev returns the `superPermission` needed for a specific `operationType` of the `execute(..)`
985
+ */
526
986
  function _extractSuperPermissionFromOperation(uint256 operationType)
527
987
  internal
528
988
  pure
989
+ virtual
529
990
  returns (bytes32 superPermission)
530
991
  {
531
- if (operationType == OPERATION_CALL) return _PERMISSION_SUPER_CALL;
532
- else if (operationType == OPERATION_STATICCALL) return _PERMISSION_SUPER_STATICCALL;
533
- else if (operationType == OPERATION_DELEGATECALL) return _PERMISSION_SUPER_DELEGATECALL;
534
- }
535
-
536
- /**
537
- * @dev verify if `from` is authorised to interact with address `to` via the linked ERC725Account
538
- * @param from the caller address
539
- * @param to the address to interact with
540
- */
541
- function _verifyAllowedAddress(address from, address to) internal view {
542
- bytes memory allowedAddresses = ERC725Y(target).getAllowedAddressesFor(from);
543
-
544
- // whitelist any address
545
- if (
546
- // if nothing in the list
547
- allowedAddresses.length == 0 ||
548
- // if not correctly abi-encoded array of address[]
549
- !LSP2Utils.isEncodedArrayOfAddresses(allowedAddresses)
550
- ) return;
551
-
552
- address[] memory allowedAddressesList = abi.decode(allowedAddresses, (address[]));
553
-
554
- for (uint256 ii = 0; ii < allowedAddressesList.length; ii++) {
555
- if (to == allowedAddressesList[ii]) return;
556
- }
557
- revert NotAllowedAddress(from, to);
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;
558
995
  }
559
996
 
560
997
  /**
561
- * @dev if `from` is restricted to interact with contracts that implement a specific interface,
562
- * verify that `to` implements one of these interface.
998
+ * @dev revert if `from`'s `addressPermissions` doesn't contain `permissionsRequired`
563
999
  * @param from the caller address
564
- * @param to the address of the contract to interact with
1000
+ * @param addressPermissions the caller's permissions BitArray
1001
+ * @param permissionRequired the required permission
565
1002
  */
566
- function _verifyAllowedStandard(address from, address to) internal view {
567
- bytes memory allowedStandards = ERC725Y(target).getAllowedStandardsFor(from);
568
-
569
- // whitelist any standard interface (ERC165)
570
- if (
571
- // if nothing in the list
572
- allowedStandards.length == 0 ||
573
- // if not correctly abi-encoded array of bytes4[]
574
- !LSP2Utils.isBytes4EncodedArray(allowedStandards)
575
- ) return;
576
-
577
- bytes4[] memory allowedStandardsList = abi.decode(allowedStandards, (bytes4[]));
578
-
579
- for (uint256 ii = 0; ii < allowedStandardsList.length; ii++) {
580
- if (to.supportsERC165Interface(allowedStandardsList[ii])) return;
581
- }
582
- revert NotAllowedStandard(from, to);
583
- }
584
-
585
- /**
586
- * @dev verify if `from` is authorised to use the linked ERC725Account
587
- * to run a specific function `functionSelector` at a target contract
588
- * @param from the caller address
589
- * @param functionSelector the bytes4 function selector of the function to run
590
- * at the target contract
591
- */
592
- function _verifyAllowedFunction(address from, bytes4 functionSelector) internal view {
593
- bytes memory allowedFunctions = ERC725Y(target).getAllowedFunctionsFor(from);
594
-
595
- // whitelist any function
596
- if (
597
- // if nothing in the list
598
- allowedFunctions.length == 0 ||
599
- // if not correctly abi-encoded array of bytes4[]
600
- !LSP2Utils.isBytes4EncodedArray(allowedFunctions)
601
- ) return;
602
-
603
- bytes4[] memory allowedFunctionsList = abi.decode(allowedFunctions, (bytes4[]));
604
-
605
- for (uint256 ii = 0; ii < allowedFunctionsList.length; ii++) {
606
- if (functionSelector == allowedFunctionsList[ii]) return;
607
- }
608
- revert NotAllowedFunction(from, functionSelector);
609
- }
610
-
611
- function _countTrailingZeroBytes(bytes32 key) internal pure returns (uint256) {
612
- uint256 index = 31;
613
-
614
- // CHECK each bytes of the key, starting from the end (right to left)
615
- // skip each empty bytes `0x00` to find the first non-empty byte
616
- while (key[index] == 0x00 && index != 0) index--;
617
-
618
- return 32 - (index + 1);
619
- }
620
-
621
1003
  function _requirePermissions(
622
1004
  address from,
623
1005
  bytes32 addressPermissions,
624
1006
  bytes32 permissionRequired
625
- ) internal pure returns (bool) {
1007
+ ) internal pure virtual {
626
1008
  if (!addressPermissions.hasPermission(permissionRequired)) {
627
- string memory permissionErrorString = _getPermissionErrorString(permissionRequired);
1009
+ string memory permissionErrorString = _getPermissionName(permissionRequired);
628
1010
  revert NotAuthorised(from, permissionErrorString);
629
1011
  }
630
1012
  }
631
1013
 
632
- function _getPermissionErrorString(bytes32 permission)
1014
+ /**
1015
+ * @dev returns the name of the permission as a string
1016
+ */
1017
+ function _getPermissionName(bytes32 permission)
633
1018
  internal
634
1019
  pure
1020
+ virtual
635
1021
  returns (string memory errorMessage)
636
1022
  {
637
1023
  if (permission == _PERMISSION_CHANGEOWNER) return "TRANSFEROWNERSHIP";
638
1024
  if (permission == _PERMISSION_CHANGEPERMISSIONS) return "CHANGEPERMISSIONS";
639
- if (permission == _PERMISSION_ADDPERMISSIONS) return "ADDPERMISSIONS";
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";
640
1033
  if (permission == _PERMISSION_SETDATA) return "SETDATA";
641
1034
  if (permission == _PERMISSION_CALL) return "CALL";
642
1035
  if (permission == _PERMISSION_STATICCALL) return "STATICCALL";
643
1036
  if (permission == _PERMISSION_DELEGATECALL) return "DELEGATECALL";
644
- // TODO: add support to display CREATE or CREATE2 in the revert error
645
1037
  if (permission == _PERMISSION_DEPLOY) return "DEPLOY";
646
1038
  if (permission == _PERMISSION_TRANSFERVALUE) return "TRANSFERVALUE";
647
1039
  if (permission == _PERMISSION_SIGN) return "SIGN";
648
1040
  }
1041
+
1042
+ /**
1043
+ * @dev Initialise _reentrancyStatus to _NOT_ENTERED.
1044
+ */
1045
+ function _setupLSP6ReentrancyGuard() internal virtual {
1046
+ _reentrancyStatus = false;
1047
+ }
1048
+
1049
+ /**
1050
+ * @dev Update the status from `_NON_ENTERED` to `_ENTERED` and checks if
1051
+ * the status is `_ENTERED` in order to revert the call unless the caller has the REENTRANCY permission
1052
+ * Used in the beginning of the `nonReentrant` modifier, before the method execution starts.
1053
+ */
1054
+ function _nonReentrantBefore(address from) internal virtual {
1055
+ if (_reentrancyStatus) {
1056
+ // CHECK the caller has REENTRANCY permission
1057
+ _requirePermissions(
1058
+ from,
1059
+ ERC725Y(_target).getPermissionsFor(from),
1060
+ _PERMISSION_REENTRANCY
1061
+ );
1062
+ } else {
1063
+ _reentrancyStatus = true;
1064
+ }
1065
+ }
1066
+
1067
+ /**
1068
+ * @dev Resets the status to `_NOT_ENTERED`
1069
+ * Used in the end of the `nonReentrant` modifier after the method execution is terminated
1070
+ */
1071
+ function _nonReentrantAfter() internal virtual {
1072
+ // By storing the original value once again, a refund is triggered (see
1073
+ // https://eips.ethereum.org/EIPS/eip-2200)
1074
+ _reentrancyStatus = false;
1075
+ }
649
1076
  }