@metamask-previews/profile-sync-controller 24.0.0-preview-b13da658 → 24.0.0-preview-a421f85b

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 (66) hide show
  1. package/CHANGELOG.md +0 -9
  2. package/dist/controllers/user-storage/UserStorageController.cjs +80 -4
  3. package/dist/controllers/user-storage/UserStorageController.cjs.map +1 -1
  4. package/dist/controllers/user-storage/UserStorageController.d.cts +54 -5
  5. package/dist/controllers/user-storage/UserStorageController.d.cts.map +1 -1
  6. package/dist/controllers/user-storage/UserStorageController.d.mts +54 -5
  7. package/dist/controllers/user-storage/UserStorageController.d.mts.map +1 -1
  8. package/dist/controllers/user-storage/UserStorageController.mjs +76 -0
  9. package/dist/controllers/user-storage/UserStorageController.mjs.map +1 -1
  10. package/dist/controllers/user-storage/account-syncing/constants.cjs +51 -0
  11. package/dist/controllers/user-storage/account-syncing/constants.cjs.map +1 -0
  12. package/dist/controllers/user-storage/account-syncing/constants.d.cts +4 -0
  13. package/dist/controllers/user-storage/account-syncing/constants.d.cts.map +1 -0
  14. package/dist/controllers/user-storage/account-syncing/constants.d.mts +4 -0
  15. package/dist/controllers/user-storage/account-syncing/constants.d.mts.map +1 -0
  16. package/dist/controllers/user-storage/account-syncing/constants.mjs +48 -0
  17. package/dist/controllers/user-storage/account-syncing/constants.mjs.map +1 -0
  18. package/dist/controllers/user-storage/account-syncing/controller-integration.cjs +271 -0
  19. package/dist/controllers/user-storage/account-syncing/controller-integration.cjs.map +1 -0
  20. package/dist/controllers/user-storage/account-syncing/controller-integration.d.cts +38 -0
  21. package/dist/controllers/user-storage/account-syncing/controller-integration.d.cts.map +1 -0
  22. package/dist/controllers/user-storage/account-syncing/controller-integration.d.mts +38 -0
  23. package/dist/controllers/user-storage/account-syncing/controller-integration.d.mts.map +1 -0
  24. package/dist/controllers/user-storage/account-syncing/controller-integration.mjs +265 -0
  25. package/dist/controllers/user-storage/account-syncing/controller-integration.mjs.map +1 -0
  26. package/dist/controllers/user-storage/account-syncing/setup-subscriptions.cjs +41 -0
  27. package/dist/controllers/user-storage/account-syncing/setup-subscriptions.cjs.map +1 -0
  28. package/dist/controllers/user-storage/account-syncing/setup-subscriptions.d.cts +8 -0
  29. package/dist/controllers/user-storage/account-syncing/setup-subscriptions.d.cts.map +1 -0
  30. package/dist/controllers/user-storage/account-syncing/setup-subscriptions.d.mts +8 -0
  31. package/dist/controllers/user-storage/account-syncing/setup-subscriptions.d.mts.map +1 -0
  32. package/dist/controllers/user-storage/account-syncing/setup-subscriptions.mjs +37 -0
  33. package/dist/controllers/user-storage/account-syncing/setup-subscriptions.mjs.map +1 -0
  34. package/dist/controllers/user-storage/account-syncing/sync-utils.cjs +61 -0
  35. package/dist/controllers/user-storage/account-syncing/sync-utils.cjs.map +1 -0
  36. package/dist/controllers/user-storage/account-syncing/sync-utils.d.cts +30 -0
  37. package/dist/controllers/user-storage/account-syncing/sync-utils.d.cts.map +1 -0
  38. package/dist/controllers/user-storage/account-syncing/sync-utils.d.mts +30 -0
  39. package/dist/controllers/user-storage/account-syncing/sync-utils.d.mts.map +1 -0
  40. package/dist/controllers/user-storage/account-syncing/sync-utils.mjs +55 -0
  41. package/dist/controllers/user-storage/account-syncing/sync-utils.mjs.map +1 -0
  42. package/dist/controllers/user-storage/account-syncing/types.cjs +3 -0
  43. package/dist/controllers/user-storage/account-syncing/types.cjs.map +1 -0
  44. package/dist/controllers/user-storage/account-syncing/types.d.cts +25 -0
  45. package/dist/controllers/user-storage/account-syncing/types.d.cts.map +1 -0
  46. package/dist/controllers/user-storage/account-syncing/types.d.mts +25 -0
  47. package/dist/controllers/user-storage/account-syncing/types.d.mts.map +1 -0
  48. package/dist/controllers/user-storage/account-syncing/types.mjs +2 -0
  49. package/dist/controllers/user-storage/account-syncing/types.mjs.map +1 -0
  50. package/dist/controllers/user-storage/account-syncing/utils.cjs +36 -0
  51. package/dist/controllers/user-storage/account-syncing/utils.cjs.map +1 -0
  52. package/dist/controllers/user-storage/account-syncing/utils.d.cts +18 -0
  53. package/dist/controllers/user-storage/account-syncing/utils.d.cts.map +1 -0
  54. package/dist/controllers/user-storage/account-syncing/utils.d.mts +18 -0
  55. package/dist/controllers/user-storage/account-syncing/utils.d.mts.map +1 -0
  56. package/dist/controllers/user-storage/account-syncing/utils.mjs +31 -0
  57. package/dist/controllers/user-storage/account-syncing/utils.mjs.map +1 -0
  58. package/dist/controllers/user-storage/constants.cjs +3 -0
  59. package/dist/controllers/user-storage/constants.cjs.map +1 -1
  60. package/dist/controllers/user-storage/constants.d.cts +2 -0
  61. package/dist/controllers/user-storage/constants.d.cts.map +1 -1
  62. package/dist/controllers/user-storage/constants.d.mts +2 -0
  63. package/dist/controllers/user-storage/constants.d.mts.map +1 -1
  64. package/dist/controllers/user-storage/constants.mjs +3 -0
  65. package/dist/controllers/user-storage/constants.mjs.map +1 -1
  66. package/package.json +3 -1
