@lukso/lsp-smart-contracts 0.7.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/CONTRIBUTING.md +39 -0
  2. package/README.md +93 -40
  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 +26 -5
  17. package/artifacts/LSP7CappedSupplyInitAbstract.json +26 -5
  18. package/artifacts/LSP7CompatibleERC20.json +26 -5
  19. package/artifacts/LSP7CompatibleERC20InitAbstract.json +28 -7
  20. package/artifacts/LSP7CompatibleERC20Mintable.json +29 -8
  21. package/artifacts/LSP7CompatibleERC20MintableInit.json +29 -8
  22. package/artifacts/LSP7DigitalAsset.json +26 -5
  23. package/artifacts/LSP7DigitalAssetInitAbstract.json +26 -5
  24. package/artifacts/LSP7Mintable.json +29 -8
  25. package/artifacts/LSP7MintableInit.json +29 -8
  26. package/artifacts/LSP8CappedSupply.json +27 -6
  27. package/artifacts/LSP8CappedSupplyInitAbstract.json +27 -6
  28. package/artifacts/LSP8CompatibleERC721.json +28 -7
  29. package/artifacts/LSP8CompatibleERC721InitAbstract.json +30 -9
  30. package/artifacts/LSP8CompatibleERC721Mintable.json +31 -10
  31. package/artifacts/LSP8CompatibleERC721MintableInit.json +31 -10
  32. package/artifacts/LSP8IdentifiableDigitalAsset.json +27 -6
  33. package/artifacts/LSP8IdentifiableDigitalAssetInitAbstract.json +27 -6
  34. package/artifacts/LSP8Mintable.json +30 -9
  35. package/artifacts/LSP8MintableInit.json +30 -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/common/constants.d.ts +738 -0
  42. package/common/constants.js +785 -0
  43. package/contracts/Custom/EIP191Signer.sol +12 -0
  44. package/contracts/Factories/Create2Factory.sol +1 -3
  45. package/contracts/LSP0ERC725Account/LSP0Constants.sol +13 -2
  46. package/contracts/LSP0ERC725Account/LSP0ERC725Account.sol +2 -1
  47. package/contracts/LSP0ERC725Account/LSP0ERC725AccountCore.sol +320 -63
  48. package/contracts/LSP0ERC725Account/LSP0ERC725AccountInit.sol +2 -2
  49. package/contracts/LSP0ERC725Account/LSP0ERC725AccountInitAbstract.sol +2 -1
  50. package/contracts/LSP0ERC725Account/LSP0Utils.sol +50 -0
  51. package/contracts/LSP10ReceivedVaults/LSP10Constants.sol +2 -2
  52. package/contracts/LSP10ReceivedVaults/LSP10Utils.sol +212 -0
  53. package/contracts/LSP11BasicSocialRecovery/ILSP11BasicSocialRecovery.sol +177 -0
  54. package/contracts/LSP11BasicSocialRecovery/LSP11BasicSocialRecovery.sol +23 -0
  55. package/contracts/LSP11BasicSocialRecovery/LSP11BasicSocialRecoveryCore.sol +258 -0
  56. package/contracts/LSP11BasicSocialRecovery/LSP11BasicSocialRecoveryInit.sol +26 -0
  57. package/contracts/LSP11BasicSocialRecovery/LSP11BasicSocialRecoveryInitAbstract.sol +19 -0
  58. package/contracts/LSP11BasicSocialRecovery/LSP11Constants.sol +6 -0
  59. package/contracts/LSP11BasicSocialRecovery/LSP11Errors.sol +60 -0
  60. package/contracts/LSP14Ownable2Step/ILSP14Ownable2Step.sol +53 -0
  61. package/contracts/LSP14Ownable2Step/LSP14Constants.sol +15 -0
  62. package/contracts/LSP14Ownable2Step/LSP14Errors.sol +12 -0
  63. package/contracts/LSP14Ownable2Step/LSP14Ownable2Step.sol +212 -0
  64. package/contracts/LSP16UniversalFactory/LSP16UniversalFactory.sol +246 -0
  65. package/contracts/LSP17ContractExtension/LSP17Constants.sol +17 -0
  66. package/contracts/LSP17ContractExtension/LSP17Errors.sol +7 -0
  67. package/contracts/LSP17ContractExtension/LSP17Extendable.sol +104 -0
  68. package/contracts/LSP17ContractExtension/LSP17Extension.sol +45 -0
  69. package/contracts/LSP17ContractExtension/LSP17Utils.sol +20 -0
  70. package/contracts/LSP1UniversalReceiver/ILSP1UniversalReceiver.sol +5 -5
  71. package/contracts/LSP1UniversalReceiver/LSP1Constants.sol +5 -3
  72. package/contracts/LSP1UniversalReceiver/LSP1Errors.sol +21 -0
  73. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateUP/LSP1UniversalReceiverDelegateUP.sol +155 -37
  74. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateVault/LSP1UniversalReceiverDelegateVault.sol +64 -31
  75. package/contracts/LSP1UniversalReceiver/LSP1Utils.sol +53 -40
  76. package/contracts/LSP2ERC725YJSONSchema/LSP2Utils.sol +45 -23
  77. package/contracts/LSP3UniversalProfile/LSP3Constants.sol +1 -1
  78. package/contracts/LSP4DigitalAssetMetadata/ILSP4Compatibility.sol +1 -1
  79. package/contracts/LSP4DigitalAssetMetadata/LSP4Compatibility.sol +1 -1
  80. package/contracts/LSP4DigitalAssetMetadata/LSP4Constants.sol +1 -1
  81. package/contracts/LSP4DigitalAssetMetadata/LSP4DigitalAssetMetadata.sol +2 -2
  82. package/contracts/LSP4DigitalAssetMetadata/LSP4DigitalAssetMetadataInitAbstract.sol +2 -2
  83. package/contracts/LSP4DigitalAssetMetadata/LSP4Errors.sol +3 -3
  84. package/contracts/LSP5ReceivedAssets/LSP5Constants.sol +2 -2
  85. package/contracts/LSP5ReceivedAssets/LSP5Utils.sol +102 -114
  86. package/contracts/LSP6KeyManager/ILSP6KeyManager.sol +25 -7
  87. package/contracts/LSP6KeyManager/LSP6Constants.sol +43 -38
  88. package/contracts/LSP6KeyManager/LSP6Errors.sol +90 -21
  89. package/contracts/LSP6KeyManager/LSP6KeyManager.sol +2 -1
  90. package/contracts/LSP6KeyManager/LSP6KeyManagerCore.sol +224 -506
  91. package/contracts/LSP6KeyManager/LSP6KeyManagerInit.sol +1 -1
  92. package/contracts/LSP6KeyManager/LSP6KeyManagerInitAbstract.sol +3 -2
  93. package/contracts/LSP6KeyManager/LSP6Modules/LSP6ExecuteModule.sol +229 -0
  94. package/contracts/LSP6KeyManager/LSP6Modules/LSP6OwnershipModule.sol +25 -0
  95. package/contracts/LSP6KeyManager/LSP6Modules/LSP6SetDataModule.sol +606 -0
  96. package/contracts/LSP6KeyManager/LSP6Utils.sol +156 -35
  97. package/contracts/LSP7DigitalAsset/ILSP7DigitalAsset.sol +8 -8
  98. package/contracts/LSP7DigitalAsset/LSP7Constants.sol +6 -6
  99. package/contracts/LSP7DigitalAsset/LSP7DigitalAsset.sol +1 -1
  100. package/contracts/LSP7DigitalAsset/LSP7DigitalAssetCore.sol +36 -36
  101. package/contracts/LSP7DigitalAsset/LSP7DigitalAssetInitAbstract.sol +1 -1
  102. package/contracts/LSP7DigitalAsset/LSP7Errors.sol +33 -1
  103. package/contracts/LSP7DigitalAsset/extensions/ILSP7CompatibleERC20.sol +1 -1
  104. package/contracts/LSP7DigitalAsset/extensions/LSP7Burnable.sol +24 -0
  105. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupply.sol +28 -8
  106. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupplyInitAbstract.sol +28 -11
  107. package/contracts/LSP7DigitalAsset/extensions/LSP7CompatibleERC20.sol +87 -20
  108. package/contracts/LSP7DigitalAsset/extensions/LSP7CompatibleERC20InitAbstract.sol +97 -19
  109. package/contracts/LSP7DigitalAsset/presets/ILSP7Mintable.sol +3 -3
  110. package/contracts/LSP7DigitalAsset/presets/LSP7CompatibleERC20Mintable.sol +3 -4
  111. package/contracts/LSP7DigitalAsset/presets/LSP7CompatibleERC20MintableInit.sol +2 -2
  112. package/contracts/LSP7DigitalAsset/presets/LSP7CompatibleERC20MintableInitAbstract.sol +3 -3
  113. package/contracts/LSP7DigitalAsset/presets/LSP7Mintable.sol +3 -5
  114. package/contracts/LSP7DigitalAsset/presets/LSP7MintableInit.sol +2 -2
  115. package/contracts/LSP7DigitalAsset/presets/LSP7MintableInitAbstract.sol +3 -3
  116. package/contracts/LSP8IdentifiableDigitalAsset/ILSP8IdentifiableDigitalAsset.sol +7 -7
  117. package/contracts/LSP8IdentifiableDigitalAsset/LSP8Constants.sol +7 -7
  118. package/contracts/LSP8IdentifiableDigitalAsset/LSP8Errors.sol +45 -2
  119. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAsset.sol +2 -2
  120. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAssetCore.sol +57 -60
  121. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAssetInitAbstract.sol +1 -1
  122. package/contracts/LSP8IdentifiableDigitalAsset/extensions/ILSP8CompatibleERC721.sol +5 -2
  123. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8Burnable.sol +20 -0
  124. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupply.sol +27 -7
  125. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupplyInitAbstract.sol +27 -10
  126. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721.sol +234 -30
  127. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721InitAbstract.sol +238 -30
  128. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8Enumerable.sol +32 -4
  129. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8EnumerableInitAbstract.sol +35 -8
  130. package/contracts/LSP8IdentifiableDigitalAsset/presets/ILSP8Mintable.sol +3 -3
  131. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8CompatibleERC721Mintable.sol +3 -4
  132. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8CompatibleERC721MintableInit.sol +3 -3
  133. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8CompatibleERC721MintableInitAbstract.sol +3 -3
  134. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8Mintable.sol +3 -4
  135. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8MintableInit.sol +2 -2
  136. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8MintableInitAbstract.sol +3 -3
  137. package/contracts/LSP9Vault/LSP9Constants.sol +10 -10
  138. package/contracts/LSP9Vault/LSP9Errors.sol +8 -0
  139. package/contracts/LSP9Vault/LSP9Vault.sol +6 -4
  140. package/contracts/LSP9Vault/LSP9VaultCore.sol +350 -127
  141. package/contracts/LSP9Vault/LSP9VaultInit.sol +2 -2
  142. package/contracts/LSP9Vault/LSP9VaultInitAbstract.sol +4 -3
  143. package/contracts/Legacy/Registries/AddressRegistry.sol +1 -1
  144. package/contracts/Legacy/Registries/AddressRegistryRequiresERC725.sol +1 -1
  145. package/contracts/Legacy/UniversalReceiverAddressStore.sol +4 -14
  146. package/contracts/UniversalProfile.sol +2 -2
  147. package/contracts/UniversalProfileInit.sol +2 -2
  148. package/contracts/UniversalProfileInitAbstract.sol +1 -1
  149. package/contracts/Utils/GasLib.sol +1 -1
  150. package/module/constants.d.ts +738 -0
  151. package/module/constants.js +782 -0
  152. package/package.json +42 -12
  153. package/artifacts/UniversalFactory.json +0 -127
  154. package/constants.js +0 -594
  155. package/contracts/Custom/ClaimOwnership.sol +0 -96
  156. package/contracts/Custom/ERC165Checker.sol +0 -130
  157. package/contracts/Custom/IClaimOwnership.sol +0 -12
  158. package/contracts/Factories/UniversalFactory.sol +0 -159
  159. package/contracts/LSP1UniversalReceiver/ILSP1UniversalReceiverDelegate.sol +0 -24
  160. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateUP/Handling/TokenAndVaultHandling.sol +0 -82
  161. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateVault/Handling/TokenHandling.sol +0 -73
  162. package/contracts/LSP2ERC725YJSONSchema/LSP2Errors.sol +0 -9
  163. package/contracts/LSP7DigitalAsset/extensions/ILSP7CappedSupply.sol +0 -17
  164. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupplyCore.sol +0 -57
  165. package/contracts/LSP7DigitalAsset/extensions/LSP7CompatibleERC20Core.sol +0 -111
  166. package/contracts/LSP8IdentifiableDigitalAsset/extensions/ILSP8CappedSupply.sol +0 -17
  167. package/contracts/LSP8IdentifiableDigitalAsset/extensions/ILSP8Enumerable.sol +0 -17
  168. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupplyCore.sol +0 -58
  169. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleConstants.sol +0 -6
  170. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721Core.sol +0 -211
  171. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8EnumerableCore.sol +0 -50
  172. package/contracts/Utils/UtilsLib.sol +0 -31
@@ -0,0 +1,606 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.5;
3
+
4
+ // modules
5
+ import {ERC725Y} from "@erc725/smart-contracts/contracts/ERC725Y.sol";
6
+
7
+ // libraries
8
+ import {GasLib} from "../../Utils/GasLib.sol";
9
+ import {LSP6Utils} from "../LSP6Utils.sol";
10
+
11
+ // constants
12
+ import {
13
+ _LSP6KEY_ADDRESSPERMISSIONS_ARRAY,
14
+ _LSP6KEY_ADDRESSPERMISSIONS_ARRAY_PREFIX,
15
+ _LSP6KEY_ADDRESSPERMISSIONS_PREFIX,
16
+ _LSP6KEY_ADDRESSPERMISSIONS_PERMISSIONS_PREFIX,
17
+ _LSP6KEY_ADDRESSPERMISSIONS_ALLOWEDCALLS_PREFIX,
18
+ _LSP6KEY_ADDRESSPERMISSIONS_AllowedERC725YDataKeys_PREFIX,
19
+ _PERMISSION_SETDATA,
20
+ _PERMISSION_SUPER_SETDATA,
21
+ _PERMISSION_ADDCONTROLLER,
22
+ _PERMISSION_CHANGEPERMISSIONS,
23
+ _PERMISSION_ADDEXTENSIONS,
24
+ _PERMISSION_CHANGEEXTENSIONS,
25
+ _PERMISSION_ADDUNIVERSALRECEIVERDELEGATE,
26
+ _PERMISSION_CHANGEUNIVERSALRECEIVERDELEGATE
27
+ } from "../LSP6Constants.sol";
28
+ import {
29
+ _LSP1_UNIVERSAL_RECEIVER_DELEGATE_PREFIX,
30
+ _LSP1_UNIVERSAL_RECEIVER_DELEGATE_KEY
31
+ } from "../../LSP1UniversalReceiver/LSP1Constants.sol";
32
+ import {_LSP17_EXTENSION_PREFIX} from "../../LSP17ContractExtension/LSP17Constants.sol";
33
+
34
+ // errors
35
+ import {
36
+ NotRecognisedPermissionKey,
37
+ AddressPermissionArrayIndexValueNotAnAddress,
38
+ InvalidEncodedAllowedCalls,
39
+ InvalidEncodedAllowedERC725YDataKeys,
40
+ NoERC725YDataKeysAllowed,
41
+ NotAllowedERC725YDataKey,
42
+ NotAuthorised
43
+ } from "../LSP6Errors.sol";
44
+
45
+ abstract contract LSP6SetDataModule {
46
+ using LSP6Utils for *;
47
+
48
+ /**
49
+ * @dev verify if the `controllerAddress` has the permissions required to set a data key on the ERC725Y storage of the `controlledContract`.
50
+ * @param controlledContract the address of the ERC725Y contract where the data key is set.
51
+ * @param controllerAddress the address of the controller who wants to set the data key.
52
+ * @param controllerPermissions the permissions of the controller address.
53
+ * @param inputDataKey the data key to set on the `controlledContract`.
54
+ * @param inputDataValue the data value to set for the `inputDataKey`.
55
+ */
56
+ function _verifyCanSetData(
57
+ address controlledContract,
58
+ address controllerAddress,
59
+ bytes32 controllerPermissions,
60
+ bytes32 inputDataKey,
61
+ bytes memory inputDataValue
62
+ ) internal view virtual {
63
+ bytes32 requiredPermission = _getPermissionRequiredToSetDataKey(
64
+ controlledContract,
65
+ inputDataKey,
66
+ inputDataValue
67
+ );
68
+
69
+ // CHECK if allowed to set an ERC725Y Data Key
70
+ if (requiredPermission == _PERMISSION_SETDATA) {
71
+ // Skip if caller has SUPER permissions
72
+ if (controllerPermissions.hasPermission(_PERMISSION_SUPER_SETDATA)) return;
73
+
74
+ _requirePermissions(controllerAddress, controllerPermissions, _PERMISSION_SETDATA);
75
+
76
+ _verifyAllowedERC725YSingleKey(
77
+ controllerAddress,
78
+ inputDataKey,
79
+ ERC725Y(controlledContract).getAllowedERC725YDataKeysFor(controllerAddress)
80
+ );
81
+ } else {
82
+ // Otherwise CHECK the required permission if setting LSP6 permissions, LSP1 Delegate or LSP17 Extensions.
83
+ _requirePermissions(controllerAddress, controllerPermissions, requiredPermission);
84
+ }
85
+ }
86
+
87
+ /**
88
+ * @dev verify if the `controllerAddress` has the permissions required to set an array of data keys on the ERC725Y storage of the `controlledContract`.
89
+ * @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.
92
+ * @param inputDataKeys an array of data keys to set on the `controlledContract`.
93
+ * @param inputDataValues an array of data values to set for the `inputDataKeys`.
94
+ */
95
+ function _verifyCanSetData(
96
+ address controlledContract,
97
+ address controllerAddress,
98
+ bytes32 controllerPermissions,
99
+ bytes32[] memory inputDataKeys,
100
+ bytes[] memory inputDataValues
101
+ ) internal view virtual {
102
+ bool isSettingERC725YKeys;
103
+ bool[] memory validatedInputDataKeys = new bool[](inputDataKeys.length);
104
+
105
+ bytes32 requiredPermission;
106
+
107
+ uint256 ii;
108
+ do {
109
+ requiredPermission = _getPermissionRequiredToSetDataKey(
110
+ controlledContract,
111
+ inputDataKeys[ii],
112
+ inputDataValues[ii]
113
+ );
114
+
115
+ if (requiredPermission == _PERMISSION_SETDATA) {
116
+ isSettingERC725YKeys = true;
117
+ } else {
118
+ // CHECK the required permissions if setting LSP6 permissions, LSP1 Delegate or LSP17 Extensions.
119
+ _requirePermissions(controllerAddress, controllerPermissions, requiredPermission);
120
+ validatedInputDataKeys[ii] = true;
121
+ }
122
+
123
+ ii = GasLib.uncheckedIncrement(ii);
124
+ } while (ii < inputDataKeys.length);
125
+
126
+ // CHECK if allowed to set one (or multiple) ERC725Y Data Keys
127
+ if (isSettingERC725YKeys) {
128
+ // Skip if caller has SUPER permissions
129
+ if (controllerPermissions.hasPermission(_PERMISSION_SUPER_SETDATA)) return;
130
+
131
+ _requirePermissions(controllerAddress, controllerPermissions, _PERMISSION_SETDATA);
132
+
133
+ _verifyAllowedERC725YDataKeys(
134
+ controllerAddress,
135
+ inputDataKeys,
136
+ ERC725Y(controlledContract).getAllowedERC725YDataKeysFor(controllerAddress),
137
+ validatedInputDataKeys
138
+ );
139
+ }
140
+ }
141
+
142
+ /**
143
+ * @dev retrieve the permission required based on the data key to be set on the `controlledContract`.
144
+ * @param controlledContract the address of the ERC725Y contract where the data key is verified.
145
+ * @param inputDataKey the data key to set on the `controlledContract`. Can be related to LSP6 Permissions, LSP1 Delegate or LSP17 Extensions.
146
+ * @param inputDataValue the data value to set for the `inputDataKey`.
147
+ * @return the permission required to set the `inputDataKey` on the `controlledContract`.
148
+ */
149
+ function _getPermissionRequiredToSetDataKey(
150
+ address controlledContract,
151
+ bytes32 inputDataKey,
152
+ bytes memory inputDataValue
153
+ ) internal view virtual returns (bytes32) {
154
+ // AddressPermissions[] or AddressPermissions[index]
155
+ if (bytes16(inputDataKey) == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY_PREFIX) {
156
+ return
157
+ _getPermissionToSetPermissionsArray(
158
+ controlledContract,
159
+ inputDataKey,
160
+ inputDataValue
161
+ );
162
+
163
+ // AddressPermissions:...
164
+ } else if (bytes6(inputDataKey) == _LSP6KEY_ADDRESSPERMISSIONS_PREFIX) {
165
+ // AddressPermissions:Permissions:<address>
166
+ if (bytes12(inputDataKey) == _LSP6KEY_ADDRESSPERMISSIONS_PERMISSIONS_PREFIX) {
167
+ return _getPermissionToSetControllerPermissions(controlledContract, inputDataKey);
168
+
169
+ // AddressPermissions:AllowedCalls:<address>
170
+ } else if (bytes12(inputDataKey) == _LSP6KEY_ADDRESSPERMISSIONS_ALLOWEDCALLS_PREFIX) {
171
+ return
172
+ _getPermissionToSetAllowedCalls(
173
+ controlledContract,
174
+ inputDataKey,
175
+ inputDataValue
176
+ );
177
+
178
+ // AddressPermissions:AllowedERC725YKeys:<address>
179
+ } else if (
180
+ bytes12(inputDataKey) == _LSP6KEY_ADDRESSPERMISSIONS_AllowedERC725YDataKeys_PREFIX
181
+ ) {
182
+ return
183
+ _getPermissionToSetAllowedERC725YDataKeys(
184
+ controlledContract,
185
+ inputDataKey,
186
+ inputDataValue
187
+ );
188
+
189
+ // if the first 6 bytes of the input data key are "AddressPermissions:..." but did not match
190
+ // with anything above, this is not a standard LSP6 permission data key so we revert.
191
+ } else {
192
+ /**
193
+ * @dev more permissions types starting with `AddressPermissions:...` can be implemented by overriding this function.
194
+ *
195
+ * // AddressPermissions:MyCustomPermissions:<address>
196
+ * bytes12 CUSTOM_PERMISSION_PREFIX = 0x4b80742de2bf9e659ba40000
197
+ *
198
+ * if (bytes12(dataKey) == CUSTOM_PERMISSION_PREFIX) {
199
+ * // custom logic
200
+ * }
201
+ *
202
+ * super._getPermissionRequiredToSetDataKey(...)
203
+ */
204
+ revert NotRecognisedPermissionKey(inputDataKey);
205
+ }
206
+
207
+ // LSP1UniversalReceiverDelegate or LSP1UniversalReceiverDelegate:<typeId>
208
+ } else if (
209
+ inputDataKey == _LSP1_UNIVERSAL_RECEIVER_DELEGATE_KEY ||
210
+ bytes12(inputDataKey) == _LSP1_UNIVERSAL_RECEIVER_DELEGATE_PREFIX
211
+ ) {
212
+ return _getPermissionToSetLSP1Delegate(controlledContract, inputDataKey);
213
+
214
+ // LSP17Extension:<bytes4>
215
+ } else if (bytes12(inputDataKey) == _LSP17_EXTENSION_PREFIX) {
216
+ return _getPermissionToSetLSP17Extension(controlledContract, inputDataKey);
217
+ } else {
218
+ return _PERMISSION_SETDATA;
219
+ }
220
+ }
221
+
222
+ /**
223
+ * @dev retrieve the permission required to update the `AddressPermissions[]` array data key defined in LSP6.
224
+ * @param controlledContract the address of the ERC725Y contract where the data key is verified.
225
+ * @param inputDataKey either `AddressPermissions[]` (array length) or `AddressPermissions[index]` (array index)
226
+ * @param inputDataValue the updated value for the `inputDataKey`. MUST be:
227
+ * - a `uint256` for `AddressPermissions[]` (array length)
228
+ * - an `address` or `0x` for `AddressPermissions[index]` (array entry).
229
+ *
230
+ * @return either ADD or CHANGE PERMISSIONS.
231
+ */
232
+ function _getPermissionToSetPermissionsArray(
233
+ address controlledContract,
234
+ bytes32 inputDataKey,
235
+ bytes memory inputDataValue
236
+ ) internal view virtual returns (bytes32) {
237
+ bytes memory currentValue = ERC725Y(controlledContract).getData(inputDataKey);
238
+
239
+ // AddressPermissions[] -> array length
240
+ if (inputDataKey == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY) {
241
+ uint256 newLength = uint256(bytes32(inputDataValue));
242
+
243
+ return
244
+ newLength > uint256(bytes32(currentValue))
245
+ ? _PERMISSION_ADDCONTROLLER
246
+ : _PERMISSION_CHANGEPERMISSIONS;
247
+ }
248
+
249
+ // AddressPermissions[index] -> array index
250
+
251
+ // CHECK that we either ADD an address (20 bytes long) or REMOVE an address (0x)
252
+ if (inputDataValue.length != 0 && inputDataValue.length != 20) {
253
+ revert AddressPermissionArrayIndexValueNotAnAddress(inputDataKey, inputDataValue);
254
+ }
255
+
256
+ return currentValue.length == 0 ? _PERMISSION_ADDCONTROLLER : _PERMISSION_CHANGEPERMISSIONS;
257
+ }
258
+
259
+ /**
260
+ * @dev retrieve the permission required to set permissions for a controller address.
261
+ * @param controlledContract the address of the ERC725Y contract where the data key is verified.
262
+ * @param inputPermissionDataKey `AddressPermissions:Permissions:<controller-address>`.
263
+ * @return either ADD or CHANGE PERMISSIONS.
264
+ */
265
+ function _getPermissionToSetControllerPermissions(
266
+ address controlledContract,
267
+ bytes32 inputPermissionDataKey
268
+ ) internal view virtual returns (bytes32) {
269
+ return
270
+ // if there is nothing stored under the data key, we are trying to ADD a new controller.
271
+ // if there are already some permissions set under the data key, we are trying to CHANGE the permissions of a controller.
272
+ bytes32(ERC725Y(controlledContract).getData(inputPermissionDataKey)) == bytes32(0)
273
+ ? _PERMISSION_ADDCONTROLLER
274
+ : _PERMISSION_CHANGEPERMISSIONS;
275
+ }
276
+
277
+ /**
278
+ * @dev retrieve the permission required to set some AllowedCalls for a controller.
279
+ * @param controlledContract the address of the ERC725Y contract where the data key is verified.
280
+ * @param dataKey `AddressPermissions:AllowedCalls:<controller-address>`.
281
+ * @param dataValue the updated value for the `dataKey`. MUST be a bytes28[CompactBytesArray] of Allowed Calls.
282
+ * @return either ADD or CHANGE PERMISSIONS.
283
+ */
284
+ function _getPermissionToSetAllowedCalls(
285
+ address controlledContract,
286
+ bytes32 dataKey,
287
+ bytes memory dataValue
288
+ ) internal view virtual returns (bytes32) {
289
+ if (!LSP6Utils.isCompactBytesArrayOfAllowedCalls(dataValue)) {
290
+ revert InvalidEncodedAllowedCalls(dataValue);
291
+ }
292
+
293
+ // if there is nothing stored under the Allowed Calls of the controller,
294
+ // we are trying to ADD a list of restricted calls (standards + address + function selector)
295
+ //
296
+ // if there are already some data set under the Allowed Calls of the controller,
297
+ // we are trying to CHANGE (= edit) these restrictions.
298
+ return
299
+ ERC725Y(controlledContract).getData(dataKey).length == 0
300
+ ? _PERMISSION_ADDCONTROLLER
301
+ : _PERMISSION_CHANGEPERMISSIONS;
302
+ }
303
+
304
+ /**
305
+ * @dev retrieve the permission required to set some AllowedCalls for a controller.
306
+ * @param controlledContract the address of the ERC725Y contract where the data key is verified.
307
+ * @param dataKey or `AddressPermissions:AllowedERC725YDataKeys:<controller-address>`.
308
+ * @param dataValue the updated value for the `dataKey`. MUST be a bytes[CompactBytesArray] of Allowed ERC725Y Data Keys.
309
+ * @return either ADD or CHANGE PERMISSIONS.
310
+ */
311
+ function _getPermissionToSetAllowedERC725YDataKeys(
312
+ address controlledContract,
313
+ bytes32 dataKey,
314
+ bytes memory dataValue
315
+ ) internal view returns (bytes32) {
316
+ if (!LSP6Utils.isCompactBytesArrayOfAllowedERC725YDataKeys(dataValue)) {
317
+ revert InvalidEncodedAllowedERC725YDataKeys(dataValue);
318
+ }
319
+
320
+ // if there is nothing stored under the Allowed ERC725Y Data Keys of the controller,
321
+ // we are trying to ADD a list of restricted ERC725Y Data Keys.
322
+ //
323
+ // if there are already some data set under the Allowed ERC725Y Data Keys of the controller,
324
+ // we are trying to CHANGE (= edit) these restricted ERC725Y data keys.
325
+ return
326
+ ERC725Y(controlledContract).getData(dataKey).length == 0
327
+ ? _PERMISSION_ADDCONTROLLER
328
+ : _PERMISSION_CHANGEPERMISSIONS;
329
+ }
330
+
331
+ /**
332
+ * @dev retrieve the permission required to either add or change the address
333
+ * of a LSP1 Universal Receiver Delegate stored under a specific LSP1 data key.
334
+ * @param controlledContract the address of the ERC725Y contract where the data key is verified.
335
+ * @param lsp1DelegateDataKey either the data key for the default `LSP1UniversalReceiverDelegate`,
336
+ * or a data key for a specific `LSP1UniversalReceiverDelegate:<typeId>`, starting with `_LSP1_UNIVERSAL_RECEIVER_DELEGATE_PREFIX`.
337
+ * @return either ADD or CHANGE UNIVERSALRECEIVERDELEGATE.
338
+ */
339
+ function _getPermissionToSetLSP1Delegate(
340
+ address controlledContract,
341
+ bytes32 lsp1DelegateDataKey
342
+ ) internal view virtual returns (bytes32) {
343
+ return
344
+ ERC725Y(controlledContract).getData(lsp1DelegateDataKey).length == 0
345
+ ? _PERMISSION_ADDUNIVERSALRECEIVERDELEGATE
346
+ : _PERMISSION_CHANGEUNIVERSALRECEIVERDELEGATE;
347
+ }
348
+
349
+ /**
350
+ * @dev Verify if `controller` has the required permissions to either add or change the address
351
+ * of an LSP0 Extension stored under a specific LSP17Extension data key
352
+ * @param controlledContract the address of the ERC725Y contract where the data key is verified.
353
+ * @param lsp17ExtensionDataKey the dataKey to set with `_LSP17_EXTENSION_PREFIX` as prefix.
354
+ */
355
+ function _getPermissionToSetLSP17Extension(
356
+ address controlledContract,
357
+ bytes32 lsp17ExtensionDataKey
358
+ ) internal view virtual returns (bytes32) {
359
+ return
360
+ ERC725Y(controlledContract).getData(lsp17ExtensionDataKey).length == 0
361
+ ? _PERMISSION_ADDEXTENSIONS
362
+ : _PERMISSION_CHANGEEXTENSIONS;
363
+ }
364
+
365
+ /**
366
+ * @dev Verify if the `inputKey` is present in the list of `allowedERC725KeysCompacted` for the `controllerAddress`.
367
+ * @param controllerAddress the address of the controller.
368
+ * @param inputDataKey the data key to verify against the allowed ERC725Y Data Keys for the `controllerAddress`.
369
+ * @param allowedERC725YDataKeysCompacted a CompactBytesArray of allowed ERC725Y Data Keys for the `controllerAddress`.
370
+ */
371
+ function _verifyAllowedERC725YSingleKey(
372
+ address controllerAddress,
373
+ bytes32 inputDataKey,
374
+ bytes memory allowedERC725YDataKeysCompacted
375
+ ) internal pure virtual {
376
+ if (allowedERC725YDataKeysCompacted.length == 0)
377
+ revert NoERC725YDataKeysAllowed(controllerAddress);
378
+
379
+ /**
380
+ * The pointer will always land on the length of each bytes value:
381
+ *
382
+ * ↓↓
383
+ * 03 a00000
384
+ * 05 fff83a0011
385
+ * 20 aa0000000000000000000000000000000000000000000000000000000000cafe
386
+ * 12 bb000000000000000000000000000000beef
387
+ * 19 cc00000000000000000000000000000000000000000000deed
388
+ * ↑↑
389
+ *
390
+ */
391
+ uint256 pointer;
392
+
393
+ // information extracted from each Allowed ERC725Y Data Key.
394
+ uint256 length;
395
+ bytes32 allowedKey;
396
+ bytes32 mask;
397
+
398
+ /**
399
+ * iterate over each data key and update the `pointer` variable with the index where to find the length of each data key.
400
+ *
401
+ * 0x 03 a00000 03 fff83a 20 aa00...00cafe
402
+ * ↑↑ ↑↑ ↑↑
403
+ * first | second | third
404
+ * length | length | length
405
+ */
406
+ while (pointer < allowedERC725YDataKeysCompacted.length) {
407
+ // save the length of the allowed data key to calculate the `mask`.
408
+ length = uint16(
409
+ bytes2(
410
+ abi.encodePacked(
411
+ allowedERC725YDataKeysCompacted[pointer],
412
+ allowedERC725YDataKeysCompacted[pointer + 1]
413
+ )
414
+ )
415
+ );
416
+
417
+ /**
418
+ * The bitmask discard the last `32 - length` bytes of the input data key via ANDing &
419
+ * It is used to compare only the relevant parts of each input data key against dynamic allowed data keys.
420
+ *
421
+ * E.g.:
422
+ *
423
+ * allowed data key = 0xa00000
424
+ *
425
+ * compare this part
426
+ * vvvvvv
427
+ * input data key = 0xa00000cafecafecafecafecafecafecafe000000000000000000000011223344
428
+ *
429
+ * & discard this part
430
+ * vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
431
+ * mask = 0xffffff0000000000000000000000000000000000000000000000000000000000
432
+ */
433
+ mask =
434
+ bytes32(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) <<
435
+ (8 * (32 - length));
436
+
437
+ /*
438
+ * transform the allowed data key situated from `pointer + 1` until `pointer + 1 + length` to a bytes32 value.
439
+ * E.g. 0xfff83a -> 0xfff83a0000000000000000000000000000000000000000000000000000000000
440
+ */
441
+ // solhint-disable-next-line no-inline-assembly
442
+ assembly {
443
+ // the first 32 bytes word in memory (where allowedERC725YDataKeysCompacted is stored)
444
+ // correspond to the total number of bytes in `allowedERC725YDataKeysCompacted`
445
+ let offset := add(add(pointer, 2), 32)
446
+ let memoryAt := mload(add(allowedERC725YDataKeysCompacted, offset))
447
+ // MLOAD loads 32 bytes word, so we need to keep only the `length` number of bytes that makes up the allowed data key.
448
+ allowedKey := and(memoryAt, mask)
449
+ }
450
+
451
+ // voila you found the key ;)
452
+ if (allowedKey == (inputDataKey & mask)) return;
453
+
454
+ // move the pointer to the index of the next allowed data key
455
+ unchecked {
456
+ pointer = pointer + (length + 2);
457
+ }
458
+ }
459
+
460
+ revert NotAllowedERC725YDataKey(controllerAddress, inputDataKey);
461
+ }
462
+
463
+ /**
464
+ * @dev Verify if all the `inputDataKeys` are present in the list of `allowedERC725KeysCompacted` of the `controllerAddress`.
465
+ * @param controllerAddress the address of the controller.
466
+ * @param inputDataKeys the data keys to verify against the allowed ERC725Y Data Keys of the `controllerAddress`.
467
+ * @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.
469
+ */
470
+ function _verifyAllowedERC725YDataKeys(
471
+ address controllerAddress,
472
+ bytes32[] memory inputDataKeys,
473
+ bytes memory allowedERC725YDataKeysCompacted,
474
+ bool[] memory validatedInputKeys
475
+ ) internal pure virtual {
476
+ if (allowedERC725YDataKeysCompacted.length == 0)
477
+ revert NoERC725YDataKeysAllowed(controllerAddress);
478
+
479
+ uint256 allowedKeysFound;
480
+
481
+ // cache the input data keys from the start
482
+ uint256 inputKeysLength = inputDataKeys.length;
483
+
484
+ /**
485
+ * The pointer will always land on the length of each bytes value:
486
+ *
487
+ * ↓↓
488
+ * 03 a00000
489
+ * 05 fff83a0011
490
+ * 20 aa0000000000000000000000000000000000000000000000000000000000cafe
491
+ * 12 bb000000000000000000000000000000beef
492
+ * 19 cc00000000000000000000000000000000000000000000deed
493
+ * ↑↑
494
+ *
495
+ */
496
+ uint256 pointer;
497
+
498
+ // information extracted from each Allowed ERC725Y Data Key.
499
+ uint256 length;
500
+ bytes32 allowedKey;
501
+ bytes32 mask;
502
+
503
+ /**
504
+ * iterate over each data key and update the `pointer` variable with the index where to find the length of each data key.
505
+ *
506
+ * 0x 03 a00000 03 fff83a 20 aa00...00cafe
507
+ * ↑↑ ↑↑ ↑↑
508
+ * first | second | third
509
+ * length | length | length
510
+ */
511
+ while (pointer < allowedERC725YDataKeysCompacted.length) {
512
+ // save the length of the allowed data key to calculate the `mask`.
513
+ length = uint16(
514
+ bytes2(
515
+ abi.encodePacked(
516
+ allowedERC725YDataKeysCompacted[pointer],
517
+ allowedERC725YDataKeysCompacted[pointer + 1]
518
+ )
519
+ )
520
+ );
521
+
522
+ /**
523
+ * The bitmask discard the last `32 - length` bytes of the input data key via ANDing &
524
+ * It is used to compare only the relevant parts of each input data key against dynamic allowed data keys.
525
+ *
526
+ * E.g.:
527
+ *
528
+ * allowed data key = 0xa00000
529
+ *
530
+ * compare this part
531
+ * vvvvvv
532
+ * input data key = 0xa00000cafecafecafecafecafecafecafe000000000000000000000011223344
533
+ *
534
+ * & discard this part
535
+ * vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
536
+ * mask = 0xffffff0000000000000000000000000000000000000000000000000000000000
537
+ */
538
+ mask =
539
+ bytes32(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) <<
540
+ (8 * (32 - length));
541
+
542
+ /*
543
+ * transform the allowed data key situated from `pointer + 1` until `pointer + 1 + length` to a bytes32 value.
544
+ * E.g. 0xfff83a -> 0xfff83a0000000000000000000000000000000000000000000000000000000000
545
+ */
546
+ // solhint-disable-next-line no-inline-assembly
547
+ assembly {
548
+ // the first 32 bytes word in memory (where allowedERC725YDataKeysCompacted is stored)
549
+ // correspond to the length of allowedERC725YDataKeysCompacted (= total number of bytes)
550
+ let offset := add(add(pointer, 2), 32)
551
+ let memoryAt := mload(add(allowedERC725YDataKeysCompacted, offset))
552
+ allowedKey := and(memoryAt, mask)
553
+ }
554
+
555
+ /**
556
+ * Iterate over the `inputDataKeys` to check them against the allowed data keys.
557
+ * This until we have validated them all.
558
+ */
559
+ for (uint256 ii; ii < inputKeysLength; ii = GasLib.uncheckedIncrement(ii)) {
560
+ // if the input data key has been marked as allowed previously,
561
+ // SKIP it and move to the next input data key.
562
+ if (validatedInputKeys[ii]) continue;
563
+
564
+ // CHECK if the input data key is allowed.
565
+ if ((inputDataKeys[ii] & mask) == allowedKey) {
566
+ // if the input data key is allowed, mark it as allowed
567
+ // and increment the number of allowed keys found.
568
+ validatedInputKeys[ii] = true;
569
+ allowedKeysFound = GasLib.uncheckedIncrement(allowedKeysFound);
570
+
571
+ // Continue checking until all the inputKeys` have been found.
572
+ if (allowedKeysFound == inputKeysLength) return;
573
+ }
574
+ }
575
+
576
+ // Move the pointer to the next AllowedERC725YKey
577
+ unchecked {
578
+ pointer = pointer + (length + 2);
579
+ }
580
+ }
581
+
582
+ // 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]) {
585
+ revert NotAllowedERC725YDataKey(controllerAddress, inputDataKeys[jj]);
586
+ }
587
+ }
588
+ }
589
+
590
+ /**
591
+ * @dev revert if `controller`'s `addressPermissions` doesn't contain `permissionsRequired`
592
+ * @param controller the caller address
593
+ * @param addressPermissions the caller's permissions BitArray
594
+ * @param permissionRequired the required permission
595
+ */
596
+ function _requirePermissions(
597
+ address controller,
598
+ bytes32 addressPermissions,
599
+ bytes32 permissionRequired
600
+ ) internal pure virtual {
601
+ if (!LSP6Utils.hasPermission(addressPermissions, permissionRequired)) {
602
+ string memory permissionErrorString = LSP6Utils.getPermissionName(permissionRequired);
603
+ revert NotAuthorised(controller, permissionErrorString);
604
+ }
605
+ }
606
+ }