@lukso/lsp-smart-contracts 0.7.0 → 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 (164) hide show
  1. package/CONTRIBUTING.md +31 -0
  2. package/README.md +73 -20
  3. package/artifacts/Create2Factory.json +2 -2
  4. package/artifacts/LSP0ERC725Account.json +194 -15
  5. package/artifacts/LSP0ERC725AccountInit.json +194 -15
  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 +16 -0
  12. package/artifacts/LSP4DigitalAssetMetadata.json +16 -0
  13. package/artifacts/LSP4DigitalAssetMetadataInitAbstract.json +16 -0
  14. package/artifacts/LSP6KeyManager.json +177 -33
  15. package/artifacts/LSP6KeyManagerInit.json +177 -33
  16. package/artifacts/LSP7CappedSupply.json +21 -5
  17. package/artifacts/LSP7CappedSupplyInitAbstract.json +21 -5
  18. package/artifacts/LSP7CompatibleERC20.json +21 -5
  19. package/artifacts/LSP7CompatibleERC20InitAbstract.json +23 -7
  20. package/artifacts/LSP7CompatibleERC20Mintable.json +24 -8
  21. package/artifacts/LSP7CompatibleERC20MintableInit.json +24 -8
  22. package/artifacts/LSP7DigitalAsset.json +21 -5
  23. package/artifacts/LSP7DigitalAssetInitAbstract.json +21 -5
  24. package/artifacts/LSP7Mintable.json +24 -8
  25. package/artifacts/LSP7MintableInit.json +24 -8
  26. package/artifacts/LSP8CappedSupply.json +22 -6
  27. package/artifacts/LSP8CappedSupplyInitAbstract.json +22 -6
  28. package/artifacts/LSP8CompatibleERC721.json +23 -7
  29. package/artifacts/LSP8CompatibleERC721InitAbstract.json +25 -9
  30. package/artifacts/LSP8CompatibleERC721Mintable.json +26 -10
  31. package/artifacts/LSP8CompatibleERC721MintableInit.json +26 -10
  32. package/artifacts/LSP8IdentifiableDigitalAsset.json +22 -6
  33. package/artifacts/LSP8IdentifiableDigitalAssetInitAbstract.json +22 -6
  34. package/artifacts/LSP8Mintable.json +25 -9
  35. package/artifacts/LSP8MintableInit.json +25 -9
  36. package/artifacts/LSP9Vault.json +202 -17
  37. package/artifacts/LSP9VaultInit.json +202 -17
  38. package/artifacts/UniversalProfile.json +194 -15
  39. package/artifacts/UniversalProfileInit.json +194 -15
  40. package/artifacts/UniversalReceiverAddressStore.json +3 -3
  41. package/constants.js +377 -115
  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 +261 -63
  47. package/contracts/LSP0ERC725Account/LSP0ERC725AccountInit.sol +2 -2
  48. package/contracts/LSP0ERC725Account/LSP0ERC725AccountInitAbstract.sol +2 -1
  49. package/contracts/LSP10ReceivedVaults/LSP10Constants.sol +2 -2
  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 +157 -37
  72. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateVault/LSP1UniversalReceiverDelegateVault.sol +64 -31
  73. package/contracts/LSP1UniversalReceiver/LSP1Utils.sol +53 -40
  74. package/contracts/LSP2ERC725YJSONSchema/LSP2Utils.sol +45 -23
  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 +2 -2
  80. package/contracts/LSP4DigitalAssetMetadata/LSP4DigitalAssetMetadataInitAbstract.sol +2 -2
  81. package/contracts/LSP4DigitalAssetMetadata/LSP4Errors.sol +3 -3
  82. package/contracts/LSP5ReceivedAssets/LSP5Constants.sol +2 -2
  83. package/contracts/LSP5ReceivedAssets/LSP5Utils.sol +102 -114
  84. package/contracts/LSP6KeyManager/ILSP6KeyManager.sol +25 -7
  85. package/contracts/LSP6KeyManager/LSP6Constants.sol +43 -38
  86. package/contracts/LSP6KeyManager/LSP6Errors.sol +90 -21
  87. package/contracts/LSP6KeyManager/LSP6KeyManager.sol +2 -1
  88. package/contracts/LSP6KeyManager/LSP6KeyManagerCore.sol +772 -364
  89. package/contracts/LSP6KeyManager/LSP6KeyManagerInit.sol +1 -1
  90. package/contracts/LSP6KeyManager/LSP6KeyManagerInitAbstract.sol +3 -2
  91. package/contracts/LSP6KeyManager/LSP6Utils.sol +125 -34
  92. package/contracts/LSP7DigitalAsset/ILSP7DigitalAsset.sol +8 -8
  93. package/contracts/LSP7DigitalAsset/LSP7Constants.sol +6 -6
  94. package/contracts/LSP7DigitalAsset/LSP7DigitalAsset.sol +1 -1
  95. package/contracts/LSP7DigitalAsset/LSP7DigitalAssetCore.sol +38 -34
  96. package/contracts/LSP7DigitalAsset/LSP7DigitalAssetInitAbstract.sol +1 -1
  97. package/contracts/LSP7DigitalAsset/LSP7Errors.sol +28 -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 +2 -2
  107. package/contracts/LSP7DigitalAsset/presets/LSP7CompatibleERC20MintableInitAbstract.sol +3 -3
  108. package/contracts/LSP7DigitalAsset/presets/LSP7Mintable.sol +3 -5
  109. package/contracts/LSP7DigitalAsset/presets/LSP7MintableInit.sol +2 -2
  110. package/contracts/LSP7DigitalAsset/presets/LSP7MintableInitAbstract.sol +3 -3
  111. package/contracts/LSP8IdentifiableDigitalAsset/ILSP8IdentifiableDigitalAsset.sol +7 -7
  112. package/contracts/LSP8IdentifiableDigitalAsset/LSP8Constants.sol +7 -7
  113. package/contracts/LSP8IdentifiableDigitalAsset/LSP8Errors.sol +40 -2
  114. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAsset.sol +2 -2
  115. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAssetCore.sol +53 -50
  116. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAssetInitAbstract.sol +1 -1
  117. package/contracts/LSP8IdentifiableDigitalAsset/extensions/ILSP8CompatibleERC721.sol +5 -2
  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 +32 -4
  124. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8EnumerableInitAbstract.sol +35 -8
  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 +3 -3
  128. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8CompatibleERC721MintableInitAbstract.sol +3 -3
  129. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8Mintable.sol +3 -4
  130. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8MintableInit.sol +2 -2
  131. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8MintableInitAbstract.sol +3 -3
  132. package/contracts/LSP9Vault/LSP9Constants.sol +10 -10
  133. package/contracts/LSP9Vault/LSP9Errors.sol +8 -0
  134. package/contracts/LSP9Vault/LSP9Vault.sol +6 -4
  135. package/contracts/LSP9Vault/LSP9VaultCore.sol +289 -126
  136. package/contracts/LSP9Vault/LSP9VaultInit.sol +2 -2
  137. package/contracts/LSP9Vault/LSP9VaultInitAbstract.sol +4 -3
  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 -14
  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 +1 -1
  145. package/package.json +17 -9
  146. package/artifacts/UniversalFactory.json +0 -127
  147. package/contracts/Custom/ClaimOwnership.sol +0 -96
  148. package/contracts/Custom/ERC165Checker.sol +0 -130
  149. package/contracts/Custom/IClaimOwnership.sol +0 -12
  150. package/contracts/Factories/UniversalFactory.sol +0 -159
  151. package/contracts/LSP1UniversalReceiver/ILSP1UniversalReceiverDelegate.sol +0 -24
  152. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateUP/Handling/TokenAndVaultHandling.sol +0 -82
  153. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateVault/Handling/TokenHandling.sol +0 -73
  154. package/contracts/LSP2ERC725YJSONSchema/LSP2Errors.sol +0 -9
  155. package/contracts/LSP7DigitalAsset/extensions/ILSP7CappedSupply.sol +0 -17
  156. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupplyCore.sol +0 -57
  157. package/contracts/LSP7DigitalAsset/extensions/LSP7CompatibleERC20Core.sol +0 -111
  158. package/contracts/LSP8IdentifiableDigitalAsset/extensions/ILSP8CappedSupply.sol +0 -17
  159. package/contracts/LSP8IdentifiableDigitalAsset/extensions/ILSP8Enumerable.sol +0 -17
  160. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupplyCore.sol +0 -58
  161. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleConstants.sol +0 -6
  162. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721Core.sol +0 -211
  163. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8EnumerableCore.sol +0 -50
  164. package/contracts/Utils/UtilsLib.sol +0 -31
