@lukso/lsp-smart-contracts 0.6.0 → 0.7.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 (140) hide show
  1. package/README.md +13 -12
  2. package/artifacts/Create2Factory.json +2 -2
  3. package/artifacts/LSP0ERC725Account.json +66 -27
  4. package/artifacts/LSP0ERC725AccountInit.json +71 -53
  5. package/artifacts/LSP1UniversalReceiverDelegateUP.json +23 -2
  6. package/artifacts/LSP1UniversalReceiverDelegateVault.json +23 -2
  7. package/artifacts/LSP4Compatibility.json +15 -9
  8. package/artifacts/LSP4DigitalAssetMetadata.json +25 -9
  9. package/artifacts/{LSP4DigitalAssetMetadataInit.json → LSP4DigitalAssetMetadataInitAbstract.json} +27 -60
  10. package/artifacts/LSP6KeyManager.json +93 -13
  11. package/artifacts/LSP6KeyManagerInit.json +98 -13
  12. package/artifacts/LSP7CappedSupply.json +56 -35
  13. package/artifacts/{LSP7CappedSupplyInit.json → LSP7CappedSupplyInitAbstract.json} +58 -104
  14. package/artifacts/LSP7CompatibleERC20.json +704 -0
  15. package/artifacts/LSP7CompatibleERC20InitAbstract.json +717 -0
  16. package/artifacts/LSP7CompatibleERC20Mintable.json +753 -0
  17. package/artifacts/LSP7CompatibleERC20MintableInit.json +773 -0
  18. package/artifacts/LSP7DigitalAsset.json +56 -61
  19. package/artifacts/LSP7DigitalAssetInitAbstract.json +540 -0
  20. package/artifacts/LSP7Mintable.json +59 -38
  21. package/artifacts/LSP7MintableInit.json +64 -64
  22. package/artifacts/LSP8CappedSupply.json +62 -9
  23. package/artifacts/{LSP8CappedSupplyInit.json → LSP8CappedSupplyInitAbstract.json} +64 -73
  24. package/artifacts/LSP8CompatibleERC721.json +907 -0
  25. package/artifacts/LSP8CompatibleERC721InitAbstract.json +920 -0
  26. package/artifacts/LSP8CompatibleERC721Mintable.json +967 -0
  27. package/artifacts/LSP8CompatibleERC721MintableInit.json +987 -0
  28. package/artifacts/LSP8IdentifiableDigitalAsset.json +62 -30
  29. package/artifacts/LSP8IdentifiableDigitalAssetInitAbstract.json +628 -0
  30. package/artifacts/LSP8Mintable.json +64 -11
  31. package/artifacts/LSP8MintableInit.json +69 -37
  32. package/artifacts/LSP9Vault.json +63 -24
  33. package/artifacts/LSP9VaultInit.json +68 -50
  34. package/artifacts/UniversalFactory.json +2 -2
  35. package/artifacts/UniversalProfile.json +66 -27
  36. package/artifacts/UniversalProfileInit.json +71 -53
  37. package/artifacts/UniversalReceiverAddressStore.json +7 -2
  38. package/constants.js +273 -31
  39. package/contracts/Custom/ClaimOwnership.sol +74 -9
  40. package/contracts/Custom/ERC165Checker.sol +1 -1
  41. package/contracts/Custom/IClaimOwnership.sol +3 -1
  42. package/contracts/Factories/Create2Factory.sol +3 -13
  43. package/contracts/Factories/UniversalFactory.sol +20 -16
  44. package/contracts/LSP0ERC725Account/LSP0Constants.sol +1 -1
  45. package/contracts/LSP0ERC725Account/LSP0ERC725Account.sol +3 -3
  46. package/contracts/LSP0ERC725Account/LSP0ERC725AccountCore.sol +49 -37
  47. package/contracts/LSP0ERC725Account/LSP0ERC725AccountInit.sol +10 -3
  48. package/contracts/LSP0ERC725Account/LSP0ERC725AccountInitAbstract.sol +3 -3
  49. package/contracts/LSP10ReceivedVaults/LSP10Constants.sol +2 -2
  50. package/contracts/LSP1UniversalReceiver/ILSP1UniversalReceiver.sol +3 -0
  51. package/contracts/LSP1UniversalReceiver/ILSP1UniversalReceiverDelegate.sol +4 -2
  52. package/contracts/LSP1UniversalReceiver/LSP1Constants.sol +1 -1
  53. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateUP/Handling/TokenAndVaultHandling.sol +22 -16
  54. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateUP/LSP1UniversalReceiverDelegateUP.sol +14 -10
  55. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateVault/Handling/TokenHandling.sol +35 -29
  56. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateVault/LSP1UniversalReceiverDelegateVault.sol +10 -3
  57. package/contracts/LSP1UniversalReceiver/LSP1Utils.sol +24 -9
  58. package/contracts/LSP2ERC725YJSONSchema/LSP2Errors.sol +9 -0
  59. package/contracts/LSP2ERC725YJSONSchema/LSP2Utils.sol +157 -75
  60. package/contracts/LSP3UniversalProfile/LSP3Constants.sol +1 -1
  61. package/contracts/LSP4DigitalAssetMetadata/LSP4Constants.sol +1 -1
  62. package/contracts/LSP4DigitalAssetMetadata/LSP4DigitalAssetMetadata.sol +24 -4
  63. package/contracts/LSP4DigitalAssetMetadata/LSP4DigitalAssetMetadataInitAbstract.sol +26 -5
  64. package/contracts/LSP4DigitalAssetMetadata/LSP4Errors.sol +16 -0
  65. package/contracts/LSP5ReceivedAssets/LSP5Constants.sol +2 -2
  66. package/contracts/LSP5ReceivedAssets/LSP5Utils.sol +151 -77
  67. package/contracts/LSP6KeyManager/ILSP6KeyManager.sol +16 -16
  68. package/contracts/LSP6KeyManager/LSP6Constants.sol +16 -21
  69. package/contracts/LSP6KeyManager/LSP6Errors.sol +42 -5
  70. package/contracts/LSP6KeyManager/LSP6KeyManager.sol +6 -4
  71. package/contracts/LSP6KeyManager/LSP6KeyManagerCore.sol +293 -284
  72. package/contracts/LSP6KeyManager/LSP6KeyManagerInit.sol +11 -4
  73. package/contracts/LSP6KeyManager/LSP6KeyManagerInitAbstract.sol +5 -3
  74. package/contracts/LSP6KeyManager/LSP6Utils.sol +24 -27
  75. package/contracts/LSP7DigitalAsset/ILSP7DigitalAsset.sol +18 -5
  76. package/contracts/LSP7DigitalAsset/LSP7Constants.sol +1 -1
  77. package/contracts/LSP7DigitalAsset/LSP7DigitalAsset.sol +8 -4
  78. package/contracts/LSP7DigitalAsset/LSP7DigitalAssetCore.sol +35 -24
  79. package/contracts/LSP7DigitalAsset/LSP7DigitalAssetInitAbstract.sol +5 -3
  80. package/contracts/LSP7DigitalAsset/LSP7Errors.sol +2 -0
  81. package/contracts/LSP7DigitalAsset/extensions/{ILSP7CompatibilityForERC20.sol → ILSP7CompatibleERC20.sol} +6 -6
  82. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupplyCore.sol +1 -1
  83. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupplyInitAbstract.sol +5 -2
  84. package/contracts/LSP7DigitalAsset/extensions/{LSP7CompatibilityForERC20.sol → LSP7CompatibleERC20.sol} +6 -7
  85. package/contracts/LSP7DigitalAsset/extensions/{LSP7CompatibilityForERC20Core.sol → LSP7CompatibleERC20Core.sol} +23 -19
  86. package/contracts/LSP7DigitalAsset/extensions/{LSP7CompatibilityForERC20InitAbstract.sol → LSP7CompatibleERC20InitAbstract.sol} +11 -10
  87. package/contracts/LSP7DigitalAsset/presets/LSP7CompatibleERC20Mintable.sol +22 -0
  88. package/contracts/{LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibilityForERC721Init.sol → LSP7DigitalAsset/presets/LSP7CompatibleERC20MintableInit.sol} +12 -7
  89. package/contracts/LSP7DigitalAsset/presets/LSP7CompatibleERC20MintableInitAbstract.sol +27 -0
  90. package/contracts/LSP7DigitalAsset/presets/LSP7Mintable.sol +9 -7
  91. package/contracts/LSP7DigitalAsset/presets/LSP7MintableInit.sol +11 -4
  92. package/contracts/LSP7DigitalAsset/presets/LSP7MintableInitAbstract.sol +8 -7
  93. package/contracts/LSP8IdentifiableDigitalAsset/ILSP8IdentifiableDigitalAsset.sol +3 -1
  94. package/contracts/LSP8IdentifiableDigitalAsset/LSP8Constants.sol +4 -4
  95. package/contracts/LSP8IdentifiableDigitalAsset/LSP8Errors.sol +6 -0
  96. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAsset.sol +1 -1
  97. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAssetCore.sol +31 -30
  98. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAssetInitAbstract.sol +3 -1
  99. package/contracts/LSP8IdentifiableDigitalAsset/extensions/{ILSP8CompatibilityForERC721.sol → ILSP8CompatibleERC721.sol} +16 -2
  100. package/contracts/LSP8IdentifiableDigitalAsset/extensions/ILSP8Enumerable.sol +17 -0
  101. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupplyCore.sol +1 -1
  102. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupplyInitAbstract.sol +4 -2
  103. package/contracts/LSP8IdentifiableDigitalAsset/extensions/{LSP8CompatibilityConstants.sol → LSP8CompatibleConstants.sol} +0 -0
  104. package/contracts/LSP8IdentifiableDigitalAsset/extensions/{LSP8CompatibilityForERC721.sol → LSP8CompatibleERC721.sol} +12 -11
  105. package/contracts/LSP8IdentifiableDigitalAsset/extensions/{LSP8CompatibilityForERC721Core.sol → LSP8CompatibleERC721Core.sol} +61 -37
  106. package/contracts/LSP8IdentifiableDigitalAsset/extensions/{LSP8CompatibilityForERC721InitAbstract.sol → LSP8CompatibleERC721InitAbstract.sol} +13 -9
  107. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8Enumerable.sol +21 -0
  108. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8EnumerableCore.sol +50 -0
  109. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8EnumerableInitAbstract.sol +24 -0
  110. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8CompatibleERC721Mintable.sol +22 -0
  111. package/contracts/{LSP7DigitalAsset/extensions/LSP7CompatibilityForERC20Init.sol → LSP8IdentifiableDigitalAsset/presets/LSP8CompatibleERC721MintableInit.sol} +12 -4
  112. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8CompatibleERC721MintableInitAbstract.sol +27 -0
  113. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8Mintable.sol +6 -4
  114. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8MintableInit.sol +7 -0
  115. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8MintableInitAbstract.sol +8 -5
  116. package/contracts/LSP9Vault/LSP9Constants.sol +6 -3
  117. package/contracts/LSP9Vault/LSP9Vault.sol +9 -5
  118. package/contracts/LSP9Vault/LSP9VaultCore.sol +152 -69
  119. package/contracts/LSP9Vault/LSP9VaultInit.sol +10 -3
  120. package/contracts/LSP9Vault/LSP9VaultInitAbstract.sol +9 -5
  121. package/contracts/Legacy/UniversalReceiverAddressStore.sol +6 -2
  122. package/contracts/UniversalProfile.sol +3 -4
  123. package/contracts/UniversalProfileInit.sol +10 -3
  124. package/contracts/UniversalProfileInitAbstract.sol +3 -5
  125. package/contracts/Utils/GasLib.sol +17 -0
  126. package/contracts/Utils/UtilsLib.sol +6 -34
  127. package/package.json +27 -30
  128. package/artifacts/LSP7CompatibilityForERC20.json +0 -686
  129. package/artifacts/LSP7CompatibilityForERC20Init.json +0 -727
  130. package/artifacts/LSP7DigitalAssetInit.json +0 -573
  131. package/artifacts/LSP8CompatibilityForERC721.json +0 -832
  132. package/artifacts/LSP8CompatibilityForERC721Init.json +0 -873
  133. package/artifacts/LSP8IdentifiableDigitalAssetInit.json +0 -624
  134. package/contracts/LSP4DigitalAssetMetadata/LSP4DigitalAssetMetadataInit.sol +0 -26
  135. package/contracts/LSP7DigitalAsset/LSP7DigitalAssetInit.sol +0 -28
  136. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupplyInit.sol +0 -19
  137. package/contracts/LSP7DigitalAsset/presets/LSP7MintableCore.sol +0 -26
  138. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAssetInit.sol +0 -26
  139. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupplyInit.sol +0 -19
  140. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8MintableCore.sol +0 -26
