@metamask-previews/profile-sync-controller 15.0.0-preview-e9f8bc88 → 15.0.0-preview-2723071f

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 (74) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/controllers/user-storage/UserStorageController.cjs +56 -3
  3. package/dist/controllers/user-storage/UserStorageController.cjs.map +1 -1
  4. package/dist/controllers/user-storage/UserStorageController.d.cts +40 -2
  5. package/dist/controllers/user-storage/UserStorageController.d.cts.map +1 -1
  6. package/dist/controllers/user-storage/UserStorageController.d.mts +40 -2
  7. package/dist/controllers/user-storage/UserStorageController.d.mts.map +1 -1
  8. package/dist/controllers/user-storage/UserStorageController.mjs +53 -0
  9. package/dist/controllers/user-storage/UserStorageController.mjs.map +1 -1
  10. package/dist/controllers/user-storage/constants.cjs +1 -0
  11. package/dist/controllers/user-storage/constants.cjs.map +1 -1
  12. package/dist/controllers/user-storage/constants.d.cts +1 -0
  13. package/dist/controllers/user-storage/constants.d.cts.map +1 -1
  14. package/dist/controllers/user-storage/constants.d.mts +1 -0
  15. package/dist/controllers/user-storage/constants.d.mts.map +1 -1
  16. package/dist/controllers/user-storage/constants.mjs +1 -0
  17. package/dist/controllers/user-storage/constants.mjs.map +1 -1
  18. package/dist/controllers/user-storage/contact-syncing/constants.cjs +12 -0
  19. package/dist/controllers/user-storage/contact-syncing/constants.cjs.map +1 -0
  20. package/dist/controllers/user-storage/contact-syncing/constants.d.cts +9 -0
  21. package/dist/controllers/user-storage/contact-syncing/constants.d.cts.map +1 -0
  22. package/dist/controllers/user-storage/contact-syncing/constants.d.mts +9 -0
  23. package/dist/controllers/user-storage/contact-syncing/constants.d.mts.map +1 -0
  24. package/dist/controllers/user-storage/contact-syncing/constants.mjs +9 -0
  25. package/dist/controllers/user-storage/contact-syncing/constants.mjs.map +1 -0
  26. package/dist/controllers/user-storage/contact-syncing/controller-integration.cjs +299 -0
  27. package/dist/controllers/user-storage/contact-syncing/controller-integration.cjs.map +1 -0
  28. package/dist/controllers/user-storage/contact-syncing/controller-integration.d.cts +45 -0
  29. package/dist/controllers/user-storage/contact-syncing/controller-integration.d.cts.map +1 -0
  30. package/dist/controllers/user-storage/contact-syncing/controller-integration.d.mts +45 -0
  31. package/dist/controllers/user-storage/contact-syncing/controller-integration.d.mts.map +1 -0
  32. package/dist/controllers/user-storage/contact-syncing/controller-integration.mjs +293 -0
  33. package/dist/controllers/user-storage/contact-syncing/controller-integration.mjs.map +1 -0
  34. package/dist/controllers/user-storage/contact-syncing/setup-subscriptions.cjs +55 -0
  35. package/dist/controllers/user-storage/contact-syncing/setup-subscriptions.cjs.map +1 -0
  36. package/dist/controllers/user-storage/contact-syncing/setup-subscriptions.d.cts +8 -0
  37. package/dist/controllers/user-storage/contact-syncing/setup-subscriptions.d.cts.map +1 -0
  38. package/dist/controllers/user-storage/contact-syncing/setup-subscriptions.d.mts +8 -0
  39. package/dist/controllers/user-storage/contact-syncing/setup-subscriptions.d.mts.map +1 -0
  40. package/dist/controllers/user-storage/contact-syncing/setup-subscriptions.mjs +51 -0
  41. package/dist/controllers/user-storage/contact-syncing/setup-subscriptions.mjs.map +1 -0
  42. package/dist/controllers/user-storage/contact-syncing/sync-utils.cjs +23 -0
  43. package/dist/controllers/user-storage/contact-syncing/sync-utils.cjs.map +1 -0
  44. package/dist/controllers/user-storage/contact-syncing/sync-utils.d.cts +9 -0
  45. package/dist/controllers/user-storage/contact-syncing/sync-utils.d.cts.map +1 -0
  46. package/dist/controllers/user-storage/contact-syncing/sync-utils.d.mts +9 -0
  47. package/dist/controllers/user-storage/contact-syncing/sync-utils.d.mts.map +1 -0
  48. package/dist/controllers/user-storage/contact-syncing/sync-utils.mjs +19 -0
  49. package/dist/controllers/user-storage/contact-syncing/sync-utils.mjs.map +1 -0
  50. package/dist/controllers/user-storage/contact-syncing/types.cjs +3 -0
  51. package/dist/controllers/user-storage/contact-syncing/types.cjs.map +1 -0
  52. package/dist/controllers/user-storage/contact-syncing/types.d.cts +35 -0
  53. package/dist/controllers/user-storage/contact-syncing/types.d.cts.map +1 -0
  54. package/dist/controllers/user-storage/contact-syncing/types.d.mts +35 -0
  55. package/dist/controllers/user-storage/contact-syncing/types.d.mts.map +1 -0
  56. package/dist/controllers/user-storage/contact-syncing/types.mjs +2 -0
  57. package/dist/controllers/user-storage/contact-syncing/types.mjs.map +1 -0
  58. package/dist/controllers/user-storage/contact-syncing/utils.cjs +68 -0
  59. package/dist/controllers/user-storage/contact-syncing/utils.cjs.map +1 -0
  60. package/dist/controllers/user-storage/contact-syncing/utils.d.cts +29 -0
  61. package/dist/controllers/user-storage/contact-syncing/utils.d.cts.map +1 -0
  62. package/dist/controllers/user-storage/contact-syncing/utils.d.mts +29 -0
  63. package/dist/controllers/user-storage/contact-syncing/utils.d.mts.map +1 -0
  64. package/dist/controllers/user-storage/contact-syncing/utils.mjs +63 -0
  65. package/dist/controllers/user-storage/contact-syncing/utils.mjs.map +1 -0
  66. package/dist/shared/storage-schema.cjs +3 -1
  67. package/dist/shared/storage-schema.cjs.map +1 -1
  68. package/dist/shared/storage-schema.d.cts +2 -0
  69. package/dist/shared/storage-schema.d.cts.map +1 -1
  70. package/dist/shared/storage-schema.d.mts +2 -0
  71. package/dist/shared/storage-schema.d.mts.map +1 -1
  72. package/dist/shared/storage-schema.mjs +3 -1
  73. package/dist/shared/storage-schema.mjs.map +1 -1
  74. package/package.json +2 -2