@@ -7,8 +7,7 @@ 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
 
@@ -17,48 +16,65 @@ import {GasLib} from "../Utils/GasLib.sol";
17
16
  import {BytesLib} from "solidity-bytes-utils/contracts/BytesLib.sol";
18
17
  import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
19
18
  import {Address} from "@openzeppelin/contracts/utils/Address.sol";
20
- import {ERC165Checker} from "../Custom/ERC165Checker.sol";
21
- import {LSP2Utils} from "../LSP2ERC725YJSONSchema/LSP2Utils.sol";
19
+ import {ERC165Checker} from "@openzeppelin/contracts/utils/introspection/ERC165Checker.sol";
22
20
  import {LSP6Utils} from "./LSP6Utils.sol";
21
+ import {EIP191Signer} from "../Custom/EIP191Signer.sol";
23
22
 
24
23
  // errors
25
24
  import "./LSP6Errors.sol";
26
- import {InvalidABIEncodedArray} from "../LSP2ERC725YJSONSchema/LSP2Errors.sol";
27
25
 
28
26
  // constants
29
27
  import {
30
- // ERC725X
31
- OPERATION_CALL,
32
- OPERATION_CREATE,
33
- OPERATION_CREATE2,
34
- OPERATION_STATICCALL,
35
- OPERATION_DELEGATECALL,
36
- // ERC725Y
28
+ OPERATION_0_CALL,
29
+ OPERATION_1_CREATE,
30
+ OPERATION_2_CREATE2,
31
+ OPERATION_3_STATICCALL,
32
+ OPERATION_4_DELEGATECALL,
37
33
  SETDATA_SELECTOR,
38
- SETDATA_ARRAY_SELECTOR
34
+ SETDATA_ARRAY_SELECTOR,
35
+ EXECUTE_SELECTOR
39
36
  } from "@erc725/smart-contracts/contracts/constants.sol";
