@lukso/lsp-smart-contracts 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/CONTRIBUTING.md +31 -0
  2. package/README.md +73 -20
  3. package/artifacts/Create2Factory.json +2 -2
  4. package/artifacts/LSP0ERC725Account.json +194 -15
  5. package/artifacts/LSP0ERC725AccountInit.json +194 -15
  6. package/artifacts/LSP11BasicSocialRecovery.json +493 -0
  7. package/artifacts/LSP11BasicSocialRecoveryInit.json +513 -0
  8. package/artifacts/LSP16UniversalFactory.json +236 -0
  9. package/artifacts/LSP1UniversalReceiverDelegateUP.json +134 -15
  10. package/artifacts/LSP1UniversalReceiverDelegateVault.json +80 -15
  11. package/artifacts/LSP4Compatibility.json +16 -0
  12. package/artifacts/LSP4DigitalAssetMetadata.json +16 -0
  13. package/artifacts/LSP4DigitalAssetMetadataInitAbstract.json +16 -0
  14. package/artifacts/LSP6KeyManager.json +177 -33
  15. package/artifacts/LSP6KeyManagerInit.json +177 -33
  16. package/artifacts/LSP7CappedSupply.json +21 -5
  17. package/artifacts/LSP7CappedSupplyInitAbstract.json +21 -5
  18. package/artifacts/LSP7CompatibleERC20.json +21 -5
  19. package/artifacts/LSP7CompatibleERC20InitAbstract.json +23 -7
  20. package/artifacts/LSP7CompatibleERC20Mintable.json +24 -8
  21. package/artifacts/LSP7CompatibleERC20MintableInit.json +24 -8
  22. package/artifacts/LSP7DigitalAsset.json +21 -5
  23. package/artifacts/LSP7DigitalAssetInitAbstract.json +21 -5
  24. package/artifacts/LSP7Mintable.json +24 -8
  25. package/artifacts/LSP7MintableInit.json +24 -8
  26. package/artifacts/LSP8CappedSupply.json +22 -6
  27. package/artifacts/LSP8CappedSupplyInitAbstract.json +22 -6
  28. package/artifacts/LSP8CompatibleERC721.json +23 -7
  29. package/artifacts/LSP8CompatibleERC721InitAbstract.json +25 -9
  30. package/artifacts/LSP8CompatibleERC721Mintable.json +26 -10
  31. package/artifacts/LSP8CompatibleERC721MintableInit.json +26 -10
  32. package/artifacts/LSP8IdentifiableDigitalAsset.json +22 -6
  33. package/artifacts/LSP8IdentifiableDigitalAssetInitAbstract.json +22 -6
  34. package/artifacts/LSP8Mintable.json +25 -9
  35. package/artifacts/LSP8MintableInit.json +25 -9
  36. package/artifacts/LSP9Vault.json +202 -17
  37. package/artifacts/LSP9VaultInit.json +202 -17
  38. package/artifacts/UniversalProfile.json +194 -15
  39. package/artifacts/UniversalProfileInit.json +194 -15
  40. package/artifacts/UniversalReceiverAddressStore.json +3 -3
  41. package/constants.js +377 -115
  42. package/contracts/Custom/EIP191Signer.sol +12 -0
  43. package/contracts/Factories/Create2Factory.sol +1 -3
  44. package/contracts/LSP0ERC725Account/LSP0Constants.sol +13 -2
  45. package/contracts/LSP0ERC725Account/LSP0ERC725Account.sol +2 -1
  46. package/contracts/LSP0ERC725Account/LSP0ERC725AccountCore.sol +261 -63
  47. package/contracts/LSP0ERC725Account/LSP0ERC725AccountInit.sol +2 -2
  48. package/contracts/LSP0ERC725Account/LSP0ERC725AccountInitAbstract.sol +2 -1
  49. package/contracts/LSP10ReceivedVaults/LSP10Constants.sol +2 -2
  50. package/contracts/LSP10ReceivedVaults/LSP10Utils.sol +214 -0
  51. package/contracts/LSP11BasicSocialRecovery/ILSP11BasicSocialRecovery.sol +177 -0
  52. package/contracts/LSP11BasicSocialRecovery/LSP11BasicSocialRecovery.sol +23 -0
  53. package/contracts/LSP11BasicSocialRecovery/LSP11BasicSocialRecoveryCore.sol +258 -0
  54. package/contracts/LSP11BasicSocialRecovery/LSP11BasicSocialRecoveryInit.sol +26 -0
  55. package/contracts/LSP11BasicSocialRecovery/LSP11BasicSocialRecoveryInitAbstract.sol +19 -0
  56. package/contracts/LSP11BasicSocialRecovery/LSP11Constants.sol +6 -0
  57. package/contracts/LSP11BasicSocialRecovery/LSP11Errors.sol +60 -0
  58. package/contracts/LSP14Ownable2Step/ILSP14Ownable2Step.sol +53 -0
  59. package/contracts/LSP14Ownable2Step/LSP14Constants.sol +15 -0
  60. package/contracts/LSP14Ownable2Step/LSP14Errors.sol +12 -0
  61. package/contracts/LSP14Ownable2Step/LSP14Ownable2Step.sol +212 -0
  62. package/contracts/LSP16UniversalFactory/LSP16UniversalFactory.sol +246 -0
  63. package/contracts/LSP17ContractExtension/LSP17Constants.sol +17 -0
  64. package/contracts/LSP17ContractExtension/LSP17Errors.sol +7 -0
  65. package/contracts/LSP17ContractExtension/LSP17Extendable.sol +104 -0
  66. package/contracts/LSP17ContractExtension/LSP17Extension.sol +45 -0
  67. package/contracts/LSP17ContractExtension/LSP17Utils.sol +20 -0
  68. package/contracts/LSP1UniversalReceiver/ILSP1UniversalReceiver.sol +5 -5
  69. package/contracts/LSP1UniversalReceiver/LSP1Constants.sol +5 -3
  70. package/contracts/LSP1UniversalReceiver/LSP1Errors.sol +21 -0
  71. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateUP/LSP1UniversalReceiverDelegateUP.sol +157 -37
  72. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateVault/LSP1UniversalReceiverDelegateVault.sol +64 -31
  73. package/contracts/LSP1UniversalReceiver/LSP1Utils.sol +53 -40
  74. package/contracts/LSP2ERC725YJSONSchema/LSP2Utils.sol +45 -23
  75. package/contracts/LSP3UniversalProfile/LSP3Constants.sol +1 -1
  76. package/contracts/LSP4DigitalAssetMetadata/ILSP4Compatibility.sol +1 -1
  77. package/contracts/LSP4DigitalAssetMetadata/LSP4Compatibility.sol +1 -1
  78. package/contracts/LSP4DigitalAssetMetadata/LSP4Constants.sol +1 -1
  79. package/contracts/LSP4DigitalAssetMetadata/LSP4DigitalAssetMetadata.sol +2 -2
  80. package/contracts/LSP4DigitalAssetMetadata/LSP4DigitalAssetMetadataInitAbstract.sol +2 -2
  81. package/contracts/LSP4DigitalAssetMetadata/LSP4Errors.sol +3 -3
  82. package/contracts/LSP5ReceivedAssets/LSP5Constants.sol +2 -2
  83. package/contracts/LSP5ReceivedAssets/LSP5Utils.sol +102 -114
  84. package/contracts/LSP6KeyManager/ILSP6KeyManager.sol +25 -7
  85. package/contracts/LSP6KeyManager/LSP6Constants.sol +43 -38
  86. package/contracts/LSP6KeyManager/LSP6Errors.sol +90 -21
  87. package/contracts/LSP6KeyManager/LSP6KeyManager.sol +2 -1
  88. package/contracts/LSP6KeyManager/LSP6KeyManagerCore.sol +772 -364
  89. package/contracts/LSP6KeyManager/LSP6KeyManagerInit.sol +1 -1
  90. package/contracts/LSP6KeyManager/LSP6KeyManagerInitAbstract.sol +3 -2
  91. package/contracts/LSP6KeyManager/LSP6Utils.sol +125 -34
  92. package/contracts/LSP7DigitalAsset/ILSP7DigitalAsset.sol +8 -8
  93. package/contracts/LSP7DigitalAsset/LSP7Constants.sol +6 -6
  94. package/contracts/LSP7DigitalAsset/LSP7DigitalAsset.sol +1 -1
  95. package/contracts/LSP7DigitalAsset/LSP7DigitalAssetCore.sol +38 -34
  96. package/contracts/LSP7DigitalAsset/LSP7DigitalAssetInitAbstract.sol +1 -1
  97. package/contracts/LSP7DigitalAsset/LSP7Errors.sol +28 -1
  98. package/contracts/LSP7DigitalAsset/extensions/ILSP7CompatibleERC20.sol +1 -1
  99. package/contracts/LSP7DigitalAsset/extensions/LSP7Burnable.sol +24 -0
  100. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupply.sol +28 -8
  101. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupplyInitAbstract.sol +28 -11
  102. package/contracts/LSP7DigitalAsset/extensions/LSP7CompatibleERC20.sol +87 -20
  103. package/contracts/LSP7DigitalAsset/extensions/LSP7CompatibleERC20InitAbstract.sol +97 -19
  104. package/contracts/LSP7DigitalAsset/presets/ILSP7Mintable.sol +3 -3
  105. package/contracts/LSP7DigitalAsset/presets/LSP7CompatibleERC20Mintable.sol +3 -4
  106. package/contracts/LSP7DigitalAsset/presets/LSP7CompatibleERC20MintableInit.sol +2 -2
  107. package/contracts/LSP7DigitalAsset/presets/LSP7CompatibleERC20MintableInitAbstract.sol +3 -3
  108. package/contracts/LSP7DigitalAsset/presets/LSP7Mintable.sol +3 -5
  109. package/contracts/LSP7DigitalAsset/presets/LSP7MintableInit.sol +2 -2
  110. package/contracts/LSP7DigitalAsset/presets/LSP7MintableInitAbstract.sol +3 -3
  111. package/contracts/LSP8IdentifiableDigitalAsset/ILSP8IdentifiableDigitalAsset.sol +7 -7
  112. package/contracts/LSP8IdentifiableDigitalAsset/LSP8Constants.sol +7 -7
  113. package/contracts/LSP8IdentifiableDigitalAsset/LSP8Errors.sol +40 -2
  114. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAsset.sol +2 -2
  115. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAssetCore.sol +53 -50
  116. package/contracts/LSP8IdentifiableDigitalAsset/LSP8IdentifiableDigitalAssetInitAbstract.sol +1 -1
  117. package/contracts/LSP8IdentifiableDigitalAsset/extensions/ILSP8CompatibleERC721.sol +5 -2
  118. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8Burnable.sol +20 -0
  119. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupply.sol +27 -7
  120. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupplyInitAbstract.sol +27 -10
  121. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721.sol +236 -30
  122. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721InitAbstract.sol +240 -30
  123. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8Enumerable.sol +32 -4
  124. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8EnumerableInitAbstract.sol +35 -8
  125. package/contracts/LSP8IdentifiableDigitalAsset/presets/ILSP8Mintable.sol +3 -3
  126. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8CompatibleERC721Mintable.sol +3 -4
  127. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8CompatibleERC721MintableInit.sol +3 -3
  128. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8CompatibleERC721MintableInitAbstract.sol +3 -3
  129. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8Mintable.sol +3 -4
  130. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8MintableInit.sol +2 -2
  131. package/contracts/LSP8IdentifiableDigitalAsset/presets/LSP8MintableInitAbstract.sol +3 -3
  132. package/contracts/LSP9Vault/LSP9Constants.sol +10 -10
  133. package/contracts/LSP9Vault/LSP9Errors.sol +8 -0
  134. package/contracts/LSP9Vault/LSP9Vault.sol +6 -4
  135. package/contracts/LSP9Vault/LSP9VaultCore.sol +289 -126
  136. package/contracts/LSP9Vault/LSP9VaultInit.sol +2 -2
  137. package/contracts/LSP9Vault/LSP9VaultInitAbstract.sol +4 -3
  138. package/contracts/Legacy/Registries/AddressRegistry.sol +1 -1
  139. package/contracts/Legacy/Registries/AddressRegistryRequiresERC725.sol +1 -1
  140. package/contracts/Legacy/UniversalReceiverAddressStore.sol +4 -14
  141. package/contracts/UniversalProfile.sol +2 -2
  142. package/contracts/UniversalProfileInit.sol +2 -2
  143. package/contracts/UniversalProfileInitAbstract.sol +1 -1
  144. package/contracts/Utils/GasLib.sol +1 -1
  145. package/package.json +17 -9
  146. package/artifacts/UniversalFactory.json +0 -127
  147. package/contracts/Custom/ClaimOwnership.sol +0 -96
  148. package/contracts/Custom/ERC165Checker.sol +0 -130
  149. package/contracts/Custom/IClaimOwnership.sol +0 -12
  150. package/contracts/Factories/UniversalFactory.sol +0 -159
  151. package/contracts/LSP1UniversalReceiver/ILSP1UniversalReceiverDelegate.sol +0 -24
  152. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateUP/Handling/TokenAndVaultHandling.sol +0 -82
  153. package/contracts/LSP1UniversalReceiver/LSP1UniversalReceiverDelegateVault/Handling/TokenHandling.sol +0 -73
  154. package/contracts/LSP2ERC725YJSONSchema/LSP2Errors.sol +0 -9
  155. package/contracts/LSP7DigitalAsset/extensions/ILSP7CappedSupply.sol +0 -17
  156. package/contracts/LSP7DigitalAsset/extensions/LSP7CappedSupplyCore.sol +0 -57
  157. package/contracts/LSP7DigitalAsset/extensions/LSP7CompatibleERC20Core.sol +0 -111
  158. package/contracts/LSP8IdentifiableDigitalAsset/extensions/ILSP8CappedSupply.sol +0 -17
  159. package/contracts/LSP8IdentifiableDigitalAsset/extensions/ILSP8Enumerable.sol +0 -17
  160. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CappedSupplyCore.sol +0 -58
  161. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleConstants.sol +0 -6
  162. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8CompatibleERC721Core.sol +0 -211
  163. package/contracts/LSP8IdentifiableDigitalAsset/extensions/LSP8EnumerableCore.sol +0 -50
  164. package/contracts/Utils/UtilsLib.sol +0 -31