@@ -1,5 +1,5 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.6;
2
+ pragma solidity ^0.8.5;
3
3
 
4
4
  // interfaces
5
5
  import {IERC1271} from "@openzeppelin/contracts/interfaces/IERC1271.sol";
@@ -13,27 +13,35 @@ import {ERC725Y} from "@erc725/smart-contracts/contracts/ERC725Y.sol";
13
13
  import {ERC165} from "@openzeppelin/contracts/utils/introspection/ERC165.sol";
14
14
 
15
15
  // libraries
16
+ import {GasLib} from "../Utils/GasLib.sol";
16
17
  import {BytesLib} from "solidity-bytes-utils/contracts/BytesLib.sol";
17
18
  import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
18
19
  import {Address} from "@openzeppelin/contracts/utils/Address.sol";
19
- import {ErrorHandlerLib} from "@erc725/smart-contracts/contracts/utils/ErrorHandlerLib.sol";
20
20
  import {ERC165Checker} from "../Custom/ERC165Checker.sol";
21
21
  import {LSP2Utils} from "../LSP2ERC725YJSONSchema/LSP2Utils.sol";
22
22
  import {LSP6Utils} from "./LSP6Utils.sol";
23
23
 
24
24
  // errors
25
25
  import "./LSP6Errors.sol";