40
37
  import {
41
38
  _INTERFACEID_ERC1271,
42
39
  _ERC1271_MAGICVALUE,
43
40
  _ERC1271_FAILVALUE
44
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
+
45
48
  import "./LSP6Constants.sol";
46
49
 
50
+ import {_LSP17_EXTENSION_PREFIX} from "../LSP17ContractExtension/LSP17Constants.sol";
51
+
47
52
  /**
48
- * @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.
49
54
  * @author Fabian Vogelsteller <frozeman>, Jean Cavallera (CJ42), Yamen Merhi (YamenMerhi)
50
- * @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(...)`.
51
57
  */
52
58
  abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
53
- using LSP2Utils for *;
54
59
  using LSP6Utils for *;
55
60
  using Address for address;
56
61
  using ECDSA for bytes32;
57
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;
58
71
 
59
- address public target;
60
72
  mapping(address => mapping(uint256 => uint256)) internal _nonceStore;
61
73
 
74
+ function target() public view returns (address) {
75
+ return _target;
76
+ }
77
+
62
78
  /**
63
79
  * @dev See {IERC165-supportsInterface}.
64
80
  */
@@ -72,9 +88,9 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
72
88
  /**
73
89
  * @inheritdoc ILSP6KeyManager
74
90
  */
75
- function getNonce(address from, uint256 channelId) public view returns (uint256) {
76
- uint128 nonceId = uint128(_nonceStore[from][channelId]);
77
- 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;
78
94
  }
79
95
 
80
96
  /**
@@ -88,7 +104,7 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
88
104
  address recoveredAddress = dataHash.recover(signature);
89
105
 
90
106
  return (
91
- ERC725Y(target).getPermissionsFor(recoveredAddress).hasPermission(_PERMISSION_SIGN)
107
+ ERC725Y(_target).getPermissionsFor(recoveredAddress).hasPermission(_PERMISSION_SIGN)
92
108
  ? _ERC1271_MAGICVALUE
93
109
  : _ERC1271_FAILVALUE
94
110
  );
@@ -97,28 +113,126 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
97
113
  /**
98
114
  * @inheritdoc ILSP6KeyManager
99
115
  */
100
- function execute(bytes calldata payload) public payable returns (bytes memory) {
101
- _verifyPermissions(msg.sender, payload);
116
+ function execute(bytes calldata payload) public payable virtual returns (bytes memory) {
117
+ return _execute(msg.value, payload);
118
+ }
119
+
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
+ }
102
132
 
103
- return _executePayload(payload);
133
+ bytes[] memory results = new bytes[](payloads.length);
134
+ uint256 totalValues;
135
+
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);
104
160
  }
105
161
 
106
162
  /**
107
163
  * @inheritdoc ILSP6KeyManager
108
164
  */
109
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(
110
214
  bytes memory signature,
111
215
  uint256 nonce,
216
+ uint256 msgValue,
112
217
  bytes calldata payload
113
- ) public payable returns (bytes memory) {
114
- 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,
115
225
  block.chainid,
116
- address(this), // needs to be signed for this keyManager
117
226
  nonce,
227
+ msgValue,
118
228
  payload
119
229
  );
120
230
 
121
- address signer = keccak256(blob).toEthSignedMessageHash().recover(signature);
231
+ address signer = address(this).toDataWithIntendedValidator(encodedMessage).recover(
232
+ signature
233
+ );
234
+
235
+ _nonReentrantBefore(signer);
122
236
 
123
237
  if (!_isValidNonce(signer, nonce)) {
124
238
  revert InvalidRelayNonce(signer, nonce, signature);
@@ -129,31 +243,36 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
129
243
 
130
244
  _verifyPermissions(signer, payload);
131
245
 
132
- return _executePayload(payload);
246
+ bytes memory result = _executePayload(msgValue, payload);
247
+
248
+ _nonReentrantAfter();
249
+
250
+ return result;
133
251
  }
134
252
 
135
- /**
136
- * @notice execute the received payload (obtained via `execute(...)` and `executeRelayCall(...)`)
137
- *
138
- * @param payload the payload to execute
139
- * @return bytes the result from calling the target with `_payload`
140
- */
141
- function _executePayload(bytes calldata payload) internal returns (bytes memory) {
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);
142
264
 
143
- // solhint-disable avoid-low-level-calls
144
- (bool success, bytes memory returnData) = target.call{value: msg.value, gas: gasleft()}(
145
- payload
265
+ (bool success, bytes memory returnData) = _target.call{value: msgValue, gas: gasleft()}(
266
+ payload
146
267
  );
147
268
  bytes memory result = Address.verifyCallResult(
148
269
  success,
149
270
  returnData,
150
- "LSP6: Unknown Error occured when calling the linked target contract"
271
+ "LSP6: failed executing payload"
151
272
  );
152
273
 
153
- emit Executed(msg.value, bytes4(payload));
154
- return result.length != 0 ? abi.decode(result, (bytes)) : result;
155
-
156
- }
274
+ return result.length != 0 ? abi.decode(result, (bytes)) : result;
275
+ }
157
276
 
158
277
  /**
159
278
  * @notice verify the nonce `_idx` for `_from` (obtained via `getNonce(...)`)
@@ -163,7 +282,7 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
163
282
  * @param from caller address
164
283
  * @param idx (channel id + nonce within the channel)
165
284
  */
