@lukso/lsp-smart-contracts 0.4.3 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/README.md +38 -9
  2. package/artifacts/LSP0ERC725Account.json +398 -0
  3. package/artifacts/LSP0ERC725AccountInit.json +439 -0
  4. package/artifacts/LSP1UniversalReceiverDelegateUP.json +7 -7
  5. package/artifacts/LSP1UniversalReceiverDelegateVault.json +7 -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 +50 -28
  10. package/artifacts/LSP6KeyManagerInit.json +58 -23
  11. package/artifacts/LSP7CappedSupply.json +136 -11
  12. package/artifacts/LSP7CappedSupplyInit.json +163 -35
  13. package/artifacts/LSP7CompatibilityForERC20.json +204 -13
  14. package/artifacts/LSP7CompatibilityForERC20Init.json +727 -0
  15. package/artifacts/LSP7DigitalAsset.json +128 -13
  16. package/artifacts/LSP7DigitalAssetInit.json +152 -34
  17. package/artifacts/LSP7Mintable.json +129 -14
  18. package/artifacts/LSP7MintableInit.json +153 -35
  19. package/artifacts/LSP8CappedSupply.json +137 -11
  20. package/artifacts/LSP8CappedSupplyInit.json +172 -20
  21. package/artifacts/LSP8CompatibilityForERC721.json +138 -17
  22. package/artifacts/LSP8CompatibilityForERC721Init.json +873 -0
  23. package/artifacts/LSP8IdentifiableDigitalAsset.json +129 -13
  24. package/artifacts/LSP8IdentifiableDigitalAssetInit.json +161 -19
  25. package/artifacts/LSP8Mintable.json +141 -14
  26. package/artifacts/LSP8MintableInit.json +173 -20
  27. package/artifacts/LSP9Vault.json +374 -0
  28. package/artifacts/LSP9VaultInit.json +415 -0
  29. package/artifacts/UniversalFactory.json +127 -0
  30. package/artifacts/UniversalProfile.json +78 -53
  31. package/artifacts/UniversalProfileInit.json +117 -53
  32. package/artifacts/UniversalReceiverAddressStore.json +209 -0
  33. package/constants.js +500 -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 +92 -48
  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/ILSP1UniversalReceiver.sol +3 -0
  45. package/contracts/LSP1UniversalReceiver/ILSP1UniversalReceiverDelegate.sol +4 -2
  46. package/contracts/LSP1UniversalReceiver/LSP1Constants.sol +3 -9
  47. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateUP/Handling/TokenAndVaultHandling.sol +61 -165
  48. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateUP/LSP1UniversalReceiverDelegateUP.sol +49 -6
  49. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateVault/Handling/TokenHandling.sol +50 -90
  50. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateVault/LSP1UniversalReceiverDelegateVault.sol +40 -6
  51. package/contracts/LSP1UniversalReceiver/LSP1Utils.sol +48 -0
  52. package/contracts/LSP2ERC725YJSONSchema/LSP2Utils.sol +205 -0
  53. package/contracts/LSP3UniversalProfile/LSP3Constants.sol +11 -0
  54. package/contracts/LSP4DigitalAssetMetadata/ILSP4Compatibility.sol +1 -1
  55. package/contracts/LSP4DigitalAssetMetadata/LSP4Compatibility.sol +6 -15
  56. package/contracts/LSP4DigitalAssetMetadata/LSP4Constants.sol +10 -4
  57. package/contracts/LSP4DigitalAssetMetadata/LSP4DigitalAssetMetadata.sol +11 -8
  58. package/contracts/LSP4DigitalAssetMetadata/LSP4DigitalAssetMetadataInit.sol +9 -13
  59. package/contracts/LSP4DigitalAssetMetadata/LSP4DigitalAssetMetadataInitAbstract.sol +14 -20
  60. package/contracts/LSP5ReceivedAssets/LSP5Constants.sol +10 -0
  61. package/contracts/LSP5ReceivedAssets/LSP5Utils.sol +205 -0
  62. package/contracts/LSP6KeyManager/ILSP6KeyManager.sol +21 -18
  63. package/contracts/LSP6KeyManager/LSP6Constants.sol +61 -24
  64. package/contracts/LSP6KeyManager/LSP6Errors.sol +37 -0
  65. package/contracts/LSP6KeyManager/LSP6KeyManager.sol +3 -4
  66. package/contracts/LSP6KeyManager/LSP6KeyManagerCore.sol +475 -260
  67. package/contracts/LSP6KeyManager/LSP6KeyManagerInit.sol +6 -5
  68. package/contracts/LSP6KeyManager/LSP6KeyManagerInitAbstract.sol +7 -15
  69. package/contracts/LSP6KeyManager/LSP6Utils.sol +111 -0
  70. package/contracts/LSP7DigitalAsset/ILSP7DigitalAsset.sol +3 -8
  71. package/contracts/LSP7DigitalAsset/LSP7Constants.sol +5 -4
  72. package/contracts/LSP7DigitalAsset/LSP7DigitalAsset.sol +22 -8
  73. package/contracts/LSP7DigitalAsset/LSP7DigitalAssetCore.sol +70 -96
  74. package/contracts/LSP7DigitalAsset/LSP7DigitalAssetInit.sol +8 -9
  75. package/contracts/LSP7DigitalAsset/LSP7DigitalAssetInitAbstract.sol +23 -21
  76. package/contracts/LSP7DigitalAsset/LSP7Errors.sol +23 -0
  77. package/contracts/LSP7DigitalAsset/extensions/ILSP7CappedSupply.sol +1 -1
  78. package/contracts/LSP7DigitalAsset/extensions/ILSP7CompatibilityForERC20.sol +22 -6
  79. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupply.sol +7 -6
  80. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupplyCore.sol +14 -12
  81. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupplyInit.sol +3 -8
  82. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupplyInitAbstract.sol +9 -17
  83. package/contracts/LSP7DigitalAsset/extensions/LSP7CompatibilityForERC20.sol +37 -45
  84. package/contracts/LSP7DigitalAsset/extensions/LSP7CompatibilityForERC20Core.sol +107 -0
  85. package/contracts/LSP7DigitalAsset/extensions/LSP7CompatibilityForERC20Init.sol +22 -0
  86. package/contracts/LSP7DigitalAsset/extensions/LSP7CompatibilityForERC20InitAbstract.sol +66 -0
  87. package/contracts/LSP7DigitalAsset/{extensions → presets}/ILSP7Mintable.sol +1 -1
  88. package/contracts/LSP7DigitalAsset/{extensions → presets}/LSP7Mintable.sol +4 -3
  89. package/contracts/LSP7DigitalAsset/{extensions → presets}/LSP7MintableCore.sol +5 -5
  90. package/contracts/LSP7DigitalAsset/{extensions → presets}/LSP7MintableInit.sol +3 -2
  91. package/contracts/LSP7DigitalAsset/presets/LSP7MintableInitAbstract.sol +34 -0
  92. package/contracts/LSP8IdentifiableDigitalAsset/ILSP8IdentifiableDigitalAsset.sol +5 -16
  93. package/contracts/LSP8IdentifiableDigitalAsset/LSP8Constants.sol +6 -3
  94. package/contracts/LSP8IdentifiableDigitalAsset/LSP8Errors.sol +22 -0
  95. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAsset.sol +21 -8
  96. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAssetCore.sol +102 -135
  97. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAssetInit.sol +8 -11
  98. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAssetInitAbstract.sol +22 -19
  99. package/contracts/LSP8IdentifiableDigitalAsset/extensions/ILSP8CappedSupply.sol +1 -1
  100. package/contracts/LSP8IdentifiableDigitalAsset/extensions/ILSP8CompatibilityForERC721.sol +17 -22
  101. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupply.sol +9 -15
  102. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupplyCore.sol +14 -12
  103. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupplyInit.sol +3 -8
  104. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupplyInitAbstract.sol +9 -18
  105. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibilityForERC721.sol +37 -189
  106. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibilityForERC721Core.sol +187 -0
  107. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibilityForERC721Init.sol +25 -0
  108. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibilityForERC721InitAbstract.sol +89 -0
  109. package/contracts/LSP8IdentifiableDigitalAsset/{extensions → presets}/ILSP8Mintable.sol +1 -1
  110. package/contracts/LSP8IdentifiableDigitalAsset/{extensions → presets}/LSP8Mintable.sol +4 -3
  111. package/contracts/LSP8IdentifiableDigitalAsset/{extensions → presets}/LSP8MintableCore.sol +5 -8
  112. package/contracts/LSP8IdentifiableDigitalAsset/{extensions → presets}/LSP8MintableInit.sol +4 -3
  113. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8MintableInitAbstract.sol +35 -0
  114. package/contracts/LSP9Vault/LSP9Constants.sol +14 -5
  115. package/contracts/LSP9Vault/LSP9Vault.sol +15 -37
  116. package/contracts/LSP9Vault/LSP9VaultCore.sol +105 -85
  117. package/contracts/LSP9Vault/LSP9VaultInit.sol +5 -4
  118. package/contracts/LSP9Vault/LSP9VaultInitAbstract.sol +15 -45
  119. package/contracts/Legacy/Registries/AddressRegistry.sol +43 -0
  120. package/contracts/Legacy/Registries/AddressRegistryRequiresERC725.sol +33 -0
  121. package/contracts/Legacy/UniversalReceiverAddressStore.sol +60 -0
  122. package/contracts/UniversalProfile.sol +11 -20
  123. package/contracts/UniversalProfileInit.sol +5 -4
  124. package/contracts/UniversalProfileInitAbstract.sol +14 -35
  125. package/contracts/Utils/UtilsLib.sol +47 -11
  126. package/package.json +23 -9
  127. package/artifacts/LSP1UniversalReceiverDelegateUPInit.json +0 -66
  128. package/artifacts/LSP1UniversalReceiverDelegateVaultInit.json +0 -66
  129. package/constants.ts +0 -350
  130. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateUP/LSP1UniversalReceiverDelegateUPCore.sol +0 -52
  131. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateUP/LSP1UniversalReceiverDelegateUPInit.sol +0 -26
  132. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateUP/LSP1UniversalReceiverDelegateUPInitAbstract.sol +0 -28
  133. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateVault/LSP1UniversalReceiverDelegateVaultCore.sol +0 -41
  134. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateVault/LSP1UniversalReceiverDelegateVaultInit.sol +0 -23
  135. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateVault/LSP1UniversalReceiverDelegateVaultInitAbstract.sol +0 -25
  136. package/contracts/LSP7DigitalAsset/extensions/LSP7MintableInitAbstract.sol +0 -47
  137. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8MintableInitAbstract.sol +0 -44
  138. package/contracts/UniversalProfileCore.sol +0 -56
  139. package/contracts/Utils/ERC725Utils.sol +0 -169
  140. package/contracts/Utils/LSP2Utils.sol +0 -127
  141. package/contracts/Utils/LSP6Utils.sol +0 -77
