@lukso/lsp-smart-contracts 0.8.2 → 0.10.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 (179) hide show
  1. package/README.md +29 -8
  2. package/artifacts/LSP0ERC725Account.json +90 -50
  3. package/artifacts/LSP0ERC725AccountInit.json +90 -50
  4. package/artifacts/LSP11BasicSocialRecovery.json +2 -2
  5. package/artifacts/LSP11BasicSocialRecoveryInit.json +2 -2
  6. package/artifacts/LSP16UniversalFactory.json +31 -2
  7. package/artifacts/LSP1UniversalReceiverDelegateUP.json +19 -35
  8. package/artifacts/LSP1UniversalReceiverDelegateVault.json +13 -13
  9. package/artifacts/LSP4Compatibility.json +39 -40
  10. package/artifacts/LSP4DigitalAssetMetadata.json +39 -40
  11. package/artifacts/LSP4DigitalAssetMetadataInitAbstract.json +39 -40
  12. package/artifacts/LSP6KeyManager.json +107 -8
  13. package/artifacts/LSP6KeyManagerInit.json +119 -20
  14. package/artifacts/LSP7CappedSupply.json +78 -38
  15. package/artifacts/LSP7CappedSupplyInitAbstract.json +78 -38
  16. package/artifacts/LSP7CompatibleERC20.json +78 -38
  17. package/artifacts/LSP7CompatibleERC20InitAbstract.json +78 -38
  18. package/artifacts/LSP7CompatibleERC20Mintable.json +80 -40
  19. package/artifacts/LSP7CompatibleERC20MintableInit.json +80 -40
  20. package/artifacts/LSP7DigitalAsset.json +78 -38
  21. package/artifacts/LSP7DigitalAssetInitAbstract.json +78 -38
  22. package/artifacts/LSP7Mintable.json +80 -40
  23. package/artifacts/LSP7MintableInit.json +80 -40
  24. package/artifacts/LSP8CappedSupply.json +39 -40
  25. package/artifacts/LSP8CappedSupplyInitAbstract.json +39 -40
  26. package/artifacts/LSP8CompatibleERC721.json +42 -43
  27. package/artifacts/LSP8CompatibleERC721InitAbstract.json +42 -43
  28. package/artifacts/LSP8CompatibleERC721Mintable.json +44 -45
  29. package/artifacts/LSP8CompatibleERC721MintableInit.json +44 -45
  30. package/artifacts/LSP8IdentifiableDigitalAsset.json +39 -40
  31. package/artifacts/LSP8IdentifiableDigitalAssetInitAbstract.json +39 -40
  32. package/artifacts/LSP8Mintable.json +41 -42
  33. package/artifacts/LSP8MintableInit.json +41 -42
  34. package/artifacts/LSP9Vault.json +73 -55
  35. package/artifacts/LSP9VaultInit.json +73 -55
  36. package/artifacts/UniversalProfile.json +90 -50
  37. package/artifacts/UniversalProfileInit.json +90 -50
  38. package/contracts/LSP0ERC725Account/ILSP0ERC725Account.sol +62 -0
  39. package/contracts/LSP0ERC725Account/LSP0Constants.sol +1 -1
  40. package/contracts/LSP0ERC725Account/LSP0ERC725Account.sol +36 -6
  41. package/contracts/LSP0ERC725Account/LSP0ERC725AccountCore.sol +605 -219
  42. package/contracts/LSP0ERC725Account/LSP0ERC725AccountInit.sol +31 -5
  43. package/contracts/LSP0ERC725Account/LSP0ERC725AccountInitAbstract.sol +37 -3
  44. package/contracts/LSP0ERC725Account/LSP0Utils.sol +0 -1
  45. package/contracts/LSP10ReceivedVaults/LSP10Utils.sol +71 -60
  46. package/contracts/LSP11BasicSocialRecovery/LSP11BasicSocialRecoveryCore.sol +5 -7
  47. package/contracts/LSP11BasicSocialRecovery/LSP11BasicSocialRecoveryInit.sol +0 -1
  48. package/contracts/LSP14Ownable2Step/ILSP14Ownable2Step.sol +27 -4
  49. package/contracts/LSP14Ownable2Step/LSP14Ownable2Step.sol +41 -84
  50. package/contracts/LSP16UniversalFactory/LSP16UniversalFactory.sol +8 -8
  51. package/contracts/LSP17ContractExtension/LSP17Extendable.sol +10 -5
  52. package/contracts/LSP1UniversalReceiver/ILSP1UniversalReceiver.sol +0 -4
  53. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateUP/LSP1UniversalReceiverDelegateUP.sol +129 -77
  54. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateVault/LSP1UniversalReceiverDelegateVault.sol +45 -16
  55. package/contracts/LSP1UniversalReceiver/LSP1Utils.sol +12 -0
  56. package/contracts/LSP20CallVerification/ILSP20CallVerification.sol +30 -0
  57. package/contracts/LSP20CallVerification/LSP20CallVerification.sol +83 -0
  58. package/contracts/LSP20CallVerification/LSP20Constants.sol +17 -0
  59. package/contracts/LSP20CallVerification/LSP20Errors.sol +15 -0
  60. package/contracts/LSP2ERC725YJSONSchema/LSP2Utils.sol +26 -12
  61. package/contracts/LSP4DigitalAssetMetadata/LSP4DigitalAssetMetadataInitAbstract.sol +0 -1
  62. package/contracts/LSP5ReceivedAssets/LSP5Utils.sol +76 -66
  63. package/contracts/LSP6KeyManager/ILSP6KeyManager.sol +12 -3
  64. package/contracts/LSP6KeyManager/LSP6Constants.sol +9 -3
  65. package/contracts/LSP6KeyManager/LSP6Errors.sol +22 -1
  66. package/contracts/LSP6KeyManager/LSP6KeyManagerCore.sol +172 -29
  67. package/contracts/LSP6KeyManager/LSP6KeyManagerInitAbstract.sol +0 -1
  68. package/contracts/LSP6KeyManager/LSP6Modules/LSP6ExecuteModule.sol +270 -100
  69. package/contracts/LSP6KeyManager/LSP6Modules/LSP6SetDataModule.sol +187 -67
  70. package/contracts/LSP6KeyManager/LSP6Utils.sol +31 -20
  71. package/contracts/LSP7DigitalAsset/LSP7DigitalAssetCore.sol +79 -30
  72. package/contracts/LSP7DigitalAsset/LSP7Errors.sol +5 -0
  73. package/contracts/LSP7DigitalAsset/extensions/LSP7Burnable.sol +2 -2
  74. package/contracts/LSP7DigitalAsset/extensions/LSP7BurnableInitAbstract.sol +24 -0
  75. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupply.sol +1 -2
  76. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupplyInitAbstract.sol +1 -2
  77. package/contracts/LSP7DigitalAsset/extensions/LSP7CompatibleERC20.sol +3 -5
  78. package/contracts/LSP7DigitalAsset/extensions/LSP7CompatibleERC20InitAbstract.sol +3 -5
  79. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAssetCore.sol +27 -29
  80. package/contracts/LSP8IdentifiableDigitalAsset/extensions/ILSP8CompatibleERC721.sol +1 -1
  81. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupply.sol +1 -2
  82. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupplyInitAbstract.sol +1 -2
  83. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721.sol +5 -5
  84. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721InitAbstract.sol +6 -6
  85. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8Enumerable.sol +2 -2
  86. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8EnumerableInitAbstract.sol +2 -2
  87. package/contracts/LSP9Vault/ILSP9Vault.sol +27 -0
  88. package/contracts/LSP9Vault/LSP9Constants.sol +1 -1
  89. package/contracts/LSP9Vault/LSP9Vault.sol +11 -8
  90. package/contracts/LSP9Vault/LSP9VaultCore.sol +96 -71
  91. package/contracts/LSP9Vault/LSP9VaultInit.sol +2 -1
  92. package/contracts/LSP9Vault/LSP9VaultInitAbstract.sol +19 -7
  93. package/contracts/UniversalProfileInitAbstract.sol +5 -0
  94. package/devdocs/Create2Factory.json +16 -0
  95. package/devdocs/LSP0ERC725Account.json +200 -0
  96. package/devdocs/LSP0ERC725AccountInit.json +203 -0
  97. package/devdocs/LSP11BasicSocialRecovery.json +146 -0
  98. package/devdocs/LSP11BasicSocialRecoveryInit.json +146 -0
  99. package/devdocs/LSP16UniversalFactory.json +51 -0
  100. package/devdocs/LSP1UniversalReceiverDelegateUP.json +80 -0
  101. package/devdocs/LSP1UniversalReceiverDelegateVault.json +62 -0
  102. package/devdocs/LSP4Compatibility.json +79 -0
  103. package/devdocs/LSP4DigitalAssetMetadata.json +84 -0
  104. package/devdocs/LSP4DigitalAssetMetadataInitAbstract.json +77 -0
  105. package/devdocs/LSP6KeyManager.json +257 -0
  106. package/devdocs/LSP6KeyManagerInit.json +260 -0
  107. package/devdocs/LSP7CappedSupply.json +211 -0
  108. package/devdocs/LSP7CappedSupplyInitAbstract.json +206 -0
  109. package/devdocs/LSP7CompatibleERC20.json +225 -0
  110. package/devdocs/LSP7CompatibleERC20InitAbstract.json +218 -0
  111. package/devdocs/LSP7CompatibleERC20Mintable.json +217 -0
  112. package/devdocs/LSP7CompatibleERC20MintableInit.json +227 -0
  113. package/devdocs/LSP7DigitalAsset.json +210 -0
  114. package/devdocs/LSP7DigitalAssetInitAbstract.json +202 -0
  115. package/devdocs/LSP7Mintable.json +219 -0
  116. package/devdocs/LSP7MintableInit.json +220 -0
  117. package/devdocs/LSP8CappedSupply.json +232 -0
  118. package/devdocs/LSP8CappedSupplyInitAbstract.json +227 -0
  119. package/devdocs/LSP8CompatibleERC721.json +298 -0
  120. package/devdocs/LSP8CompatibleERC721InitAbstract.json +291 -0
  121. package/devdocs/LSP8CompatibleERC721Mintable.json +290 -0
  122. package/devdocs/LSP8CompatibleERC721MintableInit.json +300 -0
  123. package/devdocs/LSP8IdentifiableDigitalAsset.json +230 -0
  124. package/devdocs/LSP8IdentifiableDigitalAssetInitAbstract.json +223 -0
  125. package/devdocs/LSP8Mintable.json +242 -0
  126. package/devdocs/LSP8MintableInit.json +245 -0
  127. package/devdocs/LSP9Vault.json +171 -0
  128. package/devdocs/LSP9VaultInit.json +175 -0
  129. package/devdocs/UniversalProfile.json +199 -0
  130. package/devdocs/UniversalProfileInit.json +202 -0
  131. package/devdocs/UniversalReceiverAddressStore.json +9 -0
  132. package/dist/constants.cjs.js +31569 -0
  133. package/dist/constants.d.ts +31681 -0
  134. package/dist/constants.es.js +31550 -0
  135. package/package.json +70 -52
  136. package/userdocs/Create2Factory.json +5 -0
  137. package/userdocs/LSP0ERC725Account.json +47 -0
  138. package/userdocs/LSP0ERC725AccountInit.json +47 -0
  139. package/userdocs/LSP11BasicSocialRecovery.json +29 -0
  140. package/userdocs/LSP11BasicSocialRecoveryInit.json +29 -0
  141. package/userdocs/LSP16UniversalFactory.json +5 -0
  142. package/userdocs/LSP1UniversalReceiverDelegateUP.json +10 -0
  143. package/userdocs/LSP1UniversalReceiverDelegateVault.json +10 -0
  144. package/userdocs/LSP4Compatibility.json +20 -0
  145. package/userdocs/LSP4DigitalAssetMetadata.json +23 -0
  146. package/userdocs/LSP4DigitalAssetMetadataInitAbstract.json +20 -0
  147. package/userdocs/LSP6KeyManager.json +18 -0
  148. package/userdocs/LSP6KeyManagerInit.json +18 -0
  149. package/userdocs/LSP7CappedSupply.json +29 -0
  150. package/userdocs/LSP7CappedSupplyInitAbstract.json +26 -0
  151. package/userdocs/LSP7CompatibleERC20.json +35 -0
  152. package/userdocs/LSP7CompatibleERC20InitAbstract.json +32 -0
  153. package/userdocs/LSP7CompatibleERC20Mintable.json +32 -0
  154. package/userdocs/LSP7CompatibleERC20MintableInit.json +35 -0
  155. package/userdocs/LSP7DigitalAsset.json +29 -0
  156. package/userdocs/LSP7DigitalAssetInitAbstract.json +26 -0
  157. package/userdocs/LSP7Mintable.json +29 -0
  158. package/userdocs/LSP7MintableInit.json +29 -0
  159. package/userdocs/LSP8CappedSupply.json +23 -0
  160. package/userdocs/LSP8CappedSupplyInitAbstract.json +20 -0
  161. package/userdocs/LSP8CompatibleERC721.json +29 -0
  162. package/userdocs/LSP8CompatibleERC721InitAbstract.json +26 -0
  163. package/userdocs/LSP8CompatibleERC721Mintable.json +26 -0
  164. package/userdocs/LSP8CompatibleERC721MintableInit.json +29 -0
  165. package/userdocs/LSP8IdentifiableDigitalAsset.json +23 -0
  166. package/userdocs/LSP8IdentifiableDigitalAssetInitAbstract.json +20 -0
  167. package/userdocs/LSP8Mintable.json +23 -0
  168. package/userdocs/LSP8MintableInit.json +23 -0
  169. package/userdocs/LSP9Vault.json +38 -0
  170. package/userdocs/LSP9VaultInit.json +38 -0
  171. package/userdocs/UniversalProfile.json +50 -0
  172. package/userdocs/UniversalProfileInit.json +50 -0
  173. package/userdocs/UniversalReceiverAddressStore.json +5 -0
  174. package/CONTRIBUTING.md +0 -57
  175. package/common/constants.d.ts +0 -738
  176. package/common/constants.js +0 -785
  177. package/contracts/Utils/GasLib.sol +0 -17
  178. package/module/constants.d.ts +0 -738
  179. package/module/constants.js +0 -782