@@ -0,0 +1,214 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.4;
3
+
4
+ // interfaces
5
+ import {IERC725Y} from "@erc725/smart-contracts/contracts/interfaces/IERC725Y.sol";
6
+
7
+ // libraries
8
+ import {BytesLib} from "solidity-bytes-utils/contracts/BytesLib.sol";
9
+ import {LSP2Utils} from "../LSP2ERC725YJSONSchema/LSP2Utils.sol";
10
+
11
+ // constants
12
+ import "../LSP10ReceivedVaults/LSP10Constants.sol";
13
+ import "../LSP9Vault/LSP9Constants.sol";
14
+
15
+ /**
16
+ * @dev reverts when the value stored under the 'LSP10ReceivedVaults[]' data key is not valid.
17
+ * The value stored under this data key should be exactly 32 bytes long.
18
+ *
19
+ * Only possible valid values are:
20
+ * - any valid uint256 values
21
+ * i.e. 0x0000000000000000000000000000000000000000000000000000000000000000 (zero), meaning empty array, no vaults received.
22
+ * i.e. 0x0000000000000000000000000000000000000000000000000000000000000005 (non-zero), meaning 5 array elements, 5 vaults received.
23
+ *
24
+ * - 0x (nothing stored under this data key, equivalent to empty array)
25
+ *
26
+ * @param invalidValue the invalid value stored under the LSP10ReceivedVaults[] data key
27
+ * @param invalidValueLength the invalid number of bytes stored under the LSP10ReceivedVaults[] data key (MUST be 32)
28
+ */
29
+ error InvalidLSP10ReceivedVaultsArrayLength(bytes invalidValue, uint256 invalidValueLength);
30
+
31
+ /**
32
+ * @dev reverts when the vault index is superior to uint64
33
+ * @param index the vault index
34
+ */
35
+ error VaultIndexSuperiorToUint64(uint256 index);
36
+
37
+ /**
38
+ * @dev reverts when the vault index is superior to uint128
39
+ * @param index the vault index
40
+ */
41
+ error VaultIndexSuperiorToUint128(uint256 index);
42
+
43
+ /**
44
+ * @title LSP10Utils
45
+ * @author Yamen Merhi <YamenMerhi>, Jean Cavallera <CJ42>
46
+ * @dev LSP5Utils is a library of functions that are used to register and manage vaults received by an ERC725Y smart contract
47
+ * based on the LSP10 - Received Vaults standard
48
+ * https://github.com/lukso-network/LIPs/blob/main/LSPs/LSP-10-ReceivedVaults.md
49
+ */
50
+ library LSP10Utils {
51
+ /**
52
+ * @dev Generating the data keys/values to be set on the receiver address after receiving vaults
53
+ * @param receiver The address receiving the vault and where the Keys should be added
54
+ * @param vault The address of the vault being received
55
+ * @param vaultMapKey The map key of the vault being received containing the interfaceId of the
56
+ * vault and the index in the array
57
+ * @param interfaceID The interfaceID of the vault being received
58
+ */
59
+ function generateReceivedVaultKeys(
60
+ address receiver,
61
+ address vault,
62
+ bytes32 vaultMapKey,
63
+ bytes4 interfaceID
64
+ ) internal view returns (bytes32[] memory keys, bytes[] memory values) {
65
+ keys = new bytes32[](3);
66
+ values = new bytes[](3);
67
+
68
+ IERC725Y account = IERC725Y(receiver);
69
+ bytes memory encodedArrayLength = getLSP10ReceivedVaultsCount(account);
70
+
71
+ // If it's the first vault to receive
72
+ if (encodedArrayLength.length == 0) {
73
+ keys[0] = _LSP10_VAULTS_ARRAY_KEY;
74
+ values[0] = bytes.concat(bytes32(uint256(1)));
75
+
76
+ keys[1] = LSP2Utils.generateArrayElementKeyAtIndex(_LSP10_VAULTS_ARRAY_KEY, 0);
77
+ values[1] = bytes.concat(bytes20(vault));
78
+
79
+ keys[2] = vaultMapKey;
80
+ values[2] = bytes.concat(interfaceID, bytes8(0));
81
+
82
+ // If the storage is already initiated
83
+ } else if (encodedArrayLength.length == 32) {
84
+ uint256 oldArrayLength = uint256(bytes32(encodedArrayLength));
85
+ uint256 newArrayLength = oldArrayLength + 1;
86
+
87
+ if (newArrayLength > type(uint64).max) {
88
+ revert VaultIndexSuperiorToUint64(newArrayLength);
89
+ }
90
+
91
+ keys[0] = _LSP10_VAULTS_ARRAY_KEY;
92
+ values[0] = bytes.concat(bytes32(newArrayLength));
93
+
94
+ keys[1] = LSP2Utils.generateArrayElementKeyAtIndex(
95
+ _LSP10_VAULTS_ARRAY_KEY,
96
+ uint128(oldArrayLength)
97
+ );
98
+ values[1] = bytes.concat(bytes20(vault));
99
+
100
+ keys[2] = vaultMapKey;
101
+ values[2] = bytes.concat(interfaceID, bytes8(uint64(oldArrayLength)));
102
+ } else {
103
+ revert InvalidLSP10ReceivedVaultsArrayLength(
104
+ encodedArrayLength,
105
+ encodedArrayLength.length
106
+ );
107
+ }
108
+ }
109
+
110
+ /**
111
+ * @dev Generating the data keys/values to be set on the sender address after sending vaults
112
+ * @param sender The address sending the vault and where the Keys should be updated
113
+ * @param vaultMapKey The map key of the vault being sent containing the interfaceId of the
114
+ * vault and the index in the array
115
+ * @param vaultInterfaceIdAndIndex The value of the map key of the vault being sent
116
+ */
117
+ function generateSentVaultKeys(
118
+ address sender,
119
+ bytes32 vaultMapKey,
120
+ bytes memory vaultInterfaceIdAndIndex
121
+ ) internal view returns (bytes32[] memory keys, bytes[] memory values) {
122
+ IERC725Y account = IERC725Y(sender);
123
+
124
+ // Updating the number of the received vaults
125
+ uint256 oldArrayLength = uint256(bytes32(account.getData(_LSP10_VAULTS_ARRAY_KEY)));
126
+
127
+ if (oldArrayLength > type(uint128).max) {
128
+ revert VaultIndexSuperiorToUint128(oldArrayLength);
129
+ }
130
+
131
+ uint128 newArrayLength = uint128(oldArrayLength) - 1;
132
+
133
+ uint64 index = extractIndexFromMap(vaultInterfaceIdAndIndex);
134
+ bytes32 vaultInArrayKey = LSP2Utils.generateArrayElementKeyAtIndex(
135
+ _LSP10_VAULTS_ARRAY_KEY,
136
+ index
137
+ );
138
+
139
+ if (index == newArrayLength) {
140
+ /**
141
+ * We will be updating/removing 3 keys:
142
+ * - Keys[0]: [Update] The arrayLengthKey to contain the new number of the received vaults
143
+ * - Keys[1]: [Remove] The element in arrayKey (Remove the address of the vault sent)
144
+ * - Keys[2]: [Remove] The mapKey (Remove the interfaceId and the index of the vault sent)
145
+ */
146
+ keys = new bytes32[](3);
147
+ values = new bytes[](3);
148
+
149
+ keys[0] = _LSP10_VAULTS_ARRAY_KEY;
150
+ values[0] = bytes.concat(bytes32(oldArrayLength - 1));
151
+
152
+ keys[1] = vaultInArrayKey;
153
+ values[1] = "";
154
+
155
+ keys[2] = vaultMapKey;
156
+ values[2] = "";
157
+
158
+ // Swapping last element in ArrayKey with the element in ArrayKey to remove || {Swap and pop} method;
159
+ // check https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/structs/EnumerableSet.sol#L80
160
+ } else {
161
+ /**
162
+ * We will be updating/removing 5 keys:
163
+ * - Keys[0]: [Update] The arrayLengthKey to contain the new number of the received vaults
164
+ * - Keys[1]: [Remove] The mapKey of the vault to remove (Remove the interfaceId and the index of the vault sent)
165
+ * - Keys[2]: [Update] The element in arrayKey to remove (Swap with the address of the last element in Array)
166
+ * - Keys[3]: [Remove] The last element in arrayKey (Remove (pop) the address of the last element as it's already swapped)
167
+ * - Keys[4]: [Update] The mapKey of the last element in array (Update the new index and the interfaceID)
168
+ */
169
+ keys = new bytes32[](5);
170
+ values = new bytes[](5);
171
+
172
+ keys[0] = _LSP10_VAULTS_ARRAY_KEY;
173
+ values[0] = bytes.concat(bytes32(oldArrayLength - 1));
174
+
175
+ keys[1] = vaultMapKey;
176
+ values[1] = "";
177
+
178
+ // Generate all data Keys/values of the last element in Array to swap
179
+ // with data Keys/values of the vault to remove
180
+ bytes32 lastVaultInArrayKey = LSP2Utils.generateArrayElementKeyAtIndex(
181
+ _LSP10_VAULTS_ARRAY_KEY,
182
+ newArrayLength
183
+ );
184
+
185
+ bytes20 lastVaultInArrayAddress = bytes20(account.getData(lastVaultInArrayKey));
186
+
187
+ bytes32 lastVaultInArrayMapKey = LSP2Utils.generateMappingKey(
188
+ _LSP10_VAULTS_MAP_KEY_PREFIX,
189
+ lastVaultInArrayAddress
190
+ );
191
+
192
+ keys[2] = vaultInArrayKey;
193
+ values[2] = bytes.concat(lastVaultInArrayAddress);
194
+
195
+ keys[3] = lastVaultInArrayKey;
196
+ values[3] = "";
197
+
198
+ keys[4] = lastVaultInArrayMapKey;
199
+ values[4] = bytes.concat(_INTERFACEID_LSP9, bytes8(index));
200
+ }
201
+ }
202
+
203
+ function getLSP10ReceivedVaultsCount(IERC725Y account) internal view returns (bytes memory) {
204
+ return account.getData(_LSP10_VAULTS_ARRAY_KEY);
205
+ }
206
+
207
+ /**
208
+ * @dev returns the index from a maping
209
+ */
210
+ function extractIndexFromMap(bytes memory mapValue) internal pure returns (uint64) {
211
+ bytes memory val = BytesLib.slice(mapValue, 4, 8);
212
+ return BytesLib.toUint64(val, 0);
213
+ }
214
+ }
@@ -0,0 +1,177 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.0;
3
+
4
+ /**
5
+ * @title interface of the LSP11 - Basic Social Recovery standard
6
+ * @dev Sets permission for a controller address after a recovery process to interact with an ERC725
7
+ * contract via the LSP6KeyManager
8
+ */
9
+ interface ILSP11BasicSocialRecovery {
10
+ /**
11
+ * @notice Emitted when setting a new guardian for the target
12
+ * @param newGuardian The address of the added guardian
13
+ */
14
+ event GuardianAdded(address indexed newGuardian);
15
+
16
+ /**
17
+ * @notice Emitted when removing an existing guardian for the target
18
+ * @param removedGuardian The address of the guardian removed
19
+ */
20
+ event GuardianRemoved(address indexed removedGuardian);
21
+
22
+ /**
23
+ * @notice Emitted when changing the guardian threshold
24
+ * @param guardianThreshold The minimum number of selection by guardians needed by a controller to start
25
+ * a recovery process
26
+ */
27
+ event GuardiansThresholdChanged(uint256 indexed guardianThreshold);
28
+
29
+ /**
30
+ * @notice Emitted when changing the secret hash
31
+ * @param secretHash The secret hash used to finish the recovery process
32
+ */
33
+ event SecretHashChanged(bytes32 indexed secretHash);
34
+
35
+ /**
36
+ * @notice Emitted when a guardian select a new potentiel controller address for the target
37
+ * @param recoveryCounter The current recovery process counter
38
+ * @param guardian The address of the guardian
39
+ * @param addressSelected The address selected by the guardian
40
+ */
41
+ event SelectedNewController(
42
+ uint256 indexed recoveryCounter,
43
+ address indexed guardian,
44
+ address indexed addressSelected
45
+ );
46
+
47
+ /**
48
+ * @notice Emitted when the recovery process is finished by the controller who
49
+ * reached the guardian threshold and submitted the string that produce the secretHash
50
+ * @param recoveryCounter The current recovery process
51
+ * @param newController The address of the new controller controlling the target by the KeyManager
52
+ * @param guardians The array of addresses containing the guardians of the target
53
+ */
54
+ event RecoveryProcessSuccessful(
55
+ uint256 indexed recoveryCounter,
56
+ address indexed newController,
57
+ bytes32 indexed newSecretHash,
58
+ address[] guardians
59
+ );
60
+
61
+ /**
62
+ * @dev The address of an ERC725 contract where we want to recover
63
+ * and set permissions for a controller address
64
+ */
65
+ function target() external view returns (address);
66
+
67
+ /**
68
+ * @dev Returns the current recovery counter
69
+ * When a recovery process is successfully finished the recovery counter is incremented
70
+ */
71
+ function getRecoveryCounter() external view returns (uint256);
72
+
73
+ /**
74
+ * @dev Returns the addresses of all guardians
75
+ * The guardians will select an address to be added as a controller
76
+ * key for the linked `target` if he reaches the guardian threshold and
77
+ * provide the correct string that produce the secretHash
78
+ */
79
+ function getGuardians() external view returns (address[] memory);
80
+
81
+ /**
82
+ * @dev Returns TRUE if the address provided is a guardian, FALSE otherwise
83
+ * @param _address The address to query
84
+ */
85
+ function isGuardian(address _address) external view returns (bool);
86
+
87
+ /**
88
+ * @dev Returns the recovery secret hash
89
+ */
90
+ function getRecoverySecretHash() external view returns (bytes32);
91
+
92
+ /**
93
+ * @dev Returns the guardian threshold
94
+ * The guardian threshold represents the minimum number of selection by guardians required
95
+ * for an address to start a recovery process
96
+ */
97
+ function getGuardiansThreshold() external view returns (uint256);
98
+
99
+ /**
100
+ * @dev Returns the address of a controller that a `guardian` selected for in order to recover the target
101
+ * @param guardian the address of a guardian to query his selection
102
+ * @return the address that `guardian` selected
103
+ */
104
+ function getGuardianChoice(address guardian) external view returns (address);
105
+
106
+ /**
107
+ * @dev Adds a guardian of the target
108
+ * @dev Can be called only by the owner
109
+ * @param newGuardian The address to add as a guardian
110
+ */
111
+ function addGuardian(address newGuardian) external;
112
+
113
+ /**
114
+ * @dev Removes a guardian of the target
115
+ * @dev Can be called only by the owner
116
+ * @param currentGuardian The address of the existing guardian to remove
117
+ *
118
+ * Requirements:
119
+ *
120
+ * - The guardians count should be higher or equal to the guardain threshold
121
+ */
122
+ function removeGuardian(address currentGuardian) external;
123
+
124
+ /**
125
+ * @dev Sets the hash of the secret string to be provided in `recoverOwnership(..)` function
126
+ * @dev Can be called only by the owner
127
+ * @param newRecoverSecretHash The hash of the secret string
128
+ *
129
+ * Requirements:
130
+ *
131
+ * - `secretHash` cannot be bytes32(0)
132
+ */
133
+ function setRecoverySecretHash(bytes32 newRecoverSecretHash) external;
134
+
135
+ /**
136
+ * @dev Sets the minimum number of selection by the guardians required so that an
137
+ * address can recover ownership
138
+ *
139
+ * @dev Can be called only by the owner
140
+ * @param guardiansThreshold The threshold to set
141
+ *
142
+ * Requirements:
143
+ * - `guardiansThreshold` cannot be more than the guardians count.
144
+ */
145
+ function setGuardiansThreshold(uint256 guardiansThreshold) external;
146
+
147
+ /**
148
+ * @dev select an address to be a potentiel controller address if he reaches
149
+ * the guardian threshold and provide the correct secret string
150
+ *
151
+ * Requirements:
152
+ * - only guardians can select an address
153
+ *
154
+ * @param addressSelected The address selected by the guardian
155
+ */
156
+ function selectNewController(address addressSelected) external;
157
+
158
+ /**
159
+ * @dev Recovers the ownership permissions of an address in the linked target
160
+ * and increment the recover counter
161
+ *
162
+ * Requirements
163
+ * - the address of the recoverer must have a selection equal or higher than the threshold
164
+ * defined in `getGuardiansThreshold(...)`
165
+ *
166
+ * - must have provided the right `plainSecret` that produces the secret Hash
167
+ *
168
+ * @param recoverer The address of the recoverer
169
+ * @param plainSecret The secret word that produce the secret Hash
170
+ * @param newHash The new secret Hash to be used in the next recovery process
171
+ */
172
+ function recoverOwnership(
173
+ address recoverer,
174
+ string memory plainSecret,
175
+ bytes32 newHash
176
+ ) external;
177
+ }
@@ -0,0 +1,23 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.0;
3
+
4
+ // modules
5
+ import {OwnableUnset} from "@erc725/smart-contracts/contracts/custom/OwnableUnset.sol";
6
+ import {LSP11BasicSocialRecoveryCore} from "./LSP11BasicSocialRecoveryCore.sol";
7
+
8
+ /**
9
+ * @title Implementation of LSP11 - Basic Social Recovery standard
10
+ * @dev Sets permission for a controller address after a recovery process to interact with an ERC725
11
+ * contract via the LSP6KeyManager
12
+ */
13
+ contract LSP11BasicSocialRecovery is LSP11BasicSocialRecoveryCore {
14
+ /**
15
+ * @notice Sets the target and the owner addresses
16
+ * @param _owner The owner of the LSP11 contract
17
+ * @param target_ The address of the ER725 contract to recover
18
+ */
19
+ constructor(address _owner, address target_) {
20
+ OwnableUnset._setOwner(_owner);
21
+ _target = target_;
22
+ }
23
+ }
@@ -0,0 +1,258 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.4;
3
+
4
+ // interfaces
5
+ import {ILSP11BasicSocialRecovery} from "./ILSP11BasicSocialRecovery.sol";
6
+
7
+ // libraries
8
+ import {LSP6Utils} from "../LSP6KeyManager/LSP6Utils.sol";
9
+
10
+ // modules
11
+ import {ERC725} from "@erc725/smart-contracts/contracts/ERC725.sol";
12
+ import {OwnableUnset} from "@erc725/smart-contracts/contracts/custom/OwnableUnset.sol";
13
+ import {ERC165} from "@openzeppelin/contracts/utils/introspection/ERC165.sol";
14
+ import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
15
+ import {LSP6KeyManager} from "../LSP6KeyManager/LSP6KeyManager.sol";
16
+
17
+ // constants
18
+ import {ALL_REGULAR_PERMISSIONS} from "../LSP6KeyManager/LSP6Constants.sol";
19
+ import {_INTERFACEID_LSP11} from "./LSP11Constants.sol";
20
+ import "./LSP11Errors.sol";
21
+
22
+ /**
23
+ * @title Core Implementation of LSP11-BasicSocialRecovery standard
24
+ * @dev Sets permission for a controller address after a recovery process to interact with an ERC725
25
+ * contract via the LSP6KeyManager
26
+ */
27
+ abstract contract LSP11BasicSocialRecoveryCore is OwnableUnset, ERC165, ILSP11BasicSocialRecovery {
28
+ using EnumerableSet for EnumerableSet.AddressSet;
29
+
30
+ address internal _target;
31
+
32
+ // The guardians threshold
33
+ uint256 internal _guardiansThreshold;
34
+
35
+ // The number of successfull recovery processes
36
+ uint256 internal _recoveryCounter;
37
+
38
+ // The secret hash to be set by the owner
39
+ bytes32 internal _recoverySecretHash;
40
+
41
+ // Stores the address selected by a guardian
42
+ // in the current `_recoveryCounter`
43
+ mapping(uint256 => mapping(address => address)) internal _guardiansChoice;
44
+
45
+ // List of guardians addresses
46
+ EnumerableSet.AddressSet internal _guardians;
47
+
48
+ /**
49
+ * @dev Throws if called by any account other than the guardians
50
+ */
51
+ modifier onlyGuardians() virtual {
52
+ if (!_guardians.contains(msg.sender)) revert CallerIsNotGuardian(msg.sender);
53
+ _;
54
+ }
55
+
56
+ /**
57
+ * @inheritdoc ERC165
58
+ */
59
+ function supportsInterface(bytes4 _interfaceId) public view virtual override returns (bool) {
60
+ return _interfaceId == _INTERFACEID_LSP11 || super.supportsInterface(_interfaceId);
61
+ }
62
+
63
+ /**
64
+ * @inheritdoc ILSP11BasicSocialRecovery
65
+ */
66
+ function target() public view virtual returns (address) {
67
+ return _target;
68
+ }
69
+
70
+ /**
71
+ * @inheritdoc ILSP11BasicSocialRecovery
72
+ */
73
+ function getRecoveryCounter() public view virtual returns (uint256) {
74
+ return _recoveryCounter;
75
+ }
76
+
77
+ /**
78
+ * @inheritdoc ILSP11BasicSocialRecovery
79
+ */
80
+ function getGuardians() public view virtual returns (address[] memory) {
81
+ return _guardians.values();
82
+ }
83
+
84
+ /**
85
+ * @inheritdoc ILSP11BasicSocialRecovery
86
+ */
87
+ function isGuardian(address _address) public view virtual returns (bool) {
88
+ return _guardians.contains(_address);
89
+ }
90
+
91
+ /**
92
+ * @inheritdoc ILSP11BasicSocialRecovery
93
+ */
94
+ function getGuardiansThreshold() public view virtual returns (uint256) {
95
+ return _guardiansThreshold;
96
+ }
97
+
98
+ /**
99
+ * @inheritdoc ILSP11BasicSocialRecovery
100
+ */
101
+ function getRecoverySecretHash() public view virtual returns (bytes32) {
102
+ return _recoverySecretHash;
103
+ }
104
+
105
+ /**
106
+ * @inheritdoc ILSP11BasicSocialRecovery
107
+ */
108
+ function getGuardianChoice(address guardian) public view virtual returns (address) {
109
+ return _guardiansChoice[_recoveryCounter][guardian];
110
+ }
111
+
112
+ /**
113
+ * @inheritdoc ILSP11BasicSocialRecovery
114
+ */
115
+ function addGuardian(address newGuardian) public virtual onlyOwner {
116
+ if (_guardians.contains(newGuardian)) revert GuardianAlreadyExist(newGuardian);
117
+
118
+ _guardians.add(newGuardian);
119
+ emit GuardianAdded(newGuardian);
120
+ }
121
+
122
+ /**
123
+ * @inheritdoc ILSP11BasicSocialRecovery
124
+ */
125
+ function removeGuardian(address existingGuardian) public virtual onlyOwner {
126
+ if (!_guardians.contains(existingGuardian)) revert GuardianDoNotExist(existingGuardian);
127
+ if (_guardians.length() == _guardiansThreshold)
128
+ revert GuardiansNumberCannotGoBelowThreshold(_guardiansThreshold);
129
+
130
+ _guardians.remove(existingGuardian);
131
+ emit GuardianRemoved(existingGuardian);
132
+ }
133
+
134
+ /**
135
+ * @inheritdoc ILSP11BasicSocialRecovery
136
+ */
137
+ function setGuardiansThreshold(uint256 newThreshold) public virtual onlyOwner {
138
+ if (newThreshold > _guardians.length())
139
+ revert ThresholdCannotBeHigherThanGuardiansNumber(newThreshold, _guardians.length());
140
+
141
+ _guardiansThreshold = newThreshold;
142
+ emit GuardiansThresholdChanged(newThreshold);
143
+ }
144
+
145
+ /**
146
+ * @inheritdoc ILSP11BasicSocialRecovery
147
+ * @dev Throws if hash provided is bytes32(0)
148
+ */
149
+ function setRecoverySecretHash(bytes32 newRecoverSecretHash) public virtual onlyOwner {
150
+ if (newRecoverSecretHash == bytes32(0)) revert SecretHashCannotBeZero();
151
+
152
+ _recoverySecretHash = newRecoverSecretHash;
153
+ emit SecretHashChanged(newRecoverSecretHash);
154
+ }
155
+
156
+ /**
157
+ * @inheritdoc ILSP11BasicSocialRecovery
158
+ */
159
+ function selectNewController(address addressSelected) public virtual onlyGuardians {
160
+ uint256 currentRecoveryCounter = _recoveryCounter;
161
+
162
+ _guardiansChoice[currentRecoveryCounter][msg.sender] = addressSelected;
163
+ emit SelectedNewController(currentRecoveryCounter, msg.sender, addressSelected);
164
+ }
165
+
166
+ /**
167
+ * @inheritdoc ILSP11BasicSocialRecovery
168
+ */
169
+ function recoverOwnership(
170
+ address recoverer,
171
+ string memory plainSecret,
172
+ bytes32 newSecretHash
173
+ ) public virtual {
174
+ // caching storage variables
175
+ uint256 currentRecoveryCounter = _recoveryCounter;
176
+ address[] memory guardians = _guardians.values();
177
+ address target_ = _target;
178
+
179
+ _validateRequirements(
180
+ recoverer,
181
+ currentRecoveryCounter,
182
+ plainSecret,
183
+ newSecretHash,
184
+ guardians
185
+ );
186
+
187
+ _recoveryCounter++;
188
+ _recoverySecretHash = newSecretHash;
189
+ emit SecretHashChanged(newSecretHash);
190
+
191
+ address keyManager = ERC725(target_).owner();
192
+
193
+ // Setting permissions for `recoverer`
194
+ (bytes32[] memory keys, bytes[] memory values) = LSP6Utils
195
+ .generatePermissionsKeysForController(
196
+ ERC725(target_),
197
+ recoverer,
198
+ ALL_REGULAR_PERMISSIONS
199
+ );
200
+
201
+ LSP6Utils.setDataViaKeyManager(keyManager, keys, values);
202
+
203
+ emit RecoveryProcessSuccessful(currentRecoveryCounter, recoverer, newSecretHash, guardians);
204
+
205
+ _cleanStorage(currentRecoveryCounter, guardians.length, guardians);
206
+ }
207
+
208
+ /**
209
+ * @dev The number of guardians should be reasonable, as the validation method
210
+ * is using a loop to check the selection of each guardian
211
+ *
212
+ * Throws if:
213
+ * - The address trying to recover didn't reach the guardiansThreshold
214
+ * - The new hash being set is bytes32(0)
215
+ * - The secret word provided is incorrect
216
+ */
217
+ function _validateRequirements(
218
+ address recoverer,
219
+ uint256 currentRecoveryCounter,
220
+ string memory plainSecret,
221
+ bytes32 newHash,
222
+ address[] memory guardians
223
+ ) internal view virtual {
224
+ if (recoverer == address(0)) revert AddressZeroNotAllowed();
225
+ uint256 callerSelections;
226
+
227
+ unchecked {
228
+ for (uint256 i; i < guardians.length; i++) {
229
+ if (_guardiansChoice[currentRecoveryCounter][guardians[i]] == recoverer)
230
+ callerSelections++;
231
+ }
232
+ }
233
+
234
+ uint256 guardiansThreshold = _guardiansThreshold;
235
+
236
+ if (callerSelections < guardiansThreshold)
237
+ revert ThresholdNotReachedForRecoverer(recoverer, callerSelections, guardiansThreshold);
238
+ if (newHash == bytes32(0)) revert SecretHashCannotBeZero();
239
+ if (keccak256(abi.encodePacked(plainSecret)) != _recoverySecretHash)
240
+ revert WrongPlainSecret();
241
+ }
242
+
243
+ /**
244
+ * @dev Remove the guardians choice after a successfull recovery process
245
+ * To avoid keeping unnecessary state
246
+ */
247
+ function _cleanStorage(
248
+ uint256 recoveryCounter,
249
+ uint256 guardiansLength,
250
+ address[] memory guardians
251
+ ) internal virtual {
252
+ unchecked {
253
+ for (uint256 i; i < guardiansLength; i++) {
254
+ delete _guardiansChoice[recoveryCounter][guardians[i]];
255
+ }
256
+ }
257
+ }
258
+ }
@@ -0,0 +1,26 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.0;
3
+
4
+ // modules
5
+ import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
6
+ import {LSP11BasicSocialRecoveryInitAbstract} from "./LSP11BasicSocialRecoveryInitAbstract.sol";
7
+
8
+ /**
9
+ * @title Deployable Proxy Implementation of LSP11 - Basic Social Recovery standard
10
+ * @dev Sets permission for a controller address after a recovery process to interact with an ERC725
11
+ * contract via the LSP6KeyManager
12
+ */
13
+ contract LSP11BasicSocialRecoveryInit is LSP11BasicSocialRecoveryInitAbstract {
14
+ constructor() {
15
+ _disableInitializers();
16
+ }
17
+
18
+ /**
19
+ * @notice Sets the target and the owner addresses
20
+ * @param _owner The owner of the LSP11 contract
21
+ * @param target_ The address of the ER725 contract to recover
22
+ */
23
+ function initialize(address target_, address _owner) public virtual initializer {
24
+ LSP11BasicSocialRecoveryInitAbstract._initialize(target_, _owner);
25
+ }
26
+ }