@@ -1,87 +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";
24
+ // errors
25
+ import "./LSP6Errors.sol";
19
26
 
20
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";
21
37
  import "./LSP6Constants.sol";
22
- import "../LSP0ERC725Account/LSP0Constants.sol";
23
- import "@erc725/smart-contracts/contracts/constants.sol";
24
-
25
- /**
26
- * @dev address `from` is not authorised to `permission`
27
- * @param permission permission required
28
- * @param from address not-authorised
29
- */
30
- error NotAuthorised(address from, string permission);
31
-
32
- /**
33
- * @dev address `from` is not authorised to interact with `disallowedAddress` via account
34
- * @param from address making the request
35
- * @param disallowedAddress address that `from` is not authorised to call
36
- */
37
- error NotAllowedAddress(address from, address disallowedAddress);
38
-
39
- /**
40
- * @dev address `from` is not authorised to run `disallowedFunction` via account
41
- * @param from address making the request
42
- * @param disallowedFunction bytes4 function selector that `from` is not authorised to run
43
- */
44
- error NotAllowedFunction(address from, bytes4 disallowedFunction);
45
38
 
46
39
  /**
47
40
  * @title Core implementation of a contract acting as a controller of an ERC725 Account, using permissions stored in the ERC725Y storage
48
- * @author Fabian Vogelsteller, Jean Cavallera
41
+ * @author Fabian Vogelsteller <frozeman>, Jean Cavallera (CJ42), Yamen Merhi (YamenMerhi)
49
42
  * @dev all the permissions can be set on the ERC725 Account using `setData(...)` with the keys constants below
50
43
  */