@@ -3,9 +3,9 @@ pragma solidity ^0.8.5;
3
3
 
4
4
  // modules
5
5
  import {ERC725Y} from "@erc725/smart-contracts/contracts/ERC725Y.sol";
6
+ import {ERC725Y_DataKeysValuesLengthMismatch} from "@erc725/smart-contracts/contracts/errors.sol";
6
7
 
7
8
  // libraries
8
- import {GasLib} from "../../Utils/GasLib.sol";
9
9
  import {LSP6Utils} from "../LSP6Utils.sol";
10
10
 
11
11
  // constants
@@ -19,7 +19,7 @@ import {
19
19
  _PERMISSION_SETDATA,
20
20
  _PERMISSION_SUPER_SETDATA,
21
21
  _PERMISSION_ADDCONTROLLER,
22
- _PERMISSION_CHANGEPERMISSIONS,
22
+ _PERMISSION_EDITPERMISSIONS,
23
23
  _PERMISSION_ADDEXTENSIONS,
24
24
  _PERMISSION_CHANGEEXTENSIONS,
25
25
  _PERMISSION_ADDUNIVERSALRECEIVERDELEGATE,
@@ -62,6 +62,7 @@ abstract contract LSP6SetDataModule {
62
62
  ) internal view virtual {
63
63
  bytes32 requiredPermission = _getPermissionRequiredToSetDataKey(
64
64
  controlledContract,
65
+ controllerPermissions,
65
66
  inputDataKey,
66
67
  inputDataValue
67
68
  );
@@ -79,6 +80,9 @@ abstract contract LSP6SetDataModule {
79
80
  ERC725Y(controlledContract).getAllowedERC725YDataKeysFor(controllerAddress)
80
81
  );
81
82
  } else {
83
+ // Do not check again if we already checked that the controller had the permissions inside `_getPermissionRequiredToSetDataKey(...)`
84
+ if (requiredPermission == bytes32(0)) return;
85
+
82
86
  // Otherwise CHECK the required permission if setting LSP6 permissions, LSP1 Delegate or LSP17 Extensions.
83
87
  _requirePermissions(controllerAddress, controllerPermissions, requiredPermission);
84
88
  }
@@ -87,20 +91,25 @@ abstract contract LSP6SetDataModule {
87
91
  /**
88
92
  * @dev verify if the `controllerAddress` has the permissions required to set an array of data keys on the ERC725Y storage of the `controlledContract`.
89
93
  * @param controlledContract the address of the ERC725Y contract where the data key is set.
90
- * @param controllerAddress the address of the controller who wants to set the data key.
91
- * @param controllerPermissions the permissions of the controller address.
94
+ * @param controller the address of the controller who wants to set the data key.
95
+ * @param permissions the permissions of the controller address.
92
96
  * @param inputDataKeys an array of data keys to set on the `controlledContract`.
93
97
  * @param inputDataValues an array of data values to set for the `inputDataKeys`.
94
98
  */
95
99
  function _verifyCanSetData(
96
100
  address controlledContract,
97
- address controllerAddress,
98
- bytes32 controllerPermissions,
101
+ address controller,
102
+ bytes32 permissions,
99
103
  bytes32[] memory inputDataKeys,
100
104
  bytes[] memory inputDataValues
101
105
  ) internal view virtual {
106
+ if (inputDataKeys.length != inputDataValues.length) {
107
+ revert ERC725Y_DataKeysValuesLengthMismatch();
108
+ }
109
+
102
110
  bool isSettingERC725YKeys;
103
111
  bool[] memory validatedInputDataKeys = new bool[](inputDataKeys.length);
112
+ uint256 inputDataKeysAllowed = 0;
104
113
 
105
114
  bytes32 requiredPermission;
106
115
 
@@ -108,6 +117,7 @@ abstract contract LSP6SetDataModule {
108
117
  do {
109
118
  requiredPermission = _getPermissionRequiredToSetDataKey(
110
119
  controlledContract,
120
+ permissions,
111
121
  inputDataKeys[ii],
112
122
  inputDataValues[ii]
113
123
  );
@@ -115,26 +125,34 @@ abstract contract LSP6SetDataModule {
115
125
  if (requiredPermission == _PERMISSION_SETDATA) {
116
126
  isSettingERC725YKeys = true;
117
127
  } else {
118
- // CHECK the required permissions if setting LSP6 permissions, LSP1 Delegate or LSP17 Extensions.
119
- _requirePermissions(controllerAddress, controllerPermissions, requiredPermission);
128
+ // if we did not check already the permissions of the controller inside `_getPermissionRequiredToSetDataKey(...)`
129
+ if (requiredPermission != bytes32(0)) {
130
+ // CHECK the required permissions for setting LSP6 permissions, LSP1 Delegate or LSP17 Extensions.
131
+ _requirePermissions(controller, permissions, requiredPermission);
132
+ }
133
+
120
134
  validatedInputDataKeys[ii] = true;
135
+ inputDataKeysAllowed++;
121
136
  }
122
137
 
123
- ii = GasLib.uncheckedIncrement(ii);
138
+ unchecked {
139
+ ++ii;
140
+ }
124
141
  } while (ii < inputDataKeys.length);
125
142
 
126
143
  // CHECK if allowed to set one (or multiple) ERC725Y Data Keys
127
144
  if (isSettingERC725YKeys) {
128
145
  // Skip if caller has SUPER permissions
129
- if (controllerPermissions.hasPermission(_PERMISSION_SUPER_SETDATA)) return;
146
+ if (permissions.hasPermission(_PERMISSION_SUPER_SETDATA)) return;
130
147
 
131
- _requirePermissions(controllerAddress, controllerPermissions, _PERMISSION_SETDATA);
148
+ _requirePermissions(controller, permissions, _PERMISSION_SETDATA);
132
149
 
133
150
  _verifyAllowedERC725YDataKeys(
134
- controllerAddress,
151
+ controller,
135
152
  inputDataKeys,
136
- ERC725Y(controlledContract).getAllowedERC725YDataKeysFor(controllerAddress),
137
- validatedInputDataKeys
153
+ ERC725Y(controlledContract).getAllowedERC725YDataKeysFor(controller),
154
+ validatedInputDataKeys,
155
+ inputDataKeysAllowed
138
156
  );
139
157
  }
140
158
  }
@@ -148,22 +166,39 @@ abstract contract LSP6SetDataModule {
148
166
  */
149
167
  function _getPermissionRequiredToSetDataKey(
150
168
  address controlledContract,
169
+ bytes32 controllerPermissions,
151
170
  bytes32 inputDataKey,
152
171
  bytes memory inputDataValue
153
172
  ) internal view virtual returns (bytes32) {
154
173
  // AddressPermissions[] or AddressPermissions[index]
155
174
  if (bytes16(inputDataKey) == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY_PREFIX) {
175
+ // this is our best attempt to save gas to avoid reading the `target` storage multiple times
176
+ // to know if we need the permission `ADDCONTROLLER` or `EDITPERMISSIONS`.
177
+ // Even if `getData(...)` is `view`, multiple external calls to fetch values from storage add to the total gas used.
178
+ bool hasBothAddControllerAndEditPermissions = controllerPermissions.hasPermission(
179
+ _PERMISSION_ADDCONTROLLER | _PERMISSION_EDITPERMISSIONS
180
+ );
181
+
156
182
  return
157
183
  _getPermissionToSetPermissionsArray(
158
184
  controlledContract,
159
185
  inputDataKey,
160
- inputDataValue
186
+ inputDataValue,
187
+ hasBothAddControllerAndEditPermissions
161
188
  );
162
189
 
163
190
  // AddressPermissions:...
164
191
  } else if (bytes6(inputDataKey) == _LSP6KEY_ADDRESSPERMISSIONS_PREFIX) {
192
+ // same as above, save gas by avoiding redundants or unecessary external calls to fetch values from the `target` storage.
193
+ bool hasBothAddControllerAndEditPermissions = controllerPermissions.hasPermission(
194
+ _PERMISSION_ADDCONTROLLER | _PERMISSION_EDITPERMISSIONS
195
+ );
196
+
165
197
  // AddressPermissions:Permissions:<address>
166
198
  if (bytes12(inputDataKey) == _LSP6KEY_ADDRESSPERMISSIONS_PERMISSIONS_PREFIX) {
199
+ // controller already has the permissions needed. Do not run internal function.
200
+ if (hasBothAddControllerAndEditPermissions) return (bytes32(0));
201
+
167
202
  return _getPermissionToSetControllerPermissions(controlledContract, inputDataKey);
168
203
 
169
204
  // AddressPermissions:AllowedCalls:<address>
@@ -172,7 +207,8 @@ abstract contract LSP6SetDataModule {
172
207
  _getPermissionToSetAllowedCalls(
173
208
  controlledContract,
174
209
  inputDataKey,
175
- inputDataValue
210
+ inputDataValue,
211
+ hasBothAddControllerAndEditPermissions
176
212
  );
177
213
 
178
214
  // AddressPermissions:AllowedERC725YKeys:<address>
@@ -183,7 +219,8 @@ abstract contract LSP6SetDataModule {
183
219
  _getPermissionToSetAllowedERC725YDataKeys(
184
220
  controlledContract,
185
221
  inputDataKey,
186
- inputDataValue
222
+ inputDataValue,
223
+ hasBothAddControllerAndEditPermissions
187
224
  );
188
225
 
189
226
  // if the first 6 bytes of the input data key are "AddressPermissions:..." but did not match
@@ -209,10 +246,31 @@ abstract contract LSP6SetDataModule {
209
246
  inputDataKey == _LSP1_UNIVERSAL_RECEIVER_DELEGATE_KEY ||
210
247
  bytes12(inputDataKey) == _LSP1_UNIVERSAL_RECEIVER_DELEGATE_PREFIX
211
248
  ) {
249
+ // same as above. If controller has both permissions, do not read the `target` storage
250
+ // to save gas by avoiding an extra external `view` call.
251
+ if (
252
+ controllerPermissions.hasPermission(
253
+ _PERMISSION_ADDUNIVERSALRECEIVERDELEGATE |
254
+ _PERMISSION_CHANGEUNIVERSALRECEIVERDELEGATE
255
+ )
256
+ ) {
257
+ return bytes32(0);
258
+ }
259
+
212
260
  return _getPermissionToSetLSP1Delegate(controlledContract, inputDataKey);
213
261
 
214
262
  // LSP17Extension:<bytes4>
215
263
  } else if (bytes12(inputDataKey) == _LSP17_EXTENSION_PREFIX) {
264
+ // same as above. If controller has both permissions, do not read the `target` storage
265
+ // to save gas by avoiding an extra external `view` call.
266
+ if (
267
+ controllerPermissions.hasPermission(
268
+ _PERMISSION_ADDEXTENSIONS | _PERMISSION_CHANGEEXTENSIONS
269
+ )
270
+ ) {
271
+ return bytes32(0);
272
+ }
273
+
216
274
  return _getPermissionToSetLSP17Extension(controlledContract, inputDataKey);
217
275
  } else {
218
276
  return _PERMISSION_SETDATA;
@@ -232,18 +290,21 @@ abstract contract LSP6SetDataModule {
232
290
  function _getPermissionToSetPermissionsArray(
233
291
  address controlledContract,
234
292
  bytes32 inputDataKey,
235
- bytes memory inputDataValue
293
+ bytes memory inputDataValue,
294
+ bool hasBothAddControllerAndEditPermissions
236
295
  ) internal view virtual returns (bytes32) {
237
- bytes memory currentValue = ERC725Y(controlledContract).getData(inputDataKey);
238
-
239
296
  // AddressPermissions[] -> array length
240
297
  if (inputDataKey == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY) {
241
- uint256 newLength = uint256(bytes32(inputDataValue));
298
+ // if the controller already has both permissions from one of the two required,
299
+ // No permission required as CHECK is already done. We don't need to read `target` storage.
300
+ if (hasBothAddControllerAndEditPermissions) return bytes32(0);
301
+
302
+ uint128 newLength = uint128(bytes16(inputDataValue));
242
303
 
243
304
  return
244
- newLength > uint256(bytes32(currentValue))
305
+ newLength > uint128(bytes16(ERC725Y(controlledContract).getData(inputDataKey)))
245
306
  ? _PERMISSION_ADDCONTROLLER
246
- : _PERMISSION_CHANGEPERMISSIONS;
307
+ : _PERMISSION_EDITPERMISSIONS;
247
308
  }
248
309
 
249
310
  // AddressPermissions[index] -> array index
@@ -253,7 +314,14 @@ abstract contract LSP6SetDataModule {
253
314
  revert AddressPermissionArrayIndexValueNotAnAddress(inputDataKey, inputDataValue);
254
315
  }
255
316
 
256
- return currentValue.length == 0 ? _PERMISSION_ADDCONTROLLER : _PERMISSION_CHANGEPERMISSIONS;
317
+ // if the controller already has both permissions from one of the two required below,
318
+ // No permission required as CHECK is already done. We don't need to read `target` storage.
319
+ if (hasBothAddControllerAndEditPermissions) return bytes32(0);
320
+
321
+ return
322
+ ERC725Y(controlledContract).getData(inputDataKey).length == 0
323
+ ? _PERMISSION_ADDCONTROLLER
324
+ : _PERMISSION_EDITPERMISSIONS;
257
325
  }
258
326
 
259
327
  /**
@@ -271,7 +339,7 @@ abstract contract LSP6SetDataModule {
271
339
  // if there are already some permissions set under the data key, we are trying to CHANGE the permissions of a controller.
272
340
  bytes32(ERC725Y(controlledContract).getData(inputPermissionDataKey)) == bytes32(0)
273
341
  ? _PERMISSION_ADDCONTROLLER
274
- : _PERMISSION_CHANGEPERMISSIONS;
342
+ : _PERMISSION_EDITPERMISSIONS;
275
343
  }
276
344
 
277
345
  /**
@@ -284,25 +352,30 @@ abstract contract LSP6SetDataModule {
284
352
  function _getPermissionToSetAllowedCalls(
285
353
  address controlledContract,
286
354
  bytes32 dataKey,
287
- bytes memory dataValue
355
+ bytes memory dataValue,
356
+ bool hasBothAddControllerAndEditPermissions
288
357
  ) internal view virtual returns (bytes32) {
289
358
  if (!LSP6Utils.isCompactBytesArrayOfAllowedCalls(dataValue)) {
290
359
  revert InvalidEncodedAllowedCalls(dataValue);
291
360
  }
292
361
 
293
- // if there is nothing stored under the Allowed Calls of the controller,
362
+ // if the controller already has both permissions from one of the two required below,
363
+ // No permission required as CHECK is already done. We don't need to read `target` storage.
364
+ if (hasBothAddControllerAndEditPermissions) return bytes32(0);
365
+
366
+ // if nothing is stored under the controller's Allowed Calls of the controller,
294
367
  // we are trying to ADD a list of restricted calls (standards + address + function selector)
295
368
  //
296
- // if there are already some data set under the Allowed Calls of the controller,
369
+ // if some data is already set under the Allowed Calls of the controller,
297
370
  // we are trying to CHANGE (= edit) these restrictions.
298
371
  return
299
372
  ERC725Y(controlledContract).getData(dataKey).length == 0
300
373
  ? _PERMISSION_ADDCONTROLLER
301
- : _PERMISSION_CHANGEPERMISSIONS;
374
+ : _PERMISSION_EDITPERMISSIONS;
302
375
  }
303
376
 
304
377
  /**
305
- * @dev retrieve the permission required to set some AllowedCalls for a controller.
378
+ * @dev retrieve the permission required to set some Allowed ERC725Y Data Keys for a controller.
306
379
  * @param controlledContract the address of the ERC725Y contract where the data key is verified.
307
380
  * @param dataKey or `AddressPermissions:AllowedERC725YDataKeys:<controller-address>`.
308
381
  * @param dataValue the updated value for the `dataKey`. MUST be a bytes[CompactBytesArray] of Allowed ERC725Y Data Keys.
@@ -311,12 +384,20 @@ abstract contract LSP6SetDataModule {
311
384
  function _getPermissionToSetAllowedERC725YDataKeys(
312
385
  address controlledContract,
313
386
  bytes32 dataKey,
314
- bytes memory dataValue
387
+ bytes memory dataValue,
388
+ bool hasBothAddControllerAndEditPermissions
315
389
  ) internal view returns (bytes32) {
316
390
  if (!LSP6Utils.isCompactBytesArrayOfAllowedERC725YDataKeys(dataValue)) {
317
- revert InvalidEncodedAllowedERC725YDataKeys(dataValue);
391
+ revert InvalidEncodedAllowedERC725YDataKeys(
392
+ dataValue,
393
+ "couldn't VALIDATE the data value"
394
+ );
318
395
  }
319
396
 
397
+ // if the controller already has both permissions from one of the two required below,
398
+ // CHECK is already done. We don't need to read `target` storage.
399
+ if (hasBothAddControllerAndEditPermissions) return bytes32(0);
400
+
320
401
  // if there is nothing stored under the Allowed ERC725Y Data Keys of the controller,
321
402
  // we are trying to ADD a list of restricted ERC725Y Data Keys.
322
403
  //
@@ -325,7 +406,7 @@ abstract contract LSP6SetDataModule {
325
406
  return
326
407
  ERC725Y(controlledContract).getData(dataKey).length == 0
327
408
  ? _PERMISSION_ADDCONTROLLER
328
- : _PERMISSION_CHANGEPERMISSIONS;
409
+ : _PERMISSION_EDITPERMISSIONS;
329
410
  }
330
411
 
331
412
  /**
@@ -379,13 +460,13 @@ abstract contract LSP6SetDataModule {
379
460
  /**
380
461
  * The pointer will always land on the length of each bytes value:
381
462
  *
382
- * ↓↓
383
- * 03 a00000
384
- * 05 fff83a0011
385
- * 20 aa0000000000000000000000000000000000000000000000000000000000cafe
386
- * 12 bb000000000000000000000000000000beef
387
- * 19 cc00000000000000000000000000000000000000000000deed
388
- * ↑↑
463
+ * ↓↓↓↓
464
+ * 0003 a00000
465
+ * 0005 fff83a0011
466
+ * 0020 aa0000000000000000000000000000000000000000000000000000000000cafe
467
+ * 0012 bb000000000000000000000000000000beef
468
+ * 0019 cc00000000000000000000000000000000000000000000deed
469
+ * ↑↑↑↑
389
470
  *
390
471
  */
391
472
  uint256 pointer;
@@ -398,10 +479,10 @@ abstract contract LSP6SetDataModule {
398
479
  /**
399
480
  * iterate over each data key and update the `pointer` variable with the index where to find the length of each data key.
400
481
  *
401
- * 0x 03 a00000 03 fff83a 20 aa00...00cafe
402
- * ↑↑ ↑↑ ↑↑
403
- * first | second | third
404
- * length | length | length
482
+ * 0x 0003 a00000 0003 fff83a 0020 aa00...00cafe
483
+ * ↑↑↑↑ ↑↑↑↑ ↑↑↑↑
484
+ * first | second | third
485
+ * length | length | length
405
486
  */
406
487
  while (pointer < allowedERC725YDataKeysCompacted.length) {
407
488
  // save the length of the allowed data key to calculate the `mask`.
@@ -414,6 +495,18 @@ abstract contract LSP6SetDataModule {
414
495
  )
415
496
  );
416
497
 
498
+ /**
499
+ * The length of a data key is 32 bytes.
500
+ * Therefore we can have a fixed allowed data key which has
501
+ * a length of 32 bytes or we can have a dynamic data key
502
+ * which can have a length of up to 31 bytes.
503
+ */
504
+ if (length > 32)
505
+ revert InvalidEncodedAllowedERC725YDataKeys(
506
+ allowedERC725YDataKeysCompacted,
507
+ "couldn't DECODE from storage"
508
+ );
509
+
417
510
  /**
418
511
  * The bitmask discard the last `32 - length` bytes of the input data key via ANDing &
419
512
  * It is used to compare only the relevant parts of each input data key against dynamic allowed data keys.
@@ -448,7 +541,6 @@ abstract contract LSP6SetDataModule {
448
541
  allowedKey := and(memoryAt, mask)
449
542
  }
450
543
 
451
- // voila you found the key ;)
452
544
  if (allowedKey == (inputDataKey & mask)) return;
453
545
 
454
546
  // move the pointer to the index of the next allowed data key
@@ -465,32 +557,32 @@ abstract contract LSP6SetDataModule {
465
557
  * @param controllerAddress the address of the controller.
466
558
  * @param inputDataKeys the data keys to verify against the allowed ERC725Y Data Keys of the `controllerAddress`.
467
559
  * @param allowedERC725YDataKeysCompacted a CompactBytesArray of allowed ERC725Y Data Keys of the `controllerAddress`.
468
- * @param validatedInputKeys an array of booleans to store the result of the verification of each data keys checked.
560
+ * @param validatedInputKeysList an array of booleans to store the result of the verification of each data keys checked.
561
+ * @param allowedDataKeysFound the number of data keys that were previously validated for other permissions like `ADDCONTROLLER`, `EDITPERMISSIONS`, etc...
469
562
  */
470
563
  function _verifyAllowedERC725YDataKeys(
471
564
  address controllerAddress,
472
565
  bytes32[] memory inputDataKeys,
473
566
  bytes memory allowedERC725YDataKeysCompacted,
474
- bool[] memory validatedInputKeys
567
+ bool[] memory validatedInputKeysList,
568
+ uint256 allowedDataKeysFound
475
569
  ) internal pure virtual {
476
570
  if (allowedERC725YDataKeysCompacted.length == 0)
477
571
  revert NoERC725YDataKeysAllowed(controllerAddress);
478
572
 
479
- uint256 allowedKeysFound;
480
-
481
573
  // cache the input data keys from the start
482
574
  uint256 inputKeysLength = inputDataKeys.length;
483
575
 
484
576
  /**
485
577
  * The pointer will always land on the length of each bytes value:
486
578
  *
487
- * ↓↓
488
- * 03 a00000
489
- * 05 fff83a0011
490
- * 20 aa0000000000000000000000000000000000000000000000000000000000cafe
491
- * 12 bb000000000000000000000000000000beef
492
- * 19 cc00000000000000000000000000000000000000000000deed
493
- * ↑↑
579
+ * ↓↓↓↓
580
+ * 0003 a00000
581
+ * 0005 fff83a0011
582
+ * 0020 aa0000000000000000000000000000000000000000000000000000000000cafe
583
+ * 0012 bb000000000000000000000000000000beef
584
+ * 0019 cc00000000000000000000000000000000000000000000deed
585
+ * ↑↑↑↑
494
586
  *
495
587
  */
496
588
  uint256 pointer;
@@ -503,10 +595,10 @@ abstract contract LSP6SetDataModule {
503
595
  /**
504
596
  * iterate over each data key and update the `pointer` variable with the index where to find the length of each data key.
505
597
  *
506
- * 0x 03 a00000 03 fff83a 20 aa00...00cafe
507
- * ↑↑ ↑↑ ↑↑
508
- * first | second | third
509
- * length | length | length
598
+ * 0x 0003 a00000 0003 fff83a 0020 aa00...00cafe
599
+ * ↑↑↑↑ ↑↑↑↑ ↑↑↑↑
600
+ * first | second | third
601
+ * length | length | length
510
602
  */
511
603
  while (pointer < allowedERC725YDataKeysCompacted.length) {
512
604
  // save the length of the allowed data key to calculate the `mask`.
@@ -519,6 +611,18 @@ abstract contract LSP6SetDataModule {
519
611
  )
520
612
  );
521
613
 
614
+ /**
615
+ * The length of a data key is 32 bytes.
616
+ * Therefore we can have a fixed allowed data key which has
617
+ * a length of 32 bytes or we can have a dynamic data key
618
+ * which can have a length of up to 31 bytes.
619
+ */
620
+ if (length > 32)
621
+ revert InvalidEncodedAllowedERC725YDataKeys(
622
+ allowedERC725YDataKeysCompacted,
623
+ "couldn't DECODE from storage"
624
+ );
625
+
522
626
  /**
523
627
  * The bitmask discard the last `32 - length` bytes of the input data key via ANDing &
524
628
  * It is used to compare only the relevant parts of each input data key against dynamic allowed data keys.
@@ -556,20 +660,32 @@ abstract contract LSP6SetDataModule {
556
660
  * Iterate over the `inputDataKeys` to check them against the allowed data keys.
557
661
  * This until we have validated them all.
558
662
  */
559
- for (uint256 ii; ii < inputKeysLength; ii = GasLib.uncheckedIncrement(ii)) {
663
+ for (uint256 ii; ii < inputKeysLength; ) {
560
664
  // if the input data key has been marked as allowed previously,
561
665
  // SKIP it and move to the next input data key.
562
- if (validatedInputKeys[ii]) continue;
666
+ if (validatedInputKeysList[ii]) {
667
+ unchecked {
668
+ ++ii;
669
+ }
670
+ continue;
671
+ }
563
672
 
564
673
  // CHECK if the input data key is allowed.
565
674
  if ((inputDataKeys[ii] & mask) == allowedKey) {
566
675
  // if the input data key is allowed, mark it as allowed
567
676
  // and increment the number of allowed keys found.
568
- validatedInputKeys[ii] = true;
569
- allowedKeysFound = GasLib.uncheckedIncrement(allowedKeysFound);
677
+ validatedInputKeysList[ii] = true;
678
+
679
+ unchecked {
680
+ allowedDataKeysFound++;
681
+ }
570
682
 
571
683
  // Continue checking until all the inputKeys` have been found.
572
- if (allowedKeysFound == inputKeysLength) return;
684
+ if (allowedDataKeysFound == inputKeysLength) return;
685
+ }
686
+
687
+ unchecked {
688
+ ++ii;
573
689
  }
574
690
  }
575
691
 
@@ -580,10 +696,14 @@ abstract contract LSP6SetDataModule {
580
696
  }
581
697
 
582
698
  // if we did not find all the input data keys, search for the first not allowed data key to revert.
583
- for (uint256 jj; jj < inputKeysLength; jj = GasLib.uncheckedIncrement(jj)) {
584
- if (!validatedInputKeys[jj]) {
699
+ for (uint256 jj; jj < inputKeysLength; ) {
700
+ if (!validatedInputKeysList[jj]) {
585
701
  revert NotAllowedERC725YDataKey(controllerAddress, inputDataKeys[jj]);
586
702
  }
703
+
704
+ unchecked {
705
+ jj++;
706
+ }
587
707
  }
588
708
  }
589
709
 
@@ -9,12 +9,8 @@ import {ILSP6KeyManager} from "./ILSP6KeyManager.sol";
9
9
  import {LSP2Utils} from "../LSP2ERC725YJSONSchema/LSP2Utils.sol";
10
10
 
11
11
  // constants
12
- import {SETDATA_ARRAY_SELECTOR} from "@erc725/smart-contracts/contracts/constants.sol";
13
12
  import "./LSP6Constants.sol";
14
13
 
15
- // errors
16
- import {NotAuthorised} from "./LSP6Errors.sol";
17
-
18
14
  library LSP6Utils {
19
15
  using LSP2Utils for bytes12;
20
16
 
@@ -107,8 +103,8 @@ library LSP6Utils {
107
103
  )
108
104
  )
109
105
  );
110
- // each entries in the allowedCalls (compact) array must be 28 bytes long
111
- if (elementLength != 28) return false;
106
+ // each entries in the allowedCalls (compact) array must be 32 bytes long
107
+ if (elementLength != 32) return false;
112
108
  pointer += elementLength + 2;
113
109
  }
114
110
  if (pointer == allowedCallsCompacted.length) return true;
@@ -155,47 +151,62 @@ library LSP6Utils {
155
151
  bytes32[] memory keys,
156
152
  bytes[] memory values
157
153
  ) internal returns (bytes memory result) {
158
- bytes memory payload = abi.encodeWithSelector(SETDATA_ARRAY_SELECTOR, keys, values);
154
+ bytes memory payload = abi.encodeWithSelector(IERC725Y.setDataBatch.selector, keys, values);
159
155
  result = ILSP6KeyManager(keyManagerAddress).execute(payload);
160
156
  }
161
157
 
162
158
  /**
163
159
  * @dev combine multiple permissions into a single bytes32
164
160
  * Make sure that the sum of the values of the input array is less than 2^256-1 to avoid overflow.
165
- * @param _permissions the array of permissions to combine
161
+ * @param permissions the array of permissions to combine
166
162
  * @return a bytes32 containing the combined permissions
167
163
  */
168
- function combinePermissions(bytes32[] memory _permissions) internal pure returns (bytes32) {
164
+ function combinePermissions(bytes32[] memory permissions) internal pure returns (bytes32) {
169
165
  uint256 result = 0;
170
- for (uint256 i = 0; i < _permissions.length; i++) {
171
- result += uint256(_permissions[i]);
166
+ for (uint256 i = 0; i < permissions.length; i++) {
167
+ result += uint256(permissions[i]);
172
168
  }
173
169
  return bytes32(result);
174
170
  }
175
171
 
176
- function generatePermissionsKeysForController(
177
- IERC725Y _account,
178
- address _address,
172
+ /**
173
+ * @dev Generate a new set of 3 x LSP6 permission data keys to add a new `controller` on `account`
174
+ * @param account the ERC725Y contract to add the controller into (used to fetch the `LSP6Permissions[]` length)
175
+ * @param controller the address of the controller to grant permissions to
176
+ * @param permissions the BitArray of permissions to grant to the controller
177
+ * @return keys an array of 3 x data keys containing:
178
+ * - keys[0] = `AddressPermissions[]` (array length)
179
+ * - keys[1] = `AddressPermissions[index]` (where to store the controller address)
180
+ * - keys[2] = `AddressPermissions:Permissions:<controller>`
181
+ *
182
+ * @return values : an array of 3 x data values containing:
183
+ * - values[0] = the new array length of `AddressPermissions[]`
184
+ * - values[1] = the address of the controller
185
+ * - values[2] = the `permissions` passed as param
186
+ */
187
+ function generateNewPermissionsKeys(
188
+ IERC725Y account,
189
+ address controller,
179
190
  bytes32 permissions
180
191
  ) internal view returns (bytes32[] memory keys, bytes[] memory values) {
181
192
  keys = new bytes32[](3);
182
193
  values = new bytes[](3);
183
194
 
184
- uint256 arrayLength = uint256(bytes32(_account.getData(_LSP6KEY_ADDRESSPERMISSIONS_ARRAY)));
185
- uint256 newArrayLength = arrayLength + 1;
195
+ uint128 arrayLength = uint128(bytes16(account.getData(_LSP6KEY_ADDRESSPERMISSIONS_ARRAY)));
196
+ uint128 newArrayLength = arrayLength + 1;
186
197
 
187
198
  keys[0] = _LSP6KEY_ADDRESSPERMISSIONS_ARRAY;
188
199
  values[0] = abi.encodePacked(newArrayLength);
189
200
 
190
201
  keys[1] = LSP2Utils.generateArrayElementKeyAtIndex(
191
202
  _LSP6KEY_ADDRESSPERMISSIONS_ARRAY,
192
- uint128(arrayLength)
203
+ arrayLength
193
204
  );
194
- values[1] = abi.encodePacked(_address);
205
+ values[1] = abi.encodePacked(controller);
195
206
 
196
207
  keys[2] = LSP2Utils.generateMappingWithGroupingKey(
197
208
  _LSP6KEY_ADDRESSPERMISSIONS_PERMISSIONS_PREFIX,
198
- bytes20(_address)
209
+ bytes20(controller)
199
210
  );
200
211
  values[2] = abi.encodePacked(permissions);
201
212
  }
@@ -209,7 +220,7 @@ library LSP6Utils {
209
220
  returns (string memory errorMessage)
210
221
  {
211
222
  if (permission == _PERMISSION_CHANGEOWNER) return "TRANSFEROWNERSHIP";
212
- if (permission == _PERMISSION_CHANGEPERMISSIONS) return "CHANGEPERMISSIONS";
223
+ if (permission == _PERMISSION_EDITPERMISSIONS) return "EDITPERMISSIONS";
213
224
  if (permission == _PERMISSION_ADDCONTROLLER) return "ADDCONTROLLER";
214
225
  if (permission == _PERMISSION_ADDEXTENSIONS) return "ADDEXTENSIONS";
215
226
  if (permission == _PERMISSION_CHANGEEXTENSIONS) return "CHANGEEXTENSIONS";