166
- function _isValidNonce(address from, uint256 idx) internal view returns (bool) {
285
+ function _isValidNonce(address from, uint256 idx) internal view virtual returns (bool) {
167
286
  // idx % (1 << 128) = nonce
168
287
  // (idx >> 128) = channel
169
288
  // equivalent to: return (nonce == _nonceStore[_from][channel]
@@ -171,75 +290,37 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
171
290
  }
172
291
 
173
292
  /**
174
- * @dev verify the permissions of the _from address that want to interact with the `target`
175
- * @param from the address making the request
176
- * @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`.
177
296
  */
178
- function _verifyPermissions(address from, bytes calldata payload) internal view {
179
- bytes4 erc725Function = bytes4(payload[:4]);
180
-
181
- // get the permissions of the caller
182
- bytes32 permissions = ERC725Y(target).getPermissionsFor(from);
183
-
297
+ function _verifyPermissions(address from, bytes calldata payload) internal view virtual {
298
+ bytes32 permissions = ERC725Y(_target).getPermissionsFor(from);
184
299
  if (permissions == bytes32(0)) revert NoPermissionsSet(from);
185
300
 
301
+ bytes4 erc725Function = bytes4(payload);
302
+
303
+ // ERC725Y.setData(bytes32,bytes)
186
304
  if (erc725Function == SETDATA_SELECTOR) {
187
305
  (bytes32 inputKey, bytes memory inputValue) = abi.decode(payload[4:], (bytes32, bytes));
188
306
 
189
- if (bytes16(inputKey) == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY_PREFIX) {
190
- // CHECK if key = AddressPermissions[] or AddressPermissions[index]
191
- _verifyCanSetPermissionsArray(inputKey, inputValue, from, permissions);
307
+ _verifyCanSetData(from, permissions, inputKey, inputValue);
192
308
 
193
- } else if (bytes6(inputKey) == _LSP6KEY_ADDRESSPERMISSIONS_PREFIX) {
194
- // CHECK for permission keys
195
- _verifyCanSetPermissions(inputKey, inputValue, from, permissions);
196
-
197
- } else {
198
- bytes32[] memory wrappedInputKey = new bytes32[](1);
199
- wrappedInputKey[0] = inputKey;
200
-
201
- _verifyCanSetData(from, permissions, wrappedInputKey);
202
- }
309
+ // ERC725Y.setData(bytes32[],bytes[])
203
310
  } else if (erc725Function == SETDATA_ARRAY_SELECTOR) {
204
311
  (bytes32[] memory inputKeys, bytes[] memory inputValues) = abi.decode(
205
312
  payload[4:],
206
313
  (bytes32[], bytes[])
207
314
  );
208
315
 
209
- bool isSettingERC725YKeys = false;
210
-
211
- // loop through each ERC725Y data keys
212
- for (uint256 ii = 0; ii < inputKeys.length; ii = GasLib.uncheckedIncrement(ii)) {
213
- bytes32 key = inputKeys[ii];
214
- bytes memory value = inputValues[ii];
215
-
216
- if (bytes16(key) == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY_PREFIX) {
217
- // CHECK if key = AddressPermissions[] or AddressPermissions[index]
218
- _verifyCanSetPermissionsArray(key, value, from, permissions);
219
-
220
- // "nullify" permission keys to not check them against allowed ERC725Y keys
221
- inputKeys[ii] = bytes32(0);
222
-
223
- } else if (bytes6(key) == _LSP6KEY_ADDRESSPERMISSIONS_PREFIX) {
224
- // CHECK for permissions keys
225
- _verifyCanSetPermissions(key, value, from, permissions);
226
-
227
- // "nullify" permission keys to not check them against allowed ERC725Y keys
228
- inputKeys[ii] = bytes32(0);
229
- } else {
230
- // if the key is any other bytes32 key
231
- isSettingERC725YKeys = true;
232
- }
233
- }
316
+ _verifyCanSetData(from, permissions, inputKeys, inputValues);
234
317
 
235
- if (isSettingERC725YKeys) {
236
- _verifyCanSetData(from, permissions, inputKeys);
237
- }
238
- } else if (erc725Function == IERC725X.execute.selector) {
318
+ // ERC725X.execute(uint256,address,uint256,bytes)
319
+ } else if (erc725Function == EXECUTE_SELECTOR) {
239
320
  _verifyCanExecute(from, permissions, payload);
240
321
  } else if (
241
- erc725Function == OwnableUnset.transferOwnership.selector ||
242
- erc725Function == IClaimOwnership.claimOwnership.selector
322
+ erc725Function == ILSP14Ownable2Step.transferOwnership.selector ||
323
+ erc725Function == ILSP14Ownable2Step.acceptOwnership.selector
243
324
  ) {
244
325
  _requirePermissions(from, permissions, _PERMISSION_CHANGEOWNER);
245
326
  } else {
@@ -248,219 +329,530 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
248
329
  }
249
330
 
250
331
  /**
251
- * @dev verify if `_from` has the required permissions to set some keys
252
- * on the linked ERC725Account
253
- * @param from the address who want to set the keys
254
- * @param permissions the permissions
255
- * @param inputKeys the data keys being set
256
- * 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`.
257
337
  */
258
338
  function _verifyCanSetData(
259
- address from,
260
- bytes32 permissions,
261
- bytes32[] memory inputKeys
262
- ) internal view {
263
- // Skip if caller has SUPER permissions
264
- if (permissions.hasPermission(_PERMISSION_SUPER_SETDATA)) return;
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;
265
353
 
266
- _requirePermissions(from, permissions, _PERMISSION_SETDATA);
354
+ _requirePermissions(controllerAddress, controllerPermissions, _PERMISSION_SETDATA);
267
355
 
268
- _verifyAllowedERC725YKeys(from, inputKeys);
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
+ }
269
365
  }
270
366
 
271
- function _verifyCanSetPermissions(
272
- bytes32 key,
273
- bytes memory value,
274
- address from,
275
- bytes32 permissions
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
276
379
  ) internal view virtual {
277
- // prettier-ignore
278
- if (bytes12(key) == _LSP6KEY_ADDRESSPERMISSIONS_PERMISSIONS_PREFIX) {
380
+ bool isSettingERC725YKeys;
381
+ bool[] memory validatedInputDataKeys = new bool[](inputDataKeys.length);
279
382
 
280
- // key = AddressPermissions:Permissions:<address>
281
- _verifyCanSetBytes32Permissions(key, from, permissions);
383
+ bytes32 requiredPermission;
282
384
 
283
- } else if (bytes12(key) == _LSP6KEY_ADDRESSPERMISSIONS_ALLOWEDADDRESSES_PREFIX) {
284
-
285
- bool isClearingArray = value.length == 0;
385
+ uint256 ii;
386
+ do {
387
+ requiredPermission = _getPermissionRequiredToSetDataKey(
388
+ inputDataKeys[ii],
389
+ inputDataValues[ii]
390
+ );
286
391
 
287
- // AddressPermissions:AllowedAddresses:<address>
288
- if (!isClearingArray && !LSP2Utils.isEncodedArrayOfAddresses(value)) {
289
- 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;
290
398
  }
291
399
 
292
- bytes memory storedAllowedAddresses = ERC725Y(target).getData(key);
293
-
294
- if (storedAllowedAddresses.length == 0) {
400
+ ii = GasLib.uncheckedIncrement(ii);
401
+ } while (ii < inputDataKeys.length);
295
402
 
296
- _requirePermissions(from, permissions, _PERMISSION_ADDPERMISSIONS);
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
- } else {
408
+ _requirePermissions(controllerAddress, controllerPermissions, _PERMISSION_SETDATA);
299
409
 
300
- _requirePermissions(from, permissions, _PERMISSION_CHANGEPERMISSIONS);
410
+ _verifyAllowedERC725YDataKeys(
411
+ controllerAddress,
412
+ inputDataKeys,
413
+ ERC725Y(_target).getAllowedERC725YDataKeysFor(controllerAddress),
414
+ validatedInputDataKeys
415
+ );
416
+ }
417
+ }
301
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);
302
467
  }
303
468
 
469
+ // LSP1UniversalReceiverDelegate or LSP1UniversalReceiverDelegate:<typeId>
304
470
  } else if (
305
- bytes12(key) == _LSP6KEY_ADDRESSPERMISSIONS_ALLOWEDSTANDARDS_PREFIX ||
306
- bytes12(key) == _LSP6KEY_ADDRESSPERMISSIONS_ALLOWEDFUNCTIONS_PREFIX
471
+ inputDataKey == _LSP1_UNIVERSAL_RECEIVER_DELEGATE_KEY ||
472
+ bytes12(inputDataKey) == _LSP1_UNIVERSAL_RECEIVER_DELEGATE_PREFIX
307
473
  ) {
308
- bool isClearingArray = value.length == 0;
474
+ return _getPermissionToSetLSP1Delegate(inputDataKey);
309
475
 
310
- // AddressPermissions:AllowedFunctions:<address>
311
- // AddressPermissions:AllowedStandards:<address>
312
- if (!isClearingArray && !LSP2Utils.isBytes4EncodedArray(value)) {
313
- revert InvalidABIEncodedArray(value, "bytes4");
314
- }
476
+ // LSP17Extension:<bytes4>
477
+ } else if (bytes12(inputDataKey) == _LSP17_EXTENSION_PREFIX) {
478
+ return _getPermissionToSetLSP17Extension(inputDataKey);
479
+ } else {
480
+ return _PERMISSION_SETDATA;
481
+ }
482
+ }
315
483
 
