@lukso/lsp-smart-contracts 0.5.0 → 0.6.2

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