@metamask-previews/profile-sync-controller 17.1.0-preview-ca9d0265 → 17.1.0-preview-e149187d
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.
- package/CHANGELOG.md +0 -5
- package/dist/controllers/user-storage/UserStorageController.cjs +3 -56
- package/dist/controllers/user-storage/UserStorageController.cjs.map +1 -1
- package/dist/controllers/user-storage/UserStorageController.d.cts +2 -40
- package/dist/controllers/user-storage/UserStorageController.d.cts.map +1 -1
- package/dist/controllers/user-storage/UserStorageController.d.mts +2 -40
- package/dist/controllers/user-storage/UserStorageController.d.mts.map +1 -1
- package/dist/controllers/user-storage/UserStorageController.mjs +0 -53
- package/dist/controllers/user-storage/UserStorageController.mjs.map +1 -1
- package/dist/controllers/user-storage/constants.cjs +0 -1
- package/dist/controllers/user-storage/constants.cjs.map +1 -1
- package/dist/controllers/user-storage/constants.d.cts +0 -1
- package/dist/controllers/user-storage/constants.d.cts.map +1 -1
- package/dist/controllers/user-storage/constants.d.mts +0 -1
- package/dist/controllers/user-storage/constants.d.mts.map +1 -1
- package/dist/controllers/user-storage/constants.mjs +0 -1
- package/dist/controllers/user-storage/constants.mjs.map +1 -1
- package/dist/shared/storage-schema.cjs +1 -3
- package/dist/shared/storage-schema.cjs.map +1 -1
- package/dist/shared/storage-schema.d.cts +0 -2
- package/dist/shared/storage-schema.d.cts.map +1 -1
- package/dist/shared/storage-schema.d.mts +0 -2
- package/dist/shared/storage-schema.d.mts.map +1 -1
- package/dist/shared/storage-schema.mjs +1 -3
- package/dist/shared/storage-schema.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/controllers/user-storage/contact-syncing/constants.cjs +0 -12
- package/dist/controllers/user-storage/contact-syncing/constants.cjs.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/constants.d.cts +0 -9
- package/dist/controllers/user-storage/contact-syncing/constants.d.cts.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/constants.d.mts +0 -9
- package/dist/controllers/user-storage/contact-syncing/constants.d.mts.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/constants.mjs +0 -9
- package/dist/controllers/user-storage/contact-syncing/constants.mjs.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/controller-integration.cjs +0 -281
- package/dist/controllers/user-storage/contact-syncing/controller-integration.cjs.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/controller-integration.d.cts +0 -44
- package/dist/controllers/user-storage/contact-syncing/controller-integration.d.cts.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/controller-integration.d.mts +0 -44
- package/dist/controllers/user-storage/contact-syncing/controller-integration.d.mts.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/controller-integration.mjs +0 -275
- package/dist/controllers/user-storage/contact-syncing/controller-integration.mjs.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/setup-subscriptions.cjs +0 -50
- package/dist/controllers/user-storage/contact-syncing/setup-subscriptions.cjs.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/setup-subscriptions.d.cts +0 -8
- package/dist/controllers/user-storage/contact-syncing/setup-subscriptions.d.cts.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/setup-subscriptions.d.mts +0 -8
- package/dist/controllers/user-storage/contact-syncing/setup-subscriptions.d.mts.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/setup-subscriptions.mjs +0 -46
- package/dist/controllers/user-storage/contact-syncing/setup-subscriptions.mjs.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/sync-utils.cjs +0 -23
- package/dist/controllers/user-storage/contact-syncing/sync-utils.cjs.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/sync-utils.d.cts +0 -9
- package/dist/controllers/user-storage/contact-syncing/sync-utils.d.cts.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/sync-utils.d.mts +0 -9
- package/dist/controllers/user-storage/contact-syncing/sync-utils.d.mts.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/sync-utils.mjs +0 -19
- package/dist/controllers/user-storage/contact-syncing/sync-utils.mjs.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/types.cjs +0 -3
- package/dist/controllers/user-storage/contact-syncing/types.cjs.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/types.d.cts +0 -35
- package/dist/controllers/user-storage/contact-syncing/types.d.cts.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/types.d.mts +0 -35
- package/dist/controllers/user-storage/contact-syncing/types.d.mts.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/types.mjs +0 -2
- package/dist/controllers/user-storage/contact-syncing/types.mjs.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/utils.cjs +0 -64
- package/dist/controllers/user-storage/contact-syncing/utils.cjs.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/utils.d.cts +0 -36
- package/dist/controllers/user-storage/contact-syncing/utils.d.cts.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/utils.d.mts +0 -36
- package/dist/controllers/user-storage/contact-syncing/utils.d.mts.map +0 -1
- package/dist/controllers/user-storage/contact-syncing/utils.mjs +0 -58
- package/dist/controllers/user-storage/contact-syncing/utils.mjs.map +0 -1
|
@@ -1,281 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.deleteContactInRemoteStorage = exports.updateContactInRemoteStorage = exports.syncContactsWithUserStorage = void 0;
|
|
4
|
-
const sync_utils_1 = require("./sync-utils.cjs");
|
|
5
|
-
const utils_1 = require("./utils.cjs");
|
|
6
|
-
const utils_2 = require("./utils.cjs");
|
|
7
|
-
const storage_schema_1 = require("../../../shared/storage-schema.cjs");
|
|
8
|
-
/**
|
|
9
|
-
* Creates a unique key for a contact based on chainId and address
|
|
10
|
-
*
|
|
11
|
-
* @param contact - The contact to create a key for
|
|
12
|
-
* @returns A unique string key
|
|
13
|
-
*/
|
|
14
|
-
function createContactKey(contact) {
|
|
15
|
-
if (!contact.address) {
|
|
16
|
-
throw new Error('Contact address is required to create storage key');
|
|
17
|
-
}
|
|
18
|
-
return `${contact.chainId}_${contact.address.toLowerCase()}`;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Syncs contacts between local storage and user storage (remote).
|
|
22
|
-
*
|
|
23
|
-
* Handles the following syncing scenarios:
|
|
24
|
-
* 1. First Sync: When local contacts exist but there are no remote contacts, uploads all local contacts.
|
|
25
|
-
* 2. New Device Sync: Downloads remote contacts that don't exist locally (empty local address book).
|
|
26
|
-
* 3. Simple Merge: Ensures both sides (local & remote) have all contacts.
|
|
27
|
-
* 4. Contact Naming Conflicts: When same contact has different names, uses most recent by timestamp.
|
|
28
|
-
* 5. Local Updates: When a contact was updated locally, syncs changes to remote if local is newer.
|
|
29
|
-
* 6. Remote Updates: When a contact was updated remotely, applies changes locally if remote is newer.
|
|
30
|
-
* 7. Local Deletions: Handled by real-time event handlers (deleteContactInRemoteStorage) to prevent false positives.
|
|
31
|
-
* 8. Remote Deletions: When a contact was deleted remotely, applies deletion locally.
|
|
32
|
-
* 9. Concurrent Updates: Resolves conflicts using timestamps to determine the winner.
|
|
33
|
-
* 10. Restore After Delete: If a contact is modified after being deleted, restores it.
|
|
34
|
-
* 11. ChainId Differences: Treats same address on different chains as separate contacts.
|
|
35
|
-
*
|
|
36
|
-
* @param config - Parameters used for syncing callbacks
|
|
37
|
-
* @param options - Parameters used for syncing operations
|
|
38
|
-
*/
|
|
39
|
-
async function syncContactsWithUserStorage(config, options) {
|
|
40
|
-
const { getMessenger, getUserStorageControllerInstance } = options;
|
|
41
|
-
const { onContactSyncErroneousSituation, onContactUpdated, onContactDeleted, } = config;
|
|
42
|
-
try {
|
|
43
|
-
// Cannot perform sync, conditions not met
|
|
44
|
-
if (!(0, sync_utils_1.canPerformContactSyncing)(options)) {
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
// Activate sync semaphore to prevent event loops
|
|
48
|
-
await getUserStorageControllerInstance().setIsContactSyncingInProgress(true);
|
|
49
|
-
// Get all local contacts from AddressBookController (exclude chain "*" contacts)
|
|
50
|
-
const localVisibleContacts = getMessenger()
|
|
51
|
-
.call('AddressBookController:list')
|
|
52
|
-
.filter((contact) => !(0, utils_2.isContactBridgedFromAccounts)(contact))
|
|
53
|
-
.filter((contact) => contact.address && contact.chainId && contact.name?.trim()) || [];
|
|
54
|
-
// Get remote contacts from user storage API
|
|
55
|
-
const remoteContacts = await getRemoteContacts(options);
|
|
56
|
-
// Filter remote contacts to exclude invalid ones (or empty array if no remote contacts)
|
|
57
|
-
const validRemoteContacts = remoteContacts?.filter((contact) => contact.address && contact.chainId && contact.name?.trim()) || [];
|
|
58
|
-
// Prepare maps for efficient lookup
|
|
59
|
-
const localContactsMap = new Map();
|
|
60
|
-
const remoteContactsMap = new Map();
|
|
61
|
-
localVisibleContacts.forEach((contact) => {
|
|
62
|
-
const key = createContactKey(contact);
|
|
63
|
-
localContactsMap.set(key, contact);
|
|
64
|
-
});
|
|
65
|
-
validRemoteContacts.forEach((contact) => {
|
|
66
|
-
const key = createContactKey(contact);
|
|
67
|
-
remoteContactsMap.set(key, contact);
|
|
68
|
-
});
|
|
69
|
-
// Lists to track contacts that need to be synced
|
|
70
|
-
const contactsToAddOrUpdateLocally = [];
|
|
71
|
-
const contactsToDeleteLocally = [];
|
|
72
|
-
const contactsToUpdateRemotely = [];
|
|
73
|
-
// SCENARIO 2 & 6: Process remote contacts - handle new device sync and remote updates
|
|
74
|
-
for (const remoteContact of validRemoteContacts) {
|
|
75
|
-
const key = createContactKey(remoteContact);
|
|
76
|
-
const localContact = localContactsMap.get(key);
|
|
77
|
-
// Handle remote contact based on its status and local existence
|
|
78
|
-
if (remoteContact.deletedAt) {
|
|
79
|
-
// SCENARIO 8: Remote deletion - should be applied locally if contact exists locally
|
|
80
|
-
if (localContact) {
|
|
81
|
-
contactsToDeleteLocally.push(remoteContact);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
else if (!localContact) {
|
|
85
|
-
// SCENARIO 2: New contact from remote - import to local
|
|
86
|
-
contactsToAddOrUpdateLocally.push(remoteContact);
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
-
// SCENARIO 4 & 6: Contact exists on both sides - check for conflicts
|
|
90
|
-
const hasContentDifference = localContact.name !== remoteContact.name ||
|
|
91
|
-
localContact.memo !== remoteContact.memo;
|
|
92
|
-
if (hasContentDifference) {
|
|
93
|
-
// Check timestamps to determine which version to keep
|
|
94
|
-
const localTimestamp = localContact.lastUpdatedAt || 0;
|
|
95
|
-
const remoteTimestamp = remoteContact.lastUpdatedAt || 0;
|
|
96
|
-
if (localTimestamp >= remoteTimestamp) {
|
|
97
|
-
// Local is newer (or same age) - use local version
|
|
98
|
-
contactsToUpdateRemotely.push(localContact);
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
// Remote is newer - use remote version
|
|
102
|
-
contactsToAddOrUpdateLocally.push(remoteContact);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
// Else: content is identical, no action needed
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
// SCENARIO 1, 3 & 5: Process local contacts not in remote - handles first sync and new local contacts
|
|
109
|
-
for (const localContact of localVisibleContacts) {
|
|
110
|
-
const key = createContactKey(localContact);
|
|
111
|
-
const remoteContact = remoteContactsMap.get(key);
|
|
112
|
-
if (!remoteContact) {
|
|
113
|
-
// New local contact or first sync - add to remote
|
|
114
|
-
contactsToUpdateRemotely.push(localContact);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
// Apply local deletions
|
|
118
|
-
for (const contact of contactsToDeleteLocally) {
|
|
119
|
-
try {
|
|
120
|
-
getMessenger().call('AddressBookController:delete', contact.chainId, contact.address);
|
|
121
|
-
if (onContactDeleted) {
|
|
122
|
-
onContactDeleted();
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
catch (error) {
|
|
126
|
-
console.error('Error deleting contact:', error);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
// Apply local additions/updates
|
|
130
|
-
for (const contact of contactsToAddOrUpdateLocally) {
|
|
131
|
-
if (!contact.deletedAt) {
|
|
132
|
-
try {
|
|
133
|
-
getMessenger().call('AddressBookController:set', contact.address, contact.name || '', contact.chainId, contact.memo || '', contact.addressType);
|
|
134
|
-
if (onContactUpdated) {
|
|
135
|
-
onContactUpdated();
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
catch (error) {
|
|
139
|
-
console.error('Error updating contact:', error);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
// Apply changes to remote storage
|
|
144
|
-
if (contactsToUpdateRemotely.length > 0) {
|
|
145
|
-
const updatedRemoteContacts = {};
|
|
146
|
-
for (const localContact of contactsToUpdateRemotely) {
|
|
147
|
-
const key = createContactKey(localContact);
|
|
148
|
-
updatedRemoteContacts[key] = {
|
|
149
|
-
...remoteContactsMap.get(key),
|
|
150
|
-
...localContact,
|
|
151
|
-
lastUpdatedAt: Date.now(), // mark as updated
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
// Save updated contacts to remote storage
|
|
155
|
-
await saveContactsToUserStorage(Object.values(updatedRemoteContacts), options);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
catch (error) {
|
|
159
|
-
if (onContactSyncErroneousSituation) {
|
|
160
|
-
onContactSyncErroneousSituation('Error synchronizing contacts', {
|
|
161
|
-
error,
|
|
162
|
-
});
|
|
163
|
-
// Re-throw the error to be handled by the caller
|
|
164
|
-
throw error;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
finally {
|
|
168
|
-
await getUserStorageControllerInstance().setIsContactSyncingInProgress(false);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
exports.syncContactsWithUserStorage = syncContactsWithUserStorage;
|
|
172
|
-
/**
|
|
173
|
-
* Retrieves remote contacts from user storage API
|
|
174
|
-
*
|
|
175
|
-
* @param options - Parameters used for retrieving remote contacts
|
|
176
|
-
* @returns Array of contacts from remote storage, or null if none found
|
|
177
|
-
*/
|
|
178
|
-
async function getRemoteContacts(options) {
|
|
179
|
-
const { getUserStorageControllerInstance } = options;
|
|
180
|
-
try {
|
|
181
|
-
const remoteContactsJsonArray = await getUserStorageControllerInstance().performGetStorageAllFeatureEntries(storage_schema_1.USER_STORAGE_FEATURE_NAMES.addressBook);
|
|
182
|
-
if (!remoteContactsJsonArray || remoteContactsJsonArray.length === 0) {
|
|
183
|
-
return null;
|
|
184
|
-
}
|
|
185
|
-
// Parse each JSON entry and convert from UserStorageContactEntry to AddressBookEntry
|
|
186
|
-
const remoteStorageEntries = remoteContactsJsonArray.map((contactJson) => {
|
|
187
|
-
const entry = JSON.parse(contactJson);
|
|
188
|
-
return (0, utils_1.mapUserStorageEntryToAddressBookEntry)(entry);
|
|
189
|
-
});
|
|
190
|
-
return remoteStorageEntries;
|
|
191
|
-
}
|
|
192
|
-
catch {
|
|
193
|
-
return null;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Saves local contacts to user storage
|
|
198
|
-
*
|
|
199
|
-
* @param contacts - The contacts to save to user storage
|
|
200
|
-
* @param options - Parameters used for saving contacts
|
|
201
|
-
*/
|
|
202
|
-
async function saveContactsToUserStorage(contacts, options) {
|
|
203
|
-
const { getUserStorageControllerInstance } = options;
|
|
204
|
-
if (!contacts || contacts.length === 0) {
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
// Convert each AddressBookEntry to UserStorageContactEntry format and create key-value pairs
|
|
208
|
-
const storageEntries = contacts.map((contact) => {
|
|
209
|
-
const key = createContactKey(contact);
|
|
210
|
-
const storageEntry = (0, utils_1.mapAddressBookEntryToUserStorageEntry)(contact);
|
|
211
|
-
return [key, JSON.stringify(storageEntry)];
|
|
212
|
-
});
|
|
213
|
-
await getUserStorageControllerInstance().performBatchSetStorage(storage_schema_1.USER_STORAGE_FEATURE_NAMES.addressBook, storageEntries);
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Updates a single contact in remote storage without performing a full sync
|
|
217
|
-
* This is used when a contact is updated locally to efficiently push changes to remote
|
|
218
|
-
*
|
|
219
|
-
* @param contact - The contact that was updated locally
|
|
220
|
-
* @param options - Parameters used for syncing operations
|
|
221
|
-
*/
|
|
222
|
-
async function updateContactInRemoteStorage(contact, options) {
|
|
223
|
-
if (!(0, sync_utils_1.canPerformContactSyncing)(options) ||
|
|
224
|
-
!contact.address ||
|
|
225
|
-
!contact.chainId ||
|
|
226
|
-
!contact.name?.trim()) {
|
|
227
|
-
return;
|
|
228
|
-
}
|
|
229
|
-
const { getUserStorageControllerInstance } = options;
|
|
230
|
-
// Create an updated entry with timestamp
|
|
231
|
-
const updatedEntry = {
|
|
232
|
-
...contact,
|
|
233
|
-
lastUpdatedAt: contact.lastUpdatedAt || Date.now(),
|
|
234
|
-
};
|
|
235
|
-
const key = createContactKey(contact);
|
|
236
|
-
const storageEntry = (0, utils_1.mapAddressBookEntryToUserStorageEntry)(updatedEntry);
|
|
237
|
-
// Save individual contact to remote storage
|
|
238
|
-
await getUserStorageControllerInstance().performSetStorage(`${storage_schema_1.USER_STORAGE_FEATURE_NAMES.addressBook}.${key}`, JSON.stringify(storageEntry));
|
|
239
|
-
}
|
|
240
|
-
exports.updateContactInRemoteStorage = updateContactInRemoteStorage;
|
|
241
|
-
/**
|
|
242
|
-
* Marks a single contact as deleted in remote storage without performing a full sync
|
|
243
|
-
* This is used when a contact is deleted locally to efficiently push the deletion to remote
|
|
244
|
-
*
|
|
245
|
-
* @param contact - The contact that was deleted locally (contains at least address and chainId)
|
|
246
|
-
* @param options - Parameters used for syncing operations
|
|
247
|
-
*/
|
|
248
|
-
async function deleteContactInRemoteStorage(contact, options) {
|
|
249
|
-
if (!(0, sync_utils_1.canPerformContactSyncing)(options) ||
|
|
250
|
-
!contact.address ||
|
|
251
|
-
!contact.chainId ||
|
|
252
|
-
!contact.name?.trim()) {
|
|
253
|
-
return;
|
|
254
|
-
}
|
|
255
|
-
const { getUserStorageControllerInstance } = options;
|
|
256
|
-
const key = createContactKey(contact);
|
|
257
|
-
try {
|
|
258
|
-
// Try to get the existing contact first
|
|
259
|
-
const existingContactJson = await getUserStorageControllerInstance().performGetStorage(`${storage_schema_1.USER_STORAGE_FEATURE_NAMES.addressBook}.${key}`);
|
|
260
|
-
if (existingContactJson) {
|
|
261
|
-
// Mark the existing contact as deleted
|
|
262
|
-
const existingStorageEntry = JSON.parse(existingContactJson);
|
|
263
|
-
const existingContact = (0, utils_1.mapUserStorageEntryToAddressBookEntry)(existingStorageEntry);
|
|
264
|
-
const now = Date.now();
|
|
265
|
-
const deletedContact = {
|
|
266
|
-
...existingContact,
|
|
267
|
-
deletedAt: now,
|
|
268
|
-
lastUpdatedAt: now,
|
|
269
|
-
};
|
|
270
|
-
const deletedStorageEntry = (0, utils_1.mapAddressBookEntryToUserStorageEntry)(deletedContact);
|
|
271
|
-
// Save the deleted contact back to storage
|
|
272
|
-
await getUserStorageControllerInstance().performSetStorage(`${storage_schema_1.USER_STORAGE_FEATURE_NAMES.addressBook}.${key}`, JSON.stringify(deletedStorageEntry));
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
catch {
|
|
276
|
-
// If contact doesn't exist in remote storage, no need to mark as deleted
|
|
277
|
-
console.warn('Contact not found in remote storage for deletion:', key);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
exports.deleteContactInRemoteStorage = deleteContactInRemoteStorage;
|
|
281
|
-
//# sourceMappingURL=controller-integration.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
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,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;YAE9D,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YACvB,MAAM,cAAc,GAAG;gBACrB,GAAG,eAAe;gBAClB,SAAS,EAAE,GAAG;gBACd,aAAa,EAAE,GAAG;aACK,CAAC;YAE1B,MAAM,mBAAmB,GACvB,IAAA,6CAAqC,EAAC,cAAc,CAAC,CAAC;YAExD,2CAA2C;YAC3C,MAAM,gCAAgC,EAAE,CAAC,iBAAiB,CACxD,GAAG,2CAA0B,CAAC,WAAW,IAAI,GAAG,EAAE,EAClD,IAAI,CAAC,SAAS,CAAC,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,44 +0,0 @@
|
|
|
1
|
-
import type { AddressBookEntry } from "@metamask/address-book-controller";
|
|
2
|
-
import type { ContactSyncingOptions } from "./types.cjs";
|
|
3
|
-
export type SyncContactsWithUserStorageConfig = {
|
|
4
|
-
onContactSyncErroneousSituation?: (errorMessage: string, sentryContext?: Record<string, unknown>) => void;
|
|
5
|
-
onContactUpdated?: () => void;
|
|
6
|
-
onContactDeleted?: () => void;
|
|
7
|
-
};
|
|
8
|
-
/**
|
|
9
|
-
* Syncs contacts between local storage and user storage (remote).
|
|
10
|
-
*
|
|
11
|
-
* Handles the following syncing scenarios:
|
|
12
|
-
* 1. First Sync: When local contacts exist but there are no remote contacts, uploads all local contacts.
|
|
13
|
-
* 2. New Device Sync: Downloads remote contacts that don't exist locally (empty local address book).
|
|
14
|
-
* 3. Simple Merge: Ensures both sides (local & remote) have all contacts.
|
|
15
|
-
* 4. Contact Naming Conflicts: When same contact has different names, uses most recent by timestamp.
|
|
16
|
-
* 5. Local Updates: When a contact was updated locally, syncs changes to remote if local is newer.
|
|
17
|
-
* 6. Remote Updates: When a contact was updated remotely, applies changes locally if remote is newer.
|
|
18
|
-
* 7. Local Deletions: Handled by real-time event handlers (deleteContactInRemoteStorage) to prevent false positives.
|
|
19
|
-
* 8. Remote Deletions: When a contact was deleted remotely, applies deletion locally.
|
|
20
|
-
* 9. Concurrent Updates: Resolves conflicts using timestamps to determine the winner.
|
|
21
|
-
* 10. Restore After Delete: If a contact is modified after being deleted, restores it.
|
|
22
|
-
* 11. ChainId Differences: Treats same address on different chains as separate contacts.
|
|
23
|
-
*
|
|
24
|
-
* @param config - Parameters used for syncing callbacks
|
|
25
|
-
* @param options - Parameters used for syncing operations
|
|
26
|
-
*/
|
|
27
|
-
export declare function syncContactsWithUserStorage(config: SyncContactsWithUserStorageConfig, options: ContactSyncingOptions): Promise<void>;
|
|
28
|
-
/**
|
|
29
|
-
* Updates a single contact in remote storage without performing a full sync
|
|
30
|
-
* This is used when a contact is updated locally to efficiently push changes to remote
|
|
31
|
-
*
|
|
32
|
-
* @param contact - The contact that was updated locally
|
|
33
|
-
* @param options - Parameters used for syncing operations
|
|
34
|
-
*/
|
|
35
|
-
export declare function updateContactInRemoteStorage(contact: AddressBookEntry, options: ContactSyncingOptions): Promise<void>;
|
|
36
|
-
/**
|
|
37
|
-
* Marks a single contact as deleted in remote storage without performing a full sync
|
|
38
|
-
* This is used when a contact is deleted locally to efficiently push the deletion to remote
|
|
39
|
-
*
|
|
40
|
-
* @param contact - The contact that was deleted locally (contains at least address and chainId)
|
|
41
|
-
* @param options - Parameters used for syncing operations
|
|
42
|
-
*/
|
|
43
|
-
export declare function deleteContactInRemoteStorage(contact: AddressBookEntry, options: ContactSyncingOptions): Promise<void>;
|
|
44
|
-
//# sourceMappingURL=controller-integration.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
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,44 +0,0 @@
|
|
|
1
|
-
import type { AddressBookEntry } from "@metamask/address-book-controller";
|
|
2
|
-
import type { ContactSyncingOptions } from "./types.mjs";
|
|
3
|
-
export type SyncContactsWithUserStorageConfig = {
|
|
4
|
-
onContactSyncErroneousSituation?: (errorMessage: string, sentryContext?: Record<string, unknown>) => void;
|
|
5
|
-
onContactUpdated?: () => void;
|
|
6
|
-
onContactDeleted?: () => void;
|
|
7
|
-
};
|
|
8
|
-
/**
|
|
9
|
-
* Syncs contacts between local storage and user storage (remote).
|
|
10
|
-
*
|
|
11
|
-
* Handles the following syncing scenarios:
|
|
12
|
-
* 1. First Sync: When local contacts exist but there are no remote contacts, uploads all local contacts.
|
|
13
|
-
* 2. New Device Sync: Downloads remote contacts that don't exist locally (empty local address book).
|
|
14
|
-
* 3. Simple Merge: Ensures both sides (local & remote) have all contacts.
|
|
15
|
-
* 4. Contact Naming Conflicts: When same contact has different names, uses most recent by timestamp.
|
|
16
|
-
* 5. Local Updates: When a contact was updated locally, syncs changes to remote if local is newer.
|
|
17
|
-
* 6. Remote Updates: When a contact was updated remotely, applies changes locally if remote is newer.
|
|
18
|
-
* 7. Local Deletions: Handled by real-time event handlers (deleteContactInRemoteStorage) to prevent false positives.
|
|
19
|
-
* 8. Remote Deletions: When a contact was deleted remotely, applies deletion locally.
|
|
20
|
-
* 9. Concurrent Updates: Resolves conflicts using timestamps to determine the winner.
|
|
21
|
-
* 10. Restore After Delete: If a contact is modified after being deleted, restores it.
|
|
22
|
-
* 11. ChainId Differences: Treats same address on different chains as separate contacts.
|
|
23
|
-
*
|
|
24
|
-
* @param config - Parameters used for syncing callbacks
|
|
25
|
-
* @param options - Parameters used for syncing operations
|
|
26
|
-
*/
|
|
27
|
-
export declare function syncContactsWithUserStorage(config: SyncContactsWithUserStorageConfig, options: ContactSyncingOptions): Promise<void>;
|
|
28
|
-
/**
|
|
29
|
-
* Updates a single contact in remote storage without performing a full sync
|
|
30
|
-
* This is used when a contact is updated locally to efficiently push changes to remote
|
|
31
|
-
*
|
|
32
|
-
* @param contact - The contact that was updated locally
|
|
33
|
-
* @param options - Parameters used for syncing operations
|
|
34
|
-
*/
|
|
35
|
-
export declare function updateContactInRemoteStorage(contact: AddressBookEntry, options: ContactSyncingOptions): Promise<void>;
|
|
36
|
-
/**
|
|
37
|
-
* Marks a single contact as deleted in remote storage without performing a full sync
|
|
38
|
-
* This is used when a contact is deleted locally to efficiently push the deletion to remote
|
|
39
|
-
*
|
|
40
|
-
* @param contact - The contact that was deleted locally (contains at least address and chainId)
|
|
41
|
-
* @param options - Parameters used for syncing operations
|
|
42
|
-
*/
|
|
43
|
-
export declare function deleteContactInRemoteStorage(contact: AddressBookEntry, options: ContactSyncingOptions): Promise<void>;
|
|
44
|
-
//# sourceMappingURL=controller-integration.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|