316
- 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);
317
500
 
318
- if (storedAllowedBytes4.length == 0) {
501
+ // AddressPermissions[] -> array length
502
+ if (inputDataKey == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY) {
503
+ uint256 newLength = uint256(bytes32(inputDataValue));
319
504
 
320
- _requirePermissions(from, permissions, _PERMISSION_ADDPERMISSIONS);
505
+ return
506
+ newLength > uint256(bytes32(currentValue))
507
+ ? _PERMISSION_ADDCONTROLLER
508
+ : _PERMISSION_CHANGEPERMISSIONS;
509
+ }
321
510
 
322
- } else {
511
+ // AddressPermissions[index] -> array index
323
512
 
324
- _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
+ }
325
517
 
326
- }
518
+ return currentValue.length == 0 ? _PERMISSION_ADDCONTROLLER : _PERMISSION_CHANGEPERMISSIONS;
519
+ }
327
520
 
328
- } else if (bytes12(key) == _LSP6KEY_ADDRESSPERMISSIONS_ALLOWEDERC725YKEYS_PREFIX) {
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
- bool isClearingArray = value.length == 0;
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
- // AddressPermissions:AllowedERC725YKeys:<address>
333
- if (!isClearingArray && !LSP2Utils.isEncodedArray(value)) {
334
- revert InvalidABIEncodedArray(value, "bytes32");
335
- }
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
+ }
336
566
 
337
- bytes memory storedAllowedERC725YKeys = ERC725Y(target).getData(key);
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
- if (storedAllowedERC725YKeys.length == 0) {
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
- _requirePermissions(from, permissions, _PERMISSION_ADDPERMISSIONS);
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
- } else {
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;
344
661
 
345
- _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
+ );
346
680
 
