@metamask-previews/profile-sync-controller 19.0.0-preview-29bd474 → 19.0.0-preview-6fc9506d

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 (38) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/controllers/user-storage/UserStorageController.cjs +10 -3
  3. package/dist/controllers/user-storage/UserStorageController.cjs.map +1 -1
  4. package/dist/controllers/user-storage/UserStorageController.d.cts +3 -1
  5. package/dist/controllers/user-storage/UserStorageController.d.cts.map +1 -1
  6. package/dist/controllers/user-storage/UserStorageController.d.mts +3 -1
  7. package/dist/controllers/user-storage/UserStorageController.d.mts.map +1 -1
  8. package/dist/controllers/user-storage/UserStorageController.mjs +10 -3
  9. package/dist/controllers/user-storage/UserStorageController.mjs.map +1 -1
  10. package/dist/controllers/user-storage/account-syncing/controller-integration.cjs +184 -168
  11. package/dist/controllers/user-storage/account-syncing/controller-integration.cjs.map +1 -1
  12. package/dist/controllers/user-storage/account-syncing/controller-integration.d.cts +3 -0
  13. package/dist/controllers/user-storage/account-syncing/controller-integration.d.cts.map +1 -1
  14. package/dist/controllers/user-storage/account-syncing/controller-integration.d.mts +3 -0
  15. package/dist/controllers/user-storage/account-syncing/controller-integration.d.mts.map +1 -1
  16. package/dist/controllers/user-storage/account-syncing/controller-integration.mjs +184 -168
  17. package/dist/controllers/user-storage/account-syncing/controller-integration.mjs.map +1 -1
  18. package/dist/controllers/user-storage/account-syncing/types.cjs.map +1 -1
  19. package/dist/controllers/user-storage/account-syncing/types.d.cts +2 -0
  20. package/dist/controllers/user-storage/account-syncing/types.d.cts.map +1 -1
  21. package/dist/controllers/user-storage/account-syncing/types.d.mts +2 -0
  22. package/dist/controllers/user-storage/account-syncing/types.d.mts.map +1 -1
  23. package/dist/controllers/user-storage/account-syncing/types.mjs.map +1 -1
  24. package/dist/controllers/user-storage/contact-syncing/controller-integration.cjs +233 -166
  25. package/dist/controllers/user-storage/contact-syncing/controller-integration.cjs.map +1 -1
  26. package/dist/controllers/user-storage/contact-syncing/controller-integration.d.cts +3 -0
  27. package/dist/controllers/user-storage/contact-syncing/controller-integration.d.cts.map +1 -1
  28. package/dist/controllers/user-storage/contact-syncing/controller-integration.d.mts +3 -0
  29. package/dist/controllers/user-storage/contact-syncing/controller-integration.d.mts.map +1 -1
  30. package/dist/controllers/user-storage/contact-syncing/controller-integration.mjs +233 -166
  31. package/dist/controllers/user-storage/contact-syncing/controller-integration.mjs.map +1 -1
  32. package/dist/controllers/user-storage/contact-syncing/types.cjs.map +1 -1
  33. package/dist/controllers/user-storage/contact-syncing/types.d.cts +2 -0
  34. package/dist/controllers/user-storage/contact-syncing/types.d.cts.map +1 -1
  35. package/dist/controllers/user-storage/contact-syncing/types.d.mts +2 -0
  36. package/dist/controllers/user-storage/contact-syncing/types.d.mts.map +1 -1
  37. package/dist/controllers/user-storage/contact-syncing/types.mjs.map +1 -1
  38. package/package.json +1 -1