26
+ import {InvalidABIEncodedArray} from "../LSP2ERC725YJSONSchema/LSP2Errors.sol";
26
27
 
27
28
  // constants
28
- // prettier-ignore
29
29
  import {
30
+ // ERC725X
30
31
  OPERATION_CALL,
31
32
  OPERATION_CREATE,
32
33
  OPERATION_CREATE2,
33
34
  OPERATION_STATICCALL,
34
- OPERATION_DELEGATECALL
35
+ OPERATION_DELEGATECALL,
36
+ // ERC725Y
37
+ SETDATA_SELECTOR,
38
+ SETDATA_ARRAY_SELECTOR
35
39
  } from "@erc725/smart-contracts/contracts/constants.sol";
36
- import {_INTERFACEID_ERC1271, _ERC1271_MAGICVALUE, _ERC1271_FAILVALUE} from "../LSP0ERC725Account/LSP0Constants.sol";
40
+ import {
41
+ _INTERFACEID_ERC1271,
42
+ _ERC1271_MAGICVALUE,
43
+ _ERC1271_FAILVALUE
44
+ } from "../LSP0ERC725Account/LSP0Constants.sol";
37
45
  import "./LSP6Constants.sol";
38
46
 
39
47
  /**
@@ -48,7 +56,7 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
48
56
  using ECDSA for bytes32;
49
57
  using ERC165Checker for address;
50
58
 
51
- address public override target;
59
+ address public target;
52
60
  mapping(address => mapping(uint256 => uint256)) internal _nonceStore;
53
61
 
54
62
  /**
@@ -64,21 +72,20 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
64
72
  /**
65
73
  * @inheritdoc ILSP6KeyManager
66
74
  */