347
- }
348
- } else {
349
681
  /**
350
- * if bytes6(key) != bytes6(keccak256("AddressPermissions"))
351
- * this is not a standard permission key according to LSP6
352
- * so we revert execution
353
- *
354
- * @dev to implement custom permissions keys, consider overriding
355
- * this function and implement specific checks
356
- *
357
- * // AddressPermissions:MyCustomPermissions:<address>
358
- * bytes12 CUSTOM_PERMISSION_PREFIX = 0x4b80742de2bf9e659ba40000
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
359
688
  *
360
- * if (bytes12(key) == CUSTOM_PERMISSION_PREFIX) {
361
- * // custom logic
362
- * }
363
- * super._verifyCanSetPermissions(...)
689
+ * compare this part
690
+ * vvvvvv
691
+ * input data key = 0xa00000cafecafecafecafecafecafecafe000000000000000000000011223344
692
+ *
693
+ * & discard this part
694
+ * vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
695
+ * mask = 0xffffff0000000000000000000000000000000000000000000000000000000000
364
696
  */
365
- revert NotRecognisedPermissionKey(key);
366
- }
367
- }
697
+ mask =
698
+ bytes32(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) <<
699
+ (8 * (32 - length));
368
700
 
369
- function _verifyCanSetBytes32Permissions(
370
- bytes32 key,
371
- address from,
372
- bytes32 callerPermissions
373
- ) internal view {
374
- if (bytes32(ERC725Y(target).getData(key)) == bytes32(0)) {
375
- // if there is nothing stored under this data key,
376
- // we are trying to ADD permissions for a NEW address
377
- _requirePermissions(from, callerPermissions, _PERMISSION_ADDPERMISSIONS);
378
- } else {
379
- // if there are already some permissions stored under this data key,
380
- // we are trying to CHANGE the permissions of an address
381
- // (that has already some EXISTING permissions set)
382
- _requirePermissions(from, callerPermissions, _PERMISSION_CHANGEPERMISSIONS);
383
- }
384
- }
385
-
386
- function _verifyCanSetPermissionsArray(
387
- bytes32 key,
388
- bytes memory value,
389
- address from,
390
- bytes32 permissions
391
- ) internal view {
392
- // key = AddressPermissions[] -> array length
393
- if (key == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY) {
394
- uint256 arrayLength = uint256(bytes32(ERC725Y(target).getData(key)));
395
- uint256 newLength = uint256(bytes32(value));
396
-
397
- if (newLength > arrayLength) {
398
- _requirePermissions(from, permissions, _PERMISSION_ADDPERMISSIONS);
399
- } else {
400
- _requirePermissions(from, permissions, _PERMISSION_CHANGEPERMISSIONS);
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)
401
713
  }
402
714
 
403
- return;
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
+ }
404
722
  }
405
723
 
406
- // key = AddressPermissions[index] -> array index
407
- _requirePermissions(from, permissions, _PERMISSION_CHANGEPERMISSIONS);
724
+ revert NotAllowedERC725YDataKey(controllerAddress, inputDataKey);
408
725
  }
409
726
 
410
- function _verifyAllowedERC725YKeys(address from, bytes32[] memory inputKeys) internal view {
411
- bytes memory allowedERC725YKeysEncoded = ERC725Y(target).getAllowedERC725YKeysFor(from);
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;
412
766
 
413
- // whitelist any ERC725Y key
414
- if (
415
- // if nothing in the list
416
- allowedERC725YKeysEncoded.length == 0 ||
417
- // if not correctly abi-encoded array
418
- !LSP2Utils.isEncodedArray(allowedERC725YKeysEncoded)
419
- ) return;
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
+ );
420
785
 
421
- bytes32[] memory allowedERC725YKeys = abi.decode(allowedERC725YKeysEncoded, (bytes32[]));
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));
422
805
 
423
- uint256 zeroBytesCount;
424
- bytes32 mask;
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
+ }
425
818
 
426
- // loop through each allowed ERC725Y key retrieved from storage
427
- for (uint256 ii = 0; ii < allowedERC725YKeys.length; ii = GasLib.uncheckedIncrement(ii)) {
428
- // required to know which part of the input key to compare against the allowed key
429
- zeroBytesCount = _countTrailingZeroBytes(allowedERC725YKeys[ii]);
430
-
431
- // loop through each keys given as input
432
- for (uint256 jj = 0; jj < inputKeys.length; jj = GasLib.uncheckedIncrement(jj)) {
433
- // skip permissions keys that have been previously checked and "nulled"
434
- if (inputKeys[jj] == bytes32(0)) continue;
435
-
436
- // use a bitmask to discard the last `n` bytes of the input key (where `n` = `zeroBytesCount`)
437
- // and compare only the relevant parts of each ERC725Y keys
438
- //
439
- // for an allowed key = 0xcafecafecafecafecafecafecafecafe00000000000000000000000000000000
440
- //
441
- // |------compare this part-------|------discard this part--------|
442
- // v v v
443
- // mask = 0xffffffffffffffffffffffffffffffff00000000000000000000000000000000
444
- // & input key = 0xcafecafecafecafecafecafecafecafe00000000000000000000000011223344
445
- //
446
- mask = bytes32(type(uint256).max) << (8 * zeroBytesCount);
447
-
448
- if (allowedERC725YKeys[ii] == (inputKeys[jj] & mask)) {
449
- // if the input key matches the allowed key
450
- // make it null to mark it as allowed
451
- 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;
452
837
  }
453
838
  }
839
+
840
+ // Move the pointer to the next AllowedERC725YKey
841
+ unchecked {
842
+ pointer = pointer + (length + 2);
843
+ }
454
844
  }
455
845
 