@@ -1 +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,uCAAuD;AACvD,uEAA4E;AAW5E;;;;;GAKG;AACH,SAAS,gBAAgB,CAAC,OAAyB;IACjD,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE;QACpB,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC;KACtE;IACD,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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mBAAmB,CAAC,CACpC,CAAC;SACH;KACF;IAAC,MAAM;QACN,yEAAyE;QACzE,OAAO,CAAC,IAAI,CAAC,mDAAmD,EAAE,GAAG,CAAC,CAAC;KACxE;AACH,CAAC;AAnDD,oEAmDC","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 { isContactBridgedFromAccounts } from './utils';\nimport { USER_STORAGE_FEATURE_NAMES } from '../../../shared/storage-schema';\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 if (!contact.address) {\n throw new Error('Contact address is required to create storage key');\n }\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) => !isContactBridgedFromAccounts(contact))\n .filter(\n (contact) =>\n contact.address && contact.chainId && contact.name?.trim(),\n ) || [];\n\n // Get remote contacts from user storage API\n const remoteContacts = await getRemoteContacts(options);\n\n // Filter remote contacts to exclude invalid ones (or empty array if no remote contacts)\n const validRemoteContacts =\n remoteContacts?.filter(\n (contact) => contact.address && contact.chainId && contact.name?.trim(),\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 validRemoteContacts.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 validRemoteContacts) {\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.deletedAt) {\n // SCENARIO 8: Remote deletion - should be applied locally if contact exists locally\n if (localContact) {\n contactsToDeleteLocally.push(remoteContact);\n }\n } else if (!localContact) {\n // SCENARIO 2: New contact from remote - import to local\n contactsToAddOrUpdateLocally.push(remoteContact);\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 }\n\n // Else: content is identical, no action needed\n }\n }\n\n // SCENARIO 1, 3 & 5: Process local contacts not in remote - handles first sync and new local contacts\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 or first sync - 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\n if (onContactDeleted) {\n onContactDeleted();\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.deletedAt) {\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\n if (onContactUpdated) {\n onContactUpdated();\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 const updatedRemoteContacts: Record<string, SyncAddressBookEntry> = {};\n for (const localContact of contactsToUpdateRemotely) {\n const key = createContactKey(localContact);\n updatedRemoteContacts[key] = {\n ...remoteContactsMap.get(key), // Start with an existing remote contact if it exists\n ...localContact, // override with local changes\n lastUpdatedAt: Date.now(), // mark as updated\n };\n }\n // Save updated contacts to remote storage\n await saveContactsToUserStorage(\n Object.values(updatedRemoteContacts),\n options,\n );\n }\n } catch (error) {\n if (onContactSyncErroneousSituation) {\n onContactSyncErroneousSituation('Error synchronizing contacts', {\n error,\n });\n\n // Re-throw the error to be handled by the caller\n throw error;\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 remoteContactsJsonArray =\n await getUserStorageControllerInstance().performGetStorageAllFeatureEntries(\n USER_STORAGE_FEATURE_NAMES.addressBook,\n );\n\n if (!remoteContactsJsonArray || remoteContactsJsonArray.length === 0) {\n return null;\n }\n\n // Parse each JSON entry and convert from UserStorageContactEntry to AddressBookEntry\n const remoteStorageEntries = remoteContactsJsonArray.map((contactJson) => {\n const entry = JSON.parse(contactJson) as UserStorageContactEntry;\n return mapUserStorageEntryToAddressBookEntry(entry);\n });\n\n return remoteStorageEntries;\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 and create key-value pairs\n const storageEntries: [string, string][] = contacts.map((contact) => {\n const key = createContactKey(contact);\n const storageEntry = mapAddressBookEntryToUserStorageEntry(contact);\n return [key, JSON.stringify(storageEntry)];\n });\n\n await getUserStorageControllerInstance().performBatchSetStorage(\n USER_STORAGE_FEATURE_NAMES.addressBook,\n 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 (\n !canPerformContactSyncing(options) ||\n !contact.address ||\n !contact.chainId ||\n !contact.name?.trim()\n ) {\n return;\n }\n\n const { getUserStorageControllerInstance } = options;\n\n // Create an updated entry with timestamp\n const updatedEntry = {\n ...contact,\n lastUpdatedAt: contact.lastUpdatedAt || Date.now(),\n } as SyncAddressBookEntry;\n\n const key = createContactKey(contact);\n const storageEntry = mapAddressBookEntryToUserStorageEntry(updatedEntry);\n\n // Save individual contact to remote storage\n await getUserStorageControllerInstance().performSetStorage(\n `${USER_STORAGE_FEATURE_NAMES.addressBook}.${key}`,\n JSON.stringify(storageEntry),\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 (\n !canPerformContactSyncing(options) ||\n !contact.address ||\n !contact.chainId ||\n !contact.name?.trim()\n ) {\n return;\n }\n\n const { getUserStorageControllerInstance } = options;\n const key = createContactKey(contact);\n\n try {\n // Try to get the existing contact first\n const existingContactJson =\n await getUserStorageControllerInstance().performGetStorage(\n `${USER_STORAGE_FEATURE_NAMES.addressBook}.${key}`,\n );\n\n if (existingContactJson) {\n // Mark the existing contact as deleted\n const existingStorageEntry = JSON.parse(\n existingContactJson,\n ) as UserStorageContactEntry;\n const existingContact =\n mapUserStorageEntryToAddressBookEntry(existingStorageEntry);\n\n const now = Date.now();\n const deletedContact = {\n ...existingContact,\n deletedAt: now,\n lastUpdatedAt: now,\n } as SyncAddressBookEntry;\n\n const deletedStorageEntry =\n mapAddressBookEntryToUserStorageEntry(deletedContact);\n\n // Save the deleted contact back to storage\n await getUserStorageControllerInstance().performSetStorage(\n `${USER_STORAGE_FEATURE_NAMES.addressBook}.${key}`,\n JSON.stringify(deletedStorageEntry),\n );\n }\n } catch {\n // If contact doesn't exist in remote storage, no need to mark as deleted\n console.warn('Contact not found in remote storage for deletion:', key);\n }\n}\n"]}
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,uCAAuD;AACvD,uEAA4E;AAW5E;;;;;GAKG;AACH,SAAS,gBAAgB,CAAC,OAAyB;IACjD,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE;QACpB,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC;KACtE;IACD,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,WAAW,EAAE,EAAE,CAAC;AAC/D,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACI,KAAK,UAAU,2BAA2B,CAC/C,MAAyC,EACzC,OAA8B;IAE9B,MAAM,EAAE,YAAY,EAAE,gCAAgC,EAAE,KAAK,EAAE,GAAG,OAAO,CAAC;IAC1E,MAAM,EACJ,+BAA+B,EAC/B,gBAAgB,EAChB,gBAAgB,GACjB,GAAG,MAAM,CAAC;IAEX,IAAI,oBAAoB,GAAuB,EAAE,CAAC;IAClD,IAAI,mBAAmB,GAA2B,EAAE,CAAC;IAErD,MAAM,WAAW,GAAG,KAAK,IAAI,EAAE;QAC7B,IAAI;YACF,0CAA0C;YAC1C,IAAI,CAAC,IAAA,qCAAwB,EAAC,OAAO,CAAC,EA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,GAAG,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;QAEtC,IAAI;YACF,wCAAwC;YACxC,MAAM,mBAAmB,GACvB,MAAM,gCAAgC,EAAE,CAAC,iBAAiB,CACxD,GAAG,2CAA0B,CAAC,WAAW,IAAI,GAAG,EAAE,CACnD,CAAC;YAEJ,IAAI,mBAAmB,EAAE;gBACvB,uCAAuC;gBACvC,MAAM,oBAAoB,GAAG,IAAI,CAAC,KAAK,CACrC,mBAAmB,CACO,CAAC;gBAC7B,MAAM,eAAe,GACnB,IAAA,6CAAqC,EAAC,oBAAoB,CAAC,CAAC;gBAE9D,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;gBACvB,MAAM,cAAc,GAAG;oBACrB,GAAG,eAAe;oBAClB,SAAS,EAAE,GAAG;oBACd,aAAa,EAAE,GAAG;iBACK,CAAC;gBAE1B,MAAM,mBAAmB,GACvB,IAAA,6CAAqC,EAAC,cAAc,CAAC,CAAC;gBAExD,2CAA2C;gBAC3C,MAAM,gCAAgC,EAAE,CAAC,iBAAiB,CACxD,GAAG,2CAA0B,CAAC,WAAW,IAAI,GAAG,EAAE,EAClD,IAAI,CAAC,SAAS,CAAC,mBAAmB,CAAC,CACpC,CAAC;aACH;SACF;QAAC,MAAM;YACN,yEAAyE;YACzE,OAAO,CAAC,IAAI,CAAC,mDAAmD,EAAE,GAAG,CAAC,CAAC;SACxE;IACH,CAAC,CAAC;IAEF,OAAO,KAAK;QACV,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,IAAI,EAAE,4BAA4B,EAAE,EAAE,aAAa,CAAC;QACpE,CAAC,CAAC,MAAM,aAAa,EAAE,CAAC;AAC5B,CAAC;AA3DD,oEA2DC","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 { isContactBridgedFromAccounts } from './utils';\nimport { USER_STORAGE_FEATURE_NAMES } from '../../../shared/storage-schema';\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 if (!contact.address) {\n throw new Error('Contact address is required to create storage key');\n }\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 * @returns Promise that resolves when contact synchronization is complete\n */\nexport async function syncContactsWithUserStorage(\n config: SyncContactsWithUserStorageConfig,\n options: ContactSyncingOptions,\n): Promise<void> {\n const { getMessenger, getUserStorageControllerInstance, trace } = options;\n const {\n onContactSyncErroneousSituation,\n onContactUpdated,\n onContactDeleted,\n } = config;\n\n let localVisibleContacts: AddressBookEntry[] = [];\n let validRemoteContacts: SyncAddressBookEntry[] = [];\n\n const performSync = async () => {\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 localVisibleContacts =\n getMessenger()\n .call('AddressBookController:list')\n .filter((contact) => !isContactBridgedFromAccounts(contact))\n .filter(\n (contact) =>\n contact.address && contact.chainId && contact.name?.trim(),\n ) || [];\n\n // Get remote contacts from user storage API\n const remoteContacts = await getRemoteContacts(options);\n\n // Filter remote contacts to exclude invalid ones (or empty array if no remote contacts)\n validRemoteContacts =\n remoteContacts?.filter(\n (contact) =>\n contact.address && contact.chainId && contact.name?.trim(),\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 validRemoteContacts.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 validRemoteContacts) {\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.deletedAt) {\n // SCENARIO 8: Remote deletion - should be applied locally if contact exists locally\n if (localContact) {\n contactsToDeleteLocally.push(remoteContact);\n }\n } else if (!localContact) {\n // SCENARIO 2: New contact from remote - import to local\n contactsToAddOrUpdateLocally.push(remoteContact);\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 }\n\n // Else: content is identical, no action needed\n }\n }\n\n // SCENARIO 1, 3 & 5: Process local contacts not in remote - handles first sync and new local contacts\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 or first sync - 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\n if (onContactDeleted) {\n onContactDeleted();\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.deletedAt) {\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\n if (onContactUpdated) {\n onContactUpdated();\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 const updatedRemoteContacts: Record<string, SyncAddressBookEntry> = {};\n for (const localContact of contactsToUpdateRemotely) {\n const key = createContactKey(localContact);\n updatedRemoteContacts[key] = {\n ...remoteContactsMap.get(key), // Start with an existing remote contact if it exists\n ...localContact, // override with local changes\n lastUpdatedAt: Date.now(), // mark as updated\n };\n }\n // Save updated contacts to remote storage\n await saveContactsToUserStorage(\n Object.values(updatedRemoteContacts),\n options,\n );\n }\n } catch (error) {\n if (onContactSyncErroneousSituation) {\n onContactSyncErroneousSituation('Error synchronizing contacts', {\n error,\n });\n\n // Re-throw the error to be handled by the caller\n throw error;\n }\n } finally {\n await getUserStorageControllerInstance().setIsContactSyncingInProgress(\n false,\n );\n }\n };\n\n if (trace) {\n // Gather pre-sync metrics for performance analysis\n const initialLocalContacts = localVisibleContacts;\n const initialValidRemoteContacts = validRemoteContacts;\n\n return await trace(\n {\n name: 'Contact Sync Full',\n data: {\n localContactCount: initialLocalContacts.length,\n remoteContactCount: initialValidRemoteContacts.length,\n isFirstSync:\n initialValidRemoteContacts.length === 0 &&\n initialLocalContacts.length > 0,\n isNewDeviceSync:\n initialLocalContacts.length === 0 &&\n initialValidRemoteContacts.length > 0,\n isRegularSync:\n initialLocalContacts.length > 0 &&\n initialValidRemoteContacts.length > 0,\n hasDataToSync:\n initialLocalContacts.length > 0 ||\n initialValidRemoteContacts.length > 0,\n expectedWorkload:\n initialLocalContacts.length + initialValidRemoteContacts.length,\n },\n },\n performSync,\n );\n }\n\n return await performSync();\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 remoteContactsJsonArray =\n await getUserStorageControllerInstance().performGetStorageAllFeatureEntries(\n USER_STORAGE_FEATURE_NAMES.addressBook,\n );\n\n if (!remoteContactsJsonArray || remoteContactsJsonArray.length === 0) {\n return null;\n }\n\n // Parse each JSON entry and convert from UserStorageContactEntry to AddressBookEntry\n const remoteStorageEntries = remoteContactsJsonArray.map((contactJson) => {\n const entry = JSON.parse(contactJson) as UserStorageContactEntry;\n return mapUserStorageEntryToAddressBookEntry(entry);\n });\n\n return remoteStorageEntries;\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 * @returns Promise that resolves when contacts are saved\n */\nasync function saveContactsToUserStorage(\n contacts: AddressBookEntry[],\n options: ContactSyncingOptions,\n): Promise<void> {\n const { getUserStorageControllerInstance, trace } = options;\n\n const saveContacts = async () => {\n if (!contacts || contacts.length === 0) {\n return;\n }\n\n // Convert each AddressBookEntry to UserStorageContactEntry format and create key-value pairs\n const storageEntries: [string, string][] = contacts.map((contact) => {\n const key = createContactKey(contact);\n const storageEntry = mapAddressBookEntryToUserStorageEntry(contact);\n return [key, JSON.stringify(storageEntry)];\n });\n\n await getUserStorageControllerInstance().performBatchSetStorage(\n USER_STORAGE_FEATURE_NAMES.addressBook,\n storageEntries,\n );\n };\n\n return trace\n ? await trace(\n {\n name: 'Contact Sync Save Batch',\n data: {\n contactCount: contacts.length,\n // Performance scaling indicators\n hasBatchOperations: contacts.length > 1,\n chainCount: new Set(contacts.map((c) => c.chainId)).size,\n hasMemosCount: contacts.filter((c) => c.memo?.length).length,\n },\n },\n saveContacts,\n )\n : await saveContacts();\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 * @returns Promise that resolves when the contact is updated\n */\nexport async function updateContactInRemoteStorage(\n contact: AddressBookEntry,\n options: ContactSyncingOptions,\n): Promise<void> {\n const { trace } = options;\n\n const updateContact = async () => {\n if (\n !canPerformContactSyncing(options) ||\n !contact.address ||\n !contact.chainId ||\n !contact.name?.trim()\n ) {\n return;\n }\n\n const { getUserStorageControllerInstance } = options;\n\n // Create an updated entry with timestamp\n const updatedEntry = {\n ...contact,\n lastUpdatedAt: contact.lastUpdatedAt || Date.now(),\n } as SyncAddressBookEntry;\n\n const key = createContactKey(contact);\n const storageEntry = mapAddressBookEntryToUserStorageEntry(updatedEntry);\n\n // Save individual contact to remote storage\n await getUserStorageControllerInstance().performSetStorage(\n `${USER_STORAGE_FEATURE_NAMES.addressBook}.${key}`,\n JSON.stringify(storageEntry),\n );\n };\n\n return trace\n ? await trace(\n {\n name: 'Contact Sync Update Remote',\n data: {\n chainId: contact.chainId,\n // Performance indicators\n hasTimestamp: Boolean(contact.lastUpdatedAt),\n hasMemo: Boolean(contact.memo?.length),\n isUpdate: Boolean(contact.lastUpdatedAt), // vs new contact\n },\n },\n updateContact,\n )\n : await updateContact();\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 * @returns Promise that resolves when the contact is marked as deleted\n */\nexport async function deleteContactInRemoteStorage(\n contact: AddressBookEntry,\n options: ContactSyncingOptions,\n): Promise<void> {\n const { trace } = options;\n\n const deleteContact = async () => {\n if (\n !canPerformContactSyncing(options) ||\n !contact.address ||\n !contact.chainId ||\n !contact.name?.trim()\n ) {\n return;\n }\n\n const { getUserStorageControllerInstance } = options;\n const key = createContactKey(contact);\n\n try {\n // Try to get the existing contact first\n const existingContactJson =\n await getUserStorageControllerInstance().performGetStorage(\n `${USER_STORAGE_FEATURE_NAMES.addressBook}.${key}`,\n );\n\n if (existingContactJson) {\n // Mark the existing contact as deleted\n const existingStorageEntry = JSON.parse(\n existingContactJson,\n ) as UserStorageContactEntry;\n const existingContact =\n mapUserStorageEntryToAddressBookEntry(existingStorageEntry);\n\n const now = Date.now();\n const deletedContact = {\n ...existingContact,\n deletedAt: now,\n lastUpdatedAt: now,\n } as SyncAddressBookEntry;\n\n const deletedStorageEntry =\n mapAddressBookEntryToUserStorageEntry(deletedContact);\n\n // Save the deleted contact back to storage\n await getUserStorageControllerInstance().performSetStorage(\n `${USER_STORAGE_FEATURE_NAMES.addressBook}.${key}`,\n JSON.stringify(deletedStorageEntry),\n );\n }\n } catch {\n // If contact doesn't exist in remote storage, no need to mark as deleted\n console.warn('Contact not found in remote storage for deletion:', key);\n }\n };\n\n return trace\n ? await trace({ name: 'Contact Sync Delete Remote' }, deleteContact)\n : await deleteContact();\n}\n"]}
@@ -23,6 +23,7 @@ export type SyncContactsWithUserStorageConfig = {
23
23
  *
24
24
  * @param config - Parameters used for syncing callbacks
25
25
  * @param options - Parameters used for syncing operations
26
+ * @returns Promise that resolves when contact synchronization is complete
26
27
  */
27
28
  export declare function syncContactsWithUserStorage(config: SyncContactsWithUserStorageConfig, options: ContactSyncingOptions): Promise<void>;
28
29
  /**
@@ -31,6 +32,7 @@ export declare function syncContactsWithUserStorage(config: SyncContactsWithUser
31
32
  *
32
33
  * @param contact - The contact that was updated locally
33
34
  * @param options - Parameters used for syncing operations
35
+ * @returns Promise that resolves when the contact is updated
34
36
  */
35
37
  export declare function updateContactInRemoteStorage(contact: AddressBookEntry, options: ContactSyncingOptions): Promise<void>;
36
38
  /**
@@ -39,6 +41,7 @@ export declare function updateContactInRemoteStorage(contact: AddressBookEntry,
39
41
  *
40
42
  * @param contact - The contact that was deleted locally (contains at least address and chainId)
41
43
  * @param options - Parameters used for syncing operations
44
+ * @returns Promise that resolves when the contact is marked as deleted
42
45
  */
43
46
  export declare function deleteContactInRemoteStorage(contact: AddressBookEntry, options: ContactSyncingOptions): Promise<void>;
44
47
  //# sourceMappingURL=controller-integration.d.cts.map
@@ -1 +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,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;AAeF;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,2BAA2B,CAC/C,MAAM,EAAE,iCAAiC,EACzC,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CAgLf;AAgED;;;;;;GAMG;AACH,wBAAsB,4BAA4B,CAChD,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CA0Bf;AAED;;;;;;GAMG;AACH,wBAAsB,4BAA4B,CAChD,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CAgDf"}
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,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;AAeF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,2BAA2B,CAC/C,MAAM,EAAE,iCAAiC,EACzC,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CAuNf;AAmFD;;;;;;;GAOG;AACH,wBAAsB,4BAA4B,CAChD,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CA8Cf;AAED;;;;;;;GAOG;AACH,wBAAsB,4BAA4B,CAChD,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CAwDf"}
@@ -23,6 +23,7 @@ export type SyncContactsWithUserStorageConfig = {
23
23
  *
24
24
  * @param config - Parameters used for syncing callbacks
25
25
  * @param options - Parameters used for syncing operations
26
+ * @returns Promise that resolves when contact synchronization is complete
26
27
  */
27
28
  export declare function syncContactsWithUserStorage(config: SyncContactsWithUserStorageConfig, options: ContactSyncingOptions): Promise<void>;
28
29
  /**
@@ -31,6 +32,7 @@ export declare function syncContactsWithUserStorage(config: SyncContactsWithUser
31
32
  *
32
33
  * @param contact - The contact that was updated locally
33
34
  * @param options - Parameters used for syncing operations
35
+ * @returns Promise that resolves when the contact is updated
34
36
  */
35
37
  export declare function updateContactInRemoteStorage(contact: AddressBookEntry, options: ContactSyncingOptions): Promise<void>;
36
38
  /**
@@ -39,6 +41,7 @@ export declare function updateContactInRemoteStorage(contact: AddressBookEntry,
39
41
  *
40
42
  * @param contact - The contact that was deleted locally (contains at least address and chainId)
41
43
  * @param options - Parameters used for syncing operations
44
+ * @returns Promise that resolves when the contact is marked as deleted
42
45
  */
43
46
  export declare function deleteContactInRemoteStorage(contact: AddressBookEntry, options: ContactSyncingOptions): Promise<void>;
44
47
  //# sourceMappingURL=controller-integration.d.mts.map
@@ -1 +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,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;AAeF;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,2BAA2B,CAC/C,MAAM,EAAE,iCAAiC,EACzC,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CAgLf;AAgED;;;;;;GAMG;AACH,wBAAsB,4BAA4B,CAChD,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CA0Bf;AAED;;;;;;GAMG;AACH,wBAAsB,4BAA4B,CAChD,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CAgDf"}
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,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;AAeF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,2BAA2B,CAC/C,MAAM,EAAE,iCAAiC,EACzC,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CAuNf;AAmFD;;;;;;;GAOG;AACH,wBAAsB,4BAA4B,CAChD,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CA8Cf;AAED;;;;;;;GAOG;AACH,wBAAsB,4BAA4B,CAChD,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CAwDf"}
@@ -32,138 +32,167 @@ function createContactKey(contact) {
32
32
  *
33
33
  * @param config - Parameters used for syncing callbacks
34
34
  * @param options - Parameters used for syncing operations
35
+ * @returns Promise that resolves when contact synchronization is complete
35
36
  */
36
37
  export async function syncContactsWithUserStorage(config, options) {
37
- const { getMessenger, getUserStorageControllerInstance } = options;
38
+ const { getMessenger, getUserStorageControllerInstance, trace } = options;
38
39
  const { onContactSyncErroneousSituation, onContactUpdated, onContactDeleted, } = config;
39
- try {
40
- // Cannot perform sync, conditions not met
41
- if (!canPerformContactSyncing(options)) {
42
- return;
43
- }
44
- // Activate sync semaphore to prevent event loops
45
- await getUserStorageControllerInstance().setIsContactSyncingInProgress(true);
46
- // Get all local contacts from AddressBookController (exclude chain "*" contacts)
47
- const localVisibleContacts = getMessenger()
48
- .call('AddressBookController:list')
49
- .filter((contact) => !isContactBridgedFromAccounts(contact))
50
- .filter((contact) => contact.address && contact.chainId && contact.name?.trim()) || [];
51
- // Get remote contacts from user storage API
52
- const remoteContacts = await getRemoteContacts(options);
53
- // Filter remote contacts to exclude invalid ones (or empty array if no remote contacts)
54
- const validRemoteContacts = remoteContacts?.filter((contact) => contact.address && contact.chainId && contact.name?.trim()) || [];
55
- // Prepare maps for efficient lookup
56
- const localContactsMap = new Map();
57
- const remoteContactsMap = new Map();
58
- localVisibleContacts.forEach((contact) => {
59
- const key = createContactKey(contact);
60
- localContactsMap.set(key, contact);
61
- });
62
- validRemoteContacts.forEach((contact) => {
63
- const key = createContactKey(contact);
64
- remoteContactsMap.set(key, contact);
65
- });
66
- // Lists to track contacts that need to be synced
67
- const contactsToAddOrUpdateLocally = [];
68
- const contactsToDeleteLocally = [];
69
- const contactsToUpdateRemotely = [];
70
- // SCENARIO 2 & 6: Process remote contacts - handle new device sync and remote updates
71
- for (const remoteContact of validRemoteContacts) {
72
- const key = createContactKey(remoteContact);
73
- const localContact = localContactsMap.get(key);
74
- // Handle remote contact based on its status and local existence
75
- if (remoteContact.deletedAt) {
76
- // SCENARIO 8: Remote deletion - should be applied locally if contact exists locally
77
- if (localContact) {
78
- contactsToDeleteLocally.push(remoteContact);
79
- }
40
+ let localVisibleContacts = [];
41
+ let validRemoteContacts = [];
42
+ const performSync = async () => {
43
+ try {
44
+ // Cannot perform sync, conditions not met
45
+ if (!canPerformContactSyncing(options)) {
46
+ return;
80
47
  }
81
- else if (!localContact) {
82
- // SCENARIO 2: New contact from remote - import to local
83
- contactsToAddOrUpdateLocally.push(remoteContact);
84
- }
85
- else {
86
- // SCENARIO 4 & 6: Contact exists on both sides - check for conflicts
87
- const hasContentDifference = localContact.name !== remoteContact.name ||
88
- localContact.memo !== remoteContact.memo;
89
- if (hasContentDifference) {
90
- // Check timestamps to determine which version to keep
91
- const localTimestamp = localContact.lastUpdatedAt || 0;
92
- const remoteTimestamp = remoteContact.lastUpdatedAt || 0;
93
- if (localTimestamp >= remoteTimestamp) {
94
- // Local is newer (or same age) - use local version
95
- contactsToUpdateRemotely.push(localContact);
48
+ // Activate sync semaphore to prevent event loops
49
+ await getUserStorageControllerInstance().setIsContactSyncingInProgress(true);
50
+ // Get all local contacts from AddressBookController (exclude chain "*" contacts)
51
+ localVisibleContacts =
52
+ getMessenger()
53
+ .call('AddressBookController:list')
54
+ .filter((contact) => !isContactBridgedFromAccounts(contact))
55
+ .filter((contact) => contact.address && contact.chainId && contact.name?.trim()) || [];
56
+ // Get remote contacts from user storage API
57
+ const remoteContacts = await getRemoteContacts(options);
58
+ // Filter remote contacts to exclude invalid ones (or empty array if no remote contacts)
59
+ validRemoteContacts =
60
+ remoteContacts?.filter((contact) => contact.address && contact.chainId && contact.name?.trim()) || [];
61
+ // Prepare maps for efficient lookup
62
+ const localContactsMap = new Map();
63
+ const remoteContactsMap = new Map();
64
+ localVisibleContacts.forEach((contact) => {
65
+ const key = createContactKey(contact);
66
+ localContactsMap.set(key, contact);
67
+ });
68
+ validRemoteContacts.forEach((contact) => {
69
+ const key = createContactKey(contact);
70
+ remoteContactsMap.set(key, contact);
71
+ });
72
+ // Lists to track contacts that need to be synced
73
+ const contactsToAddOrUpdateLocally = [];
74
+ const contactsToDeleteLocally = [];
75
+ const contactsToUpdateRemotely = [];
76
+ // SCENARIO 2 & 6: Process remote contacts - handle new device sync and remote updates
77
+ for (const remoteContact of validRemoteContacts) {
78
+ const key = createContactKey(remoteContact);
79
+ const localContact = localContactsMap.get(key);
80
+ // Handle remote contact based on its status and local existence
81
+ if (remoteContact.deletedAt) {
82
+ // SCENARIO 8: Remote deletion - should be applied locally if contact exists locally
83
+ if (localContact) {
84
+ contactsToDeleteLocally.push(remoteContact);
96
85
  }
97
- else {
98
- // Remote is newer - use remote version
99
- contactsToAddOrUpdateLocally.push(remoteContact);
86
+ }
87
+ else if (!localContact) {
88
+ // SCENARIO 2: New contact from remote - import to local
89
+ contactsToAddOrUpdateLocally.push(remoteContact);
90
+ }
91
+ else {
92
+ // SCENARIO 4 & 6: Contact exists on both sides - check for conflicts
93
+ const hasContentDifference = localContact.name !== remoteContact.name ||
94
+ localContact.memo !== remoteContact.memo;
95
+ if (hasContentDifference) {
96
+ // Check timestamps to determine which version to keep
97
+ const localTimestamp = localContact.lastUpdatedAt || 0;
98
+ const remoteTimestamp = remoteContact.lastUpdatedAt || 0;
99
+ if (localTimestamp >= remoteTimestamp) {
100
+ // Local is newer (or same age) - use local version
101
+ contactsToUpdateRemotely.push(localContact);
102
+ }
103
+ else {
104
+ // Remote is newer - use remote version
105
+ contactsToAddOrUpdateLocally.push(remoteContact);
106
+ }
100
107
  }
108
+ // Else: content is identical, no action needed
101
109
  }
102
- // Else: content is identical, no action needed
103
- }
104
- }
105
- // SCENARIO 1, 3 & 5: Process local contacts not in remote - handles first sync and new local contacts
106
- for (const localContact of localVisibleContacts) {
107
- const key = createContactKey(localContact);
108
- const remoteContact = remoteContactsMap.get(key);
109
- if (!remoteContact) {
110
- // New local contact or first sync - add to remote
111
- contactsToUpdateRemotely.push(localContact);
112
110
  }
113
- }
114
- // Apply local deletions
115
- for (const contact of contactsToDeleteLocally) {
116
- try {
117
- getMessenger().call('AddressBookController:delete', contact.chainId, contact.address);
118
- if (onContactDeleted) {
119
- onContactDeleted();
111
+ // SCENARIO 1, 3 & 5: Process local contacts not in remote - handles first sync and new local contacts
112
+ for (const localContact of localVisibleContacts) {
113
+ const key = createContactKey(localContact);
114
+ const remoteContact = remoteContactsMap.get(key);
115
+ if (!remoteContact) {
116
+ // New local contact or first sync - add to remote
117
+ contactsToUpdateRemotely.push(localContact);
120
118
  }
121
119
  }
122
- catch (error) {
123
- console.error('Error deleting contact:', error);
124
- }
125
- }
126
- // Apply local additions/updates
127
- for (const contact of contactsToAddOrUpdateLocally) {
128
- if (!contact.deletedAt) {
120
+ // Apply local deletions
121
+ for (const contact of contactsToDeleteLocally) {
129
122
  try {
130
- getMessenger().call('AddressBookController:set', contact.address, contact.name || '', contact.chainId, contact.memo || '', contact.addressType);
131
- if (onContactUpdated) {
132
- onContactUpdated();
123
+ getMessenger().call('AddressBookController:delete', contact.chainId, contact.address);
124
+ if (onContactDeleted) {
125
+ onContactDeleted();
133
126
  }
134
127
  }
135
128
  catch (error) {
136
- console.error('Error updating contact:', error);
129
+ console.error('Error deleting contact:', error);
130
+ }
131
+ }
132
+ // Apply local additions/updates
133
+ for (const contact of contactsToAddOrUpdateLocally) {
134
+ if (!contact.deletedAt) {
135
+ try {
136
+ getMessenger().call('AddressBookController:set', contact.address, contact.name || '', contact.chainId, contact.memo || '', contact.addressType);
137
+ if (onContactUpdated) {
138
+ onContactUpdated();
139
+ }
140
+ }
141
+ catch (error) {
142
+ console.error('Error updating contact:', error);
143
+ }
144
+ }
145
+ }
146
+ // Apply changes to remote storage
147
+ if (contactsToUpdateRemotely.length > 0) {
148
+ const updatedRemoteContacts = {};
149
+ for (const localContact of contactsToUpdateRemotely) {
150
+ const key = createContactKey(localContact);
151
+ updatedRemoteContacts[key] = {
152
+ ...remoteContactsMap.get(key),
153
+ ...localContact,
154
+ lastUpdatedAt: Date.now(), // mark as updated
155
+ };
137
156
  }
157
+ // Save updated contacts to remote storage
158
+ await saveContactsToUserStorage(Object.values(updatedRemoteContacts), options);
138
159
  }
139
160
  }
140
- // Apply changes to remote storage
141
- if (contactsToUpdateRemotely.length > 0) {
142
- const updatedRemoteContacts = {};
143
- for (const localContact of contactsToUpdateRemotely) {
144
- const key = createContactKey(localContact);
145
- updatedRemoteContacts[key] = {
146
- ...remoteContactsMap.get(key),
147
- ...localContact,
148
- lastUpdatedAt: Date.now(), // mark as updated
149
- };
161
+ catch (error) {
162
+ if (onContactSyncErroneousSituation) {
163
+ onContactSyncErroneousSituation('Error synchronizing contacts', {
164
+ error,
165
+ });
166
+ // Re-throw the error to be handled by the caller
167
+ throw error;
150
168
  }
151
- // Save updated contacts to remote storage
152
- await saveContactsToUserStorage(Object.values(updatedRemoteContacts), options);
153
169
  }
154
- }
155
- catch (error) {
156
- if (onContactSyncErroneousSituation) {
157
- onContactSyncErroneousSituation('Error synchronizing contacts', {
158
- error,
159
- });
160
- // Re-throw the error to be handled by the caller
161
- throw error;
170
+ finally {
171
+ await getUserStorageControllerInstance().setIsContactSyncingInProgress(false);
162
172
  }
173
+ };
174
+ if (trace) {
175
+ // Gather pre-sync metrics for performance analysis
176
+ const initialLocalContacts = localVisibleContacts;
177
+ const initialValidRemoteContacts = validRemoteContacts;
178
+ return await trace({
179
+ name: 'Contact Sync Full',
180
+ data: {
181
+ localContactCount: initialLocalContacts.length,
182
+ remoteContactCount: initialValidRemoteContacts.length,
183
+ isFirstSync: initialValidRemoteContacts.length === 0 &&
184
+ initialLocalContacts.length > 0,
185
+ isNewDeviceSync: initialLocalContacts.length === 0 &&
186
+ initialValidRemoteContacts.length > 0,
187
+ isRegularSync: initialLocalContacts.length > 0 &&
188
+ initialValidRemoteContacts.length > 0,
189
+ hasDataToSync: initialLocalContacts.length > 0 ||
190
+ initialValidRemoteContacts.length > 0,
191
+ expectedWorkload: initialLocalContacts.length + initialValidRemoteContacts.length,
192
+ },
193
+ }, performSync);
163
194
  }
164
- finally {
165
- await getUserStorageControllerInstance().setIsContactSyncingInProgress(false);
166
- }
195
+ return await performSync();
167
196
  }
168
197
  /**
169
198
  * Retrieves remote contacts from user storage API
@@ -194,19 +223,34 @@ async function getRemoteContacts(options) {
194
223
  *
195
224
  * @param contacts - The contacts to save to user storage
196
225
  * @param options - Parameters used for saving contacts
226
+ * @returns Promise that resolves when contacts are saved
197
227
  */
198
228
  async function saveContactsToUserStorage(contacts, options) {
199
- const { getUserStorageControllerInstance } = options;
200
- if (!contacts || contacts.length === 0) {
201
- return;
202
- }
203
- // Convert each AddressBookEntry to UserStorageContactEntry format and create key-value pairs
204
- const storageEntries = contacts.map((contact) => {
205
- const key = createContactKey(contact);
206
- const storageEntry = mapAddressBookEntryToUserStorageEntry(contact);
207
- return [key, JSON.stringify(storageEntry)];
208
- });
209
- await getUserStorageControllerInstance().performBatchSetStorage(USER_STORAGE_FEATURE_NAMES.addressBook, storageEntries);
229
+ const { getUserStorageControllerInstance, trace } = options;
230
+ const saveContacts = async () => {
231
+ if (!contacts || contacts.length === 0) {
232
+ return;
233
+ }
234
+ // Convert each AddressBookEntry to UserStorageContactEntry format and create key-value pairs
235
+ const storageEntries = contacts.map((contact) => {
236
+ const key = createContactKey(contact);
237
+ const storageEntry = mapAddressBookEntryToUserStorageEntry(contact);
238
+ return [key, JSON.stringify(storageEntry)];
239
+ });
240
+ await getUserStorageControllerInstance().performBatchSetStorage(USER_STORAGE_FEATURE_NAMES.addressBook, storageEntries);
241
+ };
242
+ return trace
243
+ ? await trace({
244
+ name: 'Contact Sync Save Batch',
245
+ data: {
246
+ contactCount: contacts.length,
247
+ // Performance scaling indicators
248
+ hasBatchOperations: contacts.length > 1,
249
+ chainCount: new Set(contacts.map((c) => c.chainId)).size,
250
+ hasMemosCount: contacts.filter((c) => c.memo?.length).length,
251
+ },
252
+ }, saveContacts)
253
+ : await saveContacts();
210
254
  }
211
255
  /**
212
256
  * Updates a single contact in remote storage without performing a full sync
@@ -214,24 +258,40 @@ async function saveContactsToUserStorage(contacts, options) {
214
258
  *
215
259
  * @param contact - The contact that was updated locally
216
260
  * @param options - Parameters used for syncing operations
261
+ * @returns Promise that resolves when the contact is updated
217
262
  */
218
263
  export async function updateContactInRemoteStorage(contact, options) {
219
- if (!canPerformContactSyncing(options) ||
220
- !contact.address ||
221
- !contact.chainId ||
222
- !contact.name?.trim()) {
223
- return;
224
- }
225
- const { getUserStorageControllerInstance } = options;
226
- // Create an updated entry with timestamp
227
- const updatedEntry = {
228
- ...contact,
229
- lastUpdatedAt: contact.lastUpdatedAt || Date.now(),
264
+ const { trace } = options;
265
+ const updateContact = async () => {
266
+ if (!canPerformContactSyncing(options) ||
267
+ !contact.address ||
268
+ !contact.chainId ||
269
+ !contact.name?.trim()) {
270
+ return;
271
+ }
272
+ const { getUserStorageControllerInstance } = options;
273
+ // Create an updated entry with timestamp
274
+ const updatedEntry = {
275
+ ...contact,
276
+ lastUpdatedAt: contact.lastUpdatedAt || Date.now(),
277
+ };
278
+ const key = createContactKey(contact);
279
+ const storageEntry = mapAddressBookEntryToUserStorageEntry(updatedEntry);
280
+ // Save individual contact to remote storage
281
+ await getUserStorageControllerInstance().performSetStorage(`${USER_STORAGE_FEATURE_NAMES.addressBook}.${key}`, JSON.stringify(storageEntry));
230
282
  };
231
- const key = createContactKey(contact);
232
- const storageEntry = mapAddressBookEntryToUserStorageEntry(updatedEntry);
233
- // Save individual contact to remote storage
234
- await getUserStorageControllerInstance().performSetStorage(`${USER_STORAGE_FEATURE_NAMES.addressBook}.${key}`, JSON.stringify(storageEntry));
283
+ return trace
284
+ ? await trace({
285
+ name: 'Contact Sync Update Remote',
286
+ data: {
287
+ chainId: contact.chainId,
288
+ // Performance indicators
289
+ hasTimestamp: Boolean(contact.lastUpdatedAt),
290
+ hasMemo: Boolean(contact.memo?.length),
291
+ isUpdate: Boolean(contact.lastUpdatedAt), // vs new contact
292
+ },
293
+ }, updateContact)
294
+ : await updateContact();
235
295
  }
236
296
  /**
237
297
  * Marks a single contact as deleted in remote storage without performing a full sync
@@ -239,37 +299,44 @@ export async function updateContactInRemoteStorage(contact, options) {
239
299
  *
240
300
  * @param contact - The contact that was deleted locally (contains at least address and chainId)
241
301
  * @param options - Parameters used for syncing operations
302
+ * @returns Promise that resolves when the contact is marked as deleted
242
303
  */
243
304
  export async function deleteContactInRemoteStorage(contact, options) {
244
- if (!canPerformContactSyncing(options) ||
245
- !contact.address ||
246
- !contact.chainId ||
247
- !contact.name?.trim()) {
248
- return;
249
- }
250
- const { getUserStorageControllerInstance } = options;
251
- const key = createContactKey(contact);
252
- try {
253
- // Try to get the existing contact first
254
- const existingContactJson = await getUserStorageControllerInstance().performGetStorage(`${USER_STORAGE_FEATURE_NAMES.addressBook}.${key}`);
255
- if (existingContactJson) {
256
- // Mark the existing contact as deleted
257
- const existingStorageEntry = JSON.parse(existingContactJson);
258
- const existingContact = mapUserStorageEntryToAddressBookEntry(existingStorageEntry);
259
- const now = Date.now();
260
- const deletedContact = {
261
- ...existingContact,
262
- deletedAt: now,
263
- lastUpdatedAt: now,
264
- };
265
- const deletedStorageEntry = mapAddressBookEntryToUserStorageEntry(deletedContact);
266
- // Save the deleted contact back to storage
267
- await getUserStorageControllerInstance().performSetStorage(`${USER_STORAGE_FEATURE_NAMES.addressBook}.${key}`, JSON.stringify(deletedStorageEntry));
305
+ const { trace } = options;
306
+ const deleteContact = async () => {
307
+ if (!canPerformContactSyncing(options) ||
308
+ !contact.address ||
309
+ !contact.chainId ||
310
+ !contact.name?.trim()) {
311
+ return;
268
312
  }
269
- }
270
- catch {
271
- // If contact doesn't exist in remote storage, no need to mark as deleted
272
- console.warn('Contact not found in remote storage for deletion:', key);
273
- }
313
+ const { getUserStorageControllerInstance } = options;
314
+ const key = createContactKey(contact);
315
+ try {
316
+ // Try to get the existing contact first
317
+ const existingContactJson = await getUserStorageControllerInstance().performGetStorage(`${USER_STORAGE_FEATURE_NAMES.addressBook}.${key}`);
318
+ if (existingContactJson) {
319
+ // Mark the existing contact as deleted
320
+ const existingStorageEntry = JSON.parse(existingContactJson);
321
+ const existingContact = mapUserStorageEntryToAddressBookEntry(existingStorageEntry);
322
+ const now = Date.now();
323
+ const deletedContact = {
324
+ ...existingContact,
325
+ deletedAt: now,
326
+ lastUpdatedAt: now,
327
+ };
328
+ const deletedStorageEntry = mapAddressBookEntryToUserStorageEntry(deletedContact);
329
+ // Save the deleted contact back to storage
330
+ await getUserStorageControllerInstance().performSetStorage(`${USER_STORAGE_FEATURE_NAMES.addressBook}.${key}`, JSON.stringify(deletedStorageEntry));
331
+ }
332
+ }
333
+ catch {
334
+ // If contact doesn't exist in remote storage, no need to mark as deleted
335
+ console.warn('Contact not found in remote storage for deletion:', key);
336
+ }
337
+ };
338
+ return trace
339
+ ? await trace({ name: 'Contact Sync Delete Remote' }, deleteContact)
340
+ : await deleteContact();
274
341
  }
275
342
  //# sourceMappingURL=controller-integration.mjs.map