@metamask/profile-sync-controller 23.0.0 → 25.0.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 (78) hide show
  1. package/CHANGELOG.md +47 -1
  2. package/dist/controllers/authentication/AuthenticationController.cjs +4 -0
  3. package/dist/controllers/authentication/AuthenticationController.cjs.map +1 -1
  4. package/dist/controllers/authentication/AuthenticationController.d.cts.map +1 -1
  5. package/dist/controllers/authentication/AuthenticationController.d.mts.map +1 -1
  6. package/dist/controllers/authentication/AuthenticationController.mjs +4 -0
  7. package/dist/controllers/authentication/AuthenticationController.mjs.map +1 -1
  8. package/dist/controllers/user-storage/UserStorageController.cjs +14 -87
  9. package/dist/controllers/user-storage/UserStorageController.cjs.map +1 -1
  10. package/dist/controllers/user-storage/UserStorageController.d.cts +3 -52
  11. package/dist/controllers/user-storage/UserStorageController.d.cts.map +1 -1
  12. package/dist/controllers/user-storage/UserStorageController.d.mts +3 -52
  13. package/dist/controllers/user-storage/UserStorageController.d.mts.map +1 -1
  14. package/dist/controllers/user-storage/UserStorageController.mjs +10 -83
  15. package/dist/controllers/user-storage/UserStorageController.mjs.map +1 -1
  16. package/dist/controllers/user-storage/constants.cjs +0 -3
  17. package/dist/controllers/user-storage/constants.cjs.map +1 -1
  18. package/dist/controllers/user-storage/constants.d.cts +0 -2
  19. package/dist/controllers/user-storage/constants.d.cts.map +1 -1
  20. package/dist/controllers/user-storage/constants.d.mts +0 -2
  21. package/dist/controllers/user-storage/constants.d.mts.map +1 -1
  22. package/dist/controllers/user-storage/constants.mjs +0 -3
  23. package/dist/controllers/user-storage/constants.mjs.map +1 -1
  24. package/dist/sdk/authentication-jwt-bearer/flow-srp.cjs +28 -2
  25. package/dist/sdk/authentication-jwt-bearer/flow-srp.cjs.map +1 -1
  26. package/dist/sdk/authentication-jwt-bearer/flow-srp.d.cts.map +1 -1
  27. package/dist/sdk/authentication-jwt-bearer/flow-srp.d.mts.map +1 -1
  28. package/dist/sdk/authentication-jwt-bearer/flow-srp.mjs +28 -2
  29. package/dist/sdk/authentication-jwt-bearer/flow-srp.mjs.map +1 -1
  30. package/package.json +10 -10
  31. package/dist/controllers/user-storage/account-syncing/constants.cjs +0 -51
  32. package/dist/controllers/user-storage/account-syncing/constants.cjs.map +0 -1
  33. package/dist/controllers/user-storage/account-syncing/constants.d.cts +0 -4
  34. package/dist/controllers/user-storage/account-syncing/constants.d.cts.map +0 -1
  35. package/dist/controllers/user-storage/account-syncing/constants.d.mts +0 -4
  36. package/dist/controllers/user-storage/account-syncing/constants.d.mts.map +0 -1
  37. package/dist/controllers/user-storage/account-syncing/constants.mjs +0 -48
  38. package/dist/controllers/user-storage/account-syncing/constants.mjs.map +0 -1
  39. package/dist/controllers/user-storage/account-syncing/controller-integration.cjs +0 -253
  40. package/dist/controllers/user-storage/account-syncing/controller-integration.cjs.map +0 -1
  41. package/dist/controllers/user-storage/account-syncing/controller-integration.d.cts +0 -38
  42. package/dist/controllers/user-storage/account-syncing/controller-integration.d.cts.map +0 -1
  43. package/dist/controllers/user-storage/account-syncing/controller-integration.d.mts +0 -38
  44. package/dist/controllers/user-storage/account-syncing/controller-integration.d.mts.map +0 -1
  45. package/dist/controllers/user-storage/account-syncing/controller-integration.mjs +0 -247
  46. package/dist/controllers/user-storage/account-syncing/controller-integration.mjs.map +0 -1
  47. package/dist/controllers/user-storage/account-syncing/setup-subscriptions.cjs +0 -35
  48. package/dist/controllers/user-storage/account-syncing/setup-subscriptions.cjs.map +0 -1
  49. package/dist/controllers/user-storage/account-syncing/setup-subscriptions.d.cts +0 -8
  50. package/dist/controllers/user-storage/account-syncing/setup-subscriptions.d.cts.map +0 -1
  51. package/dist/controllers/user-storage/account-syncing/setup-subscriptions.d.mts +0 -8
  52. package/dist/controllers/user-storage/account-syncing/setup-subscriptions.d.mts.map +0 -1
  53. package/dist/controllers/user-storage/account-syncing/setup-subscriptions.mjs +0 -31
  54. package/dist/controllers/user-storage/account-syncing/setup-subscriptions.mjs.map +0 -1
  55. package/dist/controllers/user-storage/account-syncing/sync-utils.cjs +0 -61
  56. package/dist/controllers/user-storage/account-syncing/sync-utils.cjs.map +0 -1
  57. package/dist/controllers/user-storage/account-syncing/sync-utils.d.cts +0 -30
  58. package/dist/controllers/user-storage/account-syncing/sync-utils.d.cts.map +0 -1
  59. package/dist/controllers/user-storage/account-syncing/sync-utils.d.mts +0 -30
  60. package/dist/controllers/user-storage/account-syncing/sync-utils.d.mts.map +0 -1
  61. package/dist/controllers/user-storage/account-syncing/sync-utils.mjs +0 -55
  62. package/dist/controllers/user-storage/account-syncing/sync-utils.mjs.map +0 -1
  63. package/dist/controllers/user-storage/account-syncing/types.cjs +0 -3
  64. package/dist/controllers/user-storage/account-syncing/types.cjs.map +0 -1
  65. package/dist/controllers/user-storage/account-syncing/types.d.cts +0 -25
  66. package/dist/controllers/user-storage/account-syncing/types.d.cts.map +0 -1
  67. package/dist/controllers/user-storage/account-syncing/types.d.mts +0 -25
  68. package/dist/controllers/user-storage/account-syncing/types.d.mts.map +0 -1
  69. package/dist/controllers/user-storage/account-syncing/types.mjs +0 -2
  70. package/dist/controllers/user-storage/account-syncing/types.mjs.map +0 -1
  71. package/dist/controllers/user-storage/account-syncing/utils.cjs +0 -36
  72. package/dist/controllers/user-storage/account-syncing/utils.cjs.map +0 -1
  73. package/dist/controllers/user-storage/account-syncing/utils.d.cts +0 -18
  74. package/dist/controllers/user-storage/account-syncing/utils.d.cts.map +0 -1
  75. package/dist/controllers/user-storage/account-syncing/utils.d.mts +0 -18
  76. package/dist/controllers/user-storage/account-syncing/utils.d.mts.map +0 -1
  77. package/dist/controllers/user-storage/account-syncing/utils.mjs +0 -31
  78. package/dist/controllers/user-storage/account-syncing/utils.mjs.map +0 -1
@@ -1,247 +0,0 @@
1
- import { KeyringTypes } from "@metamask/keyring-controller";
2
- import { canPerformAccountSyncing, getInternalAccountsList, getUserStorageAccountsList } from "./sync-utils.mjs";
3
- import { isNameDefaultAccountName, mapInternalAccountToUserStorageAccount } from "./utils.mjs";
4
- import { USER_STORAGE_FEATURE_NAMES } from "../../../shared/storage-schema.mjs";
5
- import { TraceName } from "../constants.mjs";
6
- /**
7
- * Saves an individual internal account to the user storage.
8
- *
9
- * @param internalAccount - The internal account to save
10
- * @param options - parameters used for saving the internal account
11
- * @returns Promise that resolves when the account is saved
12
- */
13
- export async function saveInternalAccountToUserStorage(internalAccount, options) {
14
- const { trace } = options;
15
- const saveAccount = async () => {
16
- const { getUserStorageControllerInstance } = options;
17
- if (!canPerformAccountSyncing(options) ||
18
- internalAccount.metadata.keyring.type !== String(KeyringTypes.hd) // sync only EVM accounts until we support multichain accounts
19
- ) {
20
- return;
21
- }
22
- // 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.
23
- // but we know this is a string?, so we can safely cast it
24
- const entropySourceId = internalAccount.options?.entropySource;
25
- try {
26
- // Map the internal account to the user storage account schema
27
- const mappedAccount = mapInternalAccountToUserStorageAccount(internalAccount);
28
- await getUserStorageControllerInstance().performSetStorage(`${USER_STORAGE_FEATURE_NAMES.accounts}.${internalAccount.address}`, JSON.stringify(mappedAccount), entropySourceId);
29
- }
30
- catch (e) {
31
- // istanbul ignore next
32
- const errorMessage = e instanceof Error ? e.message : JSON.stringify(e);
33
- throw new Error(`UserStorageController - failed to save account to user storage - ${errorMessage}`);
34
- }
35
- };
36
- if (trace) {
37
- return await trace({ name: TraceName.AccountSyncSaveIndividual }, saveAccount);
38
- }
39
- return await saveAccount();
40
- }
41
- /**
42
- * Saves the list of internal accounts to the user storage.
43
- *
44
- * @param options - parameters used for saving the list of internal accounts
45
- * @param entropySourceId - The entropy source ID used to derive the key,
46
- * when multiple sources are available (Multi-SRP).
47
- * @returns Promise that resolves when all accounts are saved
48
- */
49
- export async function saveInternalAccountsListToUserStorage(options, entropySourceId) {
50
- const { getUserStorageControllerInstance } = options;
51
- const internalAccountsList = await getInternalAccountsList(options, entropySourceId);
52
- if (!internalAccountsList?.length) {
53
- return;
54
- }
55
- const internalAccountsListFormattedForUserStorage = internalAccountsList.map(mapInternalAccountToUserStorageAccount);
56
- await getUserStorageControllerInstance().performBatchSetStorage(USER_STORAGE_FEATURE_NAMES.accounts, internalAccountsListFormattedForUserStorage.map((account) => [
57
- account.a,
58
- JSON.stringify(account),
59
- ]), entropySourceId);
60
- }
61
- /**
62
- * Syncs the internal accounts list with the user storage accounts list.
63
- * 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.
64
- * 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.
65
- *
66
- * @param config - parameters used for syncing the internal accounts list with the user storage accounts list
67
- * @param options - parameters used for syncing the internal accounts list with the user storage accounts list
68
- * @param entropySourceId - The entropy source ID used to derive the key,
69
- * @returns Promise that resolves when synchronization is complete
70
- */
71
- export async function syncInternalAccountsWithUserStorage(config, options, entropySourceId) {
72
- const { trace } = options;
73
- const performAccountSync = async () => {
74
- if (!canPerformAccountSyncing(options)) {
75
- return;
76
- }
77
- const { maxNumberOfAccountsToAdd = Infinity, onAccountAdded, onAccountNameUpdated, onAccountSyncErroneousSituation, } = config;
78
- const { getMessenger, getUserStorageControllerInstance } = options;
79
- try {
80
- await getUserStorageControllerInstance().setIsAccountSyncingInProgress(true);
81
- const userStorageAccountsList = await getUserStorageAccountsList(options, entropySourceId);
82
- if (!userStorageAccountsList || !userStorageAccountsList.length) {
83
- await saveInternalAccountsListToUserStorage(options, entropySourceId);
84
- return;
85
- }
86
- // Keep a record if erroneous situations are found during the sync
87
- // This is done so we can send the context to Sentry in case of an erroneous situation
88
- let erroneousSituationsFound = false;
89
- // Prepare an array of internal accounts to be saved to the user storage
90
- const internalAccountsToBeSavedToUserStorage = [];
91
- // Compare internal accounts list with user storage accounts list
92
- // First step: compare lengths
93
- const internalAccountsList = await getInternalAccountsList(options, entropySourceId);
94
- if (!internalAccountsList || !internalAccountsList.length) {
95
- throw new Error(`Failed to get internal accounts list`);
96
- }
97
- const hasMoreUserStorageAccountsThanInternalAccounts = userStorageAccountsList.length > internalAccountsList.length;
98
- // We don't want to remove existing accounts for a user
99
- // so we only add new accounts if the user has more accounts in user storage than internal accounts
100
- if (hasMoreUserStorageAccountsThanInternalAccounts) {
101
- const numberOfAccountsToAdd = Math.min(userStorageAccountsList.length, maxNumberOfAccountsToAdd) -
102
- internalAccountsList.length;
103
- // Create new accounts to match the user storage accounts list
104
- await getMessenger().call('KeyringController:withKeyring', {
105
- id: entropySourceId,
106
- }, async ({ keyring }) => {
107
- await keyring.addAccounts(numberOfAccountsToAdd);
108
- });
109
- // TODO: below code is kept for analytics but should probably be re-thought
110
- for (let i = 0; i < numberOfAccountsToAdd; i++) {
111
- onAccountAdded?.();
112
- }
113
- }
114
- // Second step: compare account names
115
- // Get the internal accounts list again since new accounts might have been added in the previous step
116
- const refreshedInternalAccountsList = await getInternalAccountsList(options, entropySourceId);
117
- const newlyAddedAccounts = refreshedInternalAccountsList.filter((account) => !internalAccountsList.find((a) => a.address === account.address));
118
- for (const internalAccount of refreshedInternalAccountsList) {
119
- const userStorageAccount = userStorageAccountsList.find((account) => account.a === internalAccount.address);
120
- // If the account is not present in user storage
121
- // istanbul ignore next
122
- if (!userStorageAccount) {
123
- // If the account was just added in the previous step, skip saving it, it's likely to be a bogus account
124
- if (newlyAddedAccounts.includes(internalAccount)) {
125
- erroneousSituationsFound = true;
126
- onAccountSyncErroneousSituation?.('An account was added to the internal accounts list but was not present in the user storage accounts list', {
127
- internalAccount,
128
- userStorageAccount,
129
- newlyAddedAccounts,
130
- userStorageAccountsList,
131
- internalAccountsList,
132
- refreshedInternalAccountsList,
133
- internalAccountsToBeSavedToUserStorage,
134
- });
135
- continue;
136
- }
137
- // Otherwise, it means that this internal account was present before the sync, and needs to be saved to the user storage
138
- internalAccountsToBeSavedToUserStorage.push(internalAccount);
139
- continue;
140
- }
141
- // From this point on, we know that the account is present in
142
- // both the internal accounts list and the user storage accounts list
143
- // One or both accounts have default names
144
- const isInternalAccountNameDefault = isNameDefaultAccountName(internalAccount.metadata.name);
145
- const isUserStorageAccountNameDefault = isNameDefaultAccountName(userStorageAccount.n);
146
- // Internal account has default name
147
- if (isInternalAccountNameDefault) {
148
- if (!isUserStorageAccountNameDefault) {
149
- getMessenger().call('AccountsController:updateAccountMetadata', internalAccount.id, {
150
- name: userStorageAccount.n,
151
- });
152
- onAccountNameUpdated?.();
153
- }
154
- continue;
155
- }
156
- // Internal account has custom name but user storage account has default name
157
- if (isUserStorageAccountNameDefault) {
158
- internalAccountsToBeSavedToUserStorage.push(internalAccount);
159
- continue;
160
- }
161
- // Both accounts have custom names
162
- // User storage account has a nameLastUpdatedAt timestamp
163
- // Note: not storing the undefined checks in constants to act as a type guard
164
- if (userStorageAccount.nlu !== undefined) {
165
- if (internalAccount.metadata.nameLastUpdatedAt !== undefined) {
166
- const isInternalAccountNameNewer = internalAccount.metadata.nameLastUpdatedAt >
167
- userStorageAccount.nlu;
168
- if (isInternalAccountNameNewer) {
169
- internalAccountsToBeSavedToUserStorage.push(internalAccount);
170
- continue;
171
- }
172
- }
173
- getMessenger().call('AccountsController:updateAccountMetadata', internalAccount.id, {
174
- name: userStorageAccount.n,
175
- nameLastUpdatedAt: userStorageAccount.nlu,
176
- });
177
- const areInternalAndUserStorageAccountNamesEqual = internalAccount.metadata.name === userStorageAccount.n;
178
- if (!areInternalAndUserStorageAccountNamesEqual) {
179
- onAccountNameUpdated?.();
180
- }
181
- continue;
182
- }
183
- else if (internalAccount.metadata.nameLastUpdatedAt !== undefined) {
184
- internalAccountsToBeSavedToUserStorage.push(internalAccount);
185
- continue;
186
- }
187
- }
188
- // Save the internal accounts list to the user storage
189
- if (internalAccountsToBeSavedToUserStorage.length) {
190
- await getUserStorageControllerInstance().performBatchSetStorage(USER_STORAGE_FEATURE_NAMES.accounts, internalAccountsToBeSavedToUserStorage.map((account) => [
191
- account.address,
192
- JSON.stringify(mapInternalAccountToUserStorageAccount(account)),
193
- ]), entropySourceId);
194
- }
195
- // In case we have corrupted user storage with accounts that don't exist in the internal accounts list
196
- // Delete those accounts from the user storage
197
- const userStorageAccountsToBeDeleted = userStorageAccountsList.filter((account) => !refreshedInternalAccountsList.find((a) => a.address === account.a));
198
- if (userStorageAccountsToBeDeleted.length) {
199
- await getUserStorageControllerInstance().performBatchDeleteStorage(USER_STORAGE_FEATURE_NAMES.accounts, userStorageAccountsToBeDeleted.map((account) => account.a), entropySourceId);
200
- erroneousSituationsFound = true;
201
- onAccountSyncErroneousSituation?.('An account was present in the user storage accounts list but was not found in the internal accounts list after the sync', {
202
- userStorageAccountsToBeDeleted,
203
- internalAccountsList,
204
- refreshedInternalAccountsList,
205
- internalAccountsToBeSavedToUserStorage,
206
- userStorageAccountsList,
207
- });
208
- }
209
- if (erroneousSituationsFound) {
210
- const [finalUserStorageAccountsList, finalInternalAccountsList] = await Promise.all([
211
- getUserStorageAccountsList(options, entropySourceId),
212
- getInternalAccountsList(options, entropySourceId),
213
- ]);
214
- const doesEveryAccountInInternalAccountsListExistInUserStorageAccountsList = finalInternalAccountsList.every((account) => finalUserStorageAccountsList?.some((userStorageAccount) => userStorageAccount.a === account.address));
215
- // istanbul ignore next
216
- const doesEveryAccountInUserStorageAccountsListExistInInternalAccountsList = (finalUserStorageAccountsList?.length || 0) > maxNumberOfAccountsToAdd
217
- ? true
218
- : finalUserStorageAccountsList?.every((account) => finalInternalAccountsList.some((internalAccount) => internalAccount.address === account.a));
219
- const doFinalListsMatch = doesEveryAccountInInternalAccountsListExistInUserStorageAccountsList &&
220
- doesEveryAccountInUserStorageAccountsListExistInInternalAccountsList;
221
- const context = {
222
- finalUserStorageAccountsList,
223
- finalInternalAccountsList,
224
- };
225
- if (doFinalListsMatch) {
226
- onAccountSyncErroneousSituation?.('Erroneous situations were found during the sync, but final state matches the expected state', context);
227
- }
228
- else {
229
- onAccountSyncErroneousSituation?.('Erroneous situations were found during the sync, and final state does not match the expected state', context);
230
- }
231
- }
232
- }
233
- catch (e) {
234
- // istanbul ignore next
235
- const errorMessage = e instanceof Error ? e.message : JSON.stringify(e);
236
- throw new Error(`UserStorageController - failed to sync user storage accounts list - ${errorMessage}`);
237
- }
238
- finally {
239
- await getUserStorageControllerInstance().setIsAccountSyncingInProgress(false);
240
- }
241
- };
242
- if (trace) {
243
- return await trace({ name: TraceName.AccountSyncFull }, performAccountSync);
244
- }
245
- return await performAccountSync();
246
- }
247
- //# sourceMappingURL=controller-integration.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"controller-integration.mjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/controller-integration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,qCAAqC;AAG5D,OAAO,EACL,wBAAwB,EACxB,uBAAuB,EACvB,0BAA0B,EAC3B,yBAAqB;AAEtB,OAAO,EACL,wBAAwB,EACxB,sCAAsC,EACvC,oBAAgB;AACjB,OAAO,EAAE,0BAA0B,EAAE,2CAAuC;AAC5E,OAAO,EAAE,SAAS,EAAE,yBAAqB;AAEzC;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,gCAAgC,CACpD,eAAgC,EAChC,OAA8B;IAE9B,MAAM,EAAE,KAAK,EAAE,GAAG,OAAO,CAAC;IAE1B,MAAM,WAAW,GAAG,KAAK,IAAI,EAAE;QAC7B,MAAM,EAAE,gCAAgC,EAAE,GAAG,OAAO,CAAC;QAErD,IACE,CAAC,wBAAwB,CAAC,OAAO,CAAC;YAClC,eAAe,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,KAAK,MAAM,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC,8DAA8D;UAChI;YACA,OAAO;SACR;QAED,+IAA+I;QAC/I,0DAA0D;QAC1D,MAAM,eAAe,GAAG,eAAe,CAAC,OAAO,EAAE,aAEpC,CAAC;QAEd,IAAI;YACF,8DAA8D;YAC9D,MAAM,aAAa,GACjB,sCAAsC,CAAC,eAAe,CAAC,CAAC;YAE1D,MAAM,gCAAgC,EA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AAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;YACxE,MAAM,IAAI,KAAK,CACb,uEAAuE,YAAY,EAAE,CACtF,CAAC;SACH;gBAAS;YACR,MAAM,gCAAgC,EAAE,CAAC,6BAA6B,CACpE,KAAK,CACN,CAAC;SACH;IACH,CAAC,CAAC;IAEF,IAAI,KAAK,EAAE;QACT,OAAO,MAAM,KAAK,CAAC,EAAE,IAAI,EAAE,SAAS,CAAC,eAAe,EAAE,EAAE,kBAAkB,CAAC,CAAC;KAC7E;IAED,OAAO,MAAM,kBAAkB,EAAE,CAAC;AACpC,CAAC","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 !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\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 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 // 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 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 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"]}
@@ -1,35 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.setupAccountSyncingSubscriptions = void 0;
4
- const controller_integration_1 = require("./controller-integration.cjs");
5
- const sync_utils_1 = require("./sync-utils.cjs");
6
- /**
7
- * Initialize and setup events to listen to for account syncing
8
- *
9
- * @param options - parameters used for initializing and enabling account syncing
10
- */
11
- function setupAccountSyncingSubscriptions(options) {
12
- const { getMessenger, getUserStorageControllerInstance } = options;
13
- getMessenger().subscribe('AccountsController:accountAdded', async (account) => {
14
- if (!(0, sync_utils_1.canPerformAccountSyncing)(options) ||
15
- !getUserStorageControllerInstance().state
16
- .hasAccountSyncingSyncedAtLeastOnce) {
17
- return;
18
- }
19
- const { eventQueue } = getUserStorageControllerInstance();
20
- eventQueue.push(async () => await (0, controller_integration_1.saveInternalAccountToUserStorage)(account, options));
21
- await eventQueue.run();
22
- });
23
- getMessenger().subscribe('AccountsController:accountRenamed', async (account) => {
24
- if (!(0, sync_utils_1.canPerformAccountSyncing)(options) ||
25
- !getUserStorageControllerInstance().state
26
- .hasAccountSyncingSyncedAtLeastOnce) {
27
- return;
28
- }
29
- const { eventQueue } = getUserStorageControllerInstance();
30
- eventQueue.push(async () => await (0, controller_integration_1.saveInternalAccountToUserStorage)(account, options));
31
- await eventQueue.run();
32
- });
33
- }
34
- exports.setupAccountSyncingSubscriptions = setupAccountSyncingSubscriptions;
35
- //# sourceMappingURL=setup-subscriptions.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"setup-subscriptions.cjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/setup-subscriptions.ts"],"names":[],"mappings":";;;AAAA,yEAA4E;AAC5E,iDAAwD;AAGxD;;;;GAIG;AACH,SAAgB,gCAAgC,CAC9C,OAA8B;IAE9B,MAAM,EAAE,YAAY,EAAE,gCAAgC,EAAE,GAAG,OAAO,CAAC;IAEnE,YAAY,EAAE,CAAC,SAAS,CACtB,iCAAiC,EAEjC,KAAK,EAAE,OAAO,EAAE,EAAE;QAChB,IACE,CAAC,IAAA,qCAAwB,EAAC,OAAO,CAAC;YAClC,CAAC,gCAAgC,EAAE,CAAC,KAAK;iBACtC,kCAAkC,EACrC;YACA,OAAO;SACR;QAED,MAAM,EAAE,UAAU,EAAE,GAAG,gCAAgC,EAAE,CAAC;QAE1D,UAAU,CAAC,IAAI,CACb,KAAK,IAAI,EAAE,CAAC,MAAM,IAAA,yDAAgC,EAAC,OAAO,EAAE,OAAO,CAAC,CACrE,CAAC;QACF,MAAM,UAAU,CAAC,GAAG,EAAE,CAAC;IACzB,CAAC,CACF,CAAC;IAEF,YAAY,EAAE,CAAC,SAAS,CACtB,mCAAmC,EAEnC,KAAK,EAAE,OAAO,EAAE,EAAE;QAChB,IACE,CAAC,IAAA,qCAAwB,EAAC,OAAO,CAAC;YAClC,CAAC,gCAAgC,EAAE,CAAC,KAAK;iBACtC,kCAAkC,EACrC;YACA,OAAO;SACR;QAED,MAAM,EAAE,UAAU,EAAE,GAAG,gCAAgC,EAAE,CAAC;QAE1D,UAAU,CAAC,IAAI,CACb,KAAK,IAAI,EAAE,CAAC,MAAM,IAAA,yDAAgC,EAAC,OAAO,EAAE,OAAO,CAAC,CACrE,CAAC;QACF,MAAM,UAAU,CAAC,GAAG,EAAE,CAAC;IACzB,CAAC,CACF,CAAC;AACJ,CAAC;AA9CD,4EA8CC","sourcesContent":["import { saveInternalAccountToUserStorage } from './controller-integration';\nimport { canPerformAccountSyncing } from './sync-utils';\nimport type { AccountSyncingOptions } from './types';\n\n/**\n * Initialize and setup events to listen to for account syncing\n *\n * @param options - parameters used for initializing and enabling account syncing\n */\nexport function setupAccountSyncingSubscriptions(\n options: AccountSyncingOptions,\n) {\n const { getMessenger, getUserStorageControllerInstance } = options;\n\n getMessenger().subscribe(\n 'AccountsController:accountAdded',\n\n async (account) => {\n if (\n !canPerformAccountSyncing(options) ||\n !getUserStorageControllerInstance().state\n .hasAccountSyncingSyncedAtLeastOnce\n ) {\n return;\n }\n\n const { eventQueue } = getUserStorageControllerInstance();\n\n eventQueue.push(\n async () => await saveInternalAccountToUserStorage(account, options),\n );\n await eventQueue.run();\n },\n );\n\n getMessenger().subscribe(\n 'AccountsController:accountRenamed',\n\n async (account) => {\n if (\n !canPerformAccountSyncing(options) ||\n !getUserStorageControllerInstance().state\n .hasAccountSyncingSyncedAtLeastOnce\n ) {\n return;\n }\n\n const { eventQueue } = getUserStorageControllerInstance();\n\n eventQueue.push(\n async () => await saveInternalAccountToUserStorage(account, options),\n );\n await eventQueue.run();\n },\n );\n}\n"]}
@@ -1,8 +0,0 @@
1
- import type { AccountSyncingOptions } from "./types.cjs";
2
- /**
3
- * Initialize and setup events to listen to for account syncing
4
- *
5
- * @param options - parameters used for initializing and enabling account syncing
6
- */
7
- export declare function setupAccountSyncingSubscriptions(options: AccountSyncingOptions): void;
8
- //# sourceMappingURL=setup-subscriptions.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"setup-subscriptions.d.cts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/setup-subscriptions.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,qBAAqB,EAAE,oBAAgB;AAErD;;;;GAIG;AACH,wBAAgB,gCAAgC,CAC9C,OAAO,EAAE,qBAAqB,QA6C/B"}
@@ -1,8 +0,0 @@
1
- import type { AccountSyncingOptions } from "./types.mjs";
2
- /**
3
- * Initialize and setup events to listen to for account syncing
4
- *
5
- * @param options - parameters used for initializing and enabling account syncing
6
- */
7
- export declare function setupAccountSyncingSubscriptions(options: AccountSyncingOptions): void;
8
- //# sourceMappingURL=setup-subscriptions.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"setup-subscriptions.d.mts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/setup-subscriptions.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,qBAAqB,EAAE,oBAAgB;AAErD;;;;GAIG;AACH,wBAAgB,gCAAgC,CAC9C,OAAO,EAAE,qBAAqB,QA6C/B"}
@@ -1,31 +0,0 @@
1
- import { saveInternalAccountToUserStorage } from "./controller-integration.mjs";
2
- import { canPerformAccountSyncing } from "./sync-utils.mjs";
3
- /**
4
- * Initialize and setup events to listen to for account syncing
5
- *
6
- * @param options - parameters used for initializing and enabling account syncing
7
- */
8
- export function setupAccountSyncingSubscriptions(options) {
9
- const { getMessenger, getUserStorageControllerInstance } = options;
10
- getMessenger().subscribe('AccountsController:accountAdded', async (account) => {
11
- if (!canPerformAccountSyncing(options) ||
12
- !getUserStorageControllerInstance().state
13
- .hasAccountSyncingSyncedAtLeastOnce) {
14
- return;
15
- }
16
- const { eventQueue } = getUserStorageControllerInstance();
17
- eventQueue.push(async () => await saveInternalAccountToUserStorage(account, options));
18
- await eventQueue.run();
19
- });
20
- getMessenger().subscribe('AccountsController:accountRenamed', async (account) => {
21
- if (!canPerformAccountSyncing(options) ||
22
- !getUserStorageControllerInstance().state
23
- .hasAccountSyncingSyncedAtLeastOnce) {
24
- return;
25
- }
26
- const { eventQueue } = getUserStorageControllerInstance();
27
- eventQueue.push(async () => await saveInternalAccountToUserStorage(account, options));
28
- await eventQueue.run();
29
- });
30
- }
31
- //# sourceMappingURL=setup-subscriptions.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"setup-subscriptions.mjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/setup-subscriptions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gCAAgC,EAAE,qCAAiC;AAC5E,OAAO,EAAE,wBAAwB,EAAE,yBAAqB;AAGxD;;;;GAIG;AACH,MAAM,UAAU,gCAAgC,CAC9C,OAA8B;IAE9B,MAAM,EAAE,YAAY,EAAE,gCAAgC,EAAE,GAAG,OAAO,CAAC;IAEnE,YAAY,EAAE,CAAC,SAAS,CACtB,iCAAiC,EAEjC,KAAK,EAAE,OAAO,EAAE,EAAE;QAChB,IACE,CAAC,wBAAwB,CAAC,OAAO,CAAC;YAClC,CAAC,gCAAgC,EAAE,CAAC,KAAK;iBACtC,kCAAkC,EACrC;YACA,OAAO;SACR;QAED,MAAM,EAAE,UAAU,EAAE,GAAG,gCAAgC,EAAE,CAAC;QAE1D,UAAU,CAAC,IAAI,CACb,KAAK,IAAI,EAAE,CAAC,MAAM,gCAAgC,CAAC,OAAO,EAAE,OAAO,CAAC,CACrE,CAAC;QACF,MAAM,UAAU,CAAC,GAAG,EAAE,CAAC;IACzB,CAAC,CACF,CAAC;IAEF,YAAY,EAAE,CAAC,SAAS,CACtB,mCAAmC,EAEnC,KAAK,EAAE,OAAO,EAAE,EAAE;QAChB,IACE,CAAC,wBAAwB,CAAC,OAAO,CAAC;YAClC,CAAC,gCAAgC,EAAE,CAAC,KAAK;iBACtC,kCAAkC,EACrC;YACA,OAAO;SACR;QAED,MAAM,EAAE,UAAU,EAAE,GAAG,gCAAgC,EAAE,CAAC;QAE1D,UAAU,CAAC,IAAI,CACb,KAAK,IAAI,EAAE,CAAC,MAAM,gCAAgC,CAAC,OAAO,EAAE,OAAO,CAAC,CACrE,CAAC;QACF,MAAM,UAAU,CAAC,GAAG,EAAE,CAAC;IACzB,CAAC,CACF,CAAC;AACJ,CAAC","sourcesContent":["import { saveInternalAccountToUserStorage } from './controller-integration';\nimport { canPerformAccountSyncing } from './sync-utils';\nimport type { AccountSyncingOptions } from './types';\n\n/**\n * Initialize and setup events to listen to for account syncing\n *\n * @param options - parameters used for initializing and enabling account syncing\n */\nexport function setupAccountSyncingSubscriptions(\n options: AccountSyncingOptions,\n) {\n const { getMessenger, getUserStorageControllerInstance } = options;\n\n getMessenger().subscribe(\n 'AccountsController:accountAdded',\n\n async (account) => {\n if (\n !canPerformAccountSyncing(options) ||\n !getUserStorageControllerInstance().state\n .hasAccountSyncingSyncedAtLeastOnce\n ) {\n return;\n }\n\n const { eventQueue } = getUserStorageControllerInstance();\n\n eventQueue.push(\n async () => await saveInternalAccountToUserStorage(account, options),\n );\n await eventQueue.run();\n },\n );\n\n getMessenger().subscribe(\n 'AccountsController:accountRenamed',\n\n async (account) => {\n if (\n !canPerformAccountSyncing(options) ||\n !getUserStorageControllerInstance().state\n .hasAccountSyncingSyncedAtLeastOnce\n ) {\n return;\n }\n\n const { eventQueue } = getUserStorageControllerInstance();\n\n eventQueue.push(\n async () => await saveInternalAccountToUserStorage(account, options),\n );\n await eventQueue.run();\n },\n );\n}\n"]}
@@ -1,61 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getUserStorageAccountsList = exports.getInternalAccountsList = exports.canPerformAccountSyncing = void 0;
4
- const keyring_controller_1 = require("@metamask/keyring-controller");
5
- const storage_schema_1 = require("../../../shared/storage-schema.cjs");
6
- /**
7
- * Checks if account syncing can be performed based on a set of conditions
8
- *
9
- * @param options - parameters used for checking if account syncing can be performed
10
- * @returns Returns true if account syncing can be performed, false otherwise.
11
- */
12
- function canPerformAccountSyncing(options) {
13
- const { getMessenger, getUserStorageControllerInstance } = options;
14
- const { isBackupAndSyncEnabled, isAccountSyncingEnabled, isAccountSyncingInProgress, } = getUserStorageControllerInstance().state;
15
- const isAuthEnabled = getMessenger().call('AuthenticationController:isSignedIn');
16
- if (!isBackupAndSyncEnabled ||
17
- !isAccountSyncingEnabled ||
18
- !isAuthEnabled ||
19
- isAccountSyncingInProgress) {
20
- return false;
21
- }
22
- return true;
23
- }
24
- exports.canPerformAccountSyncing = canPerformAccountSyncing;
25
- /**
26
- * Get the list of internal accounts
27
- * This function returns only the internal accounts that are from the primary SRP
28
- * and are from the HD keyring
29
- *
30
- * @param options - parameters used for getting the list of internal accounts
31
- * @param entropySourceId - The entropy source ID used to derive the key,
32
- * when multiple sources are available (Multi-SRP).
33
- * @returns the list of internal accounts
34
- */
35
- async function getInternalAccountsList(options, entropySourceId) {
36
- const { getMessenger } = options;
37
- let internalAccountsList = getMessenger().call('AccountsController:listAccounts');
38
- const doEachInternalAccountHaveEntropySource = internalAccountsList.every((account) => Boolean(account.options.entropySource));
39
- if (!doEachInternalAccountHaveEntropySource) {
40
- await getMessenger().call('AccountsController:updateAccounts');
41
- internalAccountsList = getMessenger().call('AccountsController:listAccounts');
42
- }
43
- return internalAccountsList.filter((account) => entropySourceId === account.options.entropySource &&
44
- account.metadata.keyring.type === String(keyring_controller_1.KeyringTypes.hd));
45
- }
46
- exports.getInternalAccountsList = getInternalAccountsList;
47
- /**
48
- * Get the list of user storage accounts
49
- *
50
- * @param options - parameters used for getting the list of user storage accounts
51
- * @param entropySourceId - The entropy source ID used to derive the storage key,
52
- * when multiple sources are available (Multi-SRP).
53
- * @returns the list of user storage accounts
54
- */
55
- async function getUserStorageAccountsList(options, entropySourceId) {
56
- const { getUserStorageControllerInstance } = options;
57
- const rawAccountsListResponse = await getUserStorageControllerInstance().performGetStorageAllFeatureEntries(storage_schema_1.USER_STORAGE_FEATURE_NAMES.accounts, entropySourceId);
58
- return (rawAccountsListResponse?.map((rawAccount) => JSON.parse(rawAccount)) ?? null);
59
- }
60
- exports.getUserStorageAccountsList = getUserStorageAccountsList;
61
- //# sourceMappingURL=sync-utils.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sync-utils.cjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/sync-utils.ts"],"names":[],"mappings":";;;AAAA,qEAA4D;AAI5D,uEAA4E;AAE5E;;;;;GAKG;AACH,SAAgB,wBAAwB,CACtC,OAA8B;IAE9B,MAAM,EAAE,YAAY,EAAE,gCAAgC,EAAE,GAAG,OAAO,CAAC;IAEnE,MAAM,EACJ,sBAAsB,EACtB,uBAAuB,EACvB,0BAA0B,GAC3B,GAAG,gCAAgC,EAAE,CAAC,KAAK,CAAC;IAC7C,MAAM,aAAa,GAAG,YAAY,EAAE,CAAC,IAAI,CACvC,qCAAqC,CACtC,CAAC;IAEF,IACE,CAAC,sBAAsB;QACvB,CAAC,uBAAuB;QACxB,CAAC,aAAa;QACd,0BAA0B,EAC1B;QACA,OAAO,KAAK,CAAC;KACd;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAxBD,4DAwBC;AAED;;;;;;;;;GASG;AACI,KAAK,UAAU,uBAAuB,CAC3C,OAA8B,EAC9B,eAAuB;IAEvB,MAAM,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC;IAEjC,IAAI,oBAAoB,GAAG,YAAY,EAAE,CAAC,IAAI,CAC5C,iCAAiC,CAClC,CAAC;IAEF,MAAM,sCAAsC,GAAG,oBAAoB,CAAC,KAAK,CACvE,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC,CACpD,CAAC;IAEF,IAAI,CAAC,sCAAsC,EAAE;QAC3C,MAAM,YAAY,EAAE,CAAC,IAAI,CAAC,mCAAmC,CAAC,CAAC;QAC/D,oBAAoB,GAAG,YAAY,EAAE,CAAC,IAAI,CACxC,iCAAiC,CAClC,CAAC;KACH;IAED,OAAO,oBAAoB,CAAC,MAAM,CAChC,CAAC,OAAO,EAAE,EAAE,CACV,eAAe,KAAK,OAAO,CAAC,OAAO,CAAC,aAAa;QACjD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,KAAK,MAAM,CAAC,iCAAY,CAAC,EAAE,CAAC,CAC5D,CAAC;AACJ,CAAC;AA1BD,0DA0BC;AAED;;;;;;;GAOG;AACI,KAAK,UAAU,0BAA0B,CAC9C,OAA8B,EAC9B,eAAwB;IAExB,MAAM,EAAE,gCAAgC,EAAE,GAAG,OAAO,CAAC;IAErD,MAAM,uBAAuB,GAC3B,MAAM,gCAAgC,EAAE,CAAC,kCAAkC,CACzE,2CAA0B,CAAC,QAAQ,EACnC,eAAe,CAChB,CAAC;IAEJ,OAAO,CACL,uBAAuB,EAAE,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,IAAI,IAAI,CAC7E,CAAC;AACJ,CAAC;AAfD,gEAeC","sourcesContent":["import { KeyringTypes } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport type { AccountSyncingOptions, UserStorageAccount } from './types';\nimport { USER_STORAGE_FEATURE_NAMES } from '../../../shared/storage-schema';\n\n/**\n * Checks if account syncing can be performed based on a set of conditions\n *\n * @param options - parameters used for checking if account syncing can be performed\n * @returns Returns true if account syncing can be performed, false otherwise.\n */\nexport function canPerformAccountSyncing(\n options: AccountSyncingOptions,\n): boolean {\n const { getMessenger, getUserStorageControllerInstance } = options;\n\n const {\n isBackupAndSyncEnabled,\n isAccountSyncingEnabled,\n isAccountSyncingInProgress,\n } = getUserStorageControllerInstance().state;\n const isAuthEnabled = getMessenger().call(\n 'AuthenticationController:isSignedIn',\n );\n\n if (\n !isBackupAndSyncEnabled ||\n !isAccountSyncingEnabled ||\n !isAuthEnabled ||\n isAccountSyncingInProgress\n ) {\n return false;\n }\n\n return true;\n}\n\n/**\n * Get the list of internal accounts\n * This function returns only the internal accounts that are from the primary SRP\n * and are from the HD keyring\n *\n * @param options - parameters used for getting 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 the list of internal accounts\n */\nexport async function getInternalAccountsList(\n options: AccountSyncingOptions,\n entropySourceId: string,\n): Promise<InternalAccount[]> {\n const { getMessenger } = options;\n\n let internalAccountsList = getMessenger().call(\n 'AccountsController:listAccounts',\n );\n\n const doEachInternalAccountHaveEntropySource = internalAccountsList.every(\n (account) => Boolean(account.options.entropySource),\n );\n\n if (!doEachInternalAccountHaveEntropySource) {\n await getMessenger().call('AccountsController:updateAccounts');\n internalAccountsList = getMessenger().call(\n 'AccountsController:listAccounts',\n );\n }\n\n return internalAccountsList.filter(\n (account) =>\n entropySourceId === account.options.entropySource &&\n account.metadata.keyring.type === String(KeyringTypes.hd), // sync only EVM accounts until we support multichain accounts\n );\n}\n\n/**\n * Get the list of user storage accounts\n *\n * @param options - parameters used for getting the list of user storage accounts\n * @param entropySourceId - The entropy source ID used to derive the storage key,\n * when multiple sources are available (Multi-SRP).\n * @returns the list of user storage accounts\n */\nexport async function getUserStorageAccountsList(\n options: AccountSyncingOptions,\n entropySourceId?: string,\n): Promise<UserStorageAccount[] | null> {\n const { getUserStorageControllerInstance } = options;\n\n const rawAccountsListResponse =\n await getUserStorageControllerInstance().performGetStorageAllFeatureEntries(\n USER_STORAGE_FEATURE_NAMES.accounts,\n entropySourceId,\n );\n\n return (\n rawAccountsListResponse?.map((rawAccount) => JSON.parse(rawAccount)) ?? null\n );\n}\n"]}
@@ -1,30 +0,0 @@
1
- import type { InternalAccount } from "@metamask/keyring-internal-api";
2
- import type { AccountSyncingOptions, UserStorageAccount } from "./types.cjs";
3
- /**
4
- * Checks if account syncing can be performed based on a set of conditions
5
- *
6
- * @param options - parameters used for checking if account syncing can be performed
7
- * @returns Returns true if account syncing can be performed, false otherwise.
8
- */
9
- export declare function canPerformAccountSyncing(options: AccountSyncingOptions): boolean;
10
- /**
11
- * Get the list of internal accounts
12
- * This function returns only the internal accounts that are from the primary SRP
13
- * and are from the HD keyring
14
- *
15
- * @param options - parameters used for getting the list of internal accounts
16
- * @param entropySourceId - The entropy source ID used to derive the key,
17
- * when multiple sources are available (Multi-SRP).
18
- * @returns the list of internal accounts
19
- */
20
- export declare function getInternalAccountsList(options: AccountSyncingOptions, entropySourceId: string): Promise<InternalAccount[]>;
21
- /**
22
- * Get the list of user storage accounts
23
- *
24
- * @param options - parameters used for getting the list of user storage accounts
25
- * @param entropySourceId - The entropy source ID used to derive the storage key,
26
- * when multiple sources are available (Multi-SRP).
27
- * @returns the list of user storage accounts
28
- */
29
- export declare function getUserStorageAccountsList(options: AccountSyncingOptions, entropySourceId?: string): Promise<UserStorageAccount[] | null>;
30
- //# sourceMappingURL=sync-utils.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sync-utils.d.cts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/sync-utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAEtE,OAAO,KAAK,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,oBAAgB;AAGzE;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAsBT;AAED;;;;;;;;;GASG;AACH,wBAAsB,uBAAuB,CAC3C,OAAO,EAAE,qBAAqB,EAC9B,eAAe,EAAE,MAAM,GACtB,OAAO,CAAC,eAAe,EAAE,CAAC,CAuB5B;AAED;;;;;;;GAOG;AACH,wBAAsB,0BAA0B,CAC9C,OAAO,EAAE,qBAAqB,EAC9B,eAAe,CAAC,EAAE,MAAM,GACvB,OAAO,CAAC,kBAAkB,EAAE,GAAG,IAAI,CAAC,CAYtC"}
@@ -1,30 +0,0 @@
1
- import type { InternalAccount } from "@metamask/keyring-internal-api";
2
- import type { AccountSyncingOptions, UserStorageAccount } from "./types.mjs";
3
- /**
4
- * Checks if account syncing can be performed based on a set of conditions
5
- *
6
- * @param options - parameters used for checking if account syncing can be performed
7
- * @returns Returns true if account syncing can be performed, false otherwise.
8
- */
9
- export declare function canPerformAccountSyncing(options: AccountSyncingOptions): boolean;
10
- /**
11
- * Get the list of internal accounts
12
- * This function returns only the internal accounts that are from the primary SRP
13
- * and are from the HD keyring
14
- *
15
- * @param options - parameters used for getting the list of internal accounts
16
- * @param entropySourceId - The entropy source ID used to derive the key,
17
- * when multiple sources are available (Multi-SRP).
18
- * @returns the list of internal accounts
19
- */
20
- export declare function getInternalAccountsList(options: AccountSyncingOptions, entropySourceId: string): Promise<InternalAccount[]>;
21
- /**
22
- * Get the list of user storage accounts
23
- *
24
- * @param options - parameters used for getting the list of user storage accounts
25
- * @param entropySourceId - The entropy source ID used to derive the storage key,
26
- * when multiple sources are available (Multi-SRP).
27
- * @returns the list of user storage accounts
28
- */
29
- export declare function getUserStorageAccountsList(options: AccountSyncingOptions, entropySourceId?: string): Promise<UserStorageAccount[] | null>;
30
- //# sourceMappingURL=sync-utils.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sync-utils.d.mts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/sync-utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAEtE,OAAO,KAAK,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,oBAAgB;AAGzE;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAsBT;AAED;;;;;;;;;GASG;AACH,wBAAsB,uBAAuB,CAC3C,OAAO,EAAE,qBAAqB,EAC9B,eAAe,EAAE,MAAM,GACtB,OAAO,CAAC,eAAe,EAAE,CAAC,CAuB5B;AAED;;;;;;;GAOG;AACH,wBAAsB,0BAA0B,CAC9C,OAAO,EAAE,qBAAqB,EAC9B,eAAe,CAAC,EAAE,MAAM,GACvB,OAAO,CAAC,kBAAkB,EAAE,GAAG,IAAI,CAAC,CAYtC"}
@@ -1,55 +0,0 @@
1
- import { KeyringTypes } from "@metamask/keyring-controller";
2
- import { USER_STORAGE_FEATURE_NAMES } from "../../../shared/storage-schema.mjs";
3
- /**
4
- * Checks if account syncing can be performed based on a set of conditions
5
- *
6
- * @param options - parameters used for checking if account syncing can be performed
7
- * @returns Returns true if account syncing can be performed, false otherwise.
8
- */
9
- export function canPerformAccountSyncing(options) {
10
- const { getMessenger, getUserStorageControllerInstance } = options;
11
- const { isBackupAndSyncEnabled, isAccountSyncingEnabled, isAccountSyncingInProgress, } = getUserStorageControllerInstance().state;
12
- const isAuthEnabled = getMessenger().call('AuthenticationController:isSignedIn');
13
- if (!isBackupAndSyncEnabled ||
14
- !isAccountSyncingEnabled ||
15
- !isAuthEnabled ||
16
- isAccountSyncingInProgress) {
17
- return false;
18
- }
19
- return true;
20
- }
21
- /**
22
- * Get the list of internal accounts
23
- * This function returns only the internal accounts that are from the primary SRP
24
- * and are from the HD keyring
25
- *
26
- * @param options - parameters used for getting the list of internal accounts
27
- * @param entropySourceId - The entropy source ID used to derive the key,
28
- * when multiple sources are available (Multi-SRP).
29
- * @returns the list of internal accounts
30
- */
31
- export async function getInternalAccountsList(options, entropySourceId) {
32
- const { getMessenger } = options;
33
- let internalAccountsList = getMessenger().call('AccountsController:listAccounts');
34
- const doEachInternalAccountHaveEntropySource = internalAccountsList.every((account) => Boolean(account.options.entropySource));
35
- if (!doEachInternalAccountHaveEntropySource) {
36
- await getMessenger().call('AccountsController:updateAccounts');
37
- internalAccountsList = getMessenger().call('AccountsController:listAccounts');
38
- }
39
- return internalAccountsList.filter((account) => entropySourceId === account.options.entropySource &&
40
- account.metadata.keyring.type === String(KeyringTypes.hd));
41
- }
42
- /**
43
- * Get the list of user storage accounts
44
- *
45
- * @param options - parameters used for getting the list of user storage accounts
46
- * @param entropySourceId - The entropy source ID used to derive the storage key,
47
- * when multiple sources are available (Multi-SRP).
48
- * @returns the list of user storage accounts
49
- */
50
- export async function getUserStorageAccountsList(options, entropySourceId) {
51
- const { getUserStorageControllerInstance } = options;
52
- const rawAccountsListResponse = await getUserStorageControllerInstance().performGetStorageAllFeatureEntries(USER_STORAGE_FEATURE_NAMES.accounts, entropySourceId);
53
- return (rawAccountsListResponse?.map((rawAccount) => JSON.parse(rawAccount)) ?? null);
54
- }
55
- //# sourceMappingURL=sync-utils.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sync-utils.mjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/sync-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,qCAAqC;AAI5D,OAAO,EAAE,0BAA0B,EAAE,2CAAuC;AAE5E;;;;;GAKG;AACH,MAAM,UAAU,wBAAwB,CACtC,OAA8B;IAE9B,MAAM,EAAE,YAAY,EAAE,gCAAgC,EAAE,GAAG,OAAO,CAAC;IAEnE,MAAM,EACJ,sBAAsB,EACtB,uBAAuB,EACvB,0BAA0B,GAC3B,GAAG,gCAAgC,EAAE,CAAC,KAAK,CAAC;IAC7C,MAAM,aAAa,GAAG,YAAY,EAAE,CAAC,IAAI,CACvC,qCAAqC,CACtC,CAAC;IAEF,IACE,CAAC,sBAAsB;QACvB,CAAC,uBAAuB;QACxB,CAAC,aAAa;QACd,0BAA0B,EAC1B;QACA,OAAO,KAAK,CAAC;KACd;IAED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type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport type { AccountSyncingOptions, UserStorageAccount } from './types';\nimport { USER_STORAGE_FEATURE_NAMES } from '../../../shared/storage-schema';\n\n/**\n * Checks if account syncing can be performed based on a set of conditions\n *\n * @param options - parameters used for checking if account syncing can be performed\n * @returns Returns true if account syncing can be performed, false otherwise.\n */\nexport function canPerformAccountSyncing(\n options: AccountSyncingOptions,\n): boolean {\n const { getMessenger, getUserStorageControllerInstance } = options;\n\n const {\n isBackupAndSyncEnabled,\n isAccountSyncingEnabled,\n isAccountSyncingInProgress,\n } = getUserStorageControllerInstance().state;\n const isAuthEnabled = getMessenger().call(\n 'AuthenticationController:isSignedIn',\n );\n\n if (\n !isBackupAndSyncEnabled ||\n !isAccountSyncingEnabled ||\n !isAuthEnabled ||\n isAccountSyncingInProgress\n ) {\n return false;\n }\n\n return true;\n}\n\n/**\n * Get the list of internal accounts\n * This function returns only the internal accounts that are from the primary SRP\n * and are from the HD keyring\n *\n * @param options - parameters used for getting 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 the list of internal accounts\n */\nexport async function getInternalAccountsList(\n options: AccountSyncingOptions,\n entropySourceId: string,\n): Promise<InternalAccount[]> {\n const { getMessenger } = options;\n\n let internalAccountsList = getMessenger().call(\n 'AccountsController:listAccounts',\n );\n\n const doEachInternalAccountHaveEntropySource = internalAccountsList.every(\n (account) => Boolean(account.options.entropySource),\n );\n\n if (!doEachInternalAccountHaveEntropySource) {\n await getMessenger().call('AccountsController:updateAccounts');\n internalAccountsList = getMessenger().call(\n 'AccountsController:listAccounts',\n );\n }\n\n return internalAccountsList.filter(\n (account) =>\n entropySourceId === account.options.entropySource &&\n account.metadata.keyring.type === String(KeyringTypes.hd), // sync only EVM accounts until we support multichain accounts\n );\n}\n\n/**\n * Get the list of user storage accounts\n *\n * @param options - parameters used for getting the list of user storage accounts\n * @param entropySourceId - The entropy source ID used to derive the storage key,\n * when multiple sources are available (Multi-SRP).\n * @returns the list of user storage accounts\n */\nexport async function getUserStorageAccountsList(\n options: AccountSyncingOptions,\n entropySourceId?: string,\n): Promise<UserStorageAccount[] | null> {\n const { getUserStorageControllerInstance } = options;\n\n const rawAccountsListResponse =\n await getUserStorageControllerInstance().performGetStorageAllFeatureEntries(\n USER_STORAGE_FEATURE_NAMES.accounts,\n entropySourceId,\n );\n\n return (\n rawAccountsListResponse?.map((rawAccount) => JSON.parse(rawAccount)) ?? null\n );\n}\n"]}