456
- for (uint256 ii = 0; ii < inputKeys.length; ii = GasLib.uncheckedIncrement(ii)) {
457
- 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
+ }
458
851
  }
459
852
  }
460
853
 
461
854
  /**
462
- * @dev verify if `from` has the required permissions to make an external call
463
- * via the linked ERC725Account
855
+ * @dev verify if `from` has the required permissions to interact with other addresses using the target.
464
856
  * @param from the address who want to run the execute function on the ERC725Account
465
857
  * @param permissions the permissions of the caller
466
858
  * @param payload the ABI encoded payload `target.execute(...)`
@@ -469,19 +861,19 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
469
861
  address from,
470
862
  bytes32 permissions,
471
863
  bytes calldata payload
472
- ) internal view {
864
+ ) internal view virtual {
865
+ // MUST be one of the ERC725X operation types.
473
866
  uint256 operationType = uint256(bytes32(payload[4:36]));
474
- require(operationType < 5, "LSP6KeyManager: invalid operation type");
475
867
 
476
- require(
477
- operationType != OPERATION_DELEGATECALL,
478
- "LSP6KeyManager: operation DELEGATECALL is currently disallowed"
479
- );
868
+ // DELEGATECALL is disallowed by default on the LSP6 Key Manager.
869
+ if (operationType == OPERATION_4_DELEGATECALL) {
870
+ revert DelegateCallDisallowedViaKeyManager();
871
+ }
480
872
 
481
873
  uint256 value = uint256(bytes32(payload[68:100]));
482
874
 
483
875
  // prettier-ignore
484
- bool isContractCreation = operationType == OPERATION_CREATE || operationType == OPERATION_CREATE2;
876
+ bool isContractCreation = operationType == OPERATION_1_CREATE || operationType == OPERATION_2_CREATE2;
485
877
  bool isCallDataPresent = payload.length > 164;
486
878
 
487
879
  // SUPER operation only applies to contract call, not contract creation
@@ -489,6 +881,12 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
489
881
  ? false
490
882
  : permissions.hasPermission(_extractSuperPermissionFromOperation(operationType));
491
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
+
492
890
  if (isCallDataPresent && !hasSuperOperation) {
493
891
  _requirePermissions(from, permissions, _extractPermissionFromOperation(operationType));
494
892
  }
@@ -502,8 +900,8 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
502
900
  // Skip on contract creation (CREATE or CREATE2)
503
901
  if (isContractCreation) return;
504
902
 
505
- // Skip if caller has SUPER permissions for operations
506
- 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;
507
905
 
508
906
  // Skip if caller has SUPER permission for value transfers
509
907
  if (hasSuperTransferValue && !isCallDataPresent && value != 0) return;
@@ -511,18 +909,56 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
511
909
  // Skip if both SUPER permissions are present
512
910
  if (hasSuperOperation && hasSuperTransferValue) return;
513
911
 
514
- // 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
515
917
  address to = address(bytes20(payload[48:68]));
516
- _verifyAllowedAddress(from, to);
517
918
 
518
- if (to.code.length != 0) {
519
- // CHECK for ALLOWED STANDARDS
520
- _verifyAllowedStandard(from, to);
919
+ bool containsFunctionCall = payload.length >= 168;
920
+ bytes4 selector;
921
+ if (containsFunctionCall) selector = bytes4(payload[164:168]);
521
922
 
522
- // CHECK for ALLOWED FUNCTIONS
523
- // extract bytes4 function selector from payload passed to ERC725X.execute(...)
524
- if (payload.length >= 168) _verifyAllowedFunction(from, bytes4(payload[164:168]));
923
+ bytes memory allowedCalls = ERC725Y(_target).getAllowedCallsFor(from);
924
+ uint256 allowedCallsLength = allowedCalls.length;
925
+
926
+ if (allowedCallsLength == 0) {
927
+ revert NoCallsAllowed(from);
525
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);
526
962
  }
527
963
 
528
964
  /**
@@ -534,129 +970,66 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
534
970
  function _extractPermissionFromOperation(uint256 operationType)
535
971
  internal
536
972
  pure
973
+ virtual
537
974
  returns (bytes32 permissionsRequired)
538
975
  {
539
- if (operationType == OPERATION_CALL) return _PERMISSION_CALL;
540
- else if (operationType == OPERATION_CREATE) return _PERMISSION_DEPLOY;
541
- else if (operationType == OPERATION_CREATE2) return _PERMISSION_DEPLOY;
542
- else if (operationType == OPERATION_STATICCALL) return _PERMISSION_STATICCALL;
543
- 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;
544
981
  }
545
982
 
983
+ /**
984
+ * @dev returns the `superPermission` needed for a specific `operationType` of the `execute(..)`
985
+ */
546
986
  function _extractSuperPermissionFromOperation(uint256 operationType)
547
987
  internal
548
988
  pure
989
+ virtual
549
990
  returns (bytes32 superPermission)