67
- function getNonce(address _from, uint256 _channel) public view override returns (uint256) {
68
- uint128 nonceId = uint128(_nonceStore[_from][_channel]);
69
- return (uint256(_channel) << 128) | nonceId;
75
+ function getNonce(address from, uint256 channelId) public view returns (uint256) {
76
+ uint128 nonceId = uint128(_nonceStore[from][channelId]);
77
+ return (uint256(channelId) << 128) | nonceId;
70
78
  }
71
79
 
72
80
  /**
73
81
  * @inheritdoc IERC1271
74
82
  */
75
- function isValidSignature(bytes32 _hash, bytes memory _signature)
83
+ function isValidSignature(bytes32 dataHash, bytes memory signature)
76
84
  public
77
85
  view
78
- override
79
86
  returns (bytes4 magicValue)
80
87
  {
81
- address recoveredAddress = _hash.recover(_signature);
88
+ address recoveredAddress = dataHash.recover(signature);
82
89
 
83
90
  return (
84
91
  ERC725Y(target).getPermissionsFor(recoveredAddress).hasPermission(_PERMISSION_SIGN)
@@ -90,301 +97,318 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
90
97
  /**
91
98
  * @inheritdoc ILSP6KeyManager
92
99
  */
93
- function execute(bytes calldata _calldata) external payable override returns (bytes memory) {
94
- _verifyPermissions(msg.sender, _calldata);
95
-
96
- // solhint-disable avoid-low-level-calls
97
- (bool success, bytes memory result) = target.call{value: msg.value, gas: gasleft()}(
98
- _calldata
99
- );
100
+ function execute(bytes calldata payload) public payable returns (bytes memory) {
101
+ _verifyPermissions(msg.sender, payload);
100
102
 
101
- if (!success) {
102
- ErrorHandlerLib.revertWithParsedError(result);
103
- }
104
-
105
- emit Executed(msg.value, bytes4(_calldata));
106
- return result.length != 0 ? abi.decode(result, (bytes)) : result;
103
+ return _executePayload(payload);
107
104
  }
108
105
 
109
106
  /**
110
107
  * @inheritdoc ILSP6KeyManager
111
108
  */
112
109
  function executeRelayCall(
113
- bytes memory _signature,
114
- uint256 _nonce,
115
- bytes calldata _calldata
116
- ) external payable override returns (bytes memory) {
110
+ bytes memory signature,
111
+ uint256 nonce,
112
+ bytes calldata payload
113
+ ) public payable returns (bytes memory) {
117
114
  bytes memory blob = abi.encodePacked(
118
115
  block.chainid,
119
116
  address(this), // needs to be signed for this keyManager
120
- _nonce,
121
- _calldata
117
+ nonce,
118
+ payload
122
119
  );
123
120
 
124
- address signer = keccak256(blob).toEthSignedMessageHash().recover(_signature);
121
+ address signer = keccak256(blob).toEthSignedMessageHash().recover(signature);
125
122
 
126
- require(_isValidNonce(signer, _nonce), "executeRelayCall: Invalid nonce");
123
+ if (!_isValidNonce(signer, nonce)) {
124
+ revert InvalidRelayNonce(signer, nonce, signature);
125
+ }
127
126
 
128
127
  // increase nonce after successful verification
129
- _nonceStore[signer][_nonce >> 128]++;
128
+ _nonceStore[signer][nonce >> 128]++;
130
129
 
131
- _verifyPermissions(signer, _calldata);
130
+ _verifyPermissions(signer, payload);
132
131
 
133
- // solhint-disable avoid-low-level-calls
134
- (bool success, bytes memory result) = target.call{value: msg.value, gas: gasleft()}(
135
- _calldata
132
+ return _executePayload(payload);
133
+ }
134
+
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) {
142
+
143
+ // solhint-disable avoid-low-level-calls
144
+ (bool success, bytes memory returnData) = target.call{value: msg.value, gas: gasleft()}(
145
+ payload
146
+ );
147
+ bytes memory result = Address.verifyCallResult(
148
+ success,
149
+ returnData,
150
+ "LSP6: Unknown Error occured when calling the linked target contract"
136
151
  );
137
152
 
138
- if (!success) {
139
- ErrorHandlerLib.revertWithParsedError(result);
140
- }
153
+ emit Executed(msg.value, bytes4(payload));
154
+ return result.length != 0 ? abi.decode(result, (bytes)) : result;
141
155
 
142
- emit Executed(msg.value, bytes4(_calldata));
143
- return result.length != 0 ? abi.decode(result, (bytes)) : result;
144
- }
156
+ }
145
157
 
146
158
  /**
147
159
  * @notice verify the nonce `_idx` for `_from` (obtained via `getNonce(...)`)
148
160
  * @dev "idx" is a 256bits (unsigned) integer, where:
149
161
  * - the 128 leftmost bits = channelId
150
162
  * and - the 128 rightmost bits = nonce within the channel
151
- * @param _from caller address
152
- * @param _idx (channel id + nonce within the channel)
163
+ * @param from caller address
164
+ * @param idx (channel id + nonce within the channel)
153
165
  */
154
- function _isValidNonce(address _from, uint256 _idx) internal view returns (bool) {
166
+ function _isValidNonce(address from, uint256 idx) internal view returns (bool) {
155
167
  // idx % (1 << 128) = nonce
156
168
  // (idx >> 128) = channel
157
169
  // equivalent to: return (nonce == _nonceStore[_from][channel]
158
- return (_idx % (1 << 128)) == (_nonceStore[_from][_idx >> 128]);
170
+ return (idx % (1 << 128)) == (_nonceStore[from][idx >> 128]);
159
171
  }
160
172
 
161
173
  /**
162
174
  * @dev verify the permissions of the _from address that want to interact with the `target`
163
- * @param _from the address making the request
164
- * @param _calldata the payload that will be run on `target`
175
+ * @param from the address making the request
176
+ * @param payload the payload that will be run on `target`
165
177
  */
166
- function _verifyPermissions(address _from, bytes calldata _calldata) internal view {
167
- bytes4 erc725Function = bytes4(_calldata[:4]);
178
+ function _verifyPermissions(address from, bytes calldata payload) internal view {
179
+ bytes4 erc725Function = bytes4(payload[:4]);
168
180
 
169
181
  // get the permissions of the caller
170
- bytes32 permissions = ERC725Y(target).getPermissionsFor(_from);
182
+ bytes32 permissions = ERC725Y(target).getPermissionsFor(from);
171
183
 
172
- if (permissions == bytes32(0)) revert NoPermissionsSet(_from);
184
+ if (permissions == bytes32(0)) revert NoPermissionsSet(from);
173
185
 
174
- // prettier-ignore
175
- if (erc725Function == setDataSingleSelector) {
186
+ if (erc725Function == SETDATA_SELECTOR) {
187
+ (bytes32 inputKey, bytes memory inputValue) = abi.decode(payload[4:], (bytes32, bytes));
176
188
 
177
- (bytes32 inputKey, bytes memory inputValue) = abi.decode(_calldata[4:], (bytes32, bytes));
189
+ if (bytes16(inputKey) == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY_PREFIX) {
190
+ // CHECK if key = AddressPermissions[] or AddressPermissions[index]
191
+ _verifyCanSetPermissionsArray(inputKey, inputValue, from, permissions);
178
192
 
179
- if (
193
+ } else if (bytes6(inputKey) == _LSP6KEY_ADDRESSPERMISSIONS_PREFIX) {
180
194
  // CHECK for permission keys
181
- bytes6(inputKey) == _LSP6KEY_ADDRESSPERMISSIONS_PREFIX ||
182
- bytes16(inputKey) == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY_PREFIX
183
- ) {
184
-
185
- _verifyCanSetPermissions(inputKey, inputValue, _from, permissions);
195
+ _verifyCanSetPermissions(inputKey, inputValue, from, permissions);
186
196
 
187
197
  } else {
198
+ bytes32[] memory wrappedInputKey = new bytes32[](1);
199
+ wrappedInputKey[0] = inputKey;
188
200
 
189
- bytes32[] memory inputKeys = new bytes32[](1);
190
- inputKeys[0] = inputKey;
191
-
192
- _verifyCanSetData(_from, permissions, inputKeys);
201
+ _verifyCanSetData(from, permissions, wrappedInputKey);
193
202
  }
194
-
195
- } else if (erc725Function == setDataMultipleSelector) {
196
-
197
- (bytes32[] memory inputKeys, bytes[] memory inputValues) = abi.decode(_calldata[4:], (bytes32[], bytes[]));
203
+ } else if (erc725Function == SETDATA_ARRAY_SELECTOR) {
204
+ (bytes32[] memory inputKeys, bytes[] memory inputValues) = abi.decode(
205
+ payload[4:],
206
+ (bytes32[], bytes[])
207
+ );
198
208
 
199
209
  bool isSettingERC725YKeys = false;
200
-
210
+
201
211
  // loop through each ERC725Y data keys
202
- for (uint256 ii = 0; ii < inputKeys.length; ii++) {
212
+ for (uint256 ii = 0; ii < inputKeys.length; ii = GasLib.uncheckedIncrement(ii)) {
203
213
  bytes32 key = inputKeys[ii];
204
214
  bytes memory value = inputValues[ii];
205
215
 
206
- if (
207
- // CHECK for permission keys
208
- bytes6(key) == _LSP6KEY_ADDRESSPERMISSIONS_PREFIX ||
209
- bytes16(key) == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY_PREFIX
210
- ) {
211
- _verifyCanSetPermissions(key, value, _from, permissions);
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);
212
226
 
213
- // "nullify" permission keys
214
- // to not check them against allowed ERC725Y keys
227
+ // "nullify" permission keys to not check them against allowed ERC725Y keys
215
228
  inputKeys[ii] = bytes32(0);
216
229
  } else {
217
230
  // if the key is any other bytes32 key
218
231
  isSettingERC725YKeys = true;
219
232
  }
220
-
221
233
  }
222
234
 
223
235
  if (isSettingERC725YKeys) {
224
- _verifyCanSetData(_from, permissions, inputKeys);
236
+ _verifyCanSetData(from, permissions, inputKeys);
225
237
  }
226
-
227
238
  } else if (erc725Function == IERC725X.execute.selector) {
228
-
229
- _verifyCanExecute(_from, permissions, _calldata);
230
-
239
+ _verifyCanExecute(from, permissions, payload);
231
240
  } else if (
232
241
  erc725Function == OwnableUnset.transferOwnership.selector ||
233
242
  erc725Function == IClaimOwnership.claimOwnership.selector
234
243
  ) {
235
-
236
- _requirePermissions(_from, permissions, _PERMISSION_CHANGEOWNER);
237
-
244
+ _requirePermissions(from, permissions, _PERMISSION_CHANGEOWNER);
238
245
  } else {
239
- revert("_verifyPermissions: invalid ERC725 selector");
246
+ revert InvalidERC725Function(erc725Function);
240
247
  }
241
248
  }
242
249
 
243
250
  /**
244
251
  * @dev verify if `_from` has the required permissions to set some keys
245
252
  * on the linked ERC725Account
246
- * @param _from the address who want to set the keys
247
- * @param _permissions the permissions
248
- * @param _inputKeys the data keys being set
253
+ * @param from the address who want to set the keys
254
+ * @param permissions the permissions
255
+ * @param inputKeys the data keys being set
249
256
  * containing a list of keys-value pairs
250
257
  */
251
258
  function _verifyCanSetData(
252
- address _from,
253
- bytes32 _permissions,
254
- bytes32[] memory _inputKeys
259
+ address from,
260
+ bytes32 permissions,
261
+ bytes32[] memory inputKeys
255
262
  ) internal view {
256
263
  // Skip if caller has SUPER permissions
257
- if (_permissions.hasPermission(_PERMISSION_SUPER_SETDATA)) return;
264
+ if (permissions.hasPermission(_PERMISSION_SUPER_SETDATA)) return;
258
265
 
259
- _requirePermissions(_from, _permissions, _PERMISSION_SETDATA);
266
+ _requirePermissions(from, permissions, _PERMISSION_SETDATA);
260
267
 
261
- _verifyAllowedERC725YKeys(_from, _inputKeys);
268
+ _verifyAllowedERC725YKeys(from, inputKeys);
262
269
  }
263
270
 
264
271
  function _verifyCanSetPermissions(
265
- bytes32 _key,
266
- bytes memory _value,
267
- address _from,
268
- bytes32 _permissions
269
- ) internal view {
272
+ bytes32 key,
273
+ bytes memory value,
274
+ address from,
275
+ bytes32 permissions
276
+ ) internal view virtual {
270
277
  // prettier-ignore
271
- if (bytes12(_key) == _LSP6KEY_ADDRESSPERMISSIONS_PERMISSIONS_PREFIX) {
272
-
273
- // key = AddressPermissions:Permissions:<address>
274
- _verifyCanSetBytes32Permissions(_key, _from, _permissions);
275
-
276
- } else if (_key == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY) {
278
+ if (bytes12(key) == _LSP6KEY_ADDRESSPERMISSIONS_PERMISSIONS_PREFIX) {
277
279
 
278
- // key = AddressPermissions[]
279
- _verifyCanSetPermissionsArray(_key, _value, _from, _permissions);
280
-
281
- } else if (bytes16(_key) == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY_PREFIX) {
280
+ // key = AddressPermissions:Permissions:<address>
281
+ _verifyCanSetBytes32Permissions(key, from, permissions);
282
282
 
283
- // key = AddressPermissions[index]
284
- _requirePermissions(_from, _permissions, _PERMISSION_CHANGEPERMISSIONS);
283
+ } else if (bytes12(key) == _LSP6KEY_ADDRESSPERMISSIONS_ALLOWEDADDRESSES_PREFIX) {
285
284
 
286
- } else if (bytes12(_key) == _LSP6KEY_ADDRESSPERMISSIONS_ALLOWEDADDRESSES_PREFIX) {
285
+ bool isClearingArray = value.length == 0;
287
286
 
288
287
  // AddressPermissions:AllowedAddresses:<address>
289
- require(
290
- LSP2Utils.isEncodedArrayOfAddresses(_value),
291
- "LSP6KeyManager: invalid ABI encoded array of addresses"
292
- );
288
+ if (!isClearingArray && !LSP2Utils.isEncodedArrayOfAddresses(value)) {
289
+ revert InvalidABIEncodedArray(value, "address");
290
+ }
293
291
 
294
- bytes memory storedAllowedAddresses = ERC725Y(target).getData(_key);
292
+ bytes memory storedAllowedAddresses = ERC725Y(target).getData(key);
295
293
 
296
294
  if (storedAllowedAddresses.length == 0) {
297
295
 
298
- _requirePermissions(_from, _permissions, _PERMISSION_ADDPERMISSIONS);
296
+ _requirePermissions(from, permissions, _PERMISSION_ADDPERMISSIONS);
299
297
 
300
298
  } else {
301
299
 
302
- _requirePermissions(_from, _permissions, _PERMISSION_CHANGEPERMISSIONS);
300
+ _requirePermissions(from, permissions, _PERMISSION_CHANGEPERMISSIONS);
303
301
 
304
302
  }
305
303
 
306
304
  } else if (
307
- bytes12(_key) == _LSP6KEY_ADDRESSPERMISSIONS_ALLOWEDFUNCTIONS_PREFIX ||
308
- bytes12(_key) == _LSP6KEY_ADDRESSPERMISSIONS_ALLOWEDSTANDARDS_PREFIX
305
+ bytes12(key) == _LSP6KEY_ADDRESSPERMISSIONS_ALLOWEDSTANDARDS_PREFIX ||
306
+ bytes12(key) == _LSP6KEY_ADDRESSPERMISSIONS_ALLOWEDFUNCTIONS_PREFIX
309
307
  ) {
308
+ bool isClearingArray = value.length == 0;
310
309
 
311
310
  // AddressPermissions:AllowedFunctions:<address>
312
311
  // AddressPermissions:AllowedStandards:<address>
313
- require(
314
- LSP2Utils.isBytes4EncodedArray(_value),
315
- "LSP6KeyManager: invalid ABI encoded array of bytes4"
316
- );
312
+ if (!isClearingArray && !LSP2Utils.isBytes4EncodedArray(value)) {
313
+ revert InvalidABIEncodedArray(value, "bytes4");
314
+ }
317
315
 
318
- bytes memory storedAllowedBytes4 = ERC725Y(target).getData(_key);
316
+ bytes memory storedAllowedBytes4 = ERC725Y(target).getData(key);
319
317
 
320
318
  if (storedAllowedBytes4.length == 0) {
321
319
 
322
- _requirePermissions(_from, _permissions, _PERMISSION_ADDPERMISSIONS);
320
+ _requirePermissions(from, permissions, _PERMISSION_ADDPERMISSIONS);
323
321
 
324
322
  } else {
325
323
 
326
- _requirePermissions(_from, _permissions, _PERMISSION_CHANGEPERMISSIONS);
324
+ _requirePermissions(from, permissions, _PERMISSION_CHANGEPERMISSIONS);
327
325
 
328
326
  }
329
327
 
330
- } else if (bytes12(_key) == _LSP6KEY_ADDRESSPERMISSIONS_ALLOWEDERC725YKEYS_PREFIX) {
328
+ } else if (bytes12(key) == _LSP6KEY_ADDRESSPERMISSIONS_ALLOWEDERC725YKEYS_PREFIX) {
329
+
330
+ bool isClearingArray = value.length == 0;
331
331
 
332
332
  // AddressPermissions:AllowedERC725YKeys:<address>
333
- require(
334
- LSP2Utils.isEncodedArray(_value),
335
- "LSP6KeyManager: invalid ABI encoded array of bytes32"
336
- );
333
+ if (!isClearingArray && !LSP2Utils.isEncodedArray(value)) {
334
+ revert InvalidABIEncodedArray(value, "bytes32");
335
+ }
337
336
 
338
- bytes memory storedAllowedERC725YKeys = ERC725Y(target).getData(_key);
337
+ bytes memory storedAllowedERC725YKeys = ERC725Y(target).getData(key);
339
338
 
340
339
  if (storedAllowedERC725YKeys.length == 0) {
341
340
 
342
- _requirePermissions(_from, _permissions, _PERMISSION_ADDPERMISSIONS);
341
+ _requirePermissions(from, permissions, _PERMISSION_ADDPERMISSIONS);
343
342
 
344
343
  } else {
345
344
 
346
- _requirePermissions(_from, _permissions, _PERMISSION_CHANGEPERMISSIONS);
345
+ _requirePermissions(from, permissions, _PERMISSION_CHANGEPERMISSIONS);
347
346
 
348
347
  }
349
-
348
+ } else {
349
+ /**
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
359
+ *
360
+ * if (bytes12(key) == CUSTOM_PERMISSION_PREFIX) {
361
+ * // custom logic
362
+ * }
363
+ * super._verifyCanSetPermissions(...)
364
+ */
365
+ revert NotRecognisedPermissionKey(key);
350
366
  }
351
367
  }
352
368
 
353
369
  function _verifyCanSetBytes32Permissions(
354
- bytes32 _key,
355
- address _from,
356
- bytes32 _callerPermissions
370
+ bytes32 key,
371
+ address from,
372
+ bytes32 callerPermissions
357
373
  ) internal view {
358
- if (bytes32(ERC725Y(target).getData(_key)) == bytes32(0)) {
374
+ if (bytes32(ERC725Y(target).getData(key)) == bytes32(0)) {
359
375
  // if there is nothing stored under this data key,
360
376
  // we are trying to ADD permissions for a NEW address
361
- _requirePermissions(_from, _callerPermissions, _PERMISSION_ADDPERMISSIONS);
377
+ _requirePermissions(from, callerPermissions, _PERMISSION_ADDPERMISSIONS);
362
378
  } else {
363
379
  // if there are already some permissions stored under this data key,
364
380
  // we are trying to CHANGE the permissions of an address
365
381
  // (that has already some EXISTING permissions set)
366
- _requirePermissions(_from, _callerPermissions, _PERMISSION_CHANGEPERMISSIONS);
382
+ _requirePermissions(from, callerPermissions, _PERMISSION_CHANGEPERMISSIONS);
367
383
  }
368
384
  }
369
385
 
370
386
  function _verifyCanSetPermissionsArray(
371
- bytes32 _key,
372
- bytes memory _value,
373
- address _from,
374
- bytes32 _permissions
387
+ bytes32 key,
388
+ bytes memory value,
389
+ address from,
390
+ bytes32 permissions
375
391
  ) internal view {
376
- uint256 arrayLength = uint256(bytes32(ERC725Y(target).getData(_key)));
377
- uint256 newLength = uint256(bytes32(_value));
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));
378
396
 
379
- if (newLength > arrayLength) {
380
- _requirePermissions(_from, _permissions, _PERMISSION_ADDPERMISSIONS);
381
- } else {
382
- _requirePermissions(_from, _permissions, _PERMISSION_CHANGEPERMISSIONS);
397
+ if (newLength > arrayLength) {
398
+ _requirePermissions(from, permissions, _PERMISSION_ADDPERMISSIONS);
399
+ } else {
400
+ _requirePermissions(from, permissions, _PERMISSION_CHANGEPERMISSIONS);
401
+ }
402
+
403
+ return;
383
404
  }
405
+
406
+ // key = AddressPermissions[index] -> array index
407
+ _requirePermissions(from, permissions, _PERMISSION_CHANGEPERMISSIONS);
384
408
  }
385
409
 
386
- function _verifyAllowedERC725YKeys(address _from, bytes32[] memory _inputKeys) internal view {
387
- bytes memory allowedERC725YKeysEncoded = ERC725Y(target).getAllowedERC725YKeysFor(_from);
410
+ function _verifyAllowedERC725YKeys(address from, bytes32[] memory inputKeys) internal view {
411
+ bytes memory allowedERC725YKeysEncoded = ERC725Y(target).getAllowedERC725YKeysFor(from);
388
412
 
389
413
  // whitelist any ERC725Y key
390
414
  if (
@@ -400,93 +424,79 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
400
424
  bytes32 mask;
401
425
 
402
426
  // loop through each allowed ERC725Y key retrieved from storage
403
- for (uint256 ii = 0; ii < allowedERC725YKeys.length; ii++) {
427
+ for (uint256 ii = 0; ii < allowedERC725YKeys.length; ii = GasLib.uncheckedIncrement(ii)) {
404
428
  // required to know which part of the input key to compare against the allowed key
405
- zeroBytesCount = _countZeroBytes(allowedERC725YKeys[ii]);
429
+ zeroBytesCount = _countTrailingZeroBytes(allowedERC725YKeys[ii]);
406
430
 
407
431
  // loop through each keys given as input
408
- for (uint256 jj = 0; jj < _inputKeys.length; jj++) {
409
- // skip permissions keys that have been previously "nulled"
410
- if (_inputKeys[jj] == bytes32(0)) continue;
411
-
412
- // solhint-disable no-inline-assembly
413
- assembly {
414
- // use a bitmask to discard the last `n` bytes of the input key
415
- // so to compare only the relevant parts of each ERC725Y keys
416
- //
417
- // `n = zeroBytesCount`
418
- //
419
- // eg:
420
- //
421
- // allowed key = 0xcafecafecafecafecafecafecafecafe00000000000000000000000000000000
422
- //
423
- // compare this part
424
- // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
425
- // input key = 0xcafecafecafecafecafecafecafecafe00000000000000000000000011223344
426
- //
427
- // & discard this part
428
- // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
429
- // mask = 0xffffffffffffffffffffffffffffffff00000000000000000000000000000000
430
- //
431
- // prettier-ignore
432
- mask := shl(mul(8, zeroBytesCount), 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)
433
- }
434
-
435
- if (allowedERC725YKeys[ii] == (_inputKeys[jj] & mask)) {
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)) {
436
449
  // if the input key matches the allowed key
437
450
  // make it null to mark it as allowed
438
- _inputKeys[jj] = bytes32(0);
451
+ inputKeys[jj] = bytes32(0);
439
452
  }
440
453
  }
441
454
  }
442
455
 
443
- for (uint256 ii = 0; ii < _inputKeys.length; ii++) {
444
- if (_inputKeys[ii] != bytes32(0)) revert NotAllowedERC725YKey(_from, _inputKeys[ii]);
456
+ for (uint256 ii = 0; ii < inputKeys.length; ii = GasLib.uncheckedIncrement(ii)) {
457
+ if (inputKeys[ii] != bytes32(0)) revert NotAllowedERC725YKey(from, inputKeys[ii]);
445
458
  }
446
459
  }
447
460
 
448
461
  /**
449
- * @dev verify if `_from` has the required permissions to make an external call
462
+ * @dev verify if `from` has the required permissions to make an external call
450
463
  * via the linked ERC725Account
451
- * @param _from the address who want to run the execute function on the ERC725Account
452
- * @param _permissions the permissions of the caller
453
- * @param _calldata the ABI encoded payload `target.execute(...)`
464
+ * @param from the address who want to run the execute function on the ERC725Account
465
+ * @param permissions the permissions of the caller
466
+ * @param payload the ABI encoded payload `target.execute(...)`
454
467
  */
455
468
  function _verifyCanExecute(
456
- address _from,
457
- bytes32 _permissions,
458
- bytes calldata _calldata
469
+ address from,
470
+ bytes32 permissions,
471
+ bytes calldata payload
459
472
  ) internal view {
460
- uint256 operationType = uint256(bytes32(_calldata[4:36]));
473
+ uint256 operationType = uint256(bytes32(payload[4:36]));
461
474
  require(operationType < 5, "LSP6KeyManager: invalid operation type");
462
475
 
463
- // TODO: if re-enable delegatecall, add check to ensure owner() + initialized() are not overriden after delegatecall
464
476
  require(
465
477
  operationType != OPERATION_DELEGATECALL,
466
478
  "LSP6KeyManager: operation DELEGATECALL is currently disallowed"
467
479
  );
468
480
 
469
- uint256 value = uint256(bytes32(_calldata[68:100]));
481
+ uint256 value = uint256(bytes32(payload[68:100]));
470
482
 
471
483
  // prettier-ignore
472
484
  bool isContractCreation = operationType == OPERATION_CREATE || operationType == OPERATION_CREATE2;
473
- bool isCallDataPresent = _calldata.length > 164;
485
+ bool isCallDataPresent = payload.length > 164;
474
486
 
475
487
  // SUPER operation only applies to contract call, not contract creation
476
488
  bool hasSuperOperation = isContractCreation
477
489
  ? false
478
- : _permissions.hasPermission(_extractSuperPermissionFromOperation(operationType));
490
+ : permissions.hasPermission(_extractSuperPermissionFromOperation(operationType));
479
491
 
480
- if (isCallDataPresent) {
481
- // prettier-ignore
482
- hasSuperOperation || _requirePermissions(_from, _permissions, _extractPermissionFromOperation(operationType));
492
+ if (isCallDataPresent && !hasSuperOperation) {
493
+ _requirePermissions(from, permissions, _extractPermissionFromOperation(operationType));
483
494
  }
484
495
 
485
- bool hasSuperTransferValue = _permissions.hasPermission(_PERMISSION_SUPER_TRANSFERVALUE);
496
+ bool hasSuperTransferValue = permissions.hasPermission(_PERMISSION_SUPER_TRANSFERVALUE);
486
497
 
487
- if (value > 0) {
488
- // prettier-ignore
489
- hasSuperTransferValue || _requirePermissions(_from, _permissions, _PERMISSION_TRANSFERVALUE);
498
+ if (value != 0 && !hasSuperTransferValue) {
499
+ _requirePermissions(from, permissions, _PERMISSION_TRANSFERVALUE);
490
500
  }
491
501
 
492
502
  // Skip on contract creation (CREATE or CREATE2)
@@ -496,60 +506,60 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
496
506
  if (hasSuperOperation && isCallDataPresent && value == 0) return;
497
507
 
498
508
  // Skip if caller has SUPER permission for value transfers
499
- if (hasSuperTransferValue && !isCallDataPresent && value > 0) return;
509
+ if (hasSuperTransferValue && !isCallDataPresent && value != 0) return;
500
510
 
501
511
  // Skip if both SUPER permissions are present
502
512
  if (hasSuperOperation && hasSuperTransferValue) return;
503
513
 
504
514
  // CHECK for ALLOWED ADDRESSES
505
- address to = address(bytes20(_calldata[48:68]));
506
- _verifyAllowedAddress(_from, to);
515
+ address to = address(bytes20(payload[48:68]));
516
+ _verifyAllowedAddress(from, to);
507
517
 
508
- if (to.code.length > 0) {
518
+ if (to.code.length != 0) {
509
519
  // CHECK for ALLOWED STANDARDS
510
- _verifyAllowedStandard(_from, to);
520
+ _verifyAllowedStandard(from, to);
511
521
 
512
522
  // CHECK for ALLOWED FUNCTIONS
513
523
  // extract bytes4 function selector from payload passed to ERC725X.execute(...)
514
- if (_calldata.length >= 168) _verifyAllowedFunction(_from, bytes4(_calldata[164:168]));
524
+ if (payload.length >= 168) _verifyAllowedFunction(from, bytes4(payload[164:168]));
515
525
  }
516
526
  }
517
527
 
518
528
  /**
519
529
  * @dev extract the required permission + a descriptive string, based on the `_operationType`
520
530
  * being run via ERC725Account.execute(...)
521
- * @param _operationType 0 = CALL, 1 = CREATE, 2 = CREATE2, etc... See ERC725X docs for more infos.
522
- * @return permissionsRequired_ (bytes32) the permission associated with the `_operationType`
531
+ * @param operationType 0 = CALL, 1 = CREATE, 2 = CREATE2, etc... See ERC725X docs for more infos.
532
+ * @return permissionsRequired (bytes32) the permission associated with the `_operationType`
523
533
  */
524
- function _extractPermissionFromOperation(uint256 _operationType)
534
+ function _extractPermissionFromOperation(uint256 operationType)
525
535
  internal
526
536
  pure
527
- returns (bytes32 permissionsRequired_)
537
+ returns (bytes32 permissionsRequired)
528
538
  {
529
- if (_operationType == OPERATION_CALL) return _PERMISSION_CALL;
530
- else if (_operationType == OPERATION_CREATE) return _PERMISSION_DEPLOY;
531
- else if (_operationType == OPERATION_CREATE2) return _PERMISSION_DEPLOY;
532
- else if (_operationType == OPERATION_STATICCALL) return _PERMISSION_STATICCALL;
533
- else if (_operationType == OPERATION_DELEGATECALL) return _PERMISSION_DELEGATECALL;
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;
534
544
  }
535
545
 
536
- function _extractSuperPermissionFromOperation(uint256 _operationType)
546
+ function _extractSuperPermissionFromOperation(uint256 operationType)
537
547
  internal
538
548
  pure
539
- returns (bytes32 superPermission_)
549
+ returns (bytes32 superPermission)
540
550
  {
541
- if (_operationType == OPERATION_CALL) return _PERMISSION_SUPER_CALL;
542
- else if (_operationType == OPERATION_STATICCALL) return _PERMISSION_SUPER_STATICCALL;
543
- else if (_operationType == OPERATION_DELEGATECALL) return _PERMISSION_SUPER_DELEGATECALL;
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;
544
554
  }
545
555
 
546
556
  /**
547
- * @dev verify if `_from` is authorised to interact with address `_to` via the linked ERC725Account
548
- * @param _from the caller address
549
- * @param _to the address to interact with
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
550
560
  */
551
- function _verifyAllowedAddress(address _from, address _to) internal view {
552
- bytes memory allowedAddresses = ERC725Y(target).getAllowedAddressesFor(_from);
561
+ function _verifyAllowedAddress(address from, address to) internal view {
562
+ bytes memory allowedAddresses = ERC725Y(target).getAllowedAddressesFor(from);
553
563
 
554
564
  // whitelist any address
555
565
  if (
@@ -561,20 +571,20 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
561
571
 
562
572
  address[] memory allowedAddressesList = abi.decode(allowedAddresses, (address[]));
563
573
 
564
- for (uint256 ii = 0; ii < allowedAddressesList.length; ii++) {
565
- if (_to == allowedAddressesList[ii]) return;
574
+ for (uint256 ii = 0; ii < allowedAddressesList.length; ii = GasLib.uncheckedIncrement(ii)) {
575
+ if (to == allowedAddressesList[ii]) return;
566
576
  }
567
- revert NotAllowedAddress(_from, _to);
577
+ revert NotAllowedAddress(from, to);
568
578
  }
569
579
 
570
580
  /**
571
- * @dev if `_from` is restricted to interact with contracts that implement a specific interface,
572
- * verify that `_to` implements one of these interface.
573
- * @param _from the caller address
574
- * @param _to the address of the contract to interact with
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
575
585
  */
576
- function _verifyAllowedStandard(address _from, address _to) internal view {
577
- bytes memory allowedStandards = ERC725Y(target).getAllowedStandardsFor(_from);
586
+ function _verifyAllowedStandard(address from, address to) internal view {
587
+ bytes memory allowedStandards = ERC725Y(target).getAllowedStandardsFor(from);
578
588
 
579
589
  // whitelist any standard interface (ERC165)
580
590
  if (
@@ -586,21 +596,21 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
586
596
 
587
597
  bytes4[] memory allowedStandardsList = abi.decode(allowedStandards, (bytes4[]));
588
598
 
589
- for (uint256 ii = 0; ii < allowedStandardsList.length; ii++) {
590
- if (_to.supportsERC165Interface(allowedStandardsList[ii])) return;
599
+ for (uint256 ii = 0; ii < allowedStandardsList.length; ii = GasLib.uncheckedIncrement(ii)) {
600
+ if (to.supportsERC165Interface(allowedStandardsList[ii])) return;
591
601
  }
592
- revert("Not Allowed Standards");
602
+ revert NotAllowedStandard(from, to);
593
603
  }
594
604
 
595
605
  /**
596
- * @dev verify if `_from` is authorised to use the linked ERC725Account
597
- * to run a specific function `_functionSelector` at a target contract
598
- * @param _from the caller address
599
- * @param _functionSelector the bytes4 function selector of the function to run
606
+ * @dev verify if `from` is authorised to use the linked ERC725Account
607
+ * to run a specific function `functionSelector` at a target contract
608
+ * @param from the caller address
609
+ * @param functionSelector the bytes4 function selector of the function to run
600
610
  * at the target contract
601
611
  */
602
- function _verifyAllowedFunction(address _from, bytes4 _functionSelector) internal view {
603
- bytes memory allowedFunctions = ERC725Y(target).getAllowedFunctionsFor(_from);
612
+ function _verifyAllowedFunction(address from, bytes4 functionSelector) internal view {
613
+ bytes memory allowedFunctions = ERC725Y(target).getAllowedFunctionsFor(from);
604
614
 
605
615
  // whitelist any function
606
616
  if (
@@ -612,48 +622,47 @@ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
612
622
 
613
623
  bytes4[] memory allowedFunctionsList = abi.decode(allowedFunctions, (bytes4[]));
614
624
 
615
- for (uint256 ii = 0; ii < allowedFunctionsList.length; ii++) {
616
- if (_functionSelector == allowedFunctionsList[ii]) return;
625
+ for (uint256 ii = 0; ii < allowedFunctionsList.length; ii = GasLib.uncheckedIncrement(ii)) {
626
+ if (functionSelector == allowedFunctionsList[ii]) return;
617
627
  }
618
- revert NotAllowedFunction(_from, _functionSelector);
628
+ revert NotAllowedFunction(from, functionSelector);
619
629
  }
620
630
 
621
- function _countZeroBytes(bytes32 _key) internal pure returns (uint256) {
622
- uint256 index = 31;
631
+ function _countTrailingZeroBytes(bytes32 dataKey) internal pure returns (uint256) {
632
+ uint256 nByte = 32;
623
633
 
624
- // CHECK each bytes of the key, starting from the end (right to left)
625
- // skip the empty bytes `0x00` to find the first non-empty bytes
626
- while (_key[index] == 0x00) index--;
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--;
627
637
 
628
- return 32 - (index + 1);
638
+ return 32 - nByte;
629
639
  }
630
640
 
631
641
  function _requirePermissions(
632
- address _from,
633
- bytes32 _addressPermissions,
634
- bytes32 _permissionRequired
635
- ) internal pure returns (bool) {
636
- if (!_addressPermissions.hasPermission(_permissionRequired)) {
637
- string memory permissionErrorString = _getPermissionErrorString(_permissionRequired);
638
- revert NotAuthorised(_from, permissionErrorString);
642
+ address from,
643
+ bytes32 addressPermissions,
644
+ bytes32 permissionRequired
645
+ ) internal pure {
646
+ if (!addressPermissions.hasPermission(permissionRequired)) {
647
+ string memory permissionErrorString = _getPermissionErrorString(permissionRequired);
648
+ revert NotAuthorised(from, permissionErrorString);
639
649
  }
640
650
  }
641
651
 
642
- function _getPermissionErrorString(bytes32 _permission)
652
+ function _getPermissionErrorString(bytes32 permission)
643
653
  internal
644
654
  pure
645
655
  returns (string memory errorMessage)
646
656
  {
647
- if (_permission == _PERMISSION_CHANGEOWNER) return "TRANSFEROWNERSHIP";
648
- if (_permission == _PERMISSION_CHANGEPERMISSIONS) return "CHANGEPERMISSIONS";
649
- if (_permission == _PERMISSION_ADDPERMISSIONS) return "ADDPERMISSIONS";
650
- if (_permission == _PERMISSION_SETDATA) return "SETDATA";
651
- if (_permission == _PERMISSION_CALL) return "CALL";
652
- if (_permission == _PERMISSION_STATICCALL) return "STATICCALL";
653
- if (_permission == _PERMISSION_DELEGATECALL) return "DELEGATECALL";
654
- // TODO: add support to display CREATE or CREATE2 in the revert error
655
- if (_permission == _PERMISSION_DEPLOY) return "DEPLOY";
656
- if (_permission == _PERMISSION_TRANSFERVALUE) return "TRANSFERVALUE";
657
- if (_permission == _PERMISSION_SIGN) return "SIGN";
657
+ if (permission == _PERMISSION_CHANGEOWNER) return "TRANSFEROWNERSHIP";
658
+ if (permission == _PERMISSION_CHANGEPERMISSIONS) return "CHANGEPERMISSIONS";
659
+ if (permission == _PERMISSION_ADDPERMISSIONS) return "ADDPERMISSIONS";
660
+ if (permission == _PERMISSION_SETDATA) return "SETDATA";
661
+ if (permission == _PERMISSION_CALL) return "CALL";
662
+ if (permission == _PERMISSION_STATICCALL) return "STATICCALL";
663
+ if (permission == _PERMISSION_DELEGATECALL) return "DELEGATECALL";
664
+ if (permission == _PERMISSION_DEPLOY) return "DEPLOY";
665
+ if (permission == _PERMISSION_TRANSFERVALUE) return "TRANSFERVALUE";
666
+ if (permission == _PERMISSION_SIGN) return "SIGN";
658
667
  }
659
668
  }