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