550
991
  {
551
- if (operationType == OPERATION_CALL) return _PERMISSION_SUPER_CALL;
552
- else if (operationType == OPERATION_STATICCALL) return _PERMISSION_SUPER_STATICCALL;
553
- else if (operationType == OPERATION_DELEGATECALL) return _PERMISSION_SUPER_DELEGATECALL;
554
- }
555
-
556
- /**
557
- * @dev verify if `from` is authorised to interact with address `to` via the linked ERC725Account
558
- * @param from the caller address
559
- * @param to the address to interact with
560
- */
561
- function _verifyAllowedAddress(address from, address to) internal view {
562
- bytes memory allowedAddresses = ERC725Y(target).getAllowedAddressesFor(from);
563
-
564
- // whitelist any address
565
- if (
566
- // if nothing in the list
567
- allowedAddresses.length == 0 ||
568
- // if not correctly abi-encoded array of address[]
569
- !LSP2Utils.isEncodedArrayOfAddresses(allowedAddresses)
570
- ) return;
571
-
572
- address[] memory allowedAddressesList = abi.decode(allowedAddresses, (address[]));
573
-
574
- for (uint256 ii = 0; ii < allowedAddressesList.length; ii = GasLib.uncheckedIncrement(ii)) {
575
- if (to == allowedAddressesList[ii]) return;
576
- }
577
- revert NotAllowedAddress(from, to);
578
- }
579
-
580
- /**
581
- * @dev if `from` is restricted to interact with contracts that implement a specific interface,
582
- * verify that `to` implements one of these interface.
583
- * @param from the caller address
584
- * @param to the address of the contract to interact with
585
- */
586
- function _verifyAllowedStandard(address from, address to) internal view {
587
- bytes memory allowedStandards = ERC725Y(target).getAllowedStandardsFor(from);
588
-
589
- // whitelist any standard interface (ERC165)
590
- if (
591
- // if nothing in the list
592
- allowedStandards.length == 0 ||
593
- // if not correctly abi-encoded array of bytes4[]
594
- !LSP2Utils.isBytes4EncodedArray(allowedStandards)
595
- ) return;
596
-
597
- bytes4[] memory allowedStandardsList = abi.decode(allowedStandards, (bytes4[]));
598
-
599
- for (uint256 ii = 0; ii < allowedStandardsList.length; ii = GasLib.uncheckedIncrement(ii)) {
600
- if (to.supportsERC165Interface(allowedStandardsList[ii])) return;
601
- }
602
- revert NotAllowedStandard(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;
603
995
  }
604
996
 
605
997
  /**
606
- * @dev verify if `from` is authorised to use the linked ERC725Account
607
- * to run a specific function `functionSelector` at a target contract
998
+ * @dev revert if `from`'s `addressPermissions` doesn't contain `permissionsRequired`
608
999
  * @param from the caller address
609
- * @param functionSelector the bytes4 function selector of the function to run
610
- * at the target contract
1000
+ * @param addressPermissions the caller's permissions BitArray
1001
+ * @param permissionRequired the required permission
611
1002
  */
612
- function _verifyAllowedFunction(address from, bytes4 functionSelector) internal view {
613
- bytes memory allowedFunctions = ERC725Y(target).getAllowedFunctionsFor(from);
614
-
615
- // whitelist any function
616
- if (
617
- // if nothing in the list
618
- allowedFunctions.length == 0 ||
619
- // if not correctly abi-encoded array of bytes4[]
620
- !LSP2Utils.isBytes4EncodedArray(allowedFunctions)
621
- ) return;
622
-
623
- bytes4[] memory allowedFunctionsList = abi.decode(allowedFunctions, (bytes4[]));
624
-
625
- for (uint256 ii = 0; ii < allowedFunctionsList.length; ii = GasLib.uncheckedIncrement(ii)) {
626
- if (functionSelector == allowedFunctionsList[ii]) return;
627
- }
628
- revert NotAllowedFunction(from, functionSelector);
629
- }
630
-
631
- function _countTrailingZeroBytes(bytes32 dataKey) internal pure returns (uint256) {
632
- uint256 nByte = 32;
633
-
634
- // CHECK each bytes of the data key, starting from the end (right to left)
635
- // skip each empty bytes `0x00` until we find the first non-empty byte
636
- while (nByte > 0 && dataKey[nByte - 1] == 0x00) nByte--;
637
-
638
- return 32 - nByte;
639
- }
640
-
641
1003
  function _requirePermissions(
642
1004
  address from,
643
1005
  bytes32 addressPermissions,
644
1006
  bytes32 permissionRequired
645
- ) internal pure {
1007
+ ) internal pure virtual {
646
1008
  if (!addressPermissions.hasPermission(permissionRequired)) {
647
- string memory permissionErrorString = _getPermissionErrorString(permissionRequired);
1009
+ string memory permissionErrorString = _getPermissionName(permissionRequired);
648
1010
  revert NotAuthorised(from, permissionErrorString);
649
1011
  }
650
1012
  }
651
1013
 
652
- function _getPermissionErrorString(bytes32 permission)
1014
+ /**
1015
+ * @dev returns the name of the permission as a string
1016
+ */
1017
+ function _getPermissionName(bytes32 permission)
653
1018
  internal
654
1019
  pure
1020
+ virtual
655
1021
  returns (string memory errorMessage)
656
1022
  {
657
1023
  if (permission == _PERMISSION_CHANGEOWNER) return "TRANSFEROWNERSHIP";
658
1024
  if (permission == _PERMISSION_CHANGEPERMISSIONS) return "CHANGEPERMISSIONS";
659
- 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";
660
1033
  if (permission == _PERMISSION_SETDATA) return "SETDATA";
661
1034
  if (permission == _PERMISSION_CALL) return "CALL";
662
1035
  if (permission == _PERMISSION_STATICCALL) return "STATICCALL";
@@ -665,4 +1038,39 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
665
1038
  if (permission == _PERMISSION_TRANSFERVALUE) return "TRANSFERVALUE";
666
1039
  if (permission == _PERMISSION_SIGN) return "SIGN";
667
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
+ }
668
1076
  }