@@ -0,0 +1,271 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.syncInternalAccountsWithUserStorage = exports.saveInternalAccountsListToUserStorage = exports.saveInternalAccountToUserStorage = void 0;
4
+ const keyring_controller_1 = require("@metamask/keyring-controller");
5
+ const sync_utils_1 = require("./sync-utils.cjs");
6
+ const utils_1 = require("./utils.cjs");
7
+ const storage_schema_1 = require("../../../shared/storage-schema.cjs");
8
+ const constants_1 = require("../constants.cjs");
9
+ /**
10
+ * Saves an individual internal account to the user storage.
11
+ *
12
+ * @param internalAccount - The internal account to save
13
+ * @param options - parameters used for saving the internal account
14
+ * @returns Promise that resolves when the account is saved
15
+ */
16
+ async function saveInternalAccountToUserStorage(internalAccount, options) {
17
+ const { trace } = options;
18
+ const saveAccount = async () => {
19
+ const { getUserStorageControllerInstance } = options;
20
+ if (getUserStorageControllerInstance().getIsMultichainAccountSyncingEnabled()) {
21
+ // If multichain account syncing is enabled, we do not push account syncing V1 data anymore.
22
+ // AccountTreeController handles proper multichain account syncing
23
+ return;
24
+ }
25
+ if (!(0, sync_utils_1.canPerformAccountSyncing)(options) ||
26
+ internalAccount.metadata.keyring.type !== String(keyring_controller_1.KeyringTypes.hd) // sync only EVM accounts until we support multichain accounts
27
+ ) {
28
+ return;
29
+ }
30
+ // properties of `options` are (wrongly?) typed as `Json` and eslint crashes if we try to interpret it as such and call a `?.toString()` on it.
31
+ // but we know this is a string?, so we can safely cast it
32
+ const entropySourceId = internalAccount.options?.entropySource;
33
+ try {
34
+ // Map the internal account to the user storage account schema
35
+ const mappedAccount = (0, utils_1.mapInternalAccountToUserStorageAccount)(internalAccount);
36
+ await getUserStorageControllerInstance().performSetStorage(`${storage_schema_1.USER_STORAGE_FEATURE_NAMES.accounts}.${internalAccount.address}`, JSON.stringify(mappedAccount), entropySourceId);
37
+ }
38
+ catch (e) {
39
+ // istanbul ignore next
40
+ const errorMessage = e instanceof Error ? e.message : JSON.stringify(e);
41
+ throw new Error(`UserStorageController - failed to save account to user storage - ${errorMessage}`);
42
+ }
43
+ };
44
+ if (trace) {
45
+ return await trace({ name: constants_1.TraceName.AccountSyncSaveIndividual }, saveAccount);
46
+ }
47
+ return await saveAccount();
48
+ }
49
+ exports.saveInternalAccountToUserStorage = saveInternalAccountToUserStorage;
50
+ /**
51
+ * Saves the list of internal accounts to the user storage.
52
+ *
53
+ * @param options - parameters used for saving the list of internal accounts
54
+ * @param entropySourceId - The entropy source ID used to derive the key,
55
+ * when multiple sources are available (Multi-SRP).
56
+ * @returns Promise that resolves when all accounts are saved
57
+ */
58
+ async function saveInternalAccountsListToUserStorage(options, entropySourceId) {
59
+ const { getUserStorageControllerInstance } = options;
60
+ if (getUserStorageControllerInstance().getIsMultichainAccountSyncingEnabled()) {
61
+ // If multichain account syncing is enabled, we do not push account syncing V1 data anymore.
62
+ // AccountTreeController handles proper multichain account syncing
63
+ return;
64
+ }
65
+ const internalAccountsList = await (0, sync_utils_1.getInternalAccountsList)(options, entropySourceId);
66
+ if (!internalAccountsList?.length) {
67
+ return;
68
+ }
69
+ const internalAccountsListFormattedForUserStorage = internalAccountsList.map(utils_1.mapInternalAccountToUserStorageAccount);
70
+ await getUserStorageControllerInstance().performBatchSetStorage(storage_schema_1.USER_STORAGE_FEATURE_NAMES.accounts, internalAccountsListFormattedForUserStorage.map((account) => [
71
+ account.a,
72
+ JSON.stringify(account),
73
+ ]), entropySourceId);
74
+ }
75
+ exports.saveInternalAccountsListToUserStorage = saveInternalAccountsListToUserStorage;
76
+ /**
77
+ * Syncs the internal accounts list with the user storage accounts list.
78
+ * This method is used to make sure that the internal accounts list is up-to-date with the user storage accounts list and vice-versa.
79
+ * It will add new accounts to the internal accounts list, update/merge conflicting names and re-upload the results in some cases to the user storage.
80
+ *
81
+ * @param config - parameters used for syncing the internal accounts list with the user storage accounts list
82
+ * @param options - parameters used for syncing the internal accounts list with the user storage accounts list
83
+ * @param entropySourceId - The entropy source ID used to derive the key,
84
+ * @returns Promise that resolves when synchronization is complete
85
+ */
86
+ async function syncInternalAccountsWithUserStorage(config, options, entropySourceId) {
87
+ const { trace } = options;
88
+ const performAccountSync = async () => {
89
+ if (!(0, sync_utils_1.canPerformAccountSyncing)(options)) {
90
+ return;
91
+ }
92
+ const { maxNumberOfAccountsToAdd = Infinity, onAccountAdded, onAccountNameUpdated, onAccountSyncErroneousSituation, } = config;
93
+ const { getMessenger, getUserStorageControllerInstance } = options;
94
+ try {
95
+ await getUserStorageControllerInstance().setIsAccountSyncingInProgress(true);
96
+ const userStorageAccountsList = await (0, sync_utils_1.getUserStorageAccountsList)(options, entropySourceId);
97
+ if (!userStorageAccountsList || !userStorageAccountsList.length) {
98
+ await saveInternalAccountsListToUserStorage(options, entropySourceId);
99
+ return;
100
+ }
101
+ // Keep a record if erroneous situations are found during the sync
102
+ // This is done so we can send the context to Sentry in case of an erroneous situation
103
+ let erroneousSituationsFound = false;
104
+ // Prepare an array of internal accounts to be saved to the user storage
105
+ const internalAccountsToBeSavedToUserStorage = [];
106
+ // Compare internal accounts list with user storage accounts list
107
+ // First step: compare lengths
108
+ const internalAccountsList = await (0, sync_utils_1.getInternalAccountsList)(options, entropySourceId);
109
+ if (!internalAccountsList || !internalAccountsList.length) {
110
+ throw new Error(`Failed to get internal accounts list`);
111
+ }
112
+ const hasMoreUserStorageAccountsThanInternalAccounts = userStorageAccountsList.length > internalAccountsList.length;
113
+ // We don't want to remove existing accounts for a user
114
+ // so we only add new accounts if the user has more accounts in user storage than internal accounts
115
+ if (hasMoreUserStorageAccountsThanInternalAccounts) {
116
+ const numberOfAccountsToAdd = Math.min(userStorageAccountsList.length, maxNumberOfAccountsToAdd) -
117
+ internalAccountsList.length;
118
+ // Create new accounts to match the user storage accounts list
119
+ await getMessenger().call('KeyringController:withKeyring', {
120
+ id: entropySourceId,
121
+ }, async ({ keyring }) => {
122
+ await keyring.addAccounts(numberOfAccountsToAdd);
123
+ });
124
+ // TODO: below code is kept for analytics but should probably be re-thought
125
+ for (let i = 0; i < numberOfAccountsToAdd; i++) {
126
+ onAccountAdded?.();
127
+ }
128
+ }
129
+ // Second step: compare account names
130
+ // Get the internal accounts list again since new accounts might have been added in the previous step
131
+ const refreshedInternalAccountsList = await (0, sync_utils_1.getInternalAccountsList)(options, entropySourceId);
132
+ const newlyAddedAccounts = refreshedInternalAccountsList.filter((account) => !internalAccountsList.find((a) => a.address === account.address));
133
+ for (const internalAccount of refreshedInternalAccountsList) {
134
+ const userStorageAccount = userStorageAccountsList.find((account) => account.a === internalAccount.address);
135
+ // If the account is not present in user storage
136
+ // istanbul ignore next
137
+ if (!userStorageAccount) {
138
+ // If the account was just added in the previous step, skip saving it, it's likely to be a bogus account
139
+ if (newlyAddedAccounts.includes(internalAccount)) {
140
+ erroneousSituationsFound = true;
141
+ onAccountSyncErroneousSituation?.('An account was added to the internal accounts list but was not present in the user storage accounts list', {
142
+ internalAccount,
143
+ userStorageAccount,
144
+ newlyAddedAccounts,
145
+ userStorageAccountsList,
146
+ internalAccountsList,
147
+ refreshedInternalAccountsList,
148
+ internalAccountsToBeSavedToUserStorage,
149
+ });
150
+ continue;
151
+ }
152
+ // Otherwise, it means that this internal account was present before the sync, and needs to be saved to the user storage
153
+ internalAccountsToBeSavedToUserStorage.push(internalAccount);
154
+ continue;
155
+ }
156
+ // From this point on, we know that the account is present in
157
+ // both the internal accounts list and the user storage accounts list
158
+ // One or both accounts have default names
159
+ const isInternalAccountNameDefault = (0, utils_1.isNameDefaultAccountName)(internalAccount.metadata.name);
160
+ const isUserStorageAccountNameDefault = (0, utils_1.isNameDefaultAccountName)(userStorageAccount.n);
161
+ // Internal account has default name
162
+ if (isInternalAccountNameDefault) {
163
+ if (!isUserStorageAccountNameDefault) {
164
+ getMessenger().call('AccountsController:updateAccountMetadata', internalAccount.id, {
165
+ name: userStorageAccount.n,
166
+ });
167
+ onAccountNameUpdated?.();
168
+ }
169
+ continue;
170
+ }
171
+ // Internal account has custom name but user storage account has default name
172
+ if (isUserStorageAccountNameDefault) {
173
+ internalAccountsToBeSavedToUserStorage.push(internalAccount);
174
+ continue;
175
+ }
176
+ // Both accounts have custom names
177
+ // User storage account has a nameLastUpdatedAt timestamp
178
+ // Note: not storing the undefined checks in constants to act as a type guard
179
+ if (userStorageAccount.nlu !== undefined) {
180
+ if (internalAccount.metadata.nameLastUpdatedAt !== undefined) {
181
+ const isInternalAccountNameNewer = internalAccount.metadata.nameLastUpdatedAt >
182
+ userStorageAccount.nlu;
183
+ if (isInternalAccountNameNewer) {
184
+ internalAccountsToBeSavedToUserStorage.push(internalAccount);
185
+ continue;
186
+ }
187
+ }
188
+ getMessenger().call('AccountsController:updateAccountMetadata', internalAccount.id, {
189
+ name: userStorageAccount.n,
190
+ nameLastUpdatedAt: userStorageAccount.nlu,
191
+ });
192
+ const areInternalAndUserStorageAccountNamesEqual = internalAccount.metadata.name === userStorageAccount.n;
193
+ if (!areInternalAndUserStorageAccountNamesEqual) {
194
+ onAccountNameUpdated?.();
195
+ }
196
+ continue;
197
+ }
198
+ else if (internalAccount.metadata.nameLastUpdatedAt !== undefined) {
199
+ internalAccountsToBeSavedToUserStorage.push(internalAccount);
200
+ continue;
201
+ }
202
+ }
203
+ // Save the internal accounts list to the user storage
204
+ if (internalAccountsToBeSavedToUserStorage.length) {
205
+ if (!getUserStorageControllerInstance().getIsMultichainAccountSyncingEnabled()) {
206
+ // If multichain account syncing is enabled, we do not push account syncing V1 data anymore.
207
+ // AccountTreeController handles proper multichain account syncing
208
+ await getUserStorageControllerInstance().performBatchSetStorage(storage_schema_1.USER_STORAGE_FEATURE_NAMES.accounts, internalAccountsToBeSavedToUserStorage.map((account) => [
209
+ account.address,
210
+ JSON.stringify((0, utils_1.mapInternalAccountToUserStorageAccount)(account)),
211
+ ]), entropySourceId);
212
+ }
213
+ }
214
+ // In case we have corrupted user storage with accounts that don't exist in the internal accounts list
215
+ // Delete those accounts from the user storage
216
+ const userStorageAccountsToBeDeleted = userStorageAccountsList.filter((account) => !refreshedInternalAccountsList.find((a) => a.address === account.a));
217
+ if (userStorageAccountsToBeDeleted.length) {
218
+ if (!getUserStorageControllerInstance().getIsMultichainAccountSyncingEnabled()) {
219
+ // If multichain account syncing is enabled, we do not push account syncing V1 data anymore.
220
+ // AccountTreeController handles proper multichain account syncing
221
+ await getUserStorageControllerInstance().performBatchDeleteStorage(storage_schema_1.USER_STORAGE_FEATURE_NAMES.accounts, userStorageAccountsToBeDeleted.map((account) => account.a), entropySourceId);
222
+ erroneousSituationsFound = true;
223
+ onAccountSyncErroneousSituation?.('An account was present in the user storage accounts list but was not found in the internal accounts list after the sync', {
224
+ userStorageAccountsToBeDeleted,
225
+ internalAccountsList,
226
+ refreshedInternalAccountsList,
227
+ internalAccountsToBeSavedToUserStorage,
228
+ userStorageAccountsList,
229
+ });
230
+ }
231
+ }
232
+ if (erroneousSituationsFound) {
233
+ const [finalUserStorageAccountsList, finalInternalAccountsList] = await Promise.all([
234
+ (0, sync_utils_1.getUserStorageAccountsList)(options, entropySourceId),
235
+ (0, sync_utils_1.getInternalAccountsList)(options, entropySourceId),
236
+ ]);
237
+ const doesEveryAccountInInternalAccountsListExistInUserStorageAccountsList = finalInternalAccountsList.every((account) => finalUserStorageAccountsList?.some((userStorageAccount) => userStorageAccount.a === account.address));
238
+ // istanbul ignore next
239
+ const doesEveryAccountInUserStorageAccountsListExistInInternalAccountsList = (finalUserStorageAccountsList?.length || 0) > maxNumberOfAccountsToAdd
240
+ ? true
241
+ : finalUserStorageAccountsList?.every((account) => finalInternalAccountsList.some((internalAccount) => internalAccount.address === account.a));
242
+ const doFinalListsMatch = doesEveryAccountInInternalAccountsListExistInUserStorageAccountsList &&
243
+ doesEveryAccountInUserStorageAccountsListExistInInternalAccountsList;
244
+ const context = {
245
+ finalUserStorageAccountsList,
246
+ finalInternalAccountsList,
247
+ };
248
+ if (doFinalListsMatch) {
249
+ onAccountSyncErroneousSituation?.('Erroneous situations were found during the sync, but final state matches the expected state', context);
250
+ }
251
+ else {
252
+ onAccountSyncErroneousSituation?.('Erroneous situations were found during the sync, and final state does not match the expected state', context);
253
+ }
254
+ }
255
+ }
256
+ catch (e) {
257
+ // istanbul ignore next
258
+ const errorMessage = e instanceof Error ? e.message : JSON.stringify(e);
259
+ throw new Error(`UserStorageController - failed to sync user storage accounts list - ${errorMessage}`);
260
+ }
261
+ finally {
262
+ await getUserStorageControllerInstance().setIsAccountSyncingInProgress(false);
263
+ }
264
+ };
265
+ if (trace) {
266
+ return await trace({ name: constants_1.TraceName.AccountSyncFull }, performAccountSync);
267
+ }
268
+ return await performAccountSync();
269
+ }
270
+ exports.syncInternalAccountsWithUserStorage = syncInternalAccountsWithUserStorage;
271
+ //# sourceMappingURL=controller-integration.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"controller-integration.cjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/controller-integration.ts"],"names":[],"mappings":";;;AAAA,qEAA4D;AAG5D,iDAIsB;AAEtB,uCAGiB;AACjB,uEAA4E;AAC5E,gDAAyC;AAEzC;;;;;;GAMG;AACI,KAAK,UAAU,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KAAK,CACN,CAAC;SACH;IACH,CAAC,CAAC;IAEF,IAAI,KAAK,EAAE;QACT,OAAO,MAAM,KAAK,CAAC,EAAE,IAAI,EAAE,qBAAS,CAAC,eAAe,EAAE,EAAE,kBAAkB,CAAC,CAAC;KAC7E;IAED,OAAO,MAAM,kBAAkB,EAAE,CAAC;AACpC,CAAC;AAjTD,kFAiTC","sourcesContent":["import { KeyringTypes } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport {\n canPerformAccountSyncing,\n getInternalAccountsList,\n getUserStorageAccountsList,\n} from './sync-utils';\nimport type { AccountSyncingOptions } from './types';\nimport {\n isNameDefaultAccountName,\n mapInternalAccountToUserStorageAccount,\n} from './utils';\nimport { USER_STORAGE_FEATURE_NAMES } from '../../../shared/storage-schema';\nimport { TraceName } from '../constants';\n\n/**\n * Saves an individual internal account to the user storage.\n *\n * @param internalAccount - The internal account to save\n * @param options - parameters used for saving the internal account\n * @returns Promise that resolves when the account is saved\n */\nexport async function saveInternalAccountToUserStorage(\n internalAccount: InternalAccount,\n options: AccountSyncingOptions,\n): Promise<void> {\n const { trace } = options;\n\n const saveAccount = async () => {\n const { getUserStorageControllerInstance } = options;\n\n if (\n getUserStorageControllerInstance().getIsMultichainAccountSyncingEnabled()\n ) {\n // If multichain account syncing is enabled, we do not push account syncing V1 data anymore.\n // AccountTreeController handles proper multichain account syncing\n return;\n }\n\n if (\n !canPerformAccountSyncing(options) ||\n internalAccount.metadata.keyring.type !== String(KeyringTypes.hd) // sync only EVM accounts until we support multichain accounts\n ) {\n return;\n }\n\n // properties of `options` are (wrongly?) typed as `Json` and eslint crashes if we try to interpret it as such and call a `?.toString()` on it.\n // but we know this is a string?, so we can safely cast it\n const entropySourceId = internalAccount.options?.entropySource as\n | string\n | undefined;\n\n try {\n // Map the internal account to the user storage account schema\n const mappedAccount =\n mapInternalAccountToUserStorageAccount(internalAccount);\n\n await getUserStorageControllerInstance().performSetStorage(\n `${USER_STORAGE_FEATURE_NAMES.accounts}.${internalAccount.address}`,\n JSON.stringify(mappedAccount),\n entropySourceId,\n );\n } catch (e) {\n // istanbul ignore next\n const errorMessage = e instanceof Error ? e.message : JSON.stringify(e);\n throw new Error(\n `UserStorageController - failed to save account to user storage - ${errorMessage}`,\n );\n }\n };\n\n if (trace) {\n return await trace(\n { name: TraceName.AccountSyncSaveIndividual },\n saveAccount,\n );\n }\n\n return await saveAccount();\n}\n\n/**\n * Saves the list of internal accounts to the user storage.\n *\n * @param options - parameters used for saving the list of internal accounts\n * @param entropySourceId - The entropy source ID used to derive the key,\n * when multiple sources are available (Multi-SRP).\n * @returns Promise that resolves when all accounts are saved\n */\nexport async function saveInternalAccountsListToUserStorage(\n options: AccountSyncingOptions,\n entropySourceId: string,\n): Promise<void> {\n const { getUserStorageControllerInstance } = options;\n if (\n getUserStorageControllerInstance().getIsMultichainAccountSyncingEnabled()\n ) {\n // If multichain account syncing is enabled, we do not push account syncing V1 data anymore.\n // AccountTreeController handles proper multichain account syncing\n return;\n }\n\n const internalAccountsList = await getInternalAccountsList(\n options,\n entropySourceId,\n );\n\n if (!internalAccountsList?.length) {\n return;\n }\n\n const internalAccountsListFormattedForUserStorage = internalAccountsList.map(\n mapInternalAccountToUserStorageAccount,\n );\n\n await getUserStorageControllerInstance().performBatchSetStorage(\n USER_STORAGE_FEATURE_NAMES.accounts,\n internalAccountsListFormattedForUserStorage.map((account) => [\n account.a,\n JSON.stringify(account),\n ]),\n entropySourceId,\n );\n}\n\ntype SyncInternalAccountsWithUserStorageConfig = {\n maxNumberOfAccountsToAdd?: number;\n onAccountAdded?: () => void;\n onAccountNameUpdated?: () => void;\n onAccountSyncErroneousSituation?: (\n errorMessage: string,\n sentryContext?: Record<string, unknown>,\n ) => void;\n};\n\n/**\n * Syncs the internal accounts list with the user storage accounts list.\n * This method is used to make sure that the internal accounts list is up-to-date with the user storage accounts list and vice-versa.\n * It will add new accounts to the internal accounts list, update/merge conflicting names and re-upload the results in some cases to the user storage.\n *\n * @param config - parameters used for syncing the internal accounts list with the user storage accounts list\n * @param options - parameters used for syncing the internal accounts list with the user storage accounts list\n * @param entropySourceId - The entropy source ID used to derive the key,\n * @returns Promise that resolves when synchronization is complete\n */\nexport async function syncInternalAccountsWithUserStorage(\n config: SyncInternalAccountsWithUserStorageConfig,\n options: AccountSyncingOptions,\n entropySourceId: string,\n): Promise<void> {\n const { trace } = options;\n\n const performAccountSync = async () => {\n if (!canPerformAccountSyncing(options)) {\n return;\n }\n\n const {\n maxNumberOfAccountsToAdd = Infinity,\n onAccountAdded,\n onAccountNameUpdated,\n onAccountSyncErroneousSituation,\n } = config;\n const { getMessenger, getUserStorageControllerInstance } = options;\n\n try {\n await getUserStorageControllerInstance().setIsAccountSyncingInProgress(\n true,\n );\n\n const userStorageAccountsList = await getUserStorageAccountsList(\n options,\n entropySourceId,\n );\n\n if (!userStorageAccountsList || !userStorageAccountsList.length) {\n await saveInternalAccountsListToUserStorage(options, entropySourceId);\n return;\n }\n // Keep a record if erroneous situations are found during the sync\n // This is done so we can send the context to Sentry in case of an erroneous situation\n let erroneousSituationsFound = false;\n\n // Prepare an array of internal accounts to be saved to the user storage\n const internalAccountsToBeSavedToUserStorage: InternalAccount[] = [];\n\n // Compare internal accounts list with user storage accounts list\n // First step: compare lengths\n const internalAccountsList = await getInternalAccountsList(\n options,\n entropySourceId,\n );\n\n if (!internalAccountsList || !internalAccountsList.length) {\n throw new Error(`Failed to get internal accounts list`);\n }\n\n const hasMoreUserStorageAccountsThanInternalAccounts =\n userStorageAccountsList.length > internalAccountsList.length;\n\n // We don't want to remove existing accounts for a user\n // so we only add new accounts if the user has more accounts in user storage than internal accounts\n if (hasMoreUserStorageAccountsThanInternalAccounts) {\n const numberOfAccountsToAdd =\n Math.min(userStorageAccountsList.length, maxNumberOfAccountsToAdd) -\n internalAccountsList.length;\n\n // Create new accounts to match the user storage accounts list\n await getMessenger().call(\n 'KeyringController:withKeyring',\n {\n id: entropySourceId,\n },\n async ({ keyring }) => {\n await keyring.addAccounts(numberOfAccountsToAdd);\n },\n );\n\n // TODO: below code is kept for analytics but should probably be re-thought\n for (let i = 0; i < numberOfAccountsToAdd; i++) {\n onAccountAdded?.();\n }\n }\n\n // Second step: compare account names\n // Get the internal accounts list again since new accounts might have been added in the previous step\n const refreshedInternalAccountsList = await getInternalAccountsList(\n options,\n entropySourceId,\n );\n\n const newlyAddedAccounts = refreshedInternalAccountsList.filter(\n (account) =>\n !internalAccountsList.find((a) => a.address === account.address),\n );\n\n for (const internalAccount of refreshedInternalAccountsList) {\n const userStorageAccount = userStorageAccountsList.find(\n (account) => account.a === internalAccount.address,\n );\n\n // If the account is not present in user storage\n // istanbul ignore next\n if (!userStorageAccount) {\n // If the account was just added in the previous step, skip saving it, it's likely to be a bogus account\n if (newlyAddedAccounts.includes(internalAccount)) {\n erroneousSituationsFound = true;\n onAccountSyncErroneousSituation?.(\n 'An account was added to the internal accounts list but was not present in the user storage accounts list',\n {\n internalAccount,\n userStorageAccount,\n newlyAddedAccounts,\n userStorageAccountsList,\n internalAccountsList,\n refreshedInternalAccountsList,\n internalAccountsToBeSavedToUserStorage,\n },\n );\n continue;\n }\n // Otherwise, it means that this internal account was present before the sync, and needs to be saved to the user storage\n internalAccountsToBeSavedToUserStorage.push(internalAccount);\n continue;\n }\n\n // From this point on, we know that the account is present in\n // both the internal accounts list and the user storage accounts list\n\n // One or both accounts have default names\n const isInternalAccountNameDefault = isNameDefaultAccountName(\n internalAccount.metadata.name,\n );\n const isUserStorageAccountNameDefault = isNameDefaultAccountName(\n userStorageAccount.n,\n );\n\n // Internal account has default name\n if (isInternalAccountNameDefault) {\n if (!isUserStorageAccountNameDefault) {\n getMessenger().call(\n 'AccountsController:updateAccountMetadata',\n internalAccount.id,\n {\n name: userStorageAccount.n,\n },\n );\n\n onAccountNameUpdated?.();\n }\n continue;\n }\n\n // Internal account has custom name but user storage account has default name\n if (isUserStorageAccountNameDefault) {\n internalAccountsToBeSavedToUserStorage.push(internalAccount);\n continue;\n }\n\n // Both accounts have custom names\n\n // User storage account has a nameLastUpdatedAt timestamp\n // Note: not storing the undefined checks in constants to act as a type guard\n if (userStorageAccount.nlu !== undefined) {\n if (internalAccount.metadata.nameLastUpdatedAt !== undefined) {\n const isInternalAccountNameNewer =\n internalAccount.metadata.nameLastUpdatedAt >\n userStorageAccount.nlu;\n\n if (isInternalAccountNameNewer) {\n internalAccountsToBeSavedToUserStorage.push(internalAccount);\n continue;\n }\n }\n\n getMessenger().call(\n 'AccountsController:updateAccountMetadata',\n internalAccount.id,\n {\n name: userStorageAccount.n,\n nameLastUpdatedAt: userStorageAccount.nlu,\n },\n );\n\n const areInternalAndUserStorageAccountNamesEqual =\n internalAccount.metadata.name === userStorageAccount.n;\n\n if (!areInternalAndUserStorageAccountNamesEqual) {\n onAccountNameUpdated?.();\n }\n\n continue;\n } else if (internalAccount.metadata.nameLastUpdatedAt !== undefined) {\n internalAccountsToBeSavedToUserStorage.push(internalAccount);\n continue;\n }\n }\n\n // Save the internal accounts list to the user storage\n if (internalAccountsToBeSavedToUserStorage.length) {\n if (\n !getUserStorageControllerInstance().getIsMultichainAccountSyncingEnabled()\n ) {\n // If multichain account syncing is enabled, we do not push account syncing V1 data anymore.\n // AccountTreeController handles proper multichain account syncing\n await getUserStorageControllerInstance().performBatchSetStorage(\n USER_STORAGE_FEATURE_NAMES.accounts,\n internalAccountsToBeSavedToUserStorage.map((account) => [\n account.address,\n JSON.stringify(mapInternalAccountToUserStorageAccount(account)),\n ]),\n entropySourceId,\n );\n }\n }\n\n // In case we have corrupted user storage with accounts that don't exist in the internal accounts list\n // Delete those accounts from the user storage\n const userStorageAccountsToBeDeleted = userStorageAccountsList.filter(\n (account) =>\n !refreshedInternalAccountsList.find((a) => a.address === account.a),\n );\n\n if (userStorageAccountsToBeDeleted.length) {\n if (\n !getUserStorageControllerInstance().getIsMultichainAccountSyncingEnabled()\n ) {\n // If multichain account syncing is enabled, we do not push account syncing V1 data anymore.\n // AccountTreeController handles proper multichain account syncing\n await getUserStorageControllerInstance().performBatchDeleteStorage(\n USER_STORAGE_FEATURE_NAMES.accounts,\n userStorageAccountsToBeDeleted.map((account) => account.a),\n entropySourceId,\n );\n erroneousSituationsFound = true;\n onAccountSyncErroneousSituation?.(\n 'An account was present in the user storage accounts list but was not found in the internal accounts list after the sync',\n {\n userStorageAccountsToBeDeleted,\n internalAccountsList,\n refreshedInternalAccountsList,\n internalAccountsToBeSavedToUserStorage,\n userStorageAccountsList,\n },\n );\n }\n }\n\n if (erroneousSituationsFound) {\n const [finalUserStorageAccountsList, finalInternalAccountsList] =\n await Promise.all([\n getUserStorageAccountsList(options, entropySourceId),\n getInternalAccountsList(options, entropySourceId),\n ]);\n\n const doesEveryAccountInInternalAccountsListExistInUserStorageAccountsList =\n finalInternalAccountsList.every((account) =>\n finalUserStorageAccountsList?.some(\n (userStorageAccount) => userStorageAccount.a === account.address,\n ),\n );\n\n // istanbul ignore next\n const doesEveryAccountInUserStorageAccountsListExistInInternalAccountsList =\n (finalUserStorageAccountsList?.length || 0) > maxNumberOfAccountsToAdd\n ? true\n : finalUserStorageAccountsList?.every((account) =>\n finalInternalAccountsList.some(\n (internalAccount) => internalAccount.address === account.a,\n ),\n );\n\n const doFinalListsMatch =\n doesEveryAccountInInternalAccountsListExistInUserStorageAccountsList &&\n doesEveryAccountInUserStorageAccountsListExistInInternalAccountsList;\n\n const context = {\n finalUserStorageAccountsList,\n finalInternalAccountsList,\n };\n if (doFinalListsMatch) {\n onAccountSyncErroneousSituation?.(\n 'Erroneous situations were found during the sync, but final state matches the expected state',\n context,\n );\n } else {\n onAccountSyncErroneousSituation?.(\n 'Erroneous situations were found during the sync, and final state does not match the expected state',\n context,\n );\n }\n }\n } catch (e) {\n // istanbul ignore next\n const errorMessage = e instanceof Error ? e.message : JSON.stringify(e);\n throw new Error(\n `UserStorageController - failed to sync user storage accounts list - ${errorMessage}`,\n );\n } finally {\n await getUserStorageControllerInstance().setIsAccountSyncingInProgress(\n false,\n );\n }\n };\n\n if (trace) {\n return await trace({ name: TraceName.AccountSyncFull }, performAccountSync);\n }\n\n return await performAccountSync();\n}\n"]}
@@ -0,0 +1,38 @@
1
+ import type { InternalAccount } from "@metamask/keyring-internal-api";
2
+ import type { AccountSyncingOptions } from "./types.cjs";
3
+ /**
4
+ * Saves an individual internal account to the user storage.
5
+ *
6
+ * @param internalAccount - The internal account to save
7
+ * @param options - parameters used for saving the internal account
8
+ * @returns Promise that resolves when the account is saved
9
+ */
10
+ export declare function saveInternalAccountToUserStorage(internalAccount: InternalAccount, options: AccountSyncingOptions): Promise<void>;
11
+ /**
12
+ * Saves the list of internal accounts to the user storage.
13
+ *
14
+ * @param options - parameters used for saving the list of internal accounts
15
+ * @param entropySourceId - The entropy source ID used to derive the key,
16
+ * when multiple sources are available (Multi-SRP).
17
+ * @returns Promise that resolves when all accounts are saved
18
+ */
19
+ export declare function saveInternalAccountsListToUserStorage(options: AccountSyncingOptions, entropySourceId: string): Promise<void>;
20
+ type SyncInternalAccountsWithUserStorageConfig = {
21
+ maxNumberOfAccountsToAdd?: number;
22
+ onAccountAdded?: () => void;
23
+ onAccountNameUpdated?: () => void;
24
+ onAccountSyncErroneousSituation?: (errorMessage: string, sentryContext?: Record<string, unknown>) => void;
25
+ };
26
+ /**
27
+ * Syncs the internal accounts list with the user storage accounts list.
28
+ * This method is used to make sure that the internal accounts list is up-to-date with the user storage accounts list and vice-versa.
29
+ * It will add new accounts to the internal accounts list, update/merge conflicting names and re-upload the results in some cases to the user storage.
30
+ *
31
+ * @param config - parameters used for syncing the internal accounts list with the user storage accounts list
32
+ * @param options - parameters used for syncing the internal accounts list with the user storage accounts list
33
+ * @param entropySourceId - The entropy source ID used to derive the key,
34
+ * @returns Promise that resolves when synchronization is complete
35
+ */
36
+ export declare function syncInternalAccountsWithUserStorage(config: SyncInternalAccountsWithUserStorageConfig, options: AccountSyncingOptions, entropySourceId: string): Promise<void>;
37
+ export {};
38
+ //# sourceMappingURL=controller-integration.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"controller-integration.d.cts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/controller-integration.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAOtE,OAAO,KAAK,EAAE,qBAAqB,EAAE,oBAAgB;AAQrD;;;;;;GAMG;AACH,wBAAsB,gCAAgC,CACpD,eAAe,EAAE,eAAe,EAChC,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CAsDf;AAED;;;;;;;GAOG;AACH,wBAAsB,qCAAqC,CACzD,OAAO,EAAE,qBAAqB,EAC9B,eAAe,EAAE,MAAM,GACtB,OAAO,CAAC,IAAI,CAAC,CA+Bf;AAED,KAAK,yCAAyC,GAAG;IAC/C,wBAAwB,CAAC,EAAE,MAAM,CAAC;IAClC,cAAc,CAAC,EAAE,MAAM,IAAI,CAAC;IAC5B,oBAAoB,CAAC,EAAE,MAAM,IAAI,CAAC;IAClC,+BAA+B,CAAC,EAAE,CAChC,YAAY,EAAE,MAAM,EACpB,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KACpC,IAAI,CAAC;CACX,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAsB,mCAAmC,CACvD,MAAM,EAAE,yCAAyC,EACjD,OAAO,EAAE,qBAAqB,EAC9B,eAAe,EAAE,MAAM,GACtB,OAAO,CAAC,IAAI,CAAC,CA6Sf"}
@@ -0,0 +1,38 @@
1
+ import type { InternalAccount } from "@metamask/keyring-internal-api";
2
+ import type { AccountSyncingOptions } from "./types.mjs";
3
+ /**
4
+ * Saves an individual internal account to the user storage.
5
+ *
6
+ * @param internalAccount - The internal account to save
7
+ * @param options - parameters used for saving the internal account
8
+ * @returns Promise that resolves when the account is saved
9
+ */
10
+ export declare function saveInternalAccountToUserStorage(internalAccount: InternalAccount, options: AccountSyncingOptions): Promise<void>;
11
+ /**
12
+ * Saves the list of internal accounts to the user storage.
13
+ *
14
+ * @param options - parameters used for saving the list of internal accounts
15
+ * @param entropySourceId - The entropy source ID used to derive the key,
16
+ * when multiple sources are available (Multi-SRP).
17
+ * @returns Promise that resolves when all accounts are saved
18
+ */
19
+ export declare function saveInternalAccountsListToUserStorage(options: AccountSyncingOptions, entropySourceId: string): Promise<void>;
20
+ type SyncInternalAccountsWithUserStorageConfig = {
21
+ maxNumberOfAccountsToAdd?: number;
22
+ onAccountAdded?: () => void;
23
+ onAccountNameUpdated?: () => void;
24
+ onAccountSyncErroneousSituation?: (errorMessage: string, sentryContext?: Record<string, unknown>) => void;
25
+ };
26
+ /**
27
+ * Syncs the internal accounts list with the user storage accounts list.
28
+ * This method is used to make sure that the internal accounts list is up-to-date with the user storage accounts list and vice-versa.
29
+ * It will add new accounts to the internal accounts list, update/merge conflicting names and re-upload the results in some cases to the user storage.
30
+ *
31
+ * @param config - parameters used for syncing the internal accounts list with the user storage accounts list
32
+ * @param options - parameters used for syncing the internal accounts list with the user storage accounts list
33
+ * @param entropySourceId - The entropy source ID used to derive the key,
34
+ * @returns Promise that resolves when synchronization is complete
35
+ */
36
+ export declare function syncInternalAccountsWithUserStorage(config: SyncInternalAccountsWithUserStorageConfig, options: AccountSyncingOptions, entropySourceId: string): Promise<void>;
37
+ export {};
38
+ //# sourceMappingURL=controller-integration.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"controller-integration.d.mts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/controller-integration.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAOtE,OAAO,KAAK,EAAE,qBAAqB,EAAE,oBAAgB;AAQrD;;;;;;GAMG;AACH,wBAAsB,gCAAgC,CACpD,eAAe,EAAE,eAAe,EAChC,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CAsDf;AAED;;;;;;;GAOG;AACH,wBAAsB,qCAAqC,CACzD,OAAO,EAAE,qBAAqB,EAC9B,eAAe,EAAE,MAAM,GACtB,OAAO,CAAC,IAAI,CAAC,CA+Bf;AAED,KAAK,yCAAyC,GAAG;IAC/C,wBAAwB,CAAC,EAAE,MAAM,CAAC;IAClC,cAAc,CAAC,EAAE,MAAM,IAAI,CAAC;IAC5B,oBAAoB,CAAC,EAAE,MAAM,IAAI,CAAC;IAClC,+BAA+B,CAAC,EAAE,CAChC,YAAY,EAAE,MAAM,EACpB,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KACpC,IAAI,CAAC;CACX,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAsB,mCAAmC,CACvD,MAAM,EAAE,yCAAyC,EACjD,OAAO,EAAE,qBAAqB,EAC9B,eAAe,EAAE,MAAM,GACtB,OAAO,CAAC,IAAI,CAAC,CA6Sf"}