51
- abstract contract LSP6KeyManagerCore is ILSP6KeyManager, ERC165Storage {
52
- using ERC725Utils for ERC725Y;
53
- using LSP6Utils for ERC725;
44
+ abstract contract LSP6KeyManagerCore is ERC165, ILSP6KeyManager {
45
+ using LSP2Utils for *;
46
+ using LSP6Utils for *;
54
47
  using Address for address;
55
48
  using ECDSA for bytes32;
56
49
  using ERC165Checker for address;
57
50
 
58
- ERC725 public account;
51
+ address public override target;
59
52
  mapping(address => mapping(uint256 => uint256)) internal _nonceStore;
60
53
 
61
54
  /**
62
55
  * @dev See {IERC165-supportsInterface}.
63
56
  */
64
- function supportsInterface(bytes4 interfaceId)
65
- public
66
- view
67
- virtual
68
- override(ERC165Storage)
69
- returns (bool)
70
- {
57
+ function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
71
58
  return
72
- interfaceId == _INTERFACE_ID_ERC1271 ||
59
+ interfaceId == _INTERFACEID_LSP6 ||
60
+ interfaceId == _INTERFACEID_ERC1271 ||
73
61
  super.supportsInterface(interfaceId);
74
62
  }
75
63
 
76
64
  /**
77
65
  * @inheritdoc ILSP6KeyManager
78
66
  */
79
- function getNonce(address _from, uint256 _channel)
80
- public
81
- view
82
- override
83
- returns (uint256)
84
- {
67
+ function getNonce(address _from, uint256 _channel) public view override returns (uint256) {
85
68
  uint128 nonceId = uint128(_nonceStore[_from][_channel]);
86
69
  return (uint256(_channel) << 128) | nonceId;
87
70
  }
@@ -95,99 +78,69 @@ abstract contract LSP6KeyManagerCore is ILSP6KeyManager, ERC165Storage {
95
78
  override
96
79
  returns (bytes4 magicValue)
97
80
  {
98
- address recoveredAddress = ECDSA.recover(_hash, _signature);
99
- return
100
- (_PERMISSION_SIGN & account.getPermissionsFor(recoveredAddress)) ==
101
- _PERMISSION_SIGN
102
- ? _INTERFACE_ID_ERC1271
103
- : _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
+ );
104
88
  }
105
89
 
106
90
  /**
107
91
  * @inheritdoc ILSP6KeyManager
108
92
  */
109
- function execute(bytes calldata _data)
110
- external
111
- payable
112
- override
113
- returns (bytes memory)
114
- {
115
- _verifyPermissions(msg.sender, _data);
93
+ function execute(bytes calldata _calldata) external payable override returns (bytes memory) {
94
+ _verifyPermissions(msg.sender, _calldata);
116
95
 
117
96
  // solhint-disable avoid-low-level-calls
118
- (bool success, bytes memory result_) = address(account).call{
119
- value: msg.value,
120
- gas: gasleft()
121
- }(_data);
97
+ (bool success, bytes memory result) = target.call{value: msg.value, gas: gasleft()}(
98
+ _calldata
99
+ );
122
100
 
123
101
  if (!success) {
124
- // solhint-disable reason-string
125
- if (result_.length < 68) revert();
126
-
127
- // solhint-disable no-inline-assembly
128
- assembly {
129
- result_ := add(result_, 0x04)
130
- }
131
- revert(abi.decode(result_, (string)));
102
+ ErrorHandlerLib.revertWithParsedError(result);
132
103
  }
133
104
 
134
- emit Executed(msg.value, _data);
135
- 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;
136
107
  }
137
108
 
138
109
  /**
139
110
  * @inheritdoc ILSP6KeyManager
140
111
  */
141
112
  function executeRelayCall(
142
- address _signedFor,
113
+ bytes memory _signature,
143
114
  uint256 _nonce,
144
- bytes calldata _data,
145
- bytes memory _signature
115
+ bytes calldata _calldata
146
116
  ) external payable override returns (bytes memory) {
147
- require(
148
- _signedFor == address(this),
149
- "executeRelayCall: Message not signed for this keyManager"
150
- );
151
-
152
117
  bytes memory blob = abi.encodePacked(
118
+ block.chainid,
153
119
  address(this), // needs to be signed for this keyManager
154
120
  _nonce,
155
- _data
121
+ _calldata
156
122
  );
157
123
 
158
- address signer = keccak256(blob).toEthSignedMessageHash().recover(
159
- _signature
160
- );
124
+ address signer = keccak256(blob).toEthSignedMessageHash().recover(_signature);
161
125
 
162
- require(
163
- _isValidNonce(signer, _nonce),
164
- "executeRelayCall: Invalid nonce"
165
- );
126
+ require(_isValidNonce(signer, _nonce), "executeRelayCall: Invalid nonce");
166
127
 
167
128
  // increase nonce after successful verification
168
129
  _nonceStore[signer][_nonce >> 128]++;
169
130
 
170
- _verifyPermissions(signer, _data);
131
+ _verifyPermissions(signer, _calldata);
171
132
 
172
133
  // solhint-disable avoid-low-level-calls
173
- (bool success, bytes memory result_) = address(account).call{
174
- value: 0,
175
- gas: gasleft()
176
- }(_data);
134
+ (bool success, bytes memory result) = target.call{value: msg.value, gas: gasleft()}(
135
+ _calldata
136
+ );
177
137
 
178
138
  if (!success) {
179
- // solhint-disable reason-string
180
- if (result_.length < 68) revert();
181
-
182
- // solhint-disable no-inline-assembly
183
- assembly {
184
- result_ := add(result_, 0x04)
185
- }
186
- revert(abi.decode(result_, (string)));
139
+ ErrorHandlerLib.revertWithParsedError(result);
187
140
  }
188
141
 
189
- emit Executed(msg.value, _data);
190
- 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;
191
144
  }
192
145
 
193
146
  /**
@@ -198,11 +151,7 @@ abstract contract LSP6KeyManagerCore is ILSP6KeyManager, ERC165Storage {
198
151
  * @param _from caller address
199
152
  * @param _idx (channel id + nonce within the channel)
200
153
  */
201
- function _isValidNonce(address _from, uint256 _idx)
202
- internal
203
- view
204
- returns (bool)
205
- {
154
+ function _isValidNonce(address _from, uint256 _idx) internal view returns (bool) {
206
155
  // idx % (1 << 128) = nonce
207
156
  // (idx >> 128) = channel
208
157
  // equivalent to: return (nonce == _nonceStore[_from][channel]
@@ -210,39 +159,84 @@ abstract contract LSP6KeyManagerCore is ILSP6KeyManager, ERC165Storage {
210
159
  }
211
160
 
212
161
  /**
213
- * @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`
214
163
  * @param _from the address making the request
215
- * @param _data the payload that will be run on `account`
164
+ * @param _calldata the payload that will be run on `target`
216
165
  */
217
- function _verifyPermissions(address _from, bytes calldata _data)
218
- internal
219
- view
220
- {
221
- 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);
171
+
172
+ if (permissions == bytes32(0)) revert NoPermissionsSet(_from);
222
173
 
223
- if (erc725Function == account.setData.selector) {
224
- _verifyCanSetData(_from, _data);
225
- } else if (erc725Function == account.execute.selector) {
226
- _verifyCanExecute(_from, _data);
174
+ // prettier-ignore
175
+ if (erc725Function == setDataSingleSelector) {
227
176
 
228
- address to = address(bytes20(_data[48:68]));
229
- _verifyAllowedAddress(_from, to);
177
+ (bytes32 inputKey, bytes memory inputValue) = abi.decode(_calldata[4:], (bytes32, bytes));
230
178
 
231
- if (to.isContract()) {
232
- _verifyAllowedStandard(_from, to);
179
+ if (
180
+ // CHECK for permission keys
181
+ bytes6(inputKey) == _LSP6KEY_ADDRESSPERMISSIONS_PREFIX ||
182
+ bytes16(inputKey) == _LSP6KEY_ADDRESSPERMISSIONS_ARRAY_PREFIX
183
+ ) {
233
184
 
234
- if (_data.length >= 168) {
235
- // extract bytes4 function selector from payload
236
- _verifyAllowedFunction(_from, bytes4(_data[164:168]));
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;
237
219
  }
220
+
221
+ }
222
+
223
+ if (isSettingERC725YKeys) {
224
+ _verifyCanSetData(_from, permissions, inputKeys);
238
225
  }
239
- } else if (erc725Function == account.transferOwnership.selector) {
240
- bytes32 permissions = account.getPermissionsFor(_from);
241
226
 
242
- _hasPermission(_PERMISSION_CHANGEOWNER, permissions) ||
243
- _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
+
244
238
  } else {
245
- revert("_verifyPermissions: unknown ERC725 selector");
239
+ revert("_verifyPermissions: invalid ERC725 selector");
246
240
  }
247
241
  }
248
242
 
@@ -250,42 +244,204 @@ abstract contract LSP6KeyManagerCore is ILSP6KeyManager, ERC165Storage {
250
244
  * @dev verify if `_from` has the required permissions to set some keys
251
245
  * on the linked ERC725Account
252
246
  * @param _from the address who want to set the keys
253
- * @param _data the ABI encoded payload `account.setData(keys, values)`
247
+ * @param _permissions the permissions
248
+ * @param _inputKeys the data keys being set
254
249
  * containing a list of keys-value pairs
255
250
  */
256
- function _verifyCanSetData(address _from, bytes calldata _data)
257
- internal
258
- view
259
- {
260
- 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;
261
258
 
262
- uint256 keyCount = uint256(bytes32(_data[68:100]));
263
- uint256 pointer = 100;
259
+ _requirePermissions(_from, _permissions, _PERMISSION_SETDATA);
264
260
 
265
- // loop through the keys
266
- for (uint256 ii = 0; ii < keyCount; ii++) {
267
- bytes32 key = bytes32(_data[pointer:pointer + 32]);
261
+ _verifyAllowedERC725YKeys(_from, _inputKeys);
262
+ }
268
263
 
269
- // check if the key is related to setting permissions
270
- if (bytes8(key) == _SET_PERMISSIONS) {
271
- bool isNewAddress = bytes32(
272
- ERC725Y(account).getDataSingle(key)
273
- ) == bytes32(0);
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) {
274
277
 
275
- if (isNewAddress) {
276
- _hasPermission(_PERMISSION_ADDPERMISSIONS, permissions) ||
277
- _notAuthorised(_from, "ADDPERMISSIONS");
278
- } else {
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
+ );
293
+
294
+ bytes memory storedAllowedAddresses = ERC725Y(target).getData(_key);
295
+
296
+ if (storedAllowedAddresses.length == 0) {
297
+
298
+ _requirePermissions(_from, _permissions, _PERMISSION_ADDPERMISSIONS);
299
+
300
+ } else {
301
+
302
+ _requirePermissions(_from, _permissions, _PERMISSION_CHANGEPERMISSIONS);
303
+
304
+ }
305
+
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
+ }
349
+
350
+ }
351
+ }
352
+
353
+ function _verifyCanSetBytes32Permissions(
354
+ bytes32 _key,
355
+ address _from,
356
+ bytes32 _callerPermissions
357
+ ) internal view {
358
+ if (bytes32(ERC725Y(target).getData(_key)) == bytes32(0)) {
359
+ // if there is nothing stored under this data key,
360
+ // we are trying to ADD permissions for a NEW address
361
+ _requirePermissions(_from, _callerPermissions, _PERMISSION_ADDPERMISSIONS);
362
+ } else {
363
+ // if there are already some permissions stored under this data key,
364
+ // we are trying to CHANGE the permissions of an address
365
+ // (that has already some EXISTING permissions set)
366
+ _requirePermissions(_from, _callerPermissions, _PERMISSION_CHANGEPERMISSIONS);
367
+ }
368
+ }
369
+
370
+ function _verifyCanSetPermissionsArray(
371
+ bytes32 _key,
372
+ bytes memory _value,
373
+ address _from,
374
+ bytes32 _permissions
375
+ ) internal view {
376
+ uint256 arrayLength = uint256(bytes32(ERC725Y(target).getData(_key)));
377
+ uint256 newLength = uint256(bytes32(_value));
378
+
379
+ if (newLength > arrayLength) {
380
+ _requirePermissions(_from, _permissions, _PERMISSION_ADDPERMISSIONS);
381
+ } else {
382
+ _requirePermissions(_from, _permissions, _PERMISSION_CHANGEPERMISSIONS);
383
+ }
384
+ }
385
+
386
+ function _verifyAllowedERC725YKeys(address _from, bytes32[] memory _inputKeys) internal view {
387
+ bytes memory allowedERC725YKeysEncoded = ERC725Y(target).getAllowedERC725YKeysFor(_from);
388
+
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;
396
+
397
+ bytes32[] memory allowedERC725YKeys = abi.decode(allowedERC725YKeysEncoded, (bytes32[]));
398
+
399
+ uint256 zeroBytesCount;
400
+ bytes32 mask;
401
+
402
+ // loop through each allowed ERC725Y key retrieved from storage
403
+ for (uint256 ii = 0; ii < allowedERC725YKeys.length; ii++) {
404
+ // required to know which part of the input key to compare against the allowed key
405
+ zeroBytesCount = _countZeroBytes(allowedERC725YKeys[ii]);
406
+
407
+ // 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
+ //
279
431
  // prettier-ignore
280
- _hasPermission(_PERMISSION_CHANGEPERMISSIONS, permissions) ||
281
- _notAuthorised(_from, "CHANGEPERMISSIONS");
432
+ mask := shl(mul(8, zeroBytesCount), 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)
433
+ }
434
+
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);
282
439
  }
283
- } else {
284
- _hasPermission(_PERMISSION_SETDATA, permissions) ||
285
- _notAuthorised(_from, "SETDATA");
286
440
  }
441
+ }
287
442
 
288
- pointer += 32; // move calldata pointer
443
+ for (uint256 ii = 0; ii < _inputKeys.length; ii++) {
444
+ if (_inputKeys[ii] != bytes32(0)) revert NotAllowedERC725YKey(_from, _inputKeys[ii]);
289
445
  }
290
446
  }
291
447
 
@@ -293,52 +449,117 @@ abstract contract LSP6KeyManagerCore is ILSP6KeyManager, ERC165Storage {
293
449
  * @dev verify if `_from` has the required permissions to make an external call
294
450
  * via the linked ERC725Account
295
451
  * @param _from the address who want to run the execute function on the ERC725Account
296
- * @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(...)`
297
454
  */
298
- function _verifyCanExecute(address _from, bytes calldata _data)
299
- internal
300
- view
301
- {
302
- bytes32 permissions = account.getPermissionsFor(_from);
303
-
304
- uint256 operationType = uint256(bytes32(_data[4:36]));
305
- uint256 value = uint256(bytes32(_data[68:100]));
306
-
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");
462
+
463
+ // TODO: if re-enable delegatecall, add check to ensure owner() + initialized() are not overriden after delegatecall
307
464
  require(
308
- operationType != 4,
309
- "_verifyCanExecute: operation 4 `DELEGATECALL` not supported"
465
+ operationType != OPERATION_DELEGATECALL,
466
+ "LSP6KeyManager: operation DELEGATECALL is currently disallowed"
310
467
  );
311
468
 
312
- (
313
- bytes32 permissionRequired,
314
- string memory operationName
315
- ) = _extractPermissionFromOperation(operationType);
469
+ uint256 value = uint256(bytes32(_calldata[68:100]));
316
470
 
317
- _hasPermission(permissionRequired, permissions) ||
318
- _notAuthorised(_from, operationName);
471
+ // prettier-ignore
472
+ bool isContractCreation = operationType == OPERATION_CREATE || operationType == OPERATION_CREATE2;
473
+ bool isCallDataPresent = _calldata.length > 164;
319
474
 
320
- if (
321
- (value > 0) &&
322
- !_hasPermission(_PERMISSION_TRANSFERVALUE, permissions)
323
- ) _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]));
515
+ }
324
516
  }
325
517
 
326
518
  /**
327
- * @dev verify if `_from` is authorised to use the linked ERC725Account
328
- * to interact with address `_to`
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
+
546
+ /**
547
+ * @dev verify if `_from` is authorised to interact with address `_to` via the linked ERC725Account
329
548
  * @param _from the caller address
330
549
  * @param _to the address to interact with
331
550
  */
332
551
  function _verifyAllowedAddress(address _from, address _to) internal view {
333
- bytes memory allowedAddresses = account.getAllowedAddressesFor(_from);
552
+ bytes memory allowedAddresses = ERC725Y(target).getAllowedAddressesFor(_from);
334
553
 
335
- // whitelist any address if nothing in the list
336
- 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;
337
561
 
338
- address[] memory allowedAddressesList = abi.decode(
339
- allowedAddresses,
340
- (address[])
341
- );
562
+ address[] memory allowedAddressesList = abi.decode(allowedAddresses, (address[]));
342
563
 
343
564
  for (uint256 ii = 0; ii < allowedAddressesList.length; ii++) {
344
565
  if (_to == allowedAddressesList[ii]) return;
@@ -346,19 +567,27 @@ abstract contract LSP6KeyManagerCore is ILSP6KeyManager, ERC165Storage {
346
567
  revert NotAllowedAddress(_from, _to);
347
568
  }
348
569
 
349
- function _verifyAllowedStandard(address _from, address to) internal view {
350
- bytes memory allowedStandards = account.getAllowedStandardsFor(_from);
570
+ /**
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
575
+ */
576
+ function _verifyAllowedStandard(address _from, address _to) internal view {
577
+ bytes memory allowedStandards = ERC725Y(target).getAllowedStandardsFor(_from);
351
578
 
352
- // whitelist any standard interface if nothing in the list
353
- 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;
354
586
 
355
- bytes4[] memory allowedStandardsList = abi.decode(
356
- allowedStandards,
357
- (bytes4[])
358
- );
587
+ bytes4[] memory allowedStandardsList = abi.decode(allowedStandards, (bytes4[]));
359
588
 
360
589
  for (uint256 ii = 0; ii < allowedStandardsList.length; ii++) {
361
- if (to.supportsInterface(allowedStandardsList[ii])) return;
590
+ if (_to.supportsERC165Interface(allowedStandardsList[ii])) return;
362
591
  }
363
592
  revert("Not Allowed Standards");
364
593
  }
@@ -370,19 +599,18 @@ abstract contract LSP6KeyManagerCore is ILSP6KeyManager, ERC165Storage {
370
599
  * @param _functionSelector the bytes4 function selector of the function to run
371
600
  * at the target contract
372
601
  */
373
- function _verifyAllowedFunction(address _from, bytes4 _functionSelector)
374
- internal
375
- view
376
- {
377
- bytes memory allowedFunctions = account.getAllowedFunctionsFor(_from);
602
+ function _verifyAllowedFunction(address _from, bytes4 _functionSelector) internal view {
603
+ bytes memory allowedFunctions = ERC725Y(target).getAllowedFunctionsFor(_from);
378
604
 
379
- // whitelist any function if nothing in the list
380
- 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;
381
612
 
382
- bytes4[] memory allowedFunctionsList = abi.decode(
383
- allowedFunctions,
384
- (bytes4[])
385
- );
613
+ bytes4[] memory allowedFunctionsList = abi.decode(allowedFunctions, (bytes4[]));
386
614
 
387
615
  for (uint256 ii = 0; ii < allowedFunctionsList.length; ii++) {
388
616
  if (_functionSelector == allowedFunctionsList[ii]) return;
@@ -390,55 +618,42 @@ abstract contract LSP6KeyManagerCore is ILSP6KeyManager, ERC165Storage {
390
618
  revert NotAllowedFunction(_from, _functionSelector);
391
619
  }
392
620
 
393
- /**
394
- * @dev compare the permissions `_addressPermission` of an address with `_requiredPermission`
395
- * @param _requiredPermission the permission required
396
- * @param _addressPermission the permission of address that we want to check
397
- * @return true if address has enough permissions, false otherwise
398
- */
399
- function _hasPermission(
400
- bytes32 _requiredPermission,
401
- bytes32 _addressPermission
402
- ) internal pure returns (bool) {
403
- return
404
- (_requiredPermission & _addressPermission) == _requiredPermission
405
- ? true
406
- : false;
407
- }
621
+ function _countZeroBytes(bytes32 _key) internal pure returns (uint256) {
622
+ uint256 index = 31;
408
623
 
409
- /**
410
- * @dev extract the required permission + a descriptive string, based on the `_operationType`
411
- * being run via ERC725Account.execute(...)
412
- * @param _operationType 0 = CALL, 1 = CREATE, 2 = CREATE2, etc... See ERC725X docs for more infos.
413
- * @return bytes32 the permission associated with the `_operationType`
414
- * @return string the opcode associated with `_operationType`
415
- */
416
- function _extractPermissionFromOperation(uint256 _operationType)
417
- internal
418
- pure
419
- returns (bytes32, string memory)
420
- {
421
- require(
422
- _operationType < 5,
423
- "_extractPermissionFromOperation: invalid operation type"
424
- );
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--;
425
627
 
426
- if (_operationType == 0) return (_PERMISSION_CALL, "CALL");
427
- if (_operationType == 1) return (_PERMISSION_DEPLOY, "CREATE");
428
- if (_operationType == 2) return (_PERMISSION_DEPLOY, "CREATE2");
429
- if (_operationType == 3) return (_PERMISSION_DEPLOY, "STATICCALL");
628
+ return 32 - (index + 1);
430
629
  }
431
630
 
432
- /**
433
- * @dev return a boolean here to allow short-circuiting syntax && or ||
434
- * eg1: _hasEnoughPermissions(...) || revert NotAuthorized(...)
435
- * eg2: _isNotAdmin(...) && revert NotAuthorised(...)
436
- */
437
- function _notAuthorised(address _from, string memory _permission)
438
- private
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);
639
+ }
640
+ }
641
+
642
+ function _getPermissionErrorString(bytes32 _permission)
643
+ internal
439
644
  pure
440
- returns (bool)
645
+ returns (string memory errorMessage)
441
646
  {
442
- revert NotAuthorised(_from, _permission);
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";
443
658
  }
444
659
  }