@@ -0,0 +1,299 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deleteContactInRemoteStorage = exports.updateContactInRemoteStorage = exports.syncContactsWithUserStorage = exports.ADDRESS_BOOK_CONTACTS_KEY = void 0;
4
+ const sync_utils_1 = require("./sync-utils.cjs");
5
+ const utils_1 = require("./utils.cjs");
6
+ const storage_schema_1 = require("../../../shared/storage-schema.cjs");
7
+ // Define a constant to use as the key for storing all contacts
8
+ exports.ADDRESS_BOOK_CONTACTS_KEY = 'contacts';
9
+ /**
10
+ * Creates a unique key for a contact based on chainId and address
11
+ *
12
+ * @param contact - The contact to create a key for
13
+ * @returns A unique string key
14
+ */
15
+ function createContactKey(contact) {
16
+ return `${contact.chainId}:${contact.address.toLowerCase()}`;
17
+ }
18
+ /**
19
+ * Syncs contacts between local storage and user storage (remote).
20
+ *
21
+ * Handles the following syncing scenarios:
22
+ * 1. First Sync: When local contacts exist but there are no remote contacts, uploads all local contacts.
23
+ * 2. New Device Sync: Downloads remote contacts that don't exist locally (empty local address book).
24
+ * 3. Simple Merge: Ensures both sides (local & remote) have all contacts.
25
+ * 4. Contact Naming Conflicts: When same contact has different names, uses most recent by timestamp.
26
+ * 5. Local Updates: When a contact was updated locally, syncs changes to remote if local is newer.
27
+ * 6. Remote Updates: When a contact was updated remotely, applies changes locally if remote is newer.
28
+ * 7. Local Deletions: Handled by real-time event handlers (deleteContactInRemoteStorage) to prevent false positives.
29
+ * 8. Remote Deletions: When a contact was deleted remotely, applies deletion locally.
30
+ * 9. Concurrent Updates: Resolves conflicts using timestamps to determine the winner.
31
+ * 10. Restore After Delete: If a contact is modified after being deleted, restores it.
32
+ * 11. ChainId Differences: Treats same address on different chains as separate contacts.
33
+ *
34
+ * @param config - Parameters used for syncing callbacks
35
+ * @param options - Parameters used for syncing operations
36
+ */
37
+ async function syncContactsWithUserStorage(config, options) {
38
+ const { getMessenger, getUserStorageControllerInstance } = options;
39
+ const { onContactSyncErroneousSituation, onContactUpdated, onContactDeleted, } = config;
40
+ try {
41
+ // Cannot perform sync, conditions not met
42
+ if (!(0, sync_utils_1.canPerformContactSyncing)(options)) {
43
+ return;
44
+ }
45
+ // Activate sync semaphore to prevent event loops
46
+ await getUserStorageControllerInstance().setIsContactSyncingInProgress(true);
47
+ // Get all local contacts from AddressBookController (exclude chain "*" contacts)
48
+ const localVisibleContacts = getMessenger()
49
+ .call('AddressBookController:list')
50
+ .filter((contact) => String(contact.chainId) !== '*') || [];
51
+ // Get remote contacts from user storage API
52
+ const remoteContacts = await getRemoteContacts(options);
53
+ // SCENARIO 1: First Sync - No remote contacts but local contacts exist
54
+ if (!remoteContacts || remoteContacts.length === 0) {
55
+ if (localVisibleContacts.length > 0) {
56
+ await saveContactsToUserStorage(localVisibleContacts, options);
57
+ }
58
+ return;
59
+ }
60
+ // Prepare maps for efficient lookup
61
+ const localContactsMap = new Map();
62
+ const remoteContactsMap = new Map();
63
+ localVisibleContacts.forEach((contact) => {
64
+ const key = createContactKey(contact);
65
+ localContactsMap.set(key, contact);
66
+ });
67
+ remoteContacts.forEach((contact) => {
68
+ const key = createContactKey(contact);
69
+ remoteContactsMap.set(key, contact);
70
+ });
71
+ // Lists to track contacts that need to be synced
72
+ const contactsToAddOrUpdateLocally = [];
73
+ const contactsToDeleteLocally = [];
74
+ const contactsToUpdateRemotely = [];
75
+ // SCENARIO 2 & 6: Process remote contacts - handle new device sync and remote updates
76
+ for (const remoteContact of remoteContacts) {
77
+ const key = createContactKey(remoteContact);
78
+ const localContact = localContactsMap.get(key);
79
+ // Handle remote contact based on its status and local existence
80
+ if (remoteContact.deleted) {
81
+ // SCENARIO 8: Remote deletion - should be applied locally if contact exists locally
82
+ if (localContact) {
83
+ contactsToDeleteLocally.push(remoteContact);
84
+ if (onContactDeleted) {
85
+ onContactDeleted();
86
+ }
87
+ }
88
+ }
89
+ else if (!localContact) {
90
+ // SCENARIO 2: New contact from remote - import to local
91
+ contactsToAddOrUpdateLocally.push(remoteContact);
92
+ if (onContactUpdated) {
93
+ onContactUpdated();
94
+ }
95
+ }
96
+ else {
97
+ // SCENARIO 4 & 6: Contact exists on both sides - check for conflicts
98
+ const hasContentDifference = localContact.name !== remoteContact.name ||
99
+ localContact.memo !== remoteContact.memo;
100
+ if (hasContentDifference) {
101
+ // Check timestamps to determine which version to keep
102
+ const localTimestamp = localContact.lastUpdatedAt || 0;
103
+ const remoteTimestamp = remoteContact.lastUpdatedAt || 0;
104
+ if (localTimestamp >= remoteTimestamp) {
105
+ // Local is newer (or same age) - use local version
106
+ contactsToUpdateRemotely.push(localContact);
107
+ }
108
+ else {
109
+ // Remote is newer - use remote version
110
+ contactsToAddOrUpdateLocally.push(remoteContact);
111
+ if (onContactUpdated) {
112
+ onContactUpdated();
113
+ }
114
+ }
115
+ }
116
+ // Else: content is identical, no action needed
117
+ }
118
+ }
119
+ // SCENARIO 3 & 5: Process local contacts not in remote
120
+ for (const localContact of localVisibleContacts) {
121
+ const key = createContactKey(localContact);
122
+ const remoteContact = remoteContactsMap.get(key);
123
+ if (!remoteContact) {
124
+ // New local contact - add to remote
125
+ contactsToUpdateRemotely.push(localContact);
126
+ }
127
+ }
128
+ // Apply local deletions
129
+ for (const contact of contactsToDeleteLocally) {
130
+ try {
131
+ getMessenger().call('AddressBookController:delete', contact.chainId, contact.address);
132
+ }
133
+ catch (error) {
134
+ console.error('Error deleting contact:', error);
135
+ }
136
+ }
137
+ // Apply local additions/updates
138
+ for (const contact of contactsToAddOrUpdateLocally) {
139
+ if (!contact.deleted) {
140
+ try {
141
+ getMessenger().call('AddressBookController:set', contact.address, contact.name || '', contact.chainId, contact.memo || '', contact.addressType);
142
+ }
143
+ catch (error) {
144
+ console.error('Error updating contact:', error);
145
+ }
146
+ }
147
+ }
148
+ // Apply changes to remote storage
149
+ if (contactsToUpdateRemotely.length > 0) {
150
+ // Update existing remote contacts with new contacts
151
+ const updatedRemoteContacts = [...remoteContacts];
152
+ for (const localContact of contactsToUpdateRemotely) {
153
+ const key = createContactKey(localContact);
154
+ const existingIndex = updatedRemoteContacts.findIndex((c) => createContactKey(c) === key);
155
+ const now = Date.now();
156
+ const updatedEntry = {
157
+ ...localContact,
158
+ lastUpdatedAt: now,
159
+ deleted: false, // Ensure it's not deleted
160
+ };
161
+ if (existingIndex !== -1) {
162
+ // Update existing contact
163
+ updatedRemoteContacts[existingIndex] = updatedEntry;
164
+ }
165
+ else {
166
+ // Add new contact
167
+ updatedRemoteContacts.push(updatedEntry);
168
+ }
169
+ }
170
+ // Save updated contacts to remote storage
171
+ await saveContactsToUserStorage(updatedRemoteContacts, options);
172
+ }
173
+ }
174
+ catch (error) {
175
+ if (onContactSyncErroneousSituation) {
176
+ onContactSyncErroneousSituation('Error synchronizing contacts', {
177
+ error,
178
+ });
179
+ }
180
+ }
181
+ finally {
182
+ await getUserStorageControllerInstance().setIsContactSyncingInProgress(false);
183
+ }
184
+ }
185
+ exports.syncContactsWithUserStorage = syncContactsWithUserStorage;
186
+ /**
187
+ * Retrieves remote contacts from user storage API
188
+ *
189
+ * @param options - Parameters used for retrieving remote contacts
190
+ * @returns Array of contacts from remote storage, or null if none found
191
+ */
192
+ async function getRemoteContacts(options) {
193
+ const { getUserStorageControllerInstance } = options;
194
+ try {
195
+ const remoteContactsJson = await getUserStorageControllerInstance().performGetStorage(`${storage_schema_1.USER_STORAGE_FEATURE_NAMES.addressBook}.${exports.ADDRESS_BOOK_CONTACTS_KEY}`);
196
+ if (!remoteContactsJson) {
197
+ return null;
198
+ }
199
+ // Parse the JSON and convert each entry from UserStorageContactEntry to AddressBookEntry
200
+ const remoteStorageEntries = JSON.parse(remoteContactsJson);
201
+ return remoteStorageEntries.map((entry) => (0, utils_1.mapUserStorageEntryToAddressBookEntry)(entry));
202
+ }
203
+ catch {
204
+ return null;
205
+ }
206
+ }
207
+ /**
208
+ * Saves local contacts to user storage
209
+ *
210
+ * @param contacts - The contacts to save to user storage
211
+ * @param options - Parameters used for saving contacts
212
+ */
213
+ async function saveContactsToUserStorage(contacts, options) {
214
+ const { getUserStorageControllerInstance } = options;
215
+ if (!contacts || contacts.length === 0) {
216
+ return;
217
+ }
218
+ // Convert each AddressBookEntry to UserStorageContactEntry format before saving
219
+ const storageEntries = contacts.map((contact) => (0, utils_1.mapAddressBookEntryToUserStorageEntry)(contact));
220
+ await getUserStorageControllerInstance().performSetStorage(`${storage_schema_1.USER_STORAGE_FEATURE_NAMES.addressBook}.${exports.ADDRESS_BOOK_CONTACTS_KEY}`, JSON.stringify(storageEntries));
221
+ }
222
+ /**
223
+ * Updates a single contact in remote storage without performing a full sync
224
+ * This is used when a contact is updated locally to efficiently push changes to remote
225
+ *
226
+ * @param contact - The contact that was updated locally
227
+ * @param options - Parameters used for syncing operations
228
+ */
229
+ async function updateContactInRemoteStorage(contact, options) {
230
+ if (!(0, sync_utils_1.canPerformContactSyncing)(options)) {
231
+ return;
232
+ }
233
+ try {
234
+ // Get current remote contacts or initialize empty array
235
+ const remoteContacts = (await getRemoteContacts(options)) || [];
236
+ // Find if this contact already exists in remote
237
+ const key = createContactKey(contact);
238
+ const existingIndex = remoteContacts.findIndex((c) => createContactKey(c) === key);
239
+ const updatedRemoteContacts = [...remoteContacts];
240
+ // Create an updated entry with timestamp
241
+ const updatedEntry = {
242
+ ...contact,
243
+ lastUpdatedAt: contact.lastUpdatedAt || Date.now(),
244
+ deleted: false, // Explicitly set to false in case this was previously deleted
245
+ };
246
+ if (existingIndex !== -1) {
247
+ // Update existing contact
248
+ updatedRemoteContacts[existingIndex] = updatedEntry;
249
+ }
250
+ else {
251
+ // Add as new contact
252
+ updatedRemoteContacts.push(updatedEntry);
253
+ }
254
+ // Save to remote storage
255
+ await saveContactsToUserStorage(updatedRemoteContacts, options);
256
+ }
257
+ catch (error) {
258
+ console.error('🔄 Contacts Sync: Error updating contact in remote storage', error);
259
+ }
260
+ }
261
+ exports.updateContactInRemoteStorage = updateContactInRemoteStorage;
262
+ /**
263
+ * Marks a single contact as deleted in remote storage without performing a full sync
264
+ * This is used when a contact is deleted locally to efficiently push the deletion to remote
265
+ *
266
+ * @param contact - The contact that was deleted locally (contains at least address and chainId)
267
+ * @param options - Parameters used for syncing operations
268
+ */
269
+ async function deleteContactInRemoteStorage(contact, options) {
270
+ if (!(0, sync_utils_1.canPerformContactSyncing)(options)) {
271
+ return;
272
+ }
273
+ try {
274
+ // Get current remote contacts
275
+ const remoteContacts = (await getRemoteContacts(options)) || [];
276
+ // Find the contact in remote storage
277
+ const key = createContactKey(contact);
278
+ const existingIndex = remoteContacts.findIndex((c) => createContactKey(c) === key);
279
+ // If the contact exists in remote storage
280
+ if (existingIndex !== -1) {
281
+ const updatedRemoteContacts = [...remoteContacts];
282
+ const now = Date.now();
283
+ // Mark the contact as deleted
284
+ updatedRemoteContacts[existingIndex] = {
285
+ ...updatedRemoteContacts[existingIndex],
286
+ deleted: true,
287
+ deletedAt: now,
288
+ lastUpdatedAt: now,
289
+ };
290
+ // Save to remote storage
291
+ await saveContactsToUserStorage(updatedRemoteContacts, options);
292
+ }
293
+ }
294
+ catch (error) {
295
+ console.error('🔄 Contacts Sync: Error marking contact as deleted in remote storage', error);
296
+ }
297
+ }
298
+ exports.deleteContactInRemoteStorage = deleteContactInRemoteStorage;
299
+ //# sourceMappingURL=controller-integration.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"controller-integration.cjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/contact-syncing/controller-integration.ts"],"names":[],"mappings":";;;AAEA,iDAAwD;AAGxD,uCAIiB;AACjB,uEAA4E;AAE5E,+DAA+D;AAClD,QAAA,yBAAyB,GAAG,UAAU,CAAC;AAWpD;;;;;GAKG;AACH,SAAS,gBAAgB,CAAC,OAAyB;IACjD,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,WAAW,EAAE,EAAE,CAAC;AAC/D,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACI,KAAK,UAAU,2BAA2B,CAC/C,MAAyC,EACzC,OAA8B;IAE9B,MAAM,EAAE,YAAY,EAAE,gCAAgC,EAAE,GAAG,OAAO,CAAC;IACnE,MAAM,EACJ,+BAA+B,EAC/B,gBAAgB,EAChB,gBAAgB,GACjB,GAAG,MAAM,CAAC;IAEX,IAAI;QACF,0CAA0C;QAC1C,IAAI,CAAC,IAAA,qCAAwB,EAAC,OAAO,CAAC,EAAE;YACtC,OAAO;SACR;QAED,iDAAiD;QACjD,MAAM,gCAAgC,EAAE,CAAC,6BAA6B,CACpE,IAAI,CACL,CAAC;QAEF,iFA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jE;KACF;IAAC,OAAO,KAAK,EAAE;QACd,OAAO,CAAC,KAAK,CACX,sEAAsE,EACtE,KAAK,CACN,CAAC;KACH;AACH,CAAC;AAxCD,oEAwCC","sourcesContent":["import type { AddressBookEntry } from '@metamask/address-book-controller';\n\nimport { canPerformContactSyncing } from './sync-utils';\nimport type { ContactSyncingOptions } from './types';\nimport type { UserStorageContactEntry } from './types';\nimport {\n mapAddressBookEntryToUserStorageEntry,\n mapUserStorageEntryToAddressBookEntry,\n type SyncAddressBookEntry,\n} from './utils';\nimport { USER_STORAGE_FEATURE_NAMES } from '../../../shared/storage-schema';\n\n// Define a constant to use as the key for storing all contacts\nexport const ADDRESS_BOOK_CONTACTS_KEY = 'contacts';\n\nexport type SyncContactsWithUserStorageConfig = {\n onContactSyncErroneousSituation?: (\n errorMessage: string,\n sentryContext?: Record<string, unknown>,\n ) => void;\n onContactUpdated?: () => void;\n onContactDeleted?: () => void;\n};\n\n/**\n * Creates a unique key for a contact based on chainId and address\n *\n * @param contact - The contact to create a key for\n * @returns A unique string key\n */\nfunction createContactKey(contact: AddressBookEntry): string {\n return `${contact.chainId}:${contact.address.toLowerCase()}`;\n}\n\n/**\n * Syncs contacts between local storage and user storage (remote).\n *\n * Handles the following syncing scenarios:\n * 1. First Sync: When local contacts exist but there are no remote contacts, uploads all local contacts.\n * 2. New Device Sync: Downloads remote contacts that don't exist locally (empty local address book).\n * 3. Simple Merge: Ensures both sides (local & remote) have all contacts.\n * 4. Contact Naming Conflicts: When same contact has different names, uses most recent by timestamp.\n * 5. Local Updates: When a contact was updated locally, syncs changes to remote if local is newer.\n * 6. Remote Updates: When a contact was updated remotely, applies changes locally if remote is newer.\n * 7. Local Deletions: Handled by real-time event handlers (deleteContactInRemoteStorage) to prevent false positives.\n * 8. Remote Deletions: When a contact was deleted remotely, applies deletion locally.\n * 9. Concurrent Updates: Resolves conflicts using timestamps to determine the winner.\n * 10. Restore After Delete: If a contact is modified after being deleted, restores it.\n * 11. ChainId Differences: Treats same address on different chains as separate contacts.\n *\n * @param config - Parameters used for syncing callbacks\n * @param options - Parameters used for syncing operations\n */\nexport async function syncContactsWithUserStorage(\n config: SyncContactsWithUserStorageConfig,\n options: ContactSyncingOptions,\n): Promise<void> {\n const { getMessenger, getUserStorageControllerInstance } = options;\n const {\n onContactSyncErroneousSituation,\n onContactUpdated,\n onContactDeleted,\n } = config;\n\n try {\n // Cannot perform sync, conditions not met\n if (!canPerformContactSyncing(options)) {\n return;\n }\n\n // Activate sync semaphore to prevent event loops\n await getUserStorageControllerInstance().setIsContactSyncingInProgress(\n true,\n );\n\n // Get all local contacts from AddressBookController (exclude chain \"*\" contacts)\n const localVisibleContacts =\n getMessenger()\n .call('AddressBookController:list')\n .filter((contact) => String(contact.chainId) !== '*') || [];\n\n // Get remote contacts from user storage API\n const remoteContacts = await getRemoteContacts(options);\n\n // SCENARIO 1: First Sync - No remote contacts but local contacts exist\n if (!remoteContacts || remoteContacts.length === 0) {\n if (localVisibleContacts.length > 0) {\n await saveContactsToUserStorage(localVisibleContacts, options);\n }\n return;\n }\n\n // Prepare maps for efficient lookup\n const localContactsMap = new Map<string, AddressBookEntry>();\n const remoteContactsMap = new Map<string, SyncAddressBookEntry>();\n\n localVisibleContacts.forEach((contact) => {\n const key = createContactKey(contact);\n localContactsMap.set(key, contact);\n });\n\n remoteContacts.forEach((contact) => {\n const key = createContactKey(contact);\n remoteContactsMap.set(key, contact);\n });\n\n // Lists to track contacts that need to be synced\n const contactsToAddOrUpdateLocally: SyncAddressBookEntry[] = [];\n const contactsToDeleteLocally: SyncAddressBookEntry[] = [];\n const contactsToUpdateRemotely: AddressBookEntry[] = [];\n\n // SCENARIO 2 & 6: Process remote contacts - handle new device sync and remote updates\n for (const remoteContact of remoteContacts) {\n const key = createContactKey(remoteContact);\n const localContact = localContactsMap.get(key);\n\n // Handle remote contact based on its status and local existence\n if (remoteContact.deleted) {\n // SCENARIO 8: Remote deletion - should be applied locally if contact exists locally\n if (localContact) {\n contactsToDeleteLocally.push(remoteContact);\n\n if (onContactDeleted) {\n onContactDeleted();\n }\n }\n } else if (!localContact) {\n // SCENARIO 2: New contact from remote - import to local\n contactsToAddOrUpdateLocally.push(remoteContact);\n\n if (onContactUpdated) {\n onContactUpdated();\n }\n } else {\n // SCENARIO 4 & 6: Contact exists on both sides - check for conflicts\n const hasContentDifference =\n localContact.name !== remoteContact.name ||\n localContact.memo !== remoteContact.memo;\n\n if (hasContentDifference) {\n // Check timestamps to determine which version to keep\n const localTimestamp = localContact.lastUpdatedAt || 0;\n const remoteTimestamp = remoteContact.lastUpdatedAt || 0;\n\n if (localTimestamp >= remoteTimestamp) {\n // Local is newer (or same age) - use local version\n contactsToUpdateRemotely.push(localContact);\n } else {\n // Remote is newer - use remote version\n contactsToAddOrUpdateLocally.push(remoteContact);\n\n if (onContactUpdated) {\n onContactUpdated();\n }\n }\n }\n\n // Else: content is identical, no action needed\n }\n }\n\n // SCENARIO 3 & 5: Process local contacts not in remote\n for (const localContact of localVisibleContacts) {\n const key = createContactKey(localContact);\n const remoteContact = remoteContactsMap.get(key);\n\n if (!remoteContact) {\n // New local contact - add to remote\n contactsToUpdateRemotely.push(localContact);\n }\n }\n\n // Apply local deletions\n for (const contact of contactsToDeleteLocally) {\n try {\n getMessenger().call(\n 'AddressBookController:delete',\n contact.chainId,\n contact.address,\n );\n } catch (error) {\n console.error('Error deleting contact:', error);\n }\n }\n\n // Apply local additions/updates\n for (const contact of contactsToAddOrUpdateLocally) {\n if (!contact.deleted) {\n try {\n getMessenger().call(\n 'AddressBookController:set',\n contact.address,\n contact.name || '',\n contact.chainId,\n contact.memo || '',\n contact.addressType,\n );\n } catch (error) {\n console.error('Error updating contact:', error);\n }\n }\n }\n\n // Apply changes to remote storage\n if (contactsToUpdateRemotely.length > 0) {\n // Update existing remote contacts with new contacts\n const updatedRemoteContacts = [...remoteContacts];\n\n for (const localContact of contactsToUpdateRemotely) {\n const key = createContactKey(localContact);\n const existingIndex = updatedRemoteContacts.findIndex(\n (c) => createContactKey(c) === key,\n );\n\n const now = Date.now();\n const updatedEntry = {\n ...localContact,\n lastUpdatedAt: now,\n deleted: false, // Ensure it's not deleted\n } as SyncAddressBookEntry;\n\n if (existingIndex !== -1) {\n // Update existing contact\n updatedRemoteContacts[existingIndex] = updatedEntry;\n } else {\n // Add new contact\n updatedRemoteContacts.push(updatedEntry);\n }\n }\n\n // Save updated contacts to remote storage\n await saveContactsToUserStorage(updatedRemoteContacts, options);\n }\n } catch (error) {\n if (onContactSyncErroneousSituation) {\n onContactSyncErroneousSituation('Error synchronizing contacts', {\n error,\n });\n }\n } finally {\n await getUserStorageControllerInstance().setIsContactSyncingInProgress(\n false,\n );\n }\n}\n\n/**\n * Retrieves remote contacts from user storage API\n *\n * @param options - Parameters used for retrieving remote contacts\n * @returns Array of contacts from remote storage, or null if none found\n */\nasync function getRemoteContacts(\n options: ContactSyncingOptions,\n): Promise<SyncAddressBookEntry[] | null> {\n const { getUserStorageControllerInstance } = options;\n\n try {\n const remoteContactsJson =\n await getUserStorageControllerInstance().performGetStorage(\n `${USER_STORAGE_FEATURE_NAMES.addressBook}.${ADDRESS_BOOK_CONTACTS_KEY}`,\n );\n\n if (!remoteContactsJson) {\n return null;\n }\n\n // Parse the JSON and convert each entry from UserStorageContactEntry to AddressBookEntry\n const remoteStorageEntries = JSON.parse(\n remoteContactsJson,\n ) as UserStorageContactEntry[];\n return remoteStorageEntries.map((entry) =>\n mapUserStorageEntryToAddressBookEntry(entry),\n );\n } catch {\n return null;\n }\n}\n\n/**\n * Saves local contacts to user storage\n *\n * @param contacts - The contacts to save to user storage\n * @param options - Parameters used for saving contacts\n */\nasync function saveContactsToUserStorage(\n contacts: AddressBookEntry[],\n options: ContactSyncingOptions,\n): Promise<void> {\n const { getUserStorageControllerInstance } = options;\n\n if (!contacts || contacts.length === 0) {\n return;\n }\n\n // Convert each AddressBookEntry to UserStorageContactEntry format before saving\n const storageEntries = contacts.map((contact) =>\n mapAddressBookEntryToUserStorageEntry(contact),\n );\n\n await getUserStorageControllerInstance().performSetStorage(\n `${USER_STORAGE_FEATURE_NAMES.addressBook}.${ADDRESS_BOOK_CONTACTS_KEY}`,\n JSON.stringify(storageEntries),\n );\n}\n\n/**\n * Updates a single contact in remote storage without performing a full sync\n * This is used when a contact is updated locally to efficiently push changes to remote\n *\n * @param contact - The contact that was updated locally\n * @param options - Parameters used for syncing operations\n */\nexport async function updateContactInRemoteStorage(\n contact: AddressBookEntry,\n options: ContactSyncingOptions,\n): Promise<void> {\n if (!canPerformContactSyncing(options)) {\n return;\n }\n\n try {\n // Get current remote contacts or initialize empty array\n const remoteContacts = (await getRemoteContacts(options)) || [];\n\n // Find if this contact already exists in remote\n const key = createContactKey(contact);\n const existingIndex = remoteContacts.findIndex(\n (c) => createContactKey(c) === key,\n );\n\n const updatedRemoteContacts = [...remoteContacts];\n\n // Create an updated entry with timestamp\n const updatedEntry = {\n ...contact,\n lastUpdatedAt: contact.lastUpdatedAt || Date.now(),\n deleted: false, // Explicitly set to false in case this was previously deleted\n } as SyncAddressBookEntry;\n\n if (existingIndex !== -1) {\n // Update existing contact\n updatedRemoteContacts[existingIndex] = updatedEntry;\n } else {\n // Add as new contact\n updatedRemoteContacts.push(updatedEntry);\n }\n\n // Save to remote storage\n await saveContactsToUserStorage(updatedRemoteContacts, options);\n } catch (error) {\n console.error(\n '🔄 Contacts Sync: Error updating contact in remote storage',\n error,\n );\n }\n}\n\n/**\n * Marks a single contact as deleted in remote storage without performing a full sync\n * This is used when a contact is deleted locally to efficiently push the deletion to remote\n *\n * @param contact - The contact that was deleted locally (contains at least address and chainId)\n * @param options - Parameters used for syncing operations\n */\nexport async function deleteContactInRemoteStorage(\n contact: AddressBookEntry,\n options: ContactSyncingOptions,\n): Promise<void> {\n if (!canPerformContactSyncing(options)) {\n return;\n }\n\n try {\n // Get current remote contacts\n const remoteContacts = (await getRemoteContacts(options)) || [];\n\n // Find the contact in remote storage\n const key = createContactKey(contact);\n const existingIndex = remoteContacts.findIndex(\n (c) => createContactKey(c) === key,\n );\n\n // If the contact exists in remote storage\n if (existingIndex !== -1) {\n const updatedRemoteContacts = [...remoteContacts];\n const now = Date.now();\n\n // Mark the contact as deleted\n updatedRemoteContacts[existingIndex] = {\n ...updatedRemoteContacts[existingIndex],\n deleted: true,\n deletedAt: now,\n lastUpdatedAt: now,\n };\n\n // Save to remote storage\n await saveContactsToUserStorage(updatedRemoteContacts, options);\n }\n } catch (error) {\n console.error(\n '🔄 Contacts Sync: Error marking contact as deleted in remote storage',\n error,\n );\n }\n}\n"]}
@@ -0,0 +1,45 @@
1
+ import type { AddressBookEntry } from "@metamask/address-book-controller";
2
+ import type { ContactSyncingOptions } from "./types.cjs";
3
+ export declare const ADDRESS_BOOK_CONTACTS_KEY = "contacts";
4
+ export type SyncContactsWithUserStorageConfig = {
5
+ onContactSyncErroneousSituation?: (errorMessage: string, sentryContext?: Record<string, unknown>) => void;
6
+ onContactUpdated?: () => void;
7
+ onContactDeleted?: () => void;
8
+ };
9
+ /**
10
+ * Syncs contacts between local storage and user storage (remote).
11
+ *
12
+ * Handles the following syncing scenarios:
13
+ * 1. First Sync: When local contacts exist but there are no remote contacts, uploads all local contacts.
14
+ * 2. New Device Sync: Downloads remote contacts that don't exist locally (empty local address book).
15
+ * 3. Simple Merge: Ensures both sides (local & remote) have all contacts.
16
+ * 4. Contact Naming Conflicts: When same contact has different names, uses most recent by timestamp.
17
+ * 5. Local Updates: When a contact was updated locally, syncs changes to remote if local is newer.
18
+ * 6. Remote Updates: When a contact was updated remotely, applies changes locally if remote is newer.
19
+ * 7. Local Deletions: Handled by real-time event handlers (deleteContactInRemoteStorage) to prevent false positives.
20
+ * 8. Remote Deletions: When a contact was deleted remotely, applies deletion locally.
21
+ * 9. Concurrent Updates: Resolves conflicts using timestamps to determine the winner.
22
+ * 10. Restore After Delete: If a contact is modified after being deleted, restores it.
23
+ * 11. ChainId Differences: Treats same address on different chains as separate contacts.
24
+ *
25
+ * @param config - Parameters used for syncing callbacks
26
+ * @param options - Parameters used for syncing operations
27
+ */
28
+ export declare function syncContactsWithUserStorage(config: SyncContactsWithUserStorageConfig, options: ContactSyncingOptions): Promise<void>;
29
+ /**
30
+ * Updates a single contact in remote storage without performing a full sync
31
+ * This is used when a contact is updated locally to efficiently push changes to remote
32
+ *
33
+ * @param contact - The contact that was updated locally
34
+ * @param options - Parameters used for syncing operations
35
+ */
36
+ export declare function updateContactInRemoteStorage(contact: AddressBookEntry, options: ContactSyncingOptions): Promise<void>;
37
+ /**
38
+ * Marks a single contact as deleted in remote storage without performing a full sync
39
+ * This is used when a contact is deleted locally to efficiently push the deletion to remote
40
+ *
41
+ * @param contact - The contact that was deleted locally (contains at least address and chainId)
42
+ * @param options - Parameters used for syncing operations
43
+ */
44
+ export declare function deleteContactInRemoteStorage(contact: AddressBookEntry, options: ContactSyncingOptions): Promise<void>;
45
+ //# sourceMappingURL=controller-integration.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"controller-integration.d.cts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/contact-syncing/controller-integration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,0CAA0C;AAG1E,OAAO,KAAK,EAAE,qBAAqB,EAAE,oBAAgB;AAUrD,eAAO,MAAM,yBAAyB,aAAa,CAAC;AAEpD,MAAM,MAAM,iCAAiC,GAAG;IAC9C,+BAA+B,CAAC,EAAE,CAChC,YAAY,EAAE,MAAM,EACpB,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KACpC,IAAI,CAAC;IACV,gBAAgB,CAAC,EAAE,MAAM,IAAI,CAAC;IAC9B,gBAAgB,CAAC,EAAE,MAAM,IAAI,CAAC;CAC/B,CAAC;AAYF;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,2BAA2B,CAC/C,MAAM,EAAE,iCAAiC,EACzC,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CA4Lf;AA8DD;;;;;;GAMG;AACH,wBAAsB,4BAA4B,CAChD,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CAwCf;AAED;;;;;;GAMG;AACH,wBAAsB,4BAA4B,CAChD,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CAqCf"}
@@ -0,0 +1,45 @@
1
+ import type { AddressBookEntry } from "@metamask/address-book-controller";
2
+ import type { ContactSyncingOptions } from "./types.mjs";
3
+ export declare const ADDRESS_BOOK_CONTACTS_KEY = "contacts";
4
+ export type SyncContactsWithUserStorageConfig = {
5
+ onContactSyncErroneousSituation?: (errorMessage: string, sentryContext?: Record<string, unknown>) => void;
6
+ onContactUpdated?: () => void;
7
+ onContactDeleted?: () => void;
8
+ };
9
+ /**
10
+ * Syncs contacts between local storage and user storage (remote).
11
+ *
12
+ * Handles the following syncing scenarios:
13
+ * 1. First Sync: When local contacts exist but there are no remote contacts, uploads all local contacts.
14
+ * 2. New Device Sync: Downloads remote contacts that don't exist locally (empty local address book).
15
+ * 3. Simple Merge: Ensures both sides (local & remote) have all contacts.
16
+ * 4. Contact Naming Conflicts: When same contact has different names, uses most recent by timestamp.
17
+ * 5. Local Updates: When a contact was updated locally, syncs changes to remote if local is newer.
18
+ * 6. Remote Updates: When a contact was updated remotely, applies changes locally if remote is newer.
19
+ * 7. Local Deletions: Handled by real-time event handlers (deleteContactInRemoteStorage) to prevent false positives.
20
+ * 8. Remote Deletions: When a contact was deleted remotely, applies deletion locally.
21
+ * 9. Concurrent Updates: Resolves conflicts using timestamps to determine the winner.
22
+ * 10. Restore After Delete: If a contact is modified after being deleted, restores it.
23
+ * 11. ChainId Differences: Treats same address on different chains as separate contacts.
24
+ *
25
+ * @param config - Parameters used for syncing callbacks
26
+ * @param options - Parameters used for syncing operations
27
+ */
28
+ export declare function syncContactsWithUserStorage(config: SyncContactsWithUserStorageConfig, options: ContactSyncingOptions): Promise<void>;
29
+ /**
30
+ * Updates a single contact in remote storage without performing a full sync
31
+ * This is used when a contact is updated locally to efficiently push changes to remote
32
+ *
33
+ * @param contact - The contact that was updated locally
34
+ * @param options - Parameters used for syncing operations
35
+ */
36
+ export declare function updateContactInRemoteStorage(contact: AddressBookEntry, options: ContactSyncingOptions): Promise<void>;
37
+ /**
38
+ * Marks a single contact as deleted in remote storage without performing a full sync
39
+ * This is used when a contact is deleted locally to efficiently push the deletion to remote
40
+ *
41
+ * @param contact - The contact that was deleted locally (contains at least address and chainId)
42
+ * @param options - Parameters used for syncing operations
43
+ */
44
+ export declare function deleteContactInRemoteStorage(contact: AddressBookEntry, options: ContactSyncingOptions): Promise<void>;
45
+ //# sourceMappingURL=controller-integration.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"controller-integration.d.mts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/contact-syncing/controller-integration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,0CAA0C;AAG1E,OAAO,KAAK,EAAE,qBAAqB,EAAE,oBAAgB;AAUrD,eAAO,MAAM,yBAAyB,aAAa,CAAC;AAEpD,MAAM,MAAM,iCAAiC,GAAG;IAC9C,+BAA+B,CAAC,EAAE,CAChC,YAAY,EAAE,MAAM,EACpB,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KACpC,IAAI,CAAC;IACV,gBAAgB,CAAC,EAAE,MAAM,IAAI,CAAC;IAC9B,gBAAgB,CAAC,EAAE,MAAM,IAAI,CAAC;CAC/B,CAAC;AAYF;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,2BAA2B,CAC/C,MAAM,EAAE,iCAAiC,EACzC,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CA4Lf;AA8DD;;;;;;GAMG;AACH,wBAAsB,4BAA4B,CAChD,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CAwCf;AAED;;;;;;GAMG;AACH,wBAAsB,4BAA4B,CAChD,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CAqCf"}