@metamask-previews/account-tree-controller 2.0.0-preview-ee982ebe → 2.0.0-preview-7f5161b8

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 (39) hide show
  1. package/dist/AccountTreeController.cjs +3 -3
  2. package/dist/AccountTreeController.cjs.map +1 -1
  3. package/dist/AccountTreeController.mjs +3 -3
  4. package/dist/AccountTreeController.mjs.map +1 -1
  5. package/dist/backup-and-sync/analytics/traces.cjs.map +1 -1
  6. package/dist/backup-and-sync/analytics/traces.mjs.map +1 -1
  7. package/dist/backup-and-sync/authentication/utils.cjs.map +1 -1
  8. package/dist/backup-and-sync/authentication/utils.mjs.map +1 -1
  9. package/dist/backup-and-sync/service/atomic-sync-queue.cjs.map +1 -1
  10. package/dist/backup-and-sync/service/atomic-sync-queue.mjs.map +1 -1
  11. package/dist/backup-and-sync/service/index.cjs.map +1 -1
  12. package/dist/backup-and-sync/service/index.mjs.map +1 -1
  13. package/dist/backup-and-sync/syncing/group.cjs.map +1 -1
  14. package/dist/backup-and-sync/syncing/group.mjs.map +1 -1
  15. package/dist/backup-and-sync/syncing/legacy.cjs.map +1 -1
  16. package/dist/backup-and-sync/syncing/legacy.mjs.map +1 -1
  17. package/dist/backup-and-sync/syncing/metadata.cjs.map +1 -1
  18. package/dist/backup-and-sync/syncing/metadata.mjs.map +1 -1
  19. package/dist/backup-and-sync/syncing/wallet.cjs.map +1 -1
  20. package/dist/backup-and-sync/syncing/wallet.mjs.map +1 -1
  21. package/dist/backup-and-sync/user-storage/format-utils.cjs.map +1 -1
  22. package/dist/backup-and-sync/user-storage/format-utils.mjs.map +1 -1
  23. package/dist/backup-and-sync/user-storage/network-operations.cjs.map +1 -1
  24. package/dist/backup-and-sync/user-storage/network-operations.mjs.map +1 -1
  25. package/dist/backup-and-sync/user-storage/network-utils.cjs.map +1 -1
  26. package/dist/backup-and-sync/user-storage/network-utils.mjs.map +1 -1
  27. package/dist/backup-and-sync/user-storage/validation.cjs.map +1 -1
  28. package/dist/backup-and-sync/user-storage/validation.mjs.map +1 -1
  29. package/dist/backup-and-sync/utils/controller.cjs.map +1 -1
  30. package/dist/backup-and-sync/utils/controller.mjs.map +1 -1
  31. package/dist/group.cjs.map +1 -1
  32. package/dist/group.mjs.map +1 -1
  33. package/dist/rules/entropy.cjs.map +1 -1
  34. package/dist/rules/entropy.mjs.map +1 -1
  35. package/dist/rules/keyring.cjs.map +1 -1
  36. package/dist/rules/keyring.mjs.map +1 -1
  37. package/dist/rules/snap.cjs.map +1 -1
  38. package/dist/rules/snap.mjs.map +1 -1
  39. package/package.json +2 -2
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../src/backup-and-sync/service/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,uDAA0D;AAG1D,+DAAsD;AACtD,6CAAmD;AAGnD,sDAAyC;AAEzC,gEAAiD;AACjD,kDAMoB;AAMpB,4DAKyB;AACzB,8CAKkB;AAGlB;;;;;;;;GAQG;AACH,MAAa,oBAAoB;IAoB/B,YAAY,OAA6B;;QAnBhC,gDAA+B;QAExC;;WAEG;QACM,wDAAkC;QAE3C;;;WAGG;QACH,uDAAgD,IAAI,EAAC;QAErD;;;WAGG;QACH,4DAAqD,IAAI,EAAC;QAGxD,uBAAA,IAAI,iCAAY,OAAO,MAAA,CAAC;QACxB,uBAAA,IAAI,yCAAoB,IAAI,mCAAe,EAAE,MAAA,CAAC;IAChD,CAAC;IAED;;;;OAIG;IACH,IAAI,YAAY;QACd,OAAO,uBAAA,IAAI,qCAAS,CAAC,UAAU,CAAC,KAAK,CAAC,8BAA8B,CAAC;IACvE,CAAC;IAED;;;;OAIG;IACH,IAAI,oBAAoB;QACtB,OAAO,uBAAA,IAAI,qCAAS,CAAC,UAAU,CAAC,KAAK;aAClC,sCAAsC,CAAC;IAC5C,CAAC;IAED;;;;OAIG;IACH,IAAI,sBAAsB;QACxB,MAAM,0BAA0B,GAAG,u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Q,GAAG,EAC9C,KAAK,CACN,CAAC;QACF,MAAM,KAAK,CAAC;KACb;AACH,CAAC;AAED;;;;GAIG;AACH,KAAK,4DAA8B,OAAuB;IACxD,IAAI;QACF,MAAM,QAAQ,GAAG,uBAAA,IAAI,qCAAS,CAAC,iBAAiB,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAC9D,IAAI,CAAC,QAAQ,EAAE;YACb,OAAO;SACR;QAED,MAAM,MAAM,GAAG,uBAAA,IAAI,+EAAkB,MAAtB,IAAI,EAAmB,QAAQ,CAAC,CAAC;QAChD,IAAI,CAAC,MAAM,EAAE;YACX,OAAO,CAAC,4BAA4B;SACrC;QAED,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QACrC,IAAI,CAAC,KAAK,EAAE;YACV,OAAO;SACR;QAED,MAAM,eAAe,GAAG,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;QACnD,MAAM,eAAe,GAAG,MAAM,IAAA,6BAAY,EACxC,uBAAA,IAAI,qCAAS,EACb,eAAe,CAChB,CAAC;QAEF,2CAA2C;QAC3C,MAAM,oBAAoB,GAAG,MAAM,IAAA,sCAAuB,EACxD,uBAAA,IAAI,qCAAS,EACb,eAAe,EACf,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,UAAU,CAClC,CAAC;QAEF,MAAM,IAAA,2BAAiB,EACrB,uBAAA,IAAI,qCAAS,EACb,KAAK,EACL,oBAAoB,EACpB,eAAe,EACf,eAAe,CAChB,CAAC;KACH;IAAC,OAAO,KAAK,EAAE;QACd,IAAA,4BAAmB,EAAC,kCAAkC,OAAO,GAAG,EAAE,KAAK,CAAC,CAAC;QACzE,MAAM,KAAK,CAAC;KACb;AACH,CAAC","sourcesContent":["import type { AccountGroupId, AccountWalletId } from '@metamask/account-api';\nimport { AccountWalletType } from '@metamask/account-api';\nimport type { UserStorageController } from '@metamask/profile-sync-controller';\n\nimport { AtomicSyncQueue } from './atomic-sync-queue';\nimport { backupAndSyncLogger } from '../../logger';\nimport type { AccountTreeControllerState } from '../../types';\nimport type { AccountWalletEntropyObject } from '../../wallet';\nimport { TraceName } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport { getProfileId } from '../authentication';\nimport {\n createLocalGroupsFromUserStorage,\n performLegacyAccountSyncing,\n syncGroupsMetadata,\n syncGroupMetadata,\n syncWalletMetadata,\n} from '../syncing';\nimport type {\n BackupAndSyncContext,\n UserStorageSyncedWallet,\n UserStorageSyncedWalletGroup,\n} from '../types';\nimport {\n getAllGroupsFromUserStorage,\n getGroupFromUserStorage,\n getWalletFromUserStorage,\n pushGroupToUserStorageBatch,\n} from '../user-storage';\nimport {\n createStateSnapshot,\n restoreStateFromSnapshot,\n getLocalEntropyWallets,\n getLocalGroupsForEntropyWallet,\n} from '../utils';\nimport type { StateSnapshot } from '../utils';\n\n/**\n * Service responsible for managing all backup and sync operations.\n *\n * This service handles:\n * - Full sync operations\n * - Single item sync operations\n * - Sync queue management\n * - Sync state management\n */\nexport class BackupAndSyncService {\n readonly #context: BackupAndSyncContext;\n\n /**\n * Queue manager for atomic sync operations.\n */\n readonly #atomicSyncQueue: AtomicSyncQueue;\n\n /**\n * Cached promise for ongoing full sync operations.\n * Ensures multiple callers await the same sync operation.\n */\n #ongoingFullSyncPromise: Promise<void> | null = null;\n\n /**\n * Cached promise for the first ongoing full sync operation.\n * Ensures multiple callers await the same sync operation.\n */\n #firstOngoingFullSyncPromise: Promise<void> | null = null;\n\n constructor(context: BackupAndSyncContext) {\n this.#context = context;\n this.#atomicSyncQueue = new AtomicSyncQueue();\n }\n\n /**\n * Checks if syncing is currently in progress.\n *\n * @returns True if syncing is in progress.\n */\n get isInProgress(): boolean {\n return this.#context.controller.state.isAccountTreeSyncingInProgress;\n }\n\n /**\n * Checks if the account tree has been synced at least once.\n *\n * @returns True if the account tree has been synced at least once.\n */\n get hasSyncedAtLeastOnce(): boolean {\n return this.#context.controller.state\n .hasAccountTreeSyncingSyncedAtLeastOnce;\n }\n\n /**\n * Checks if backup and sync is enabled by checking UserStorageController state.\n *\n * @returns True if backup and sync + account syncing is enabled.\n */\n get isBackupAndSyncEnabled(): boolean {\n const userStorageControllerState = this.#context.messenger.call(\n 'UserStorageController:getState',\n );\n const { isAccountSyncingEnabled, isBackupAndSyncEnabled } =\n userStorageControllerState;\n\n return isBackupAndSyncEnabled && isAccountSyncingEnabled;\n }\n\n /**\n * Clears the atomic queue and resets ongoing operations.\n */\n clearState(): void {\n this.#atomicSyncQueue.clear();\n this.#ongoingFullSyncPromise = null;\n this.#firstOngoingFullSyncPromise = null;\n }\n\n /**\n * Handles changes to the user storage state.\n * Used to clear the backup and sync service state.\n *\n * @param state - The new user storage state.\n */\n handleUserStorageStateChange(\n state: UserStorageController.UserStorageControllerState,\n ): void {\n if (!state.isAccountSyncingEnabled || !state.isBackupAndSyncEnabled) {\n // If either syncing is disabled, clear the account tree state\n this.clearState();\n }\n }\n\n /**\n * Gets the entropy wallet associated with the given wallet ID.\n *\n * @param walletId - The wallet ID to look up.\n * @returns The associated entropy wallet, or undefined if not found.\n */\n #getEntropyWallet(\n walletId: AccountWalletId,\n ): AccountWalletEntropyObject | undefined {\n const wallet = this.#context.controller.state.accountTree.wallets[walletId];\n return wallet?.type === AccountWalletType.Entropy ? wallet : undefined;\n }\n\n /**\n * Sets up cleanup for ongoing sync promise tracking without affecting error propagation.\n *\n * @param promise - The promise to track and clean up\n * @returns The same promise (for chaining)\n */\n #setupOngoingPromiseCleanup(promise: Promise<void>): Promise<void> {\n this.#ongoingFullSyncPromise = promise;\n // Set up cleanup without affecting the returned promise\n promise\n .finally(() => {\n this.#ongoingFullSyncPromise = null;\n })\n .catch(() => {\n // Only ignore errors from the cleanup operation itself\n // The original promise errors are still propagated to callers\n });\n return promise;\n }\n\n /**\n * Enqueues a single wallet sync operation (fire-and-forget).\n * If the first full sync has not yet occurred, it does nothing.\n *\n * @param walletId - The wallet ID to sync.\n */\n enqueueSingleWalletSync(walletId: AccountWalletId): void {\n if (!this.isBackupAndSyncEnabled || !this.hasSyncedAtLeastOnce) {\n return;\n }\n\n // eslint-disable-next-line no-void\n void this.#atomicSyncQueue.enqueue(() =>\n this.#performSingleWalletSyncInner(walletId),\n );\n }\n\n /**\n * Enqueues a single group sync operation (fire-and-forget).\n * If the first full sync has not yet occurred, it does nothing.\n *\n * @param groupId - The group ID to sync.\n */\n enqueueSingleGroupSync(groupId: AccountGroupId): void {\n if (\n !this.isBackupAndSyncEnabled ||\n !this.hasSyncedAtLeastOnce ||\n // This prevents rate limiting scenarios where full syncs trigger group creations\n // that in turn enqueue the same single group syncs that the full sync just did.\n // This can very rarely lead to inconsistencies, but will be fixed on the next full sync.\n // TODO: let's improve this in the future by tracking the updates done in the full sync and\n // comparing against that.\n this.isInProgress\n ) {\n return;\n }\n\n // eslint-disable-next-line no-void\n void this.#atomicSyncQueue.enqueue(() =>\n this.#performSingleGroupSyncInner(groupId),\n );\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n * If a full sync is already in progress, it will return the ongoing promise.\n * This clears the atomic sync queue before starting the full sync.\n *\n * NOTE: in some very edge cases, this can be ran concurrently if triggered quickly after\n * toggling back and forth the backup and sync feature from the UI.\n *\n * @returns A promise that resolves when the sync is complete.\n */\n async performFullSync(): Promise<void> {\n if (!this.isBackupAndSyncEnabled) {\n return undefined;\n }\n\n // If there's an ongoing sync (including first sync), return it\n if (this.#ongoingFullSyncPromise) {\n return this.#ongoingFullSyncPromise;\n }\n\n // Create a new ongoing sync (sequential calls after previous completed)\n const newSyncPromise = this.#atomicSyncQueue.clearAndEnqueue(() =>\n this.#performFullSyncInner(),\n );\n\n // First sync setup - create and cache the first sync promise\n if (!this.#firstOngoingFullSyncPromise) {\n this.#firstOngoingFullSyncPromise = newSyncPromise;\n }\n\n return this.#setupOngoingPromiseCleanup(newSyncPromise);\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n *\n * If the first ever full sync is already in progress, it will return the ongoing promise.\n * If the first ever full sync has already completed, it will resolve and NOT start a new sync.\n *\n * This clears the atomic sync queue before starting the full sync.\n *\n * @returns A promise that resolves when the sync is complete.\n */\n async performFullSyncAtLeastOnce(): Promise<void> {\n if (!this.isBackupAndSyncEnabled) {\n return undefined;\n }\n\n if (!this.#firstOngoingFullSyncPromise) {\n this.#firstOngoingFullSyncPromise = this.#atomicSyncQueue.clearAndEnqueue(\n () => this.#performFullSyncInner(),\n );\n // eslint-disable-next-line no-void\n void this.#setupOngoingPromiseCleanup(this.#firstOngoingFullSyncPromise);\n }\n\n return this.#firstOngoingFullSyncPromise;\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n *\n * This method performs a comprehensive sync operation that:\n * 1. Identifies all local entropy wallets that can be synchronized\n * 2. Performs legacy account syncing if needed (for backwards compatibility)\n * - Disables subsequent legacy syncing by setting a flag in user storage\n * - Exits early if multichain account syncing is disabled after legacy sync\n * 3. Executes multichain account syncing for each wallet:\n * - Syncs wallet metadata bidirectionally\n * - Creates missing local groups from user storage data (or pushes local groups if none exist remotely)\n * - Refreshes local state to reflect newly created groups\n * - Syncs group metadata bidirectionally\n *\n * The sync is atomic per wallet with rollback on errors, but continues processing other wallets\n * if individual wallet sync fails. A global lock prevents concurrent sync operations.\n *\n * During this process, all other atomic multichain related user storage updates are blocked.\n *\n * @throws Will throw if the sync operation encounters unrecoverable errors\n */\n async #performFullSyncInner(): Promise<void> {\n // Prevent multiple syncs from running at the same time.\n // Also prevents atomic updates from being applied while syncing is in progress.\n if (this.isInProgress) {\n return;\n }\n\n // Set isAccountTreeSyncingInProgress immediately to prevent race conditions\n this.#context.controllerStateUpdateFn(\n (state: AccountTreeControllerState) => {\n state.isAccountTreeSyncingInProgress = true;\n },\n );\n\n // Encapsulate the sync logic in a function to allow tracing\n const bigSyncFn = async () => {\n try {\n // 1. Identifies all local entropy wallets that can be synchronized\n const localSyncableWallets = getLocalEntropyWallets(this.#context);\n\n if (!localSyncableWallets.length) {\n // No wallets to sync, just return. This shouldn't happen.\n return;\n }\n\n // 2. Iterate over each local wallet\n for (const wallet of localSyncableWallets) {\n const entropySourceId = wallet.metadata.entropy.id;\n\n let walletProfileId: ProfileId;\n let walletFromUserStorage: UserStorageSyncedWallet | null;\n let groupsFromUserStorage: UserStorageSyncedWalletGroup[];\n\n try {\n walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n\n [walletFromUserStorage, groupsFromUserStorage] = await Promise.all([\n getWalletFromUserStorage(this.#context, entropySourceId),\n getAllGroupsFromUserStorage(this.#context, entropySourceId),\n ]);\n\n // 2.1 Decide if we need to perform legacy account syncing\n if (\n !walletFromUserStorage ||\n !walletFromUserStorage.isLegacyAccountSyncingDisabled\n ) {\n // 2.2 Perform legacy account syncing\n // This will migrate legacy account data to the new structure.\n // This operation will only be performed once.\n await performLegacyAccountSyncing(\n this.#context,\n entropySourceId,\n walletProfileId,\n );\n }\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n const errorString = `Legacy syncing failed for wallet ${wallet.id}: ${errorMessage}`;\n\n backupAndSyncLogger(errorString);\n throw new Error(errorString);\n }\n\n // 3. Execute multichain account syncing\n let stateSnapshot: StateSnapshot | undefined;\n\n try {\n // 3.1 Wallet syncing\n // Create a state snapshot before processing each wallet for potential rollback\n stateSnapshot = createStateSnapshot(this.#context);\n\n // Sync wallet metadata bidirectionally\n await syncWalletMetadata(\n this.#context,\n wallet,\n walletFromUserStorage,\n walletProfileId,\n );\n\n // 3.2 Groups syncing\n // If groups data does not exist in user storage yet, create it\n if (!groupsFromUserStorage.length) {\n // If no groups exist in user storage, we can push all groups from the wallet to the user storage and exit\n await pushGroupToUserStorageBatch(\n this.#context,\n getLocalGroupsForEntropyWallet(this.#context, wallet.id),\n entropySourceId,\n );\n\n continue; // No need to proceed with metadata comparison if groups are new\n }\n\n // Create local groups for each group from user storage if they do not exist\n // This will ensure that we have all groups available locally before syncing metadata\n await createLocalGroupsFromUserStorage(\n this.#context,\n groupsFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n\n // Sync group metadata bidirectionally\n await syncGroupsMetadata(\n this.#context,\n wallet,\n groupsFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n const errorString = `Error during multichain account syncing for wallet ${wallet.id}: ${errorMessage}`;\n\n backupAndSyncLogger(errorString);\n\n // Attempt to rollback state changes for this wallet\n try {\n if (!stateSnapshot) {\n throw new Error(\n `State snapshot is missing for wallet ${wallet.id}`,\n );\n }\n restoreStateFromSnapshot(this.#context, stateSnapshot);\n backupAndSyncLogger(\n `Rolled back state changes for wallet ${wallet.id}`,\n );\n } catch (rollbackError) {\n backupAndSyncLogger(\n `Failed to rollback state for wallet ${wallet.id}:`,\n rollbackError instanceof Error\n ? rollbackError.message\n : String(rollbackError),\n );\n }\n\n // Continue with next wallet instead of failing the entire sync\n continue;\n }\n }\n } catch (error) {\n backupAndSyncLogger('Error during multichain account syncing:', error);\n throw error;\n }\n\n this.#context.controllerStateUpdateFn((state) => {\n state.hasAccountTreeSyncingSyncedAtLeastOnce = true;\n });\n };\n\n // Execute the big sync function with tracing and ensure state cleanup\n try {\n await this.#context.traceFn(\n {\n name: TraceName.AccountSyncFull,\n },\n bigSyncFn,\n );\n } finally {\n // Always reset state, regardless of success or failure\n this.#context.controllerStateUpdateFn(\n (state: AccountTreeControllerState) => {\n state.isAccountTreeSyncingInProgress = false;\n },\n );\n }\n }\n\n /**\n * Performs a single wallet's bidirectional metadata sync with user storage.\n *\n * @param walletId - The wallet ID to sync.\n */\n async #performSingleWalletSyncInner(\n walletId: AccountWalletId,\n ): Promise<void> {\n try {\n const wallet = this.#getEntropyWallet(walletId);\n if (!wallet) {\n return; // Only sync entropy wallets\n }\n\n const entropySourceId = wallet.metadata.entropy.id;\n const walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n const walletFromUserStorage = await getWalletFromUserStorage(\n this.#context,\n entropySourceId,\n );\n\n await syncWalletMetadata(\n this.#context,\n wallet,\n walletFromUserStorage,\n walletProfileId,\n );\n } catch (error) {\n backupAndSyncLogger(\n `Error in single wallet sync for ${walletId}:`,\n error,\n );\n throw error;\n }\n }\n\n /**\n * Performs a single group's bidirectional metadata sync with user storage.\n *\n * @param groupId - The group ID to sync.\n */\n async #performSingleGroupSyncInner(groupId: AccountGroupId): Promise<void> {\n try {\n const walletId = this.#context.groupIdToWalletId.get(groupId);\n if (!walletId) {\n return;\n }\n\n const wallet = this.#getEntropyWallet(walletId);\n if (!wallet) {\n return; // Only sync entropy wallets\n }\n\n const group = wallet.groups[groupId];\n if (!group) {\n return;\n }\n\n const entropySourceId = wallet.metadata.entropy.id;\n const walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n\n // Get the specific group from user storage\n const groupFromUserStorage = await getGroupFromUserStorage(\n this.#context,\n entropySourceId,\n group.metadata.entropy.groupIndex,\n );\n\n await syncGroupMetadata(\n this.#context,\n group,\n groupFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n } catch (error) {\n backupAndSyncLogger(`Error in single group sync for ${groupId}:`, error);\n throw error;\n }\n }\n}\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../src/backup-and-sync/service/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,uDAA0D;AAG1D,+DAAsD;AACtD,6CAAmD;AAGnD,sDAAyC;AAEzC,gEAAiD;AACjD,kDAMoB;AAMpB,4DAKyB;AACzB,8CAKkB;AAGlB;;;;;;;;GAQG;AACH,MAAa,oBAAoB;IAoB/B,YAAY,OAA6B;;QAnBhC,gDAA+B;QAExC;;WAEG;QACM,wDAAkC;QAE3C;;;WAGG;QACH,uDAAgD,IAAI,EAAC;QAErD;;;WAGG;QACH,4DAAqD,IAAI,EAAC;QAGxD,uBAAA,IAAI,iCAA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AAC,CAAC;QACrC,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,OAAO;QACT,CAAC;QAED,MAAM,eAAe,GAAG,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;QACnD,MAAM,eAAe,GAAG,MAAM,IAAA,6BAAY,EACxC,uBAAA,IAAI,qCAAS,EACb,eAAe,CAChB,CAAC;QAEF,2CAA2C;QAC3C,MAAM,oBAAoB,GAAG,MAAM,IAAA,sCAAuB,EACxD,uBAAA,IAAI,qCAAS,EACb,eAAe,EACf,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,UAAU,CAClC,CAAC;QAEF,MAAM,IAAA,2BAAiB,EACrB,uBAAA,IAAI,qCAAS,EACb,KAAK,EACL,oBAAoB,EACpB,eAAe,EACf,eAAe,CAChB,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAA,4BAAmB,EAAC,kCAAkC,OAAO,GAAG,EAAE,KAAK,CAAC,CAAC;QACzE,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["import type { AccountGroupId, AccountWalletId } from '@metamask/account-api';\nimport { AccountWalletType } from '@metamask/account-api';\nimport type { UserStorageController } from '@metamask/profile-sync-controller';\n\nimport { AtomicSyncQueue } from './atomic-sync-queue';\nimport { backupAndSyncLogger } from '../../logger';\nimport type { AccountTreeControllerState } from '../../types';\nimport type { AccountWalletEntropyObject } from '../../wallet';\nimport { TraceName } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport { getProfileId } from '../authentication';\nimport {\n createLocalGroupsFromUserStorage,\n performLegacyAccountSyncing,\n syncGroupsMetadata,\n syncGroupMetadata,\n syncWalletMetadata,\n} from '../syncing';\nimport type {\n BackupAndSyncContext,\n UserStorageSyncedWallet,\n UserStorageSyncedWalletGroup,\n} from '../types';\nimport {\n getAllGroupsFromUserStorage,\n getGroupFromUserStorage,\n getWalletFromUserStorage,\n pushGroupToUserStorageBatch,\n} from '../user-storage';\nimport {\n createStateSnapshot,\n restoreStateFromSnapshot,\n getLocalEntropyWallets,\n getLocalGroupsForEntropyWallet,\n} from '../utils';\nimport type { StateSnapshot } from '../utils';\n\n/**\n * Service responsible for managing all backup and sync operations.\n *\n * This service handles:\n * - Full sync operations\n * - Single item sync operations\n * - Sync queue management\n * - Sync state management\n */\nexport class BackupAndSyncService {\n readonly #context: BackupAndSyncContext;\n\n /**\n * Queue manager for atomic sync operations.\n */\n readonly #atomicSyncQueue: AtomicSyncQueue;\n\n /**\n * Cached promise for ongoing full sync operations.\n * Ensures multiple callers await the same sync operation.\n */\n #ongoingFullSyncPromise: Promise<void> | null = null;\n\n /**\n * Cached promise for the first ongoing full sync operation.\n * Ensures multiple callers await the same sync operation.\n */\n #firstOngoingFullSyncPromise: Promise<void> | null = null;\n\n constructor(context: BackupAndSyncContext) {\n this.#context = context;\n this.#atomicSyncQueue = new AtomicSyncQueue();\n }\n\n /**\n * Checks if syncing is currently in progress.\n *\n * @returns True if syncing is in progress.\n */\n get isInProgress(): boolean {\n return this.#context.controller.state.isAccountTreeSyncingInProgress;\n }\n\n /**\n * Checks if the account tree has been synced at least once.\n *\n * @returns True if the account tree has been synced at least once.\n */\n get hasSyncedAtLeastOnce(): boolean {\n return this.#context.controller.state\n .hasAccountTreeSyncingSyncedAtLeastOnce;\n }\n\n /**\n * Checks if backup and sync is enabled by checking UserStorageController state.\n *\n * @returns True if backup and sync + account syncing is enabled.\n */\n get isBackupAndSyncEnabled(): boolean {\n const userStorageControllerState = this.#context.messenger.call(\n 'UserStorageController:getState',\n );\n const { isAccountSyncingEnabled, isBackupAndSyncEnabled } =\n userStorageControllerState;\n\n return isBackupAndSyncEnabled && isAccountSyncingEnabled;\n }\n\n /**\n * Clears the atomic queue and resets ongoing operations.\n */\n clearState(): void {\n this.#atomicSyncQueue.clear();\n this.#ongoingFullSyncPromise = null;\n this.#firstOngoingFullSyncPromise = null;\n }\n\n /**\n * Handles changes to the user storage state.\n * Used to clear the backup and sync service state.\n *\n * @param state - The new user storage state.\n */\n handleUserStorageStateChange(\n state: UserStorageController.UserStorageControllerState,\n ): void {\n if (!state.isAccountSyncingEnabled || !state.isBackupAndSyncEnabled) {\n // If either syncing is disabled, clear the account tree state\n this.clearState();\n }\n }\n\n /**\n * Gets the entropy wallet associated with the given wallet ID.\n *\n * @param walletId - The wallet ID to look up.\n * @returns The associated entropy wallet, or undefined if not found.\n */\n #getEntropyWallet(\n walletId: AccountWalletId,\n ): AccountWalletEntropyObject | undefined {\n const wallet = this.#context.controller.state.accountTree.wallets[walletId];\n return wallet?.type === AccountWalletType.Entropy ? wallet : undefined;\n }\n\n /**\n * Sets up cleanup for ongoing sync promise tracking without affecting error propagation.\n *\n * @param promise - The promise to track and clean up\n * @returns The same promise (for chaining)\n */\n #setupOngoingPromiseCleanup(promise: Promise<void>): Promise<void> {\n this.#ongoingFullSyncPromise = promise;\n // Set up cleanup without affecting the returned promise\n promise\n .finally(() => {\n this.#ongoingFullSyncPromise = null;\n })\n .catch(() => {\n // Only ignore errors from the cleanup operation itself\n // The original promise errors are still propagated to callers\n });\n return promise;\n }\n\n /**\n * Enqueues a single wallet sync operation (fire-and-forget).\n * If the first full sync has not yet occurred, it does nothing.\n *\n * @param walletId - The wallet ID to sync.\n */\n enqueueSingleWalletSync(walletId: AccountWalletId): void {\n if (!this.isBackupAndSyncEnabled || !this.hasSyncedAtLeastOnce) {\n return;\n }\n\n // eslint-disable-next-line no-void\n void this.#atomicSyncQueue.enqueue(() =>\n this.#performSingleWalletSyncInner(walletId),\n );\n }\n\n /**\n * Enqueues a single group sync operation (fire-and-forget).\n * If the first full sync has not yet occurred, it does nothing.\n *\n * @param groupId - The group ID to sync.\n */\n enqueueSingleGroupSync(groupId: AccountGroupId): void {\n if (\n !this.isBackupAndSyncEnabled ||\n !this.hasSyncedAtLeastOnce ||\n // This prevents rate limiting scenarios where full syncs trigger group creations\n // that in turn enqueue the same single group syncs that the full sync just did.\n // This can very rarely lead to inconsistencies, but will be fixed on the next full sync.\n // TODO: let's improve this in the future by tracking the updates done in the full sync and\n // comparing against that.\n this.isInProgress\n ) {\n return;\n }\n\n // eslint-disable-next-line no-void\n void this.#atomicSyncQueue.enqueue(() =>\n this.#performSingleGroupSyncInner(groupId),\n );\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n * If a full sync is already in progress, it will return the ongoing promise.\n * This clears the atomic sync queue before starting the full sync.\n *\n * NOTE: in some very edge cases, this can be ran concurrently if triggered quickly after\n * toggling back and forth the backup and sync feature from the UI.\n *\n * @returns A promise that resolves when the sync is complete.\n */\n async performFullSync(): Promise<void> {\n if (!this.isBackupAndSyncEnabled) {\n return undefined;\n }\n\n // If there's an ongoing sync (including first sync), return it\n if (this.#ongoingFullSyncPromise) {\n return this.#ongoingFullSyncPromise;\n }\n\n // Create a new ongoing sync (sequential calls after previous completed)\n const newSyncPromise = this.#atomicSyncQueue.clearAndEnqueue(() =>\n this.#performFullSyncInner(),\n );\n\n // First sync setup - create and cache the first sync promise\n if (!this.#firstOngoingFullSyncPromise) {\n this.#firstOngoingFullSyncPromise = newSyncPromise;\n }\n\n return this.#setupOngoingPromiseCleanup(newSyncPromise);\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n *\n * If the first ever full sync is already in progress, it will return the ongoing promise.\n * If the first ever full sync has already completed, it will resolve and NOT start a new sync.\n *\n * This clears the atomic sync queue before starting the full sync.\n *\n * @returns A promise that resolves when the sync is complete.\n */\n async performFullSyncAtLeastOnce(): Promise<void> {\n if (!this.isBackupAndSyncEnabled) {\n return undefined;\n }\n\n if (!this.#firstOngoingFullSyncPromise) {\n this.#firstOngoingFullSyncPromise = this.#atomicSyncQueue.clearAndEnqueue(\n () => this.#performFullSyncInner(),\n );\n // eslint-disable-next-line no-void\n void this.#setupOngoingPromiseCleanup(this.#firstOngoingFullSyncPromise);\n }\n\n return this.#firstOngoingFullSyncPromise;\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n *\n * This method performs a comprehensive sync operation that:\n * 1. Identifies all local entropy wallets that can be synchronized\n * 2. Performs legacy account syncing if needed (for backwards compatibility)\n * - Disables subsequent legacy syncing by setting a flag in user storage\n * - Exits early if multichain account syncing is disabled after legacy sync\n * 3. Executes multichain account syncing for each wallet:\n * - Syncs wallet metadata bidirectionally\n * - Creates missing local groups from user storage data (or pushes local groups if none exist remotely)\n * - Refreshes local state to reflect newly created groups\n * - Syncs group metadata bidirectionally\n *\n * The sync is atomic per wallet with rollback on errors, but continues processing other wallets\n * if individual wallet sync fails. A global lock prevents concurrent sync operations.\n *\n * During this process, all other atomic multichain related user storage updates are blocked.\n *\n * @throws Will throw if the sync operation encounters unrecoverable errors\n */\n async #performFullSyncInner(): Promise<void> {\n // Prevent multiple syncs from running at the same time.\n // Also prevents atomic updates from being applied while syncing is in progress.\n if (this.isInProgress) {\n return;\n }\n\n // Set isAccountTreeSyncingInProgress immediately to prevent race conditions\n this.#context.controllerStateUpdateFn(\n (state: AccountTreeControllerState) => {\n state.isAccountTreeSyncingInProgress = true;\n },\n );\n\n // Encapsulate the sync logic in a function to allow tracing\n const bigSyncFn = async () => {\n try {\n // 1. Identifies all local entropy wallets that can be synchronized\n const localSyncableWallets = getLocalEntropyWallets(this.#context);\n\n if (!localSyncableWallets.length) {\n // No wallets to sync, just return. This shouldn't happen.\n return;\n }\n\n // 2. Iterate over each local wallet\n for (const wallet of localSyncableWallets) {\n const entropySourceId = wallet.metadata.entropy.id;\n\n let walletProfileId: ProfileId;\n let walletFromUserStorage: UserStorageSyncedWallet | null;\n let groupsFromUserStorage: UserStorageSyncedWalletGroup[];\n\n try {\n walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n\n [walletFromUserStorage, groupsFromUserStorage] = await Promise.all([\n getWalletFromUserStorage(this.#context, entropySourceId),\n getAllGroupsFromUserStorage(this.#context, entropySourceId),\n ]);\n\n // 2.1 Decide if we need to perform legacy account syncing\n if (\n !walletFromUserStorage ||\n !walletFromUserStorage.isLegacyAccountSyncingDisabled\n ) {\n // 2.2 Perform legacy account syncing\n // This will migrate legacy account data to the new structure.\n // This operation will only be performed once.\n await performLegacyAccountSyncing(\n this.#context,\n entropySourceId,\n walletProfileId,\n );\n }\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n const errorString = `Legacy syncing failed for wallet ${wallet.id}: ${errorMessage}`;\n\n backupAndSyncLogger(errorString);\n throw new Error(errorString);\n }\n\n // 3. Execute multichain account syncing\n let stateSnapshot: StateSnapshot | undefined;\n\n try {\n // 3.1 Wallet syncing\n // Create a state snapshot before processing each wallet for potential rollback\n stateSnapshot = createStateSnapshot(this.#context);\n\n // Sync wallet metadata bidirectionally\n await syncWalletMetadata(\n this.#context,\n wallet,\n walletFromUserStorage,\n walletProfileId,\n );\n\n // 3.2 Groups syncing\n // If groups data does not exist in user storage yet, create it\n if (!groupsFromUserStorage.length) {\n // If no groups exist in user storage, we can push all groups from the wallet to the user storage and exit\n await pushGroupToUserStorageBatch(\n this.#context,\n getLocalGroupsForEntropyWallet(this.#context, wallet.id),\n entropySourceId,\n );\n\n continue; // No need to proceed with metadata comparison if groups are new\n }\n\n // Create local groups for each group from user storage if they do not exist\n // This will ensure that we have all groups available locally before syncing metadata\n await createLocalGroupsFromUserStorage(\n this.#context,\n groupsFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n\n // Sync group metadata bidirectionally\n await syncGroupsMetadata(\n this.#context,\n wallet,\n groupsFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n const errorString = `Error during multichain account syncing for wallet ${wallet.id}: ${errorMessage}`;\n\n backupAndSyncLogger(errorString);\n\n // Attempt to rollback state changes for this wallet\n try {\n if (!stateSnapshot) {\n throw new Error(\n `State snapshot is missing for wallet ${wallet.id}`,\n );\n }\n restoreStateFromSnapshot(this.#context, stateSnapshot);\n backupAndSyncLogger(\n `Rolled back state changes for wallet ${wallet.id}`,\n );\n } catch (rollbackError) {\n backupAndSyncLogger(\n `Failed to rollback state for wallet ${wallet.id}:`,\n rollbackError instanceof Error\n ? rollbackError.message\n : String(rollbackError),\n );\n }\n\n // Continue with next wallet instead of failing the entire sync\n continue;\n }\n }\n } catch (error) {\n backupAndSyncLogger('Error during multichain account syncing:', error);\n throw error;\n }\n\n this.#context.controllerStateUpdateFn((state) => {\n state.hasAccountTreeSyncingSyncedAtLeastOnce = true;\n });\n };\n\n // Execute the big sync function with tracing and ensure state cleanup\n try {\n await this.#context.traceFn(\n {\n name: TraceName.AccountSyncFull,\n },\n bigSyncFn,\n );\n } finally {\n // Always reset state, regardless of success or failure\n this.#context.controllerStateUpdateFn(\n (state: AccountTreeControllerState) => {\n state.isAccountTreeSyncingInProgress = false;\n },\n );\n }\n }\n\n /**\n * Performs a single wallet's bidirectional metadata sync with user storage.\n *\n * @param walletId - The wallet ID to sync.\n */\n async #performSingleWalletSyncInner(\n walletId: AccountWalletId,\n ): Promise<void> {\n try {\n const wallet = this.#getEntropyWallet(walletId);\n if (!wallet) {\n return; // Only sync entropy wallets\n }\n\n const entropySourceId = wallet.metadata.entropy.id;\n const walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n const walletFromUserStorage = await getWalletFromUserStorage(\n this.#context,\n entropySourceId,\n );\n\n await syncWalletMetadata(\n this.#context,\n wallet,\n walletFromUserStorage,\n walletProfileId,\n );\n } catch (error) {\n backupAndSyncLogger(\n `Error in single wallet sync for ${walletId}:`,\n error,\n );\n throw error;\n }\n }\n\n /**\n * Performs a single group's bidirectional metadata sync with user storage.\n *\n * @param groupId - The group ID to sync.\n */\n async #performSingleGroupSyncInner(groupId: AccountGroupId): Promise<void> {\n try {\n const walletId = this.#context.groupIdToWalletId.get(groupId);\n if (!walletId) {\n return;\n }\n\n const wallet = this.#getEntropyWallet(walletId);\n if (!wallet) {\n return; // Only sync entropy wallets\n }\n\n const group = wallet.groups[groupId];\n if (!group) {\n return;\n }\n\n const entropySourceId = wallet.metadata.entropy.id;\n const walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n\n // Get the specific group from user storage\n const groupFromUserStorage = await getGroupFromUserStorage(\n this.#context,\n entropySourceId,\n group.metadata.entropy.groupIndex,\n );\n\n await syncGroupMetadata(\n this.#context,\n group,\n groupFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n } catch (error) {\n backupAndSyncLogger(`Error in single group sync for ${groupId}:`, error);\n throw error;\n }\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../src/backup-and-sync/service/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,iBAAiB,EAAE,8BAA8B;AAG1D,OAAO,EAAE,eAAe,EAAE,gCAA4B;AACtD,OAAO,EAAE,mBAAmB,EAAE,yBAAqB;AAGnD,OAAO,EAAE,SAAS,EAAE,+BAAqB;AAEzC,OAAO,EAAE,YAAY,EAAE,oCAA0B;AACjD,OAAO,EACL,gCAAgC,EAChC,2BAA2B,EAC3B,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EACnB,6BAAmB;AAMpB,OAAO,EACL,2BAA2B,EAC3B,uBAAuB,EACvB,wBAAwB,EACxB,2BAA2B,EAC5B,kCAAwB;AACzB,OAAO,EACL,mBAAmB,EACnB,wBAAwB,EACxB,sBAAsB,EACtB,8BAA8B,EAC/B,2BAAiB;AAGlB;;;;;;;;GAQG;AACH,MAAM,OAAO,oBAAoB;IAoB/B,YAAY,OAA6B;;QAnBhC,gDAA+B;QAExC;;WAEG;QACM,wDAAkC;QAE3C;;;WAGG;QACH,uDAAgD,IAAI,EAAC;QAErD;;;WAGG;QACH,4DAAqD,IAAI,EAAC;QAGxD,uBAAA,IAAI,iCAAY,OAAO,MAAA,CAAC;QACxB,uBAAA,IAAI,yCAAoB,IAAI,eAAe,EAAE,MAAA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,CAAC;KACb;AACH,CAAC","sourcesContent":["import type { AccountGroupId, AccountWalletId } from '@metamask/account-api';\nimport { AccountWalletType } from '@metamask/account-api';\nimport type { UserStorageController } from '@metamask/profile-sync-controller';\n\nimport { AtomicSyncQueue } from './atomic-sync-queue';\nimport { backupAndSyncLogger } from '../../logger';\nimport type { AccountTreeControllerState } from '../../types';\nimport type { AccountWalletEntropyObject } from '../../wallet';\nimport { TraceName } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport { getProfileId } from '../authentication';\nimport {\n createLocalGroupsFromUserStorage,\n performLegacyAccountSyncing,\n syncGroupsMetadata,\n syncGroupMetadata,\n syncWalletMetadata,\n} from '../syncing';\nimport type {\n BackupAndSyncContext,\n UserStorageSyncedWallet,\n UserStorageSyncedWalletGroup,\n} from '../types';\nimport {\n getAllGroupsFromUserStorage,\n getGroupFromUserStorage,\n getWalletFromUserStorage,\n pushGroupToUserStorageBatch,\n} from '../user-storage';\nimport {\n createStateSnapshot,\n restoreStateFromSnapshot,\n getLocalEntropyWallets,\n getLocalGroupsForEntropyWallet,\n} from '../utils';\nimport type { StateSnapshot } from '../utils';\n\n/**\n * Service responsible for managing all backup and sync operations.\n *\n * This service handles:\n * - Full sync operations\n * - Single item sync operations\n * - Sync queue management\n * - Sync state management\n */\nexport class BackupAndSyncService {\n readonly #context: BackupAndSyncContext;\n\n /**\n * Queue manager for atomic sync operations.\n */\n readonly #atomicSyncQueue: AtomicSyncQueue;\n\n /**\n * Cached promise for ongoing full sync operations.\n * Ensures multiple callers await the same sync operation.\n */\n #ongoingFullSyncPromise: Promise<void> | null = null;\n\n /**\n * Cached promise for the first ongoing full sync operation.\n * Ensures multiple callers await the same sync operation.\n */\n #firstOngoingFullSyncPromise: Promise<void> | null = null;\n\n constructor(context: BackupAndSyncContext) {\n this.#context = context;\n this.#atomicSyncQueue = new AtomicSyncQueue();\n }\n\n /**\n * Checks if syncing is currently in progress.\n *\n * @returns True if syncing is in progress.\n */\n get isInProgress(): boolean {\n return this.#context.controller.state.isAccountTreeSyncingInProgress;\n }\n\n /**\n * Checks if the account tree has been synced at least once.\n *\n * @returns True if the account tree has been synced at least once.\n */\n get hasSyncedAtLeastOnce(): boolean {\n return this.#context.controller.state\n .hasAccountTreeSyncingSyncedAtLeastOnce;\n }\n\n /**\n * Checks if backup and sync is enabled by checking UserStorageController state.\n *\n * @returns True if backup and sync + account syncing is enabled.\n */\n get isBackupAndSyncEnabled(): boolean {\n const userStorageControllerState = this.#context.messenger.call(\n 'UserStorageController:getState',\n );\n const { isAccountSyncingEnabled, isBackupAndSyncEnabled } =\n userStorageControllerState;\n\n return isBackupAndSyncEnabled && isAccountSyncingEnabled;\n }\n\n /**\n * Clears the atomic queue and resets ongoing operations.\n */\n clearState(): void {\n this.#atomicSyncQueue.clear();\n this.#ongoingFullSyncPromise = null;\n this.#firstOngoingFullSyncPromise = null;\n }\n\n /**\n * Handles changes to the user storage state.\n * Used to clear the backup and sync service state.\n *\n * @param state - The new user storage state.\n */\n handleUserStorageStateChange(\n state: UserStorageController.UserStorageControllerState,\n ): void {\n if (!state.isAccountSyncingEnabled || !state.isBackupAndSyncEnabled) {\n // If either syncing is disabled, clear the account tree state\n this.clearState();\n }\n }\n\n /**\n * Gets the entropy wallet associated with the given wallet ID.\n *\n * @param walletId - The wallet ID to look up.\n * @returns The associated entropy wallet, or undefined if not found.\n */\n #getEntropyWallet(\n walletId: AccountWalletId,\n ): AccountWalletEntropyObject | undefined {\n const wallet = this.#context.controller.state.accountTree.wallets[walletId];\n return wallet?.type === AccountWalletType.Entropy ? wallet : undefined;\n }\n\n /**\n * Sets up cleanup for ongoing sync promise tracking without affecting error propagation.\n *\n * @param promise - The promise to track and clean up\n * @returns The same promise (for chaining)\n */\n #setupOngoingPromiseCleanup(promise: Promise<void>): Promise<void> {\n this.#ongoingFullSyncPromise = promise;\n // Set up cleanup without affecting the returned promise\n promise\n .finally(() => {\n this.#ongoingFullSyncPromise = null;\n })\n .catch(() => {\n // Only ignore errors from the cleanup operation itself\n // The original promise errors are still propagated to callers\n });\n return promise;\n }\n\n /**\n * Enqueues a single wallet sync operation (fire-and-forget).\n * If the first full sync has not yet occurred, it does nothing.\n *\n * @param walletId - The wallet ID to sync.\n */\n enqueueSingleWalletSync(walletId: AccountWalletId): void {\n if (!this.isBackupAndSyncEnabled || !this.hasSyncedAtLeastOnce) {\n return;\n }\n\n // eslint-disable-next-line no-void\n void this.#atomicSyncQueue.enqueue(() =>\n this.#performSingleWalletSyncInner(walletId),\n );\n }\n\n /**\n * Enqueues a single group sync operation (fire-and-forget).\n * If the first full sync has not yet occurred, it does nothing.\n *\n * @param groupId - The group ID to sync.\n */\n enqueueSingleGroupSync(groupId: AccountGroupId): void {\n if (\n !this.isBackupAndSyncEnabled ||\n !this.hasSyncedAtLeastOnce ||\n // This prevents rate limiting scenarios where full syncs trigger group creations\n // that in turn enqueue the same single group syncs that the full sync just did.\n // This can very rarely lead to inconsistencies, but will be fixed on the next full sync.\n // TODO: let's improve this in the future by tracking the updates done in the full sync and\n // comparing against that.\n this.isInProgress\n ) {\n return;\n }\n\n // eslint-disable-next-line no-void\n void this.#atomicSyncQueue.enqueue(() =>\n this.#performSingleGroupSyncInner(groupId),\n );\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n * If a full sync is already in progress, it will return the ongoing promise.\n * This clears the atomic sync queue before starting the full sync.\n *\n * NOTE: in some very edge cases, this can be ran concurrently if triggered quickly after\n * toggling back and forth the backup and sync feature from the UI.\n *\n * @returns A promise that resolves when the sync is complete.\n */\n async performFullSync(): Promise<void> {\n if (!this.isBackupAndSyncEnabled) {\n return undefined;\n }\n\n // If there's an ongoing sync (including first sync), return it\n if (this.#ongoingFullSyncPromise) {\n return this.#ongoingFullSyncPromise;\n }\n\n // Create a new ongoing sync (sequential calls after previous completed)\n const newSyncPromise = this.#atomicSyncQueue.clearAndEnqueue(() =>\n this.#performFullSyncInner(),\n );\n\n // First sync setup - create and cache the first sync promise\n if (!this.#firstOngoingFullSyncPromise) {\n this.#firstOngoingFullSyncPromise = newSyncPromise;\n }\n\n return this.#setupOngoingPromiseCleanup(newSyncPromise);\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n *\n * If the first ever full sync is already in progress, it will return the ongoing promise.\n * If the first ever full sync has already completed, it will resolve and NOT start a new sync.\n *\n * This clears the atomic sync queue before starting the full sync.\n *\n * @returns A promise that resolves when the sync is complete.\n */\n async performFullSyncAtLeastOnce(): Promise<void> {\n if (!this.isBackupAndSyncEnabled) {\n return undefined;\n }\n\n if (!this.#firstOngoingFullSyncPromise) {\n this.#firstOngoingFullSyncPromise = this.#atomicSyncQueue.clearAndEnqueue(\n () => this.#performFullSyncInner(),\n );\n // eslint-disable-next-line no-void\n void this.#setupOngoingPromiseCleanup(this.#firstOngoingFullSyncPromise);\n }\n\n return this.#firstOngoingFullSyncPromise;\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n *\n * This method performs a comprehensive sync operation that:\n * 1. Identifies all local entropy wallets that can be synchronized\n * 2. Performs legacy account syncing if needed (for backwards compatibility)\n * - Disables subsequent legacy syncing by setting a flag in user storage\n * - Exits early if multichain account syncing is disabled after legacy sync\n * 3. Executes multichain account syncing for each wallet:\n * - Syncs wallet metadata bidirectionally\n * - Creates missing local groups from user storage data (or pushes local groups if none exist remotely)\n * - Refreshes local state to reflect newly created groups\n * - Syncs group metadata bidirectionally\n *\n * The sync is atomic per wallet with rollback on errors, but continues processing other wallets\n * if individual wallet sync fails. A global lock prevents concurrent sync operations.\n *\n * During this process, all other atomic multichain related user storage updates are blocked.\n *\n * @throws Will throw if the sync operation encounters unrecoverable errors\n */\n async #performFullSyncInner(): Promise<void> {\n // Prevent multiple syncs from running at the same time.\n // Also prevents atomic updates from being applied while syncing is in progress.\n if (this.isInProgress) {\n return;\n }\n\n // Set isAccountTreeSyncingInProgress immediately to prevent race conditions\n this.#context.controllerStateUpdateFn(\n (state: AccountTreeControllerState) => {\n state.isAccountTreeSyncingInProgress = true;\n },\n );\n\n // Encapsulate the sync logic in a function to allow tracing\n const bigSyncFn = async () => {\n try {\n // 1. Identifies all local entropy wallets that can be synchronized\n const localSyncableWallets = getLocalEntropyWallets(this.#context);\n\n if (!localSyncableWallets.length) {\n // No wallets to sync, just return. This shouldn't happen.\n return;\n }\n\n // 2. Iterate over each local wallet\n for (const wallet of localSyncableWallets) {\n const entropySourceId = wallet.metadata.entropy.id;\n\n let walletProfileId: ProfileId;\n let walletFromUserStorage: UserStorageSyncedWallet | null;\n let groupsFromUserStorage: UserStorageSyncedWalletGroup[];\n\n try {\n walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n\n [walletFromUserStorage, groupsFromUserStorage] = await Promise.all([\n getWalletFromUserStorage(this.#context, entropySourceId),\n getAllGroupsFromUserStorage(this.#context, entropySourceId),\n ]);\n\n // 2.1 Decide if we need to perform legacy account syncing\n if (\n !walletFromUserStorage ||\n !walletFromUserStorage.isLegacyAccountSyncingDisabled\n ) {\n // 2.2 Perform legacy account syncing\n // This will migrate legacy account data to the new structure.\n // This operation will only be performed once.\n await performLegacyAccountSyncing(\n this.#context,\n entropySourceId,\n walletProfileId,\n );\n }\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n const errorString = `Legacy syncing failed for wallet ${wallet.id}: ${errorMessage}`;\n\n backupAndSyncLogger(errorString);\n throw new Error(errorString);\n }\n\n // 3. Execute multichain account syncing\n let stateSnapshot: StateSnapshot | undefined;\n\n try {\n // 3.1 Wallet syncing\n // Create a state snapshot before processing each wallet for potential rollback\n stateSnapshot = createStateSnapshot(this.#context);\n\n // Sync wallet metadata bidirectionally\n await syncWalletMetadata(\n this.#context,\n wallet,\n walletFromUserStorage,\n walletProfileId,\n );\n\n // 3.2 Groups syncing\n // If groups data does not exist in user storage yet, create it\n if (!groupsFromUserStorage.length) {\n // If no groups exist in user storage, we can push all groups from the wallet to the user storage and exit\n await pushGroupToUserStorageBatch(\n this.#context,\n getLocalGroupsForEntropyWallet(this.#context, wallet.id),\n entropySourceId,\n );\n\n continue; // No need to proceed with metadata comparison if groups are new\n }\n\n // Create local groups for each group from user storage if they do not exist\n // This will ensure that we have all groups available locally before syncing metadata\n await createLocalGroupsFromUserStorage(\n this.#context,\n groupsFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n\n // Sync group metadata bidirectionally\n await syncGroupsMetadata(\n this.#context,\n wallet,\n groupsFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n const errorString = `Error during multichain account syncing for wallet ${wallet.id}: ${errorMessage}`;\n\n backupAndSyncLogger(errorString);\n\n // Attempt to rollback state changes for this wallet\n try {\n if (!stateSnapshot) {\n throw new Error(\n `State snapshot is missing for wallet ${wallet.id}`,\n );\n }\n restoreStateFromSnapshot(this.#context, stateSnapshot);\n backupAndSyncLogger(\n `Rolled back state changes for wallet ${wallet.id}`,\n );\n } catch (rollbackError) {\n backupAndSyncLogger(\n `Failed to rollback state for wallet ${wallet.id}:`,\n rollbackError instanceof Error\n ? rollbackError.message\n : String(rollbackError),\n );\n }\n\n // Continue with next wallet instead of failing the entire sync\n continue;\n }\n }\n } catch (error) {\n backupAndSyncLogger('Error during multichain account syncing:', error);\n throw error;\n }\n\n this.#context.controllerStateUpdateFn((state) => {\n state.hasAccountTreeSyncingSyncedAtLeastOnce = true;\n });\n };\n\n // Execute the big sync function with tracing and ensure state cleanup\n try {\n await this.#context.traceFn(\n {\n name: TraceName.AccountSyncFull,\n },\n bigSyncFn,\n );\n } finally {\n // Always reset state, regardless of success or failure\n this.#context.controllerStateUpdateFn(\n (state: AccountTreeControllerState) => {\n state.isAccountTreeSyncingInProgress = false;\n },\n );\n }\n }\n\n /**\n * Performs a single wallet's bidirectional metadata sync with user storage.\n *\n * @param walletId - The wallet ID to sync.\n */\n async #performSingleWalletSyncInner(\n walletId: AccountWalletId,\n ): Promise<void> {\n try {\n const wallet = this.#getEntropyWallet(walletId);\n if (!wallet) {\n return; // Only sync entropy wallets\n }\n\n const entropySourceId = wallet.metadata.entropy.id;\n const walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n const walletFromUserStorage = await getWalletFromUserStorage(\n this.#context,\n entropySourceId,\n );\n\n await syncWalletMetadata(\n this.#context,\n wallet,\n walletFromUserStorage,\n walletProfileId,\n );\n } catch (error) {\n backupAndSyncLogger(\n `Error in single wallet sync for ${walletId}:`,\n error,\n );\n throw error;\n }\n }\n\n /**\n * Performs a single group's bidirectional metadata sync with user storage.\n *\n * @param groupId - The group ID to sync.\n */\n async #performSingleGroupSyncInner(groupId: AccountGroupId): Promise<void> {\n try {\n const walletId = this.#context.groupIdToWalletId.get(groupId);\n if (!walletId) {\n return;\n }\n\n const wallet = this.#getEntropyWallet(walletId);\n if (!wallet) {\n return; // Only sync entropy wallets\n }\n\n const group = wallet.groups[groupId];\n if (!group) {\n return;\n }\n\n const entropySourceId = wallet.metadata.entropy.id;\n const walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n\n // Get the specific group from user storage\n const groupFromUserStorage = await getGroupFromUserStorage(\n this.#context,\n entropySourceId,\n group.metadata.entropy.groupIndex,\n );\n\n await syncGroupMetadata(\n this.#context,\n group,\n groupFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n } catch (error) {\n backupAndSyncLogger(`Error in single group sync for ${groupId}:`, error);\n throw error;\n }\n }\n}\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../src/backup-and-sync/service/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,iBAAiB,EAAE,8BAA8B;AAG1D,OAAO,EAAE,eAAe,EAAE,gCAA4B;AACtD,OAAO,EAAE,mBAAmB,EAAE,yBAAqB;AAGnD,OAAO,EAAE,SAAS,EAAE,+BAAqB;AAEzC,OAAO,EAAE,Y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MAAM,QAAQ,GAAG,uBAAA,IAAI,qCAAS,CAAC,iBAAiB,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAC9D,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,OAAO;QACT,CAAC;QAED,MAAM,MAAM,GAAG,uBAAA,IAAI,+EAAkB,MAAtB,IAAI,EAAmB,QAAQ,CAAC,CAAC;QAChD,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,OAAO,CAAC,4BAA4B;QACtC,CAAC;QAED,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QACrC,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,OAAO;QACT,CAAC;QAED,MAAM,eAAe,GAAG,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;QACnD,MAAM,eAAe,GAAG,MAAM,YAAY,CACxC,uBAAA,IAAI,qCAAS,EACb,eAAe,CAChB,CAAC;QAEF,2CAA2C;QAC3C,MAAM,oBAAoB,GAAG,MAAM,uBAAuB,CACxD,uBAAA,IAAI,qCAAS,EACb,eAAe,EACf,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,UAAU,CAClC,CAAC;QAEF,MAAM,iBAAiB,CACrB,uBAAA,IAAI,qCAAS,EACb,KAAK,EACL,oBAAoB,EACpB,eAAe,EACf,eAAe,CAChB,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,mBAAmB,CAAC,kCAAkC,OAAO,GAAG,EAAE,KAAK,CAAC,CAAC;QACzE,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["import type { AccountGroupId, AccountWalletId } from '@metamask/account-api';\nimport { AccountWalletType } from '@metamask/account-api';\nimport type { UserStorageController } from '@metamask/profile-sync-controller';\n\nimport { AtomicSyncQueue } from './atomic-sync-queue';\nimport { backupAndSyncLogger } from '../../logger';\nimport type { AccountTreeControllerState } from '../../types';\nimport type { AccountWalletEntropyObject } from '../../wallet';\nimport { TraceName } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport { getProfileId } from '../authentication';\nimport {\n createLocalGroupsFromUserStorage,\n performLegacyAccountSyncing,\n syncGroupsMetadata,\n syncGroupMetadata,\n syncWalletMetadata,\n} from '../syncing';\nimport type {\n BackupAndSyncContext,\n UserStorageSyncedWallet,\n UserStorageSyncedWalletGroup,\n} from '../types';\nimport {\n getAllGroupsFromUserStorage,\n getGroupFromUserStorage,\n getWalletFromUserStorage,\n pushGroupToUserStorageBatch,\n} from '../user-storage';\nimport {\n createStateSnapshot,\n restoreStateFromSnapshot,\n getLocalEntropyWallets,\n getLocalGroupsForEntropyWallet,\n} from '../utils';\nimport type { StateSnapshot } from '../utils';\n\n/**\n * Service responsible for managing all backup and sync operations.\n *\n * This service handles:\n * - Full sync operations\n * - Single item sync operations\n * - Sync queue management\n * - Sync state management\n */\nexport class BackupAndSyncService {\n readonly #context: BackupAndSyncContext;\n\n /**\n * Queue manager for atomic sync operations.\n */\n readonly #atomicSyncQueue: AtomicSyncQueue;\n\n /**\n * Cached promise for ongoing full sync operations.\n * Ensures multiple callers await the same sync operation.\n */\n #ongoingFullSyncPromise: Promise<void> | null = null;\n\n /**\n * Cached promise for the first ongoing full sync operation.\n * Ensures multiple callers await the same sync operation.\n */\n #firstOngoingFullSyncPromise: Promise<void> | null = null;\n\n constructor(context: BackupAndSyncContext) {\n this.#context = context;\n this.#atomicSyncQueue = new AtomicSyncQueue();\n }\n\n /**\n * Checks if syncing is currently in progress.\n *\n * @returns True if syncing is in progress.\n */\n get isInProgress(): boolean {\n return this.#context.controller.state.isAccountTreeSyncingInProgress;\n }\n\n /**\n * Checks if the account tree has been synced at least once.\n *\n * @returns True if the account tree has been synced at least once.\n */\n get hasSyncedAtLeastOnce(): boolean {\n return this.#context.controller.state\n .hasAccountTreeSyncingSyncedAtLeastOnce;\n }\n\n /**\n * Checks if backup and sync is enabled by checking UserStorageController state.\n *\n * @returns True if backup and sync + account syncing is enabled.\n */\n get isBackupAndSyncEnabled(): boolean {\n const userStorageControllerState = this.#context.messenger.call(\n 'UserStorageController:getState',\n );\n const { isAccountSyncingEnabled, isBackupAndSyncEnabled } =\n userStorageControllerState;\n\n return isBackupAndSyncEnabled && isAccountSyncingEnabled;\n }\n\n /**\n * Clears the atomic queue and resets ongoing operations.\n */\n clearState(): void {\n this.#atomicSyncQueue.clear();\n this.#ongoingFullSyncPromise = null;\n this.#firstOngoingFullSyncPromise = null;\n }\n\n /**\n * Handles changes to the user storage state.\n * Used to clear the backup and sync service state.\n *\n * @param state - The new user storage state.\n */\n handleUserStorageStateChange(\n state: UserStorageController.UserStorageControllerState,\n ): void {\n if (!state.isAccountSyncingEnabled || !state.isBackupAndSyncEnabled) {\n // If either syncing is disabled, clear the account tree state\n this.clearState();\n }\n }\n\n /**\n * Gets the entropy wallet associated with the given wallet ID.\n *\n * @param walletId - The wallet ID to look up.\n * @returns The associated entropy wallet, or undefined if not found.\n */\n #getEntropyWallet(\n walletId: AccountWalletId,\n ): AccountWalletEntropyObject | undefined {\n const wallet = this.#context.controller.state.accountTree.wallets[walletId];\n return wallet?.type === AccountWalletType.Entropy ? wallet : undefined;\n }\n\n /**\n * Sets up cleanup for ongoing sync promise tracking without affecting error propagation.\n *\n * @param promise - The promise to track and clean up\n * @returns The same promise (for chaining)\n */\n #setupOngoingPromiseCleanup(promise: Promise<void>): Promise<void> {\n this.#ongoingFullSyncPromise = promise;\n // Set up cleanup without affecting the returned promise\n promise\n .finally(() => {\n this.#ongoingFullSyncPromise = null;\n })\n .catch(() => {\n // Only ignore errors from the cleanup operation itself\n // The original promise errors are still propagated to callers\n });\n return promise;\n }\n\n /**\n * Enqueues a single wallet sync operation (fire-and-forget).\n * If the first full sync has not yet occurred, it does nothing.\n *\n * @param walletId - The wallet ID to sync.\n */\n enqueueSingleWalletSync(walletId: AccountWalletId): void {\n if (!this.isBackupAndSyncEnabled || !this.hasSyncedAtLeastOnce) {\n return;\n }\n\n // eslint-disable-next-line no-void\n void this.#atomicSyncQueue.enqueue(() =>\n this.#performSingleWalletSyncInner(walletId),\n );\n }\n\n /**\n * Enqueues a single group sync operation (fire-and-forget).\n * If the first full sync has not yet occurred, it does nothing.\n *\n * @param groupId - The group ID to sync.\n */\n enqueueSingleGroupSync(groupId: AccountGroupId): void {\n if (\n !this.isBackupAndSyncEnabled ||\n !this.hasSyncedAtLeastOnce ||\n // This prevents rate limiting scenarios where full syncs trigger group creations\n // that in turn enqueue the same single group syncs that the full sync just did.\n // This can very rarely lead to inconsistencies, but will be fixed on the next full sync.\n // TODO: let's improve this in the future by tracking the updates done in the full sync and\n // comparing against that.\n this.isInProgress\n ) {\n return;\n }\n\n // eslint-disable-next-line no-void\n void this.#atomicSyncQueue.enqueue(() =>\n this.#performSingleGroupSyncInner(groupId),\n );\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n * If a full sync is already in progress, it will return the ongoing promise.\n * This clears the atomic sync queue before starting the full sync.\n *\n * NOTE: in some very edge cases, this can be ran concurrently if triggered quickly after\n * toggling back and forth the backup and sync feature from the UI.\n *\n * @returns A promise that resolves when the sync is complete.\n */\n async performFullSync(): Promise<void> {\n if (!this.isBackupAndSyncEnabled) {\n return undefined;\n }\n\n // If there's an ongoing sync (including first sync), return it\n if (this.#ongoingFullSyncPromise) {\n return this.#ongoingFullSyncPromise;\n }\n\n // Create a new ongoing sync (sequential calls after previous completed)\n const newSyncPromise = this.#atomicSyncQueue.clearAndEnqueue(() =>\n this.#performFullSyncInner(),\n );\n\n // First sync setup - create and cache the first sync promise\n if (!this.#firstOngoingFullSyncPromise) {\n this.#firstOngoingFullSyncPromise = newSyncPromise;\n }\n\n return this.#setupOngoingPromiseCleanup(newSyncPromise);\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n *\n * If the first ever full sync is already in progress, it will return the ongoing promise.\n * If the first ever full sync has already completed, it will resolve and NOT start a new sync.\n *\n * This clears the atomic sync queue before starting the full sync.\n *\n * @returns A promise that resolves when the sync is complete.\n */\n async performFullSyncAtLeastOnce(): Promise<void> {\n if (!this.isBackupAndSyncEnabled) {\n return undefined;\n }\n\n if (!this.#firstOngoingFullSyncPromise) {\n this.#firstOngoingFullSyncPromise = this.#atomicSyncQueue.clearAndEnqueue(\n () => this.#performFullSyncInner(),\n );\n // eslint-disable-next-line no-void\n void this.#setupOngoingPromiseCleanup(this.#firstOngoingFullSyncPromise);\n }\n\n return this.#firstOngoingFullSyncPromise;\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n *\n * This method performs a comprehensive sync operation that:\n * 1. Identifies all local entropy wallets that can be synchronized\n * 2. Performs legacy account syncing if needed (for backwards compatibility)\n * - Disables subsequent legacy syncing by setting a flag in user storage\n * - Exits early if multichain account syncing is disabled after legacy sync\n * 3. Executes multichain account syncing for each wallet:\n * - Syncs wallet metadata bidirectionally\n * - Creates missing local groups from user storage data (or pushes local groups if none exist remotely)\n * - Refreshes local state to reflect newly created groups\n * - Syncs group metadata bidirectionally\n *\n * The sync is atomic per wallet with rollback on errors, but continues processing other wallets\n * if individual wallet sync fails. A global lock prevents concurrent sync operations.\n *\n * During this process, all other atomic multichain related user storage updates are blocked.\n *\n * @throws Will throw if the sync operation encounters unrecoverable errors\n */\n async #performFullSyncInner(): Promise<void> {\n // Prevent multiple syncs from running at the same time.\n // Also prevents atomic updates from being applied while syncing is in progress.\n if (this.isInProgress) {\n return;\n }\n\n // Set isAccountTreeSyncingInProgress immediately to prevent race conditions\n this.#context.controllerStateUpdateFn(\n (state: AccountTreeControllerState) => {\n state.isAccountTreeSyncingInProgress = true;\n },\n );\n\n // Encapsulate the sync logic in a function to allow tracing\n const bigSyncFn = async () => {\n try {\n // 1. Identifies all local entropy wallets that can be synchronized\n const localSyncableWallets = getLocalEntropyWallets(this.#context);\n\n if (!localSyncableWallets.length) {\n // No wallets to sync, just return. This shouldn't happen.\n return;\n }\n\n // 2. Iterate over each local wallet\n for (const wallet of localSyncableWallets) {\n const entropySourceId = wallet.metadata.entropy.id;\n\n let walletProfileId: ProfileId;\n let walletFromUserStorage: UserStorageSyncedWallet | null;\n let groupsFromUserStorage: UserStorageSyncedWalletGroup[];\n\n try {\n walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n\n [walletFromUserStorage, groupsFromUserStorage] = await Promise.all([\n getWalletFromUserStorage(this.#context, entropySourceId),\n getAllGroupsFromUserStorage(this.#context, entropySourceId),\n ]);\n\n // 2.1 Decide if we need to perform legacy account syncing\n if (\n !walletFromUserStorage ||\n !walletFromUserStorage.isLegacyAccountSyncingDisabled\n ) {\n // 2.2 Perform legacy account syncing\n // This will migrate legacy account data to the new structure.\n // This operation will only be performed once.\n await performLegacyAccountSyncing(\n this.#context,\n entropySourceId,\n walletProfileId,\n );\n }\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n const errorString = `Legacy syncing failed for wallet ${wallet.id}: ${errorMessage}`;\n\n backupAndSyncLogger(errorString);\n throw new Error(errorString);\n }\n\n // 3. Execute multichain account syncing\n let stateSnapshot: StateSnapshot | undefined;\n\n try {\n // 3.1 Wallet syncing\n // Create a state snapshot before processing each wallet for potential rollback\n stateSnapshot = createStateSnapshot(this.#context);\n\n // Sync wallet metadata bidirectionally\n await syncWalletMetadata(\n this.#context,\n wallet,\n walletFromUserStorage,\n walletProfileId,\n );\n\n // 3.2 Groups syncing\n // If groups data does not exist in user storage yet, create it\n if (!groupsFromUserStorage.length) {\n // If no groups exist in user storage, we can push all groups from the wallet to the user storage and exit\n await pushGroupToUserStorageBatch(\n this.#context,\n getLocalGroupsForEntropyWallet(this.#context, wallet.id),\n entropySourceId,\n );\n\n continue; // No need to proceed with metadata comparison if groups are new\n }\n\n // Create local groups for each group from user storage if they do not exist\n // This will ensure that we have all groups available locally before syncing metadata\n await createLocalGroupsFromUserStorage(\n this.#context,\n groupsFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n\n // Sync group metadata bidirectionally\n await syncGroupsMetadata(\n this.#context,\n wallet,\n groupsFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : String(error);\n const errorString = `Error during multichain account syncing for wallet ${wallet.id}: ${errorMessage}`;\n\n backupAndSyncLogger(errorString);\n\n // Attempt to rollback state changes for this wallet\n try {\n if (!stateSnapshot) {\n throw new Error(\n `State snapshot is missing for wallet ${wallet.id}`,\n );\n }\n restoreStateFromSnapshot(this.#context, stateSnapshot);\n backupAndSyncLogger(\n `Rolled back state changes for wallet ${wallet.id}`,\n );\n } catch (rollbackError) {\n backupAndSyncLogger(\n `Failed to rollback state for wallet ${wallet.id}:`,\n rollbackError instanceof Error\n ? rollbackError.message\n : String(rollbackError),\n );\n }\n\n // Continue with next wallet instead of failing the entire sync\n continue;\n }\n }\n } catch (error) {\n backupAndSyncLogger('Error during multichain account syncing:', error);\n throw error;\n }\n\n this.#context.controllerStateUpdateFn((state) => {\n state.hasAccountTreeSyncingSyncedAtLeastOnce = true;\n });\n };\n\n // Execute the big sync function with tracing and ensure state cleanup\n try {\n await this.#context.traceFn(\n {\n name: TraceName.AccountSyncFull,\n },\n bigSyncFn,\n );\n } finally {\n // Always reset state, regardless of success or failure\n this.#context.controllerStateUpdateFn(\n (state: AccountTreeControllerState) => {\n state.isAccountTreeSyncingInProgress = false;\n },\n );\n }\n }\n\n /**\n * Performs a single wallet's bidirectional metadata sync with user storage.\n *\n * @param walletId - The wallet ID to sync.\n */\n async #performSingleWalletSyncInner(\n walletId: AccountWalletId,\n ): Promise<void> {\n try {\n const wallet = this.#getEntropyWallet(walletId);\n if (!wallet) {\n return; // Only sync entropy wallets\n }\n\n const entropySourceId = wallet.metadata.entropy.id;\n const walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n const walletFromUserStorage = await getWalletFromUserStorage(\n this.#context,\n entropySourceId,\n );\n\n await syncWalletMetadata(\n this.#context,\n wallet,\n walletFromUserStorage,\n walletProfileId,\n );\n } catch (error) {\n backupAndSyncLogger(\n `Error in single wallet sync for ${walletId}:`,\n error,\n );\n throw error;\n }\n }\n\n /**\n * Performs a single group's bidirectional metadata sync with user storage.\n *\n * @param groupId - The group ID to sync.\n */\n async #performSingleGroupSyncInner(groupId: AccountGroupId): Promise<void> {\n try {\n const walletId = this.#context.groupIdToWalletId.get(groupId);\n if (!walletId) {\n return;\n }\n\n const wallet = this.#getEntropyWallet(walletId);\n if (!wallet) {\n return; // Only sync entropy wallets\n }\n\n const group = wallet.groups[groupId];\n if (!group) {\n return;\n }\n\n const entropySourceId = wallet.metadata.entropy.id;\n const walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n\n // Get the specific group from user storage\n const groupFromUserStorage = await getGroupFromUserStorage(\n this.#context,\n entropySourceId,\n group.metadata.entropy.groupIndex,\n );\n\n await syncGroupMetadata(\n this.#context,\n group,\n groupFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n } catch (error) {\n backupAndSyncLogger(`Error in single group sync for ${groupId}:`, error);\n throw error;\n }\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"group.cjs","sourceRoot":"","sources":["../../../src/backup-and-sync/syncing/group.ts"],"names":[],"mappings":";;;AAAA,6CAAoD;AAEpD,6CAAmD;AAGnD,sDAA2D;AAE3D,wCAIkB;AAClB,+EAG4C;AAC5C,8CAGkB;AAElB;;;;;;;;GAQG;AACI,MAAM,4BAA4B,GAAG,KAAK,EAC/C,OAA6B,EAC7B,eAAuB,EACvB,UAAkB,EAClB,SAAoB,EACpB,eAA6C,EAC7C,EAAE;IACF,IAAI;QACF,MAAM,oBAAoB,GAAG,IAAA,qCAA6B,EACxD,OAAO,EACP,eAAe,EACf,UAAU,CACX,CAAC;QAEF,+EAA+E;QAC/E,MAAM,OAAO,CAAC,SAAS,CAAC,IAAI,CAC1B,uDAAuD,EACvD;YACE,aAAa,EAAE,eAAe;YAC9B,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AmB,GAAG,IAAA,sCAA8B,EACxD,OAAO,EACP,MAAM,CAAC,EAAE,CACV,CAAC;IAEF,KAAK,MAAM,kBAAkB,IAAI,mBAAmB,EAAE;QACpD,MAAM,oBAAoB,GAAG,qBAAqB,CAAC,IAAI,CACrD,CAAC,KAAK,EAAE,EAAE,CACR,KAAK,CAAC,UAAU,KAAK,kBAAkB,CAAC,QAAQ,CAAC,OAAO,CAAC,UAAU,CACtE,CAAC;QAEF,MAAM,eAAe,GAAG,MAAM,qCAAqC,CACjE,OAAO,EACP,kBAAkB,EAClB,oBAAoB,EACpB,SAAS,CACV,CAAC;QAEF,uEAAuE;QACvE,IAAI,eAAe,EAAE;YACnB,0CAA0C,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC;SACrE;KACF;IAED,0DAA0D;IAC1D,IAAI,0CAA0C,CAAC,MAAM,GAAG,CAAC,EAAE;QACzD,MAAM,IAAA,gDAA2B,EAC/B,OAAO,EACP,0CAA0C,EAC1C,eAAe,CAChB,CAAC;KACH;AACH,CAAC;AA1CD,gDA0CC","sourcesContent":["import { compareAndSyncMetadata } from './metadata';\nimport type { AccountGroupMultichainAccountObject } from '../../group';\nimport { backupAndSyncLogger } from '../../logger';\nimport type { AccountWalletEntropyObject } from '../../wallet';\nimport type { BackupAndSyncAnalyticsAction } from '../analytics';\nimport { BackupAndSyncAnalyticsEvent } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport {\n UserStorageSyncedWalletGroupSchema,\n type BackupAndSyncContext,\n type UserStorageSyncedWalletGroup,\n} from '../types';\nimport {\n pushGroupToUserStorage,\n pushGroupToUserStorageBatch,\n} from '../user-storage/network-operations';\nimport {\n getLocalGroupForEntropyWallet,\n getLocalGroupsForEntropyWallet,\n} from '../utils';\n\n/**\n * Creates a multichain account group.\n *\n * @param context - The sync context containing controller and messenger.\n * @param entropySourceId - The entropy source ID.\n * @param groupIndex - The group index.\n * @param profileId - The profile ID for analytics.\n * @param analyticsAction - The analytics action to log.\n */\nexport const createMultichainAccountGroup = async (\n context: BackupAndSyncContext,\n entropySourceId: string,\n groupIndex: number,\n profileId: ProfileId,\n analyticsAction: BackupAndSyncAnalyticsAction,\n) => {\n try {\n const didGroupAlreadyExist = getLocalGroupForEntropyWallet(\n context,\n entropySourceId,\n groupIndex,\n );\n\n // This will be idempotent so we can create the group even if it already exists\n await context.messenger.call(\n 'MultichainAccountService:createMultichainAccountGroup',\n {\n entropySource: entropySourceId,\n groupIndex,\n },\n );\n\n if (!didGroupAlreadyExist) {\n context.emitAnalyticsEventFn({\n action: analyticsAction,\n profileId,\n });\n }\n } catch (error) {\n // This can happen if the Snap Keyring is not ready yet when invoking\n // `MultichainAccountService:createMultichainAccountGroup`.\n // Since `MultichainAccountService:createMultichainAccountGroup` will at\n // least create the EVM account and the account group before throwing, we can safely\n // ignore this error and swallow it.\n // Any missing Snap accounts will be added later with alignment.\n\n backupAndSyncLogger(\n `Failed to create group ${groupIndex} for entropy ${entropySourceId}:`,\n // istanbul ignore next\n error instanceof Error ? error.message : String(error),\n );\n }\n};\n\n/**\n * Creates local groups from user storage groups.\n *\n * @param context - The sync context containing controller and messenger.\n * @param groupsFromUserStorage - Array of groups from user storage.\n * @param entropySourceId - The entropy source ID.\n * @param profileId - The profile ID for analytics.\n */\nexport async function createLocalGroupsFromUserStorage(\n context: BackupAndSyncContext,\n groupsFromUserStorage: UserStorageSyncedWalletGroup[],\n entropySourceId: string,\n profileId: ProfileId,\n): Promise<void> {\n const numberOfAccountGroupsToCreate = Math.max(\n ...groupsFromUserStorage.map((g) => g.groupIndex),\n );\n\n // Creating multichain account group is idempotent, so we can safely\n // re-create every groups starting from 0.\n for (\n let groupIndex = 0;\n groupIndex <= numberOfAccountGroupsToCreate;\n groupIndex++\n ) {\n await createMultichainAccountGroup(\n context,\n entropySourceId,\n groupIndex,\n profileId,\n BackupAndSyncAnalyticsEvent.GroupAdded,\n );\n }\n}\n\n/**\n * Syncs group metadata fields and determines if the group needs to be pushed to user storage.\n *\n * @param context - The sync context containing controller and messenger.\n * @param localGroup - The local group to sync.\n * @param groupFromUserStorage - The group from user storage to compare against.\n * @param profileId - The profile ID for analytics.\n * @returns A promise that resolves to true if the group needs to be pushed to user storage.\n */\nasync function syncGroupMetadataAndCheckIfPushNeeded(\n context: BackupAndSyncContext,\n localGroup: AccountGroupMultichainAccountObject,\n groupFromUserStorage: UserStorageSyncedWalletGroup | null | undefined,\n profileId: ProfileId,\n): Promise<boolean> {\n const groupPersistedMetadata =\n context.controller.state.accountGroupsMetadata[localGroup.id];\n\n if (!groupFromUserStorage) {\n backupAndSyncLogger(\n `Group ${localGroup.id} did not exist in user storage, pushing to user storage...`,\n );\n\n return true;\n }\n\n // Track if we need to push this group to user storage\n let shouldPushGroup = false;\n\n // Compare and sync name metadata\n const shouldPushForName = await compareAndSyncMetadata({\n context,\n localMetadata: groupPersistedMetadata?.name,\n userStorageMetadata: groupFromUserStorage.name,\n validateUserStorageValue: (value) =>\n UserStorageSyncedWalletGroupSchema.schema.name.schema.value.is(value),\n applyLocalUpdate: (name: string) => {\n context.controller.setAccountGroupName(localGroup.id, name, true);\n },\n analytics: {\n action: BackupAndSyncAnalyticsEvent.GroupRenamed,\n profileId,\n },\n });\n\n shouldPushGroup ||= shouldPushForName;\n\n // Compare and sync pinned metadata\n const shouldPushForPinned = await compareAndSyncMetadata({\n context,\n localMetadata: groupPersistedMetadata?.pinned,\n userStorageMetadata: groupFromUserStorage.pinned,\n validateUserStorageValue: (value) =>\n UserStorageSyncedWalletGroupSchema.schema.pinned.schema.value.is(value),\n applyLocalUpdate: (pinned: boolean) => {\n context.controller.setAccountGroupPinned(localGroup.id, pinned);\n },\n analytics: {\n action: BackupAndSyncAnalyticsEvent.GroupPinnedStatusChanged,\n profileId,\n },\n });\n\n shouldPushGroup ||= shouldPushForPinned;\n\n // Compare and sync hidden metadata\n const shouldPushForHidden = await compareAndSyncMetadata({\n context,\n localMetadata: groupPersistedMetadata?.hidden,\n userStorageMetadata: groupFromUserStorage.hidden,\n validateUserStorageValue: (value) =>\n UserStorageSyncedWalletGroupSchema.schema.hidden.schema.value.is(value),\n applyLocalUpdate: (hidden: boolean) => {\n context.controller.setAccountGroupHidden(localGroup.id, hidden);\n },\n analytics: {\n action: BackupAndSyncAnalyticsEvent.GroupHiddenStatusChanged,\n profileId,\n },\n });\n\n shouldPushGroup ||= shouldPushForHidden;\n\n return shouldPushGroup;\n}\n\n/**\n * Syncs a single group's metadata between local and user storage.\n *\n * @param context - The sync context containing controller and messenger.\n * @param localGroup - The local group to sync.\n * @param groupFromUserStorage - The group from user storage to compare against (or null if it doesn't exist).\n * @param entropySourceId - The entropy source ID.\n * @param profileId - The profile ID for analytics.\n */\nexport async function syncGroupMetadata(\n context: BackupAndSyncContext,\n localGroup: AccountGroupMultichainAccountObject,\n groupFromUserStorage: UserStorageSyncedWalletGroup | null,\n entropySourceId: string,\n profileId: ProfileId,\n): Promise<void> {\n const shouldPushGroup = await syncGroupMetadataAndCheckIfPushNeeded(\n context,\n localGroup,\n groupFromUserStorage,\n profileId,\n );\n\n if (shouldPushGroup) {\n await pushGroupToUserStorage(context, localGroup, entropySourceId);\n }\n}\n\n/**\n * Syncs group metadata between local and user storage.\n *\n * @param context - The sync context containing controller and messenger.\n * @param wallet - The local wallet containing the groups.\n * @param groupsFromUserStorage - Array of groups from user storage.\n * @param entropySourceId - The entropy source ID.\n * @param profileId - The profile ID for analytics.\n */\nexport async function syncGroupsMetadata(\n context: BackupAndSyncContext,\n wallet: AccountWalletEntropyObject,\n groupsFromUserStorage: UserStorageSyncedWalletGroup[],\n entropySourceId: string,\n profileId: ProfileId,\n): Promise<void> {\n const localSyncableGroupsToBePushedToUserStorage: AccountGroupMultichainAccountObject[] =\n [];\n\n const localSyncableGroups = getLocalGroupsForEntropyWallet(\n context,\n wallet.id,\n );\n\n for (const localSyncableGroup of localSyncableGroups) {\n const groupFromUserStorage = groupsFromUserStorage.find(\n (group) =>\n group.groupIndex === localSyncableGroup.metadata.entropy.groupIndex,\n );\n\n const shouldPushGroup = await syncGroupMetadataAndCheckIfPushNeeded(\n context,\n localSyncableGroup,\n groupFromUserStorage,\n profileId,\n );\n\n // Add to push list if any metadata needs to be updated in user storage\n if (shouldPushGroup) {\n localSyncableGroupsToBePushedToUserStorage.push(localSyncableGroup);\n }\n }\n\n // Push all groups that need to be updated to user storage\n if (localSyncableGroupsToBePushedToUserStorage.length > 0) {\n await pushGroupToUserStorageBatch(\n context,\n localSyncableGroupsToBePushedToUserStorage,\n entropySourceId,\n );\n }\n}\n"]}
1
+ {"version":3,"file":"group.cjs","sourceRoot":"","sources":["../../../src/backup-and-sync/syncing/group.ts"],"names":[],"mappings":";;;AAAA,6CAAoD;AAEpD,6CAAmD;AAGnD,sDAA2D;AAE3D,wCAIkB;AAClB,+EAG4C;AAC5C,8CAGkB;AAElB;;;;;;;;GAQG;AACI,MAAM,4BAA4B,GAAG,KAAK,EAC/C,OAA6B,EAC7B,eAAuB,EACvB,UAAkB,EAClB,SAAoB,EACpB,eAA6C,EAC7C,EAAE;IACF,IAAI,CAAC;QACH,MAAM,oBAAoB,GAAG,IAAA,qCAA6B,EACxD,OAAO,EACP,eAAe,EACf,UAAU,CACX,CAAC;QAEF,+EAA+E;QAC/E,MAAM,OAAO,CAAC,SAAS,CAAC,IAAI,CAC1B,uDAAuD,EACvD;YACE,aAAa,EAAE,eAAe;YAC9B,UAAU;SACX,CACF,CAAC;QAEF,IAAI,CAAC,oBAAoB,EAAE,CAAC;YAC1B,OAAO,CAAC,oBAAoB,CAAC;gBAC3B,MAAM,EAAE,eAAe;gBACvB,SAAS;aACV,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,qEAAqE;QACrE,2DAA2D;QAC3D,wEAAwE;QACxE,oFAAoF;QACpF,oCAAoC;QACpC,gEAAgE;QAEhE,IAAA,4BAAmB,EACjB,0BAA0B,UAAU,gBAAgB,eAAe,GAAG;QACtE,uBAAuB;QACvB,KAAK,YA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,MAAM,qCAAqC,CACjE,OAAO,EACP,kBAAkB,EAClB,oBAAoB,EACpB,SAAS,CACV,CAAC;QAEF,uEAAuE;QACvE,IAAI,eAAe,EAAE,CAAC;YACpB,0CAA0C,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC;QACtE,CAAC;IACH,CAAC;IAED,0DAA0D;IAC1D,IAAI,0CAA0C,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC1D,MAAM,IAAA,gDAA2B,EAC/B,OAAO,EACP,0CAA0C,EAC1C,eAAe,CAChB,CAAC;IACJ,CAAC;AACH,CAAC;AA1CD,gDA0CC","sourcesContent":["import { compareAndSyncMetadata } from './metadata';\nimport type { AccountGroupMultichainAccountObject } from '../../group';\nimport { backupAndSyncLogger } from '../../logger';\nimport type { AccountWalletEntropyObject } from '../../wallet';\nimport type { BackupAndSyncAnalyticsAction } from '../analytics';\nimport { BackupAndSyncAnalyticsEvent } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport {\n UserStorageSyncedWalletGroupSchema,\n type BackupAndSyncContext,\n type UserStorageSyncedWalletGroup,\n} from '../types';\nimport {\n pushGroupToUserStorage,\n pushGroupToUserStorageBatch,\n} from '../user-storage/network-operations';\nimport {\n getLocalGroupForEntropyWallet,\n getLocalGroupsForEntropyWallet,\n} from '../utils';\n\n/**\n * Creates a multichain account group.\n *\n * @param context - The sync context containing controller and messenger.\n * @param entropySourceId - The entropy source ID.\n * @param groupIndex - The group index.\n * @param profileId - The profile ID for analytics.\n * @param analyticsAction - The analytics action to log.\n */\nexport const createMultichainAccountGroup = async (\n context: BackupAndSyncContext,\n entropySourceId: string,\n groupIndex: number,\n profileId: ProfileId,\n analyticsAction: BackupAndSyncAnalyticsAction,\n) => {\n try {\n const didGroupAlreadyExist = getLocalGroupForEntropyWallet(\n context,\n entropySourceId,\n groupIndex,\n );\n\n // This will be idempotent so we can create the group even if it already exists\n await context.messenger.call(\n 'MultichainAccountService:createMultichainAccountGroup',\n {\n entropySource: entropySourceId,\n groupIndex,\n },\n );\n\n if (!didGroupAlreadyExist) {\n context.emitAnalyticsEventFn({\n action: analyticsAction,\n profileId,\n });\n }\n } catch (error) {\n // This can happen if the Snap Keyring is not ready yet when invoking\n // `MultichainAccountService:createMultichainAccountGroup`.\n // Since `MultichainAccountService:createMultichainAccountGroup` will at\n // least create the EVM account and the account group before throwing, we can safely\n // ignore this error and swallow it.\n // Any missing Snap accounts will be added later with alignment.\n\n backupAndSyncLogger(\n `Failed to create group ${groupIndex} for entropy ${entropySourceId}:`,\n // istanbul ignore next\n error instanceof Error ? error.message : String(error),\n );\n }\n};\n\n/**\n * Creates local groups from user storage groups.\n *\n * @param context - The sync context containing controller and messenger.\n * @param groupsFromUserStorage - Array of groups from user storage.\n * @param entropySourceId - The entropy source ID.\n * @param profileId - The profile ID for analytics.\n */\nexport async function createLocalGroupsFromUserStorage(\n context: BackupAndSyncContext,\n groupsFromUserStorage: UserStorageSyncedWalletGroup[],\n entropySourceId: string,\n profileId: ProfileId,\n): Promise<void> {\n const numberOfAccountGroupsToCreate = Math.max(\n ...groupsFromUserStorage.map((g) => g.groupIndex),\n );\n\n // Creating multichain account group is idempotent, so we can safely\n // re-create every groups starting from 0.\n for (\n let groupIndex = 0;\n groupIndex <= numberOfAccountGroupsToCreate;\n groupIndex++\n ) {\n await createMultichainAccountGroup(\n context,\n entropySourceId,\n groupIndex,\n profileId,\n BackupAndSyncAnalyticsEvent.GroupAdded,\n );\n }\n}\n\n/**\n * Syncs group metadata fields and determines if the group needs to be pushed to user storage.\n *\n * @param context - The sync context containing controller and messenger.\n * @param localGroup - The local group to sync.\n * @param groupFromUserStorage - The group from user storage to compare against.\n * @param profileId - The profile ID for analytics.\n * @returns A promise that resolves to true if the group needs to be pushed to user storage.\n */\nasync function syncGroupMetadataAndCheckIfPushNeeded(\n context: BackupAndSyncContext,\n localGroup: AccountGroupMultichainAccountObject,\n groupFromUserStorage: UserStorageSyncedWalletGroup | null | undefined,\n profileId: ProfileId,\n): Promise<boolean> {\n const groupPersistedMetadata =\n context.controller.state.accountGroupsMetadata[localGroup.id];\n\n if (!groupFromUserStorage) {\n backupAndSyncLogger(\n `Group ${localGroup.id} did not exist in user storage, pushing to user storage...`,\n );\n\n return true;\n }\n\n // Track if we need to push this group to user storage\n let shouldPushGroup = false;\n\n // Compare and sync name metadata\n const shouldPushForName = await compareAndSyncMetadata({\n context,\n localMetadata: groupPersistedMetadata?.name,\n userStorageMetadata: groupFromUserStorage.name,\n validateUserStorageValue: (value) =>\n UserStorageSyncedWalletGroupSchema.schema.name.schema.value.is(value),\n applyLocalUpdate: (name: string) => {\n context.controller.setAccountGroupName(localGroup.id, name, true);\n },\n analytics: {\n action: BackupAndSyncAnalyticsEvent.GroupRenamed,\n profileId,\n },\n });\n\n shouldPushGroup ||= shouldPushForName;\n\n // Compare and sync pinned metadata\n const shouldPushForPinned = await compareAndSyncMetadata({\n context,\n localMetadata: groupPersistedMetadata?.pinned,\n userStorageMetadata: groupFromUserStorage.pinned,\n validateUserStorageValue: (value) =>\n UserStorageSyncedWalletGroupSchema.schema.pinned.schema.value.is(value),\n applyLocalUpdate: (pinned: boolean) => {\n context.controller.setAccountGroupPinned(localGroup.id, pinned);\n },\n analytics: {\n action: BackupAndSyncAnalyticsEvent.GroupPinnedStatusChanged,\n profileId,\n },\n });\n\n shouldPushGroup ||= shouldPushForPinned;\n\n // Compare and sync hidden metadata\n const shouldPushForHidden = await compareAndSyncMetadata({\n context,\n localMetadata: groupPersistedMetadata?.hidden,\n userStorageMetadata: groupFromUserStorage.hidden,\n validateUserStorageValue: (value) =>\n UserStorageSyncedWalletGroupSchema.schema.hidden.schema.value.is(value),\n applyLocalUpdate: (hidden: boolean) => {\n context.controller.setAccountGroupHidden(localGroup.id, hidden);\n },\n analytics: {\n action: BackupAndSyncAnalyticsEvent.GroupHiddenStatusChanged,\n profileId,\n },\n });\n\n shouldPushGroup ||= shouldPushForHidden;\n\n return shouldPushGroup;\n}\n\n/**\n * Syncs a single group's metadata between local and user storage.\n *\n * @param context - The sync context containing controller and messenger.\n * @param localGroup - The local group to sync.\n * @param groupFromUserStorage - The group from user storage to compare against (or null if it doesn't exist).\n * @param entropySourceId - The entropy source ID.\n * @param profileId - The profile ID for analytics.\n */\nexport async function syncGroupMetadata(\n context: BackupAndSyncContext,\n localGroup: AccountGroupMultichainAccountObject,\n groupFromUserStorage: UserStorageSyncedWalletGroup | null,\n entropySourceId: string,\n profileId: ProfileId,\n): Promise<void> {\n const shouldPushGroup = await syncGroupMetadataAndCheckIfPushNeeded(\n context,\n localGroup,\n groupFromUserStorage,\n profileId,\n );\n\n if (shouldPushGroup) {\n await pushGroupToUserStorage(context, localGroup, entropySourceId);\n }\n}\n\n/**\n * Syncs group metadata between local and user storage.\n *\n * @param context - The sync context containing controller and messenger.\n * @param wallet - The local wallet containing the groups.\n * @param groupsFromUserStorage - Array of groups from user storage.\n * @param entropySourceId - The entropy source ID.\n * @param profileId - The profile ID for analytics.\n */\nexport async function syncGroupsMetadata(\n context: BackupAndSyncContext,\n wallet: AccountWalletEntropyObject,\n groupsFromUserStorage: UserStorageSyncedWalletGroup[],\n entropySourceId: string,\n profileId: ProfileId,\n): Promise<void> {\n const localSyncableGroupsToBePushedToUserStorage: AccountGroupMultichainAccountObject[] =\n [];\n\n const localSyncableGroups = getLocalGroupsForEntropyWallet(\n context,\n wallet.id,\n );\n\n for (const localSyncableGroup of localSyncableGroups) {\n const groupFromUserStorage = groupsFromUserStorage.find(\n (group) =>\n group.groupIndex === localSyncableGroup.metadata.entropy.groupIndex,\n );\n\n const shouldPushGroup = await syncGroupMetadataAndCheckIfPushNeeded(\n context,\n localSyncableGroup,\n groupFromUserStorage,\n profileId,\n );\n\n // Add to push list if any metadata needs to be updated in user storage\n if (shouldPushGroup) {\n localSyncableGroupsToBePushedToUserStorage.push(localSyncableGroup);\n }\n }\n\n // Push all groups that need to be updated to user storage\n if (localSyncableGroupsToBePushedToUserStorage.length > 0) {\n await pushGroupToUserStorageBatch(\n context,\n localSyncableGroupsToBePushedToUserStorage,\n entropySourceId,\n );\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"group.mjs","sourceRoot":"","sources":["../../../src/backup-and-sync/syncing/group.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,uBAAmB;AAEpD,OAAO,EAAE,mBAAmB,EAAE,yBAAqB;AAGnD,OAAO,EAAE,2BAA2B,EAAE,+BAAqB;AAE3D,OAAO,EACL,kCAAkC,EAGnC,qBAAiB;AAClB,OAAO,EACL,sBAAsB,EACtB,2BAA2B,EAC5B,+CAA2C;AAC5C,OAAO,EACL,6BAA6B,EAC7B,8BAA8B,EAC/B,2BAAiB;AAElB;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,4BAA4B,GAAG,KAAK,EAC/C,OAA6B,EAC7B,eAAuB,EACvB,UAAkB,EAClB,SAAoB,EACpB,eAA6C,EAC7C,EAAE;IACF,IAAI;QACF,MAAM,oBAAoB,GAAG,6BAA6B,CACxD,OAAO,EACP,eAAe,EACf,UAAU,CACX,CAAC;QAEF,+EAA+E;QAC/E,MAAM,OAAO,CAAC,SAAS,CAAC,IAAI,CAC1B,uDAAuD,EACvD;YACE,aAAa,EAAE,eAAe;YAC9B,UAAU;SACX,CACF,CAAC;QAEF,IAAI,CAAC,oBAAoB,EAAE;YACzB,OAAO,CAAC,oBAAoB,CAAC;gBAC3B,MAAM,EAAE,eAAe;gBACvB,SAAS;aACV,CAAC,CAAC;SACJ;KACF;IAAC,OAAO,KAAK,EAAE;QACd,qEAAqE;QACrE,2DAA2D;QAC3D,wEAAwE;QACxE,oFAAoF;QACpF,oCAAoC;QACpC,gEAAgE;QAEhE,mBAAmB,CACjB,0BAA0B,UAAU,gBAAgB,eAAe,GAAG;QACtE,uBAAuB;QACvB,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CACvD,CAAC;KAC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AAM,GAAG,CAAC,EAAE;QACzD,MAAM,2BAA2B,CAC/B,OAAO,EACP,0CAA0C,EAC1C,eAAe,CAChB,CAAC;KACH;AACH,CAAC","sourcesContent":["import { compareAndSyncMetadata } from './metadata';\nimport type { AccountGroupMultichainAccountObject } from '../../group';\nimport { backupAndSyncLogger } from '../../logger';\nimport type { AccountWalletEntropyObject } from '../../wallet';\nimport type { BackupAndSyncAnalyticsAction } from '../analytics';\nimport { BackupAndSyncAnalyticsEvent } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport {\n UserStorageSyncedWalletGroupSchema,\n type BackupAndSyncContext,\n type UserStorageSyncedWalletGroup,\n} from '../types';\nimport {\n pushGroupToUserStorage,\n pushGroupToUserStorageBatch,\n} from '../user-storage/network-operations';\nimport {\n getLocalGroupForEntropyWallet,\n getLocalGroupsForEntropyWallet,\n} from '../utils';\n\n/**\n * Creates a multichain account group.\n *\n * @param context - The sync context containing controller and messenger.\n * @param entropySourceId - The entropy source ID.\n * @param groupIndex - The group index.\n * @param profileId - The profile ID for analytics.\n * @param analyticsAction - The analytics action to log.\n */\nexport const createMultichainAccountGroup = async (\n context: BackupAndSyncContext,\n entropySourceId: string,\n groupIndex: number,\n profileId: ProfileId,\n analyticsAction: BackupAndSyncAnalyticsAction,\n) => {\n try {\n const didGroupAlreadyExist = getLocalGroupForEntropyWallet(\n context,\n entropySourceId,\n groupIndex,\n );\n\n // This will be idempotent so we can create the group even if it already exists\n await context.messenger.call(\n 'MultichainAccountService:createMultichainAccountGroup',\n {\n entropySource: entropySourceId,\n groupIndex,\n },\n );\n\n if (!didGroupAlreadyExist) {\n context.emitAnalyticsEventFn({\n action: analyticsAction,\n profileId,\n });\n }\n } catch (error) {\n // This can happen if the Snap Keyring is not ready yet when invoking\n // `MultichainAccountService:createMultichainAccountGroup`.\n // Since `MultichainAccountService:createMultichainAccountGroup` will at\n // least create the EVM account and the account group before throwing, we can safely\n // ignore this error and swallow it.\n // Any missing Snap accounts will be added later with alignment.\n\n backupAndSyncLogger(\n `Failed to create group ${groupIndex} for entropy ${entropySourceId}:`,\n // istanbul ignore next\n error instanceof Error ? error.message : String(error),\n );\n }\n};\n\n/**\n * Creates local groups from user storage groups.\n *\n * @param context - The sync context containing controller and messenger.\n * @param groupsFromUserStorage - Array of groups from user storage.\n * @param entropySourceId - The entropy source ID.\n * @param profileId - The profile ID for analytics.\n */\nexport async function createLocalGroupsFromUserStorage(\n context: BackupAndSyncContext,\n groupsFromUserStorage: UserStorageSyncedWalletGroup[],\n entropySourceId: string,\n profileId: ProfileId,\n): Promise<void> {\n const numberOfAccountGroupsToCreate = Math.max(\n ...groupsFromUserStorage.map((g) => g.groupIndex),\n );\n\n // Creating multichain account group is idempotent, so we can safely\n // re-create every groups starting from 0.\n for (\n let groupIndex = 0;\n groupIndex <= numberOfAccountGroupsToCreate;\n groupIndex++\n ) {\n await createMultichainAccountGroup(\n context,\n entropySourceId,\n groupIndex,\n profileId,\n BackupAndSyncAnalyticsEvent.GroupAdded,\n );\n }\n}\n\n/**\n * Syncs group metadata fields and determines if the group needs to be pushed to user storage.\n *\n * @param context - The sync context containing controller and messenger.\n * @param localGroup - The local group to sync.\n * @param groupFromUserStorage - The group from user storage to compare against.\n * @param profileId - The profile ID for analytics.\n * @returns A promise that resolves to true if the group needs to be pushed to user storage.\n */\nasync function syncGroupMetadataAndCheckIfPushNeeded(\n context: BackupAndSyncContext,\n localGroup: AccountGroupMultichainAccountObject,\n groupFromUserStorage: UserStorageSyncedWalletGroup | null | undefined,\n profileId: ProfileId,\n): Promise<boolean> {\n const groupPersistedMetadata =\n context.controller.state.accountGroupsMetadata[localGroup.id];\n\n if (!groupFromUserStorage) {\n backupAndSyncLogger(\n `Group ${localGroup.id} did not exist in user storage, pushing to user storage...`,\n );\n\n return true;\n }\n\n // Track if we need to push this group to user storage\n let shouldPushGroup = false;\n\n // Compare and sync name metadata\n const shouldPushForName = await compareAndSyncMetadata({\n context,\n localMetadata: groupPersistedMetadata?.name,\n userStorageMetadata: groupFromUserStorage.name,\n validateUserStorageValue: (value) =>\n UserStorageSyncedWalletGroupSchema.schema.name.schema.value.is(value),\n applyLocalUpdate: (name: string) => {\n context.controller.setAccountGroupName(localGroup.id, name, true);\n },\n analytics: {\n action: BackupAndSyncAnalyticsEvent.GroupRenamed,\n profileId,\n },\n });\n\n shouldPushGroup ||= shouldPushForName;\n\n // Compare and sync pinned metadata\n const shouldPushForPinned = await compareAndSyncMetadata({\n context,\n localMetadata: groupPersistedMetadata?.pinned,\n userStorageMetadata: groupFromUserStorage.pinned,\n validateUserStorageValue: (value) =>\n UserStorageSyncedWalletGroupSchema.schema.pinned.schema.value.is(value),\n applyLocalUpdate: (pinned: boolean) => {\n context.controller.setAccountGroupPinned(localGroup.id, pinned);\n },\n analytics: {\n action: BackupAndSyncAnalyticsEvent.GroupPinnedStatusChanged,\n profileId,\n },\n });\n\n shouldPushGroup ||= shouldPushForPinned;\n\n // Compare and sync hidden metadata\n const shouldPushForHidden = await compareAndSyncMetadata({\n context,\n localMetadata: groupPersistedMetadata?.hidden,\n userStorageMetadata: groupFromUserStorage.hidden,\n validateUserStorageValue: (value) =>\n UserStorageSyncedWalletGroupSchema.schema.hidden.schema.value.is(value),\n applyLocalUpdate: (hidden: boolean) => {\n context.controller.setAccountGroupHidden(localGroup.id, hidden);\n },\n analytics: {\n action: BackupAndSyncAnalyticsEvent.GroupHiddenStatusChanged,\n profileId,\n },\n });\n\n shouldPushGroup ||= shouldPushForHidden;\n\n return shouldPushGroup;\n}\n\n/**\n * Syncs a single group's metadata between local and user storage.\n *\n * @param context - The sync context containing controller and messenger.\n * @param localGroup - The local group to sync.\n * @param groupFromUserStorage - The group from user storage to compare against (or null if it doesn't exist).\n * @param entropySourceId - The entropy source ID.\n * @param profileId - The profile ID for analytics.\n */\nexport async function syncGroupMetadata(\n context: BackupAndSyncContext,\n localGroup: AccountGroupMultichainAccountObject,\n groupFromUserStorage: UserStorageSyncedWalletGroup | null,\n entropySourceId: string,\n profileId: ProfileId,\n): Promise<void> {\n const shouldPushGroup = await syncGroupMetadataAndCheckIfPushNeeded(\n context,\n localGroup,\n groupFromUserStorage,\n profileId,\n );\n\n if (shouldPushGroup) {\n await pushGroupToUserStorage(context, localGroup, entropySourceId);\n }\n}\n\n/**\n * Syncs group metadata between local and user storage.\n *\n * @param context - The sync context containing controller and messenger.\n * @param wallet - The local wallet containing the groups.\n * @param groupsFromUserStorage - Array of groups from user storage.\n * @param entropySourceId - The entropy source ID.\n * @param profileId - The profile ID for analytics.\n */\nexport async function syncGroupsMetadata(\n context: BackupAndSyncContext,\n wallet: AccountWalletEntropyObject,\n groupsFromUserStorage: UserStorageSyncedWalletGroup[],\n entropySourceId: string,\n profileId: ProfileId,\n): Promise<void> {\n const localSyncableGroupsToBePushedToUserStorage: AccountGroupMultichainAccountObject[] =\n [];\n\n const localSyncableGroups = getLocalGroupsForEntropyWallet(\n context,\n wallet.id,\n );\n\n for (const localSyncableGroup of localSyncableGroups) {\n const groupFromUserStorage = groupsFromUserStorage.find(\n (group) =>\n group.groupIndex === localSyncableGroup.metadata.entropy.groupIndex,\n );\n\n const shouldPushGroup = await syncGroupMetadataAndCheckIfPushNeeded(\n context,\n localSyncableGroup,\n groupFromUserStorage,\n profileId,\n );\n\n // Add to push list if any metadata needs to be updated in user storage\n if (shouldPushGroup) {\n localSyncableGroupsToBePushedToUserStorage.push(localSyncableGroup);\n }\n }\n\n // Push all groups that need to be updated to user storage\n if (localSyncableGroupsToBePushedToUserStorage.length > 0) {\n await pushGroupToUserStorageBatch(\n context,\n localSyncableGroupsToBePushedToUserStorage,\n entropySourceId,\n );\n }\n}\n"]}
1
+ {"version":3,"file":"group.mjs","sourceRoot":"","sources":["../../../src/backup-and-sync/syncing/group.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,uBAAmB;AAEpD,OAAO,EAAE,mBAAmB,EAAE,yBAAqB;AAGnD,OAAO,EAAE,2BAA2B,EAAE,+BAAqB;AAE3D,OA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AAC,KAAK,UAAU,iBAAiB,CACrC,OAA6B,EAC7B,UAA+C,EAC/C,oBAAyD,EACzD,eAAuB,EACvB,SAAoB;IAEpB,MAAM,eAAe,GAAG,MAAM,qCAAqC,CACjE,OAAO,EACP,UAAU,EACV,oBAAoB,EACpB,SAAS,CACV,CAAC;IAEF,IAAI,eAAe,EAAE,CAAC;QACpB,MAAM,sBAAsB,CAAC,OAAO,EAAE,UAAU,EAAE,eAAe,CAAC,CAAC;IACrE,CAAC;AACH,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,OAA6B,EAC7B,MAAkC,EAClC,qBAAqD,EACrD,eAAuB,EACvB,SAAoB;IAEpB,MAAM,0CAA0C,GAC9C,EAAE,CAAC;IAEL,MAAM,mBAAmB,GAAG,8BAA8B,CACxD,OAAO,EACP,MAAM,CAAC,EAAE,CACV,CAAC;IAEF,KAAK,MAAM,kBAAkB,IAAI,mBAAmB,EAAE,CAAC;QACrD,MAAM,oBAAoB,GAAG,qBAAqB,CAAC,IAAI,CACrD,CAAC,KAAK,EAAE,EAAE,CACR,KAAK,CAAC,UAAU,KAAK,kBAAkB,CAAC,QAAQ,CAAC,OAAO,CAAC,UAAU,CACtE,CAAC;QAEF,MAAM,eAAe,GAAG,MAAM,qCAAqC,CACjE,OAAO,EACP,kBAAkB,EAClB,oBAAoB,EACpB,SAAS,CACV,CAAC;QAEF,uEAAuE;QACvE,IAAI,eAAe,EAAE,CAAC;YACpB,0CAA0C,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC;QACtE,CAAC;IACH,CAAC;IAED,0DAA0D;IAC1D,IAAI,0CAA0C,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC1D,MAAM,2BAA2B,CAC/B,OAAO,EACP,0CAA0C,EAC1C,eAAe,CAChB,CAAC;IACJ,CAAC;AACH,CAAC","sourcesContent":["import { compareAndSyncMetadata } from './metadata';\nimport type { AccountGroupMultichainAccountObject } from '../../group';\nimport { backupAndSyncLogger } from '../../logger';\nimport type { AccountWalletEntropyObject } from '../../wallet';\nimport type { BackupAndSyncAnalyticsAction } from '../analytics';\nimport { BackupAndSyncAnalyticsEvent } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport {\n UserStorageSyncedWalletGroupSchema,\n type BackupAndSyncContext,\n type UserStorageSyncedWalletGroup,\n} from '../types';\nimport {\n pushGroupToUserStorage,\n pushGroupToUserStorageBatch,\n} from '../user-storage/network-operations';\nimport {\n getLocalGroupForEntropyWallet,\n getLocalGroupsForEntropyWallet,\n} from '../utils';\n\n/**\n * Creates a multichain account group.\n *\n * @param context - The sync context containing controller and messenger.\n * @param entropySourceId - The entropy source ID.\n * @param groupIndex - The group index.\n * @param profileId - The profile ID for analytics.\n * @param analyticsAction - The analytics action to log.\n */\nexport const createMultichainAccountGroup = async (\n context: BackupAndSyncContext,\n entropySourceId: string,\n groupIndex: number,\n profileId: ProfileId,\n analyticsAction: BackupAndSyncAnalyticsAction,\n) => {\n try {\n const didGroupAlreadyExist = getLocalGroupForEntropyWallet(\n context,\n entropySourceId,\n groupIndex,\n );\n\n // This will be idempotent so we can create the group even if it already exists\n await context.messenger.call(\n 'MultichainAccountService:createMultichainAccountGroup',\n {\n entropySource: entropySourceId,\n groupIndex,\n },\n );\n\n if (!didGroupAlreadyExist) {\n context.emitAnalyticsEventFn({\n action: analyticsAction,\n profileId,\n });\n }\n } catch (error) {\n // This can happen if the Snap Keyring is not ready yet when invoking\n // `MultichainAccountService:createMultichainAccountGroup`.\n // Since `MultichainAccountService:createMultichainAccountGroup` will at\n // least create the EVM account and the account group before throwing, we can safely\n // ignore this error and swallow it.\n // Any missing Snap accounts will be added later with alignment.\n\n backupAndSyncLogger(\n `Failed to create group ${groupIndex} for entropy ${entropySourceId}:`,\n // istanbul ignore next\n error instanceof Error ? error.message : String(error),\n );\n }\n};\n\n/**\n * Creates local groups from user storage groups.\n *\n * @param context - The sync context containing controller and messenger.\n * @param groupsFromUserStorage - Array of groups from user storage.\n * @param entropySourceId - The entropy source ID.\n * @param profileId - The profile ID for analytics.\n */\nexport async function createLocalGroupsFromUserStorage(\n context: BackupAndSyncContext,\n groupsFromUserStorage: UserStorageSyncedWalletGroup[],\n entropySourceId: string,\n profileId: ProfileId,\n): Promise<void> {\n const numberOfAccountGroupsToCreate = Math.max(\n ...groupsFromUserStorage.map((g) => g.groupIndex),\n );\n\n // Creating multichain account group is idempotent, so we can safely\n // re-create every groups starting from 0.\n for (\n let groupIndex = 0;\n groupIndex <= numberOfAccountGroupsToCreate;\n groupIndex++\n ) {\n await createMultichainAccountGroup(\n context,\n entropySourceId,\n groupIndex,\n profileId,\n BackupAndSyncAnalyticsEvent.GroupAdded,\n );\n }\n}\n\n/**\n * Syncs group metadata fields and determines if the group needs to be pushed to user storage.\n *\n * @param context - The sync context containing controller and messenger.\n * @param localGroup - The local group to sync.\n * @param groupFromUserStorage - The group from user storage to compare against.\n * @param profileId - The profile ID for analytics.\n * @returns A promise that resolves to true if the group needs to be pushed to user storage.\n */\nasync function syncGroupMetadataAndCheckIfPushNeeded(\n context: BackupAndSyncContext,\n localGroup: AccountGroupMultichainAccountObject,\n groupFromUserStorage: UserStorageSyncedWalletGroup | null | undefined,\n profileId: ProfileId,\n): Promise<boolean> {\n const groupPersistedMetadata =\n context.controller.state.accountGroupsMetadata[localGroup.id];\n\n if (!groupFromUserStorage) {\n backupAndSyncLogger(\n `Group ${localGroup.id} did not exist in user storage, pushing to user storage...`,\n );\n\n return true;\n }\n\n // Track if we need to push this group to user storage\n let shouldPushGroup = false;\n\n // Compare and sync name metadata\n const shouldPushForName = await compareAndSyncMetadata({\n context,\n localMetadata: groupPersistedMetadata?.name,\n userStorageMetadata: groupFromUserStorage.name,\n validateUserStorageValue: (value) =>\n UserStorageSyncedWalletGroupSchema.schema.name.schema.value.is(value),\n applyLocalUpdate: (name: string) => {\n context.controller.setAccountGroupName(localGroup.id, name, true);\n },\n analytics: {\n action: BackupAndSyncAnalyticsEvent.GroupRenamed,\n profileId,\n },\n });\n\n shouldPushGroup ||= shouldPushForName;\n\n // Compare and sync pinned metadata\n const shouldPushForPinned = await compareAndSyncMetadata({\n context,\n localMetadata: groupPersistedMetadata?.pinned,\n userStorageMetadata: groupFromUserStorage.pinned,\n validateUserStorageValue: (value) =>\n UserStorageSyncedWalletGroupSchema.schema.pinned.schema.value.is(value),\n applyLocalUpdate: (pinned: boolean) => {\n context.controller.setAccountGroupPinned(localGroup.id, pinned);\n },\n analytics: {\n action: BackupAndSyncAnalyticsEvent.GroupPinnedStatusChanged,\n profileId,\n },\n });\n\n shouldPushGroup ||= shouldPushForPinned;\n\n // Compare and sync hidden metadata\n const shouldPushForHidden = await compareAndSyncMetadata({\n context,\n localMetadata: groupPersistedMetadata?.hidden,\n userStorageMetadata: groupFromUserStorage.hidden,\n validateUserStorageValue: (value) =>\n UserStorageSyncedWalletGroupSchema.schema.hidden.schema.value.is(value),\n applyLocalUpdate: (hidden: boolean) => {\n context.controller.setAccountGroupHidden(localGroup.id, hidden);\n },\n analytics: {\n action: BackupAndSyncAnalyticsEvent.GroupHiddenStatusChanged,\n profileId,\n },\n });\n\n shouldPushGroup ||= shouldPushForHidden;\n\n return shouldPushGroup;\n}\n\n/**\n * Syncs a single group's metadata between local and user storage.\n *\n * @param context - The sync context containing controller and messenger.\n * @param localGroup - The local group to sync.\n * @param groupFromUserStorage - The group from user storage to compare against (or null if it doesn't exist).\n * @param entropySourceId - The entropy source ID.\n * @param profileId - The profile ID for analytics.\n */\nexport async function syncGroupMetadata(\n context: BackupAndSyncContext,\n localGroup: AccountGroupMultichainAccountObject,\n groupFromUserStorage: UserStorageSyncedWalletGroup | null,\n entropySourceId: string,\n profileId: ProfileId,\n): Promise<void> {\n const shouldPushGroup = await syncGroupMetadataAndCheckIfPushNeeded(\n context,\n localGroup,\n groupFromUserStorage,\n profileId,\n );\n\n if (shouldPushGroup) {\n await pushGroupToUserStorage(context, localGroup, entropySourceId);\n }\n}\n\n/**\n * Syncs group metadata between local and user storage.\n *\n * @param context - The sync context containing controller and messenger.\n * @param wallet - The local wallet containing the groups.\n * @param groupsFromUserStorage - Array of groups from user storage.\n * @param entropySourceId - The entropy source ID.\n * @param profileId - The profile ID for analytics.\n */\nexport async function syncGroupsMetadata(\n context: BackupAndSyncContext,\n wallet: AccountWalletEntropyObject,\n groupsFromUserStorage: UserStorageSyncedWalletGroup[],\n entropySourceId: string,\n profileId: ProfileId,\n): Promise<void> {\n const localSyncableGroupsToBePushedToUserStorage: AccountGroupMultichainAccountObject[] =\n [];\n\n const localSyncableGroups = getLocalGroupsForEntropyWallet(\n context,\n wallet.id,\n );\n\n for (const localSyncableGroup of localSyncableGroups) {\n const groupFromUserStorage = groupsFromUserStorage.find(\n (group) =>\n group.groupIndex === localSyncableGroup.metadata.entropy.groupIndex,\n );\n\n const shouldPushGroup = await syncGroupMetadataAndCheckIfPushNeeded(\n context,\n localSyncableGroup,\n groupFromUserStorage,\n profileId,\n );\n\n // Add to push list if any metadata needs to be updated in user storage\n if (shouldPushGroup) {\n localSyncableGroupsToBePushedToUserStorage.push(localSyncableGroup);\n }\n }\n\n // Push all groups that need to be updated to user storage\n if (localSyncableGroupsToBePushedToUserStorage.length > 0) {\n await pushGroupToUserStorageBatch(\n context,\n localSyncableGroupsToBePushedToUserStorage,\n entropySourceId,\n );\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"legacy.cjs","sourceRoot":"","sources":["../../../src/backup-and-sync/syncing/legacy.ts"],"names":[],"mappings":";;;AAAA,uDAAoE;AACpE,uEAAkF;AAElF,uCAAuD;AACvD,6CAAmD;AACnD,sDAA2D;AAG3D,4DAAkE;AAClE,8CAA0D;AAE1D;;;;;;;;;;GAUG;AACI,MAAM,2BAA2B,GAAG,KAAK,EAC9C,OAA6B,EAC7B,eAAuB,EACvB,SAAoB,EACpB,EAAE;IACF,qCAAqC;IACrC,MAAM,6BAA6B,GAAG,MAAM,IAAA,8CAA+B,EACzE,OAAO,EACP,e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B,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CACnD,KAAK,CAAC,QAAQ,CAAC,QAAQ,CAAC,cAAc,CAAC,CACxC,CAAC;YACF,IAAI,UAAU,EAAE;gBACd,OAAO,CAAC,UAAU,CAAC,mBAAmB,CAAC,UAAU,CAAC,EAAE,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC;gBAE/D,OAAO,CAAC,oBAAoB,CAAC;oBAC3B,MAAM,EAAE,uCAA2B,CAAC,kBAAkB;oBACtD,SAAS;oBACT,qBAAqB,EAAE,wBAAwB,UAAU,CAAC,EAAE,OAAO,CAAC,EAAE;iBACvE,CAAC,CAAC;aACJ;SACF;KACF;IAED,OAAO,CAAC,oBAAoB,CAAC;QAC3B,MAAM,EAAE,uCAA2B,CAAC,iBAAiB;QACrD,SAAS;KACV,CAAC,CAAC;AACL,CAAC,CAAC;AAnFW,QAAA,2BAA2B,+BAmFtC","sourcesContent":["import { toMultichainAccountWalletId } from '@metamask/account-api';\nimport { getUUIDFromAddressOfNormalAccount } from '@metamask/accounts-controller';\n\nimport { createMultichainAccountGroup } from './group';\nimport { backupAndSyncLogger } from '../../logger';\nimport { BackupAndSyncAnalyticsEvent } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport type { BackupAndSyncContext } from '../types';\nimport { getAllLegacyUserStorageAccounts } from '../user-storage';\nimport { getLocalGroupsForEntropyWallet } from '../utils';\n\n/**\n * Performs a stripped down version of legacy account syncing, replacing the current\n * UserStorageController:syncInternalAccountsWithUserStorage call.\n * This ensures legacy (V1) account syncing data is correctly migrated to\n * the new AccountTreeController data structure. It should only happen\n * once per wallet.\n *\n * @param context - The sync context containing controller and messenger.\n * @param entropySourceId - The entropy source ID.\n * @param profileId - The profile ID for analytics.\n */\nexport const performLegacyAccountSyncing = async (\n context: BackupAndSyncContext,\n entropySourceId: string,\n profileId: ProfileId,\n) => {\n // 1. Get legacy account syncing data\n const legacyAccountsFromUserStorage = await getAllLegacyUserStorageAccounts(\n context,\n entropySourceId,\n );\n if (legacyAccountsFromUserStorage.length === 0) {\n backupAndSyncLogger('No legacy accounts, skipping legacy account syncing');\n\n context.emitAnalyticsEventFn({\n action: BackupAndSyncAnalyticsEvent.LegacySyncingDone,\n profileId,\n });\n\n return;\n }\n\n // 2. Create account groups accordingly\n const numberOfAccountGroupsToCreate = legacyAccountsFromUserStorage.length;\n\n backupAndSyncLogger(\n `Creating ${numberOfAccountGroupsToCreate} account groups for legacy accounts`,\n );\n\n if (numberOfAccountGroupsToCreate > 0) {\n // Creating multichain account group is idempotent, so we can safely\n // re-create every groups starting from 0.\n for (let i = 0; i < numberOfAccountGroupsToCreate; i++) {\n backupAndSyncLogger(`Creating account group ${i} for legacy account`);\n await createMultichainAccountGroup(\n context,\n entropySourceId,\n i,\n profileId,\n BackupAndSyncAnalyticsEvent.LegacyGroupAddedFromAccount,\n );\n }\n }\n\n // 3. Rename account groups if needed\n const localAccountGroups = getLocalGroupsForEntropyWallet(\n context,\n toMultichainAccountWalletId(entropySourceId),\n );\n for (const legacyAccount of legacyAccountsFromUserStorage) {\n // n: name\n // a: EVM address\n const { n, a } = legacyAccount;\n if (!a || !n) {\n backupAndSyncLogger(\n `Legacy account data is missing name or address, skipping account: ${JSON.stringify(\n legacyAccount,\n )}`,\n );\n continue;\n }\n\n if (n) {\n // Find the local group that corresponds to this EVM address\n const localAccountId = getUUIDFromAddressOfNormalAccount(a);\n const localGroup = localAccountGroups.find((group) =>\n group.accounts.includes(localAccountId),\n );\n if (localGroup) {\n context.controller.setAccountGroupName(localGroup.id, n, true);\n\n context.emitAnalyticsEventFn({\n action: BackupAndSyncAnalyticsEvent.LegacyGroupRenamed,\n profileId,\n additionalDescription: `Renamed legacy group ${localGroup.id} to ${n}`,\n });\n }\n }\n }\n\n context.emitAnalyticsEventFn({\n action: BackupAndSyncAnalyticsEvent.LegacySyncingDone,\n profileId,\n });\n};\n"]}
1
+ {"version":3,"file":"legacy.cjs","sourceRoot":"","sources":["../../../src/backup-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QAC1D,UAAU;QACV,iBAAiB;QACjB,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,GAAG,aAAa,CAAC;QAC/B,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;YACb,IAAA,4BAAmB,EACjB,qEAAqE,IAAI,CAAC,SAAS,CACjF,aAAa,CACd,EAAE,CACJ,CAAC;YACF,SAAS;QACX,CAAC;QAED,IAAI,CAAC,EAAE,CAAC;YACN,4DAA4D;YAC5D,MAAM,cAAc,GAAG,IAAA,uDAAiC,EAAC,CAAC,CAAC,CAAC;YAC5D,MAAM,UAAU,GAAG,kBAAkB,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CACnD,KAAK,CAAC,QAAQ,CAAC,QAAQ,CAAC,cAAc,CAAC,CACxC,CAAC;YACF,IAAI,UAAU,EAAE,CAAC;gBACf,OAAO,CAAC,UAAU,CAAC,mBAAmB,CAAC,UAAU,CAAC,EAAE,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC;gBAE/D,OAAO,CAAC,oBAAoB,CAAC;oBAC3B,MAAM,EAAE,uCAA2B,CAAC,kBAAkB;oBACtD,SAAS;oBACT,qBAAqB,EAAE,wBAAwB,UAAU,CAAC,EAAE,OAAO,CAAC,EAAE;iBACvE,CAAC,CAAC;YACL,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,CAAC,oBAAoB,CAAC;QAC3B,MAAM,EAAE,uCAA2B,CAAC,iBAAiB;QACrD,SAAS;KACV,CAAC,CAAC;AACL,CAAC,CAAC;AAnFW,QAAA,2BAA2B,+BAmFtC","sourcesContent":["import { toMultichainAccountWalletId } from '@metamask/account-api';\nimport { getUUIDFromAddressOfNormalAccount } from '@metamask/accounts-controller';\n\nimport { createMultichainAccountGroup } from './group';\nimport { backupAndSyncLogger } from '../../logger';\nimport { BackupAndSyncAnalyticsEvent } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport type { BackupAndSyncContext } from '../types';\nimport { getAllLegacyUserStorageAccounts } from '../user-storage';\nimport { getLocalGroupsForEntropyWallet } from '../utils';\n\n/**\n * Performs a stripped down version of legacy account syncing, replacing the current\n * UserStorageController:syncInternalAccountsWithUserStorage call.\n * This ensures legacy (V1) account syncing data is correctly migrated to\n * the new AccountTreeController data structure. It should only happen\n * once per wallet.\n *\n * @param context - The sync context containing controller and messenger.\n * @param entropySourceId - The entropy source ID.\n * @param profileId - The profile ID for analytics.\n */\nexport const performLegacyAccountSyncing = async (\n context: BackupAndSyncContext,\n entropySourceId: string,\n profileId: ProfileId,\n) => {\n // 1. Get legacy account syncing data\n const legacyAccountsFromUserStorage = await getAllLegacyUserStorageAccounts(\n context,\n entropySourceId,\n );\n if (legacyAccountsFromUserStorage.length === 0) {\n backupAndSyncLogger('No legacy accounts, skipping legacy account syncing');\n\n context.emitAnalyticsEventFn({\n action: BackupAndSyncAnalyticsEvent.LegacySyncingDone,\n profileId,\n });\n\n return;\n }\n\n // 2. Create account groups accordingly\n const numberOfAccountGroupsToCreate = legacyAccountsFromUserStorage.length;\n\n backupAndSyncLogger(\n `Creating ${numberOfAccountGroupsToCreate} account groups for legacy accounts`,\n );\n\n if (numberOfAccountGroupsToCreate > 0) {\n // Creating multichain account group is idempotent, so we can safely\n // re-create every groups starting from 0.\n for (let i = 0; i < numberOfAccountGroupsToCreate; i++) {\n backupAndSyncLogger(`Creating account group ${i} for legacy account`);\n await createMultichainAccountGroup(\n context,\n entropySourceId,\n i,\n profileId,\n BackupAndSyncAnalyticsEvent.LegacyGroupAddedFromAccount,\n );\n }\n }\n\n // 3. Rename account groups if needed\n const localAccountGroups = getLocalGroupsForEntropyWallet(\n context,\n toMultichainAccountWalletId(entropySourceId),\n );\n for (const legacyAccount of legacyAccountsFromUserStorage) {\n // n: name\n // a: EVM address\n const { n, a } = legacyAccount;\n if (!a || !n) {\n backupAndSyncLogger(\n `Legacy account data is missing name or address, skipping account: ${JSON.stringify(\n legacyAccount,\n )}`,\n );\n continue;\n }\n\n if (n) {\n // Find the local group that corresponds to this EVM address\n const localAccountId = getUUIDFromAddressOfNormalAccount(a);\n const localGroup = localAccountGroups.find((group) =>\n group.accounts.includes(localAccountId),\n );\n if (localGroup) {\n context.controller.setAccountGroupName(localGroup.id, n, true);\n\n context.emitAnalyticsEventFn({\n action: BackupAndSyncAnalyticsEvent.LegacyGroupRenamed,\n profileId,\n additionalDescription: `Renamed legacy group ${localGroup.id} to ${n}`,\n });\n }\n }\n }\n\n context.emitAnalyticsEventFn({\n action: BackupAndSyncAnalyticsEvent.LegacySyncingDone,\n profileId,\n });\n};\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"legacy.mjs","sourceRoot":"","sources":["../../../src/backup-and-sync/syncing/legacy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,8BAA8B;AACpE,OAAO,EAAE,iCAAiC,EAAE,sCAAsC;AAElF,OAAO,EAAE,4BAA4B,EAAE,oBAAgB;AACvD,OAAO,EAAE,mBAAmB,EAAE,yBAAqB;AACnD,OAAO,EAAE,2BAA2B,EAAE,+BAAqB;AAG3D,OAAO,EAAE,+BAA+B,EAAE,kCAAwB;AAClE,OAAO,EAAE,8BAA8B,EAAE,2BAAiB;AAE1D;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG,KAAK,EAC9C,OAA6B,EAC7B,eAAuB,EACvB,SAAoB,EACpB,EAAE;IACF,qCAAqC;IACrC,MAAM,6BAA6B,GAAG,MAAM,+BAA+B,CACzE,OAAO,EACP,eAAe,CAChB,CAAC;IACF,IAAI,6BAA6B,CAAC,MAAM,KAAK,CAAC,EAAE;QAC9C,mBAAmB,CAAC,qDAAqD,CAAC,CAAC;QAE3E,OAAO,CAAC,oBAAoB,CAAC;YAC3B,MAAM,EAAE,2BAA2B,CAAC,iBAAiB;YACrD,SAAS;SACV,CAAC,CAAC;QAEH,OAAO;KACR;IAED,uCAAuC;IACvC,MAAM,6BAA6B,GAAG,6BAA6B,CAAC,MAAM,CAA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AAE,wBAAwB,UAAU,CAAC,EAAE,OAAO,CAAC,EAAE;iBACvE,CAAC,CAAC;aACJ;SACF;KACF;IAED,OAAO,CAAC,oBAAoB,CAAC;QAC3B,MAAM,EAAE,2BAA2B,CAAC,iBAAiB;QACrD,SAAS;KACV,CAAC,CAAC;AACL,CAAC,CAAC","sourcesContent":["import { toMultichainAccountWalletId } from '@metamask/account-api';\nimport { getUUIDFromAddressOfNormalAccount } from '@metamask/accounts-controller';\n\nimport { createMultichainAccountGroup } from './group';\nimport { backupAndSyncLogger } from '../../logger';\nimport { BackupAndSyncAnalyticsEvent } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport type { BackupAndSyncContext } from '../types';\nimport { getAllLegacyUserStorageAccounts } from '../user-storage';\nimport { getLocalGroupsForEntropyWallet } from '../utils';\n\n/**\n * Performs a stripped down version of legacy account syncing, replacing the current\n * UserStorageController:syncInternalAccountsWithUserStorage call.\n * This ensures legacy (V1) account syncing data is correctly migrated to\n * the new AccountTreeController data structure. It should only happen\n * once per wallet.\n *\n * @param context - The sync context containing controller and messenger.\n * @param entropySourceId - The entropy source ID.\n * @param profileId - The profile ID for analytics.\n */\nexport const performLegacyAccountSyncing = async (\n context: BackupAndSyncContext,\n entropySourceId: string,\n profileId: ProfileId,\n) => {\n // 1. Get legacy account syncing data\n const legacyAccountsFromUserStorage = await getAllLegacyUserStorageAccounts(\n context,\n entropySourceId,\n );\n if (legacyAccountsFromUserStorage.length === 0) {\n backupAndSyncLogger('No legacy accounts, skipping legacy account syncing');\n\n context.emitAnalyticsEventFn({\n action: BackupAndSyncAnalyticsEvent.LegacySyncingDone,\n profileId,\n });\n\n return;\n }\n\n // 2. Create account groups accordingly\n const numberOfAccountGroupsToCreate = legacyAccountsFromUserStorage.length;\n\n backupAndSyncLogger(\n `Creating ${numberOfAccountGroupsToCreate} account groups for legacy accounts`,\n );\n\n if (numberOfAccountGroupsToCreate > 0) {\n // Creating multichain account group is idempotent, so we can safely\n // re-create every groups starting from 0.\n for (let i = 0; i < numberOfAccountGroupsToCreate; i++) {\n backupAndSyncLogger(`Creating account group ${i} for legacy account`);\n await createMultichainAccountGroup(\n context,\n entropySourceId,\n i,\n profileId,\n BackupAndSyncAnalyticsEvent.LegacyGroupAddedFromAccount,\n );\n }\n }\n\n // 3. Rename account groups if needed\n const localAccountGroups = getLocalGroupsForEntropyWallet(\n context,\n toMultichainAccountWalletId(entropySourceId),\n );\n for (const legacyAccount of legacyAccountsFromUserStorage) {\n // n: name\n // a: EVM address\n const { n, a } = legacyAccount;\n if (!a || !n) {\n backupAndSyncLogger(\n `Legacy account data is missing name or address, skipping account: ${JSON.stringify(\n legacyAccount,\n )}`,\n );\n continue;\n }\n\n if (n) {\n // Find the local group that corresponds to this EVM address\n const localAccountId = getUUIDFromAddressOfNormalAccount(a);\n const localGroup = localAccountGroups.find((group) =>\n group.accounts.includes(localAccountId),\n );\n if (localGroup) {\n context.controller.setAccountGroupName(localGroup.id, n, true);\n\n context.emitAnalyticsEventFn({\n action: BackupAndSyncAnalyticsEvent.LegacyGroupRenamed,\n profileId,\n additionalDescription: `Renamed legacy group ${localGroup.id} to ${n}`,\n });\n }\n }\n }\n\n context.emitAnalyticsEventFn({\n action: BackupAndSyncAnalyticsEvent.LegacySyncingDone,\n profileId,\n });\n};\n"]}
1
+ {"version":3,"file":"legacy.mjs","sourceRoot":"","sources":["../../../src/backup-and-sync/syncing/legacy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,8BAA8B;AACpE,OAAO,EAAE,iCAAiC,EAAE,sCAAsC;AAElF,OAAO,EAAE,4BAA4B,EAAE,oBAAgB;AACvD,OAAO,EAAE,mBAAmB,EAAE,yBAAqB;AACnD,OAAO,EAAE,2BAA2B,EAAE,+BAAqB;AAG3D,OAAO,EAAE,+BAA+B,EAAE,kCAAwB;AAClE,OAAO,EAAE,8BAA8B,EAAE,2BAAiB;AAE1D;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,2BAA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C;YACF,SAAS;QACX,CAAC;QAED,IAAI,CAAC,EAAE,CAAC;YACN,4DAA4D;YAC5D,MAAM,cAAc,GAAG,iCAAiC,CAAC,CAAC,CAAC,CAAC;YAC5D,MAAM,UAAU,GAAG,kBAAkB,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CACnD,KAAK,CAAC,QAAQ,CAAC,QAAQ,CAAC,cAAc,CAAC,CACxC,CAAC;YACF,IAAI,UAAU,EAAE,CAAC;gBACf,OAAO,CAAC,UAAU,CAAC,mBAAmB,CAAC,UAAU,CAAC,EAAE,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC;gBAE/D,OAAO,CAAC,oBAAoB,CAAC;oBAC3B,MAAM,EAAE,2BAA2B,CAAC,kBAAkB;oBACtD,SAAS;oBACT,qBAAqB,EAAE,wBAAwB,UAAU,CAAC,EAAE,OAAO,CAAC,EAAE;iBACvE,CAAC,CAAC;YACL,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,CAAC,oBAAoB,CAAC;QAC3B,MAAM,EAAE,2BAA2B,CAAC,iBAAiB;QACrD,SAAS;KACV,CAAC,CAAC;AACL,CAAC,CAAC","sourcesContent":["import { toMultichainAccountWalletId } from '@metamask/account-api';\nimport { getUUIDFromAddressOfNormalAccount } from '@metamask/accounts-controller';\n\nimport { createMultichainAccountGroup } from './group';\nimport { backupAndSyncLogger } from '../../logger';\nimport { BackupAndSyncAnalyticsEvent } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport type { BackupAndSyncContext } from '../types';\nimport { getAllLegacyUserStorageAccounts } from '../user-storage';\nimport { getLocalGroupsForEntropyWallet } from '../utils';\n\n/**\n * Performs a stripped down version of legacy account syncing, replacing the current\n * UserStorageController:syncInternalAccountsWithUserStorage call.\n * This ensures legacy (V1) account syncing data is correctly migrated to\n * the new AccountTreeController data structure. It should only happen\n * once per wallet.\n *\n * @param context - The sync context containing controller and messenger.\n * @param entropySourceId - The entropy source ID.\n * @param profileId - The profile ID for analytics.\n */\nexport const performLegacyAccountSyncing = async (\n context: BackupAndSyncContext,\n entropySourceId: string,\n profileId: ProfileId,\n) => {\n // 1. Get legacy account syncing data\n const legacyAccountsFromUserStorage = await getAllLegacyUserStorageAccounts(\n context,\n entropySourceId,\n );\n if (legacyAccountsFromUserStorage.length === 0) {\n backupAndSyncLogger('No legacy accounts, skipping legacy account syncing');\n\n context.emitAnalyticsEventFn({\n action: BackupAndSyncAnalyticsEvent.LegacySyncingDone,\n profileId,\n });\n\n return;\n }\n\n // 2. Create account groups accordingly\n const numberOfAccountGroupsToCreate = legacyAccountsFromUserStorage.length;\n\n backupAndSyncLogger(\n `Creating ${numberOfAccountGroupsToCreate} account groups for legacy accounts`,\n );\n\n if (numberOfAccountGroupsToCreate > 0) {\n // Creating multichain account group is idempotent, so we can safely\n // re-create every groups starting from 0.\n for (let i = 0; i < numberOfAccountGroupsToCreate; i++) {\n backupAndSyncLogger(`Creating account group ${i} for legacy account`);\n await createMultichainAccountGroup(\n context,\n entropySourceId,\n i,\n profileId,\n BackupAndSyncAnalyticsEvent.LegacyGroupAddedFromAccount,\n );\n }\n }\n\n // 3. Rename account groups if needed\n const localAccountGroups = getLocalGroupsForEntropyWallet(\n context,\n toMultichainAccountWalletId(entropySourceId),\n );\n for (const legacyAccount of legacyAccountsFromUserStorage) {\n // n: name\n // a: EVM address\n const { n, a } = legacyAccount;\n if (!a || !n) {\n backupAndSyncLogger(\n `Legacy account data is missing name or address, skipping account: ${JSON.stringify(\n legacyAccount,\n )}`,\n );\n continue;\n }\n\n if (n) {\n // Find the local group that corresponds to this EVM address\n const localAccountId = getUUIDFromAddressOfNormalAccount(a);\n const localGroup = localAccountGroups.find((group) =>\n group.accounts.includes(localAccountId),\n );\n if (localGroup) {\n context.controller.setAccountGroupName(localGroup.id, n, true);\n\n context.emitAnalyticsEventFn({\n action: BackupAndSyncAnalyticsEvent.LegacyGroupRenamed,\n profileId,\n additionalDescription: `Renamed legacy group ${localGroup.id} to ${n}`,\n });\n }\n }\n }\n\n context.emitAnalyticsEventFn({\n action: BackupAndSyncAnalyticsEvent.LegacySyncingDone,\n profileId,\n });\n};\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"metadata.cjs","sourceRoot":"","sources":["../../../src/backup-and-sync/syncing/metadata.ts"],"names":[],"mappings":";;;;;;AAAA,sEAAwC;AAMxC;;;;;;;;;;;;;;;;;GAiBG;AACI,KAAK,UAAU,sBAAsB,CAAI,EAC9C,OAAO,EACP,aAAa,EACb,mBAAmB,EACnB,gBAAgB,EAChB,wBAAwB,EACxB,SAAS,GAWV;IACC,MAAM,UAAU,GAAG,aAAa,EAAE,KAAK,CAAC;IACxC,MAAM,cAAc,GAAG,aAAa,EAAE,aAAa,CAAC;IACpD,MAAM,gBAAgB,GAAG,mBAAmB,EAAE,KAAK,CAAC;IACpD,MAAM,oBAAoB,GAAG,mBAAmB,EAAE,aAAa,CAAC;IAEhE,MAAM,WAAW,GAAG,IAAA,yBAAS,EAAC,UAAU,EAAE,gBAAgB,CAAC,CAAC;IAE5D,IAAI,WAAW,EAAE;QACf,OAAO,KAAK,CAAC,CAAC,sCAAsC;KACrD;IAED,MAAM,uBAAuB,GAC3B,cAAc,KAAK,SAAS;QAC5B,oBAAoB,KAAK,SAAS;QAClC,cAAc,GAAG,oBAAoB,CAAC;IAExC,2DAA2D;IAC3D,MAAM,uBAAuB,GAAG,wBAAwB,CAAC,gBAAgB,CAAC,CAAC;IAE3E,IAAI,CAAC,uBAAuB,IAAI,CAAC,aAAa,CAAC,IAAI,uBAAuB,EAAE;QAC1E,0DAA0D;QAC1D,MAAM,gBAAgB,CAAC,gBAAqB,CAAC,CAAC;QAE9C,mCAAmC;QACnC,IAAI,SAAS,EAAE;YACb,OAAO,CAAC,oBAAoB,CAAC;gBAC3B,MAAM,EAAE,SAAS,CAAC,MAAM;gBACxB,SAAS,EAAE,SAAS,CAAC,SAAS;aAC/B,CAAC,CAAC;SACJ;QAED,OAAO,KAAK,CAAC,CAAC,0DAA0D;KACzE;IAED,OAAO,IAAI,CAAC,CAAC,+EAA+E;AAC9F,CAAC;AArDD,wDAqDC","sourcesContent":["import deepEqual from 'fast-deep-equal';\n\nimport type { BackupAndSyncAnalyticsAction } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport type { BackupAndSyncContext } from '../types';\n\n/**\n * Compares metadata between local and user storage, applying the most recent version.\n *\n * @param options - Configuration object for metadata comparison.\n * @param options.context - The backup and sync context containing controller and messenger.\n * @param options.localMetadata - The local metadata object.\n * @param options.localMetadata.value - The local metadata value.\n * @param options.localMetadata.lastUpdatedAt - The local metadata timestamp.\n * @param options.userStorageMetadata - The user storage metadata object.\n * @param options.userStorageMetadata.value - The user storage metadata value.\n * @param options.userStorageMetadata.lastUpdatedAt - The user storage metadata timestamp.\n * @param options.applyLocalUpdate - Function to apply the user storage value locally.\n * @param options.validateUserStorageValue - Function to validate user storage data.\n * @param options.analytics - Optional analytics configuration for tracking updates.\n * @param options.analytics.action - The analytics action for the event.\n * @param options.analytics.profileId - The profile ID for analytics.\n * @returns Promise resolving to true if local data should be pushed to user storage.\n */\nexport async function compareAndSyncMetadata<T>({\n context,\n localMetadata,\n userStorageMetadata,\n applyLocalUpdate,\n validateUserStorageValue,\n analytics,\n}: {\n context: BackupAndSyncContext;\n localMetadata?: { value?: T; lastUpdatedAt?: number };\n userStorageMetadata?: { value?: T; lastUpdatedAt?: number };\n applyLocalUpdate: (value: T) => Promise<void> | void;\n validateUserStorageValue: (value: T | undefined) => boolean;\n analytics?: {\n action: BackupAndSyncAnalyticsAction;\n profileId: ProfileId;\n };\n}): Promise<boolean> {\n const localValue = localMetadata?.value;\n const localTimestamp = localMetadata?.lastUpdatedAt;\n const userStorageValue = userStorageMetadata?.value;\n const userStorageTimestamp = userStorageMetadata?.lastUpdatedAt;\n\n const isSameValue = deepEqual(localValue, userStorageValue);\n\n if (isSameValue) {\n return false; // No sync needed, values are the same\n }\n\n const isUserStorageMoreRecent =\n localTimestamp !== undefined &&\n userStorageTimestamp !== undefined &&\n localTimestamp < userStorageTimestamp;\n\n // Validate user storage value using the provided validator\n const isUserStorageValueValid = validateUserStorageValue(userStorageValue);\n\n if ((isUserStorageMoreRecent || !localMetadata) && isUserStorageValueValid) {\n // User storage is more recent and valid, apply it locally\n await applyLocalUpdate(userStorageValue as T);\n\n // Emit analytics event if provided\n if (analytics) {\n context.emitAnalyticsEventFn({\n action: analytics.action,\n profileId: analytics.profileId,\n });\n }\n\n return false; // Don't push to user storage since we just pulled from it\n }\n\n return true; // Local is more recent or user storage is invalid, should push to user storage\n}\n"]}
1
+ {"version":3,"file":"metadata.cjs","sourceRoot":"","sources":["../../../src/backup-and-sync/syncing/metadata.ts"],"names":[],"mappings":";;;;;;AAAA,sEAAwC;AAMxC;;;;;;;;;;;;;;;;;GAiBG;AACI,KAAK,UAAU,sBAAsB,CAAI,EAC9C,OAAO,EACP,aAAa,EACb,mBAAmB,EACnB,gBAAgB,EAChB,wBAAwB,EACxB,SAAS,GAWV;IACC,MAAM,UAAU,GAAG,aAAa,EAAE,KAAK,CAAC;IACxC,MAAM,cAAc,GAAG,aAAa,EAAE,aAAa,CAAC;IACpD,MAAM,gBAAgB,GAAG,mBAAmB,EAAE,KAAK,CAAC;IACpD,MAAM,oBAAoB,GAAG,mBAAmB,EAAE,aAAa,CAAC;IAEhE,MAAM,WAAW,GAAG,IAAA,yBAAS,EAAC,UAAU,EAAE,gBAAgB,CAAC,CAAC;IAE5D,IAAI,WAAW,EAAE,CAAC;QAChB,OAAO,KAAK,CAAC,CAAC,sCAAsC;IACtD,CAAC;IAED,MAAM,uBAAuB,GAC3B,cAAc,KAAK,SAAS;QAC5B,oBAAoB,KAAK,SAAS;QAClC,cAAc,GAAG,oBAAoB,CAAC;IAExC,2DAA2D;IAC3D,MAAM,uBAAuB,GAAG,wBAAwB,CAAC,gBAAgB,CAAC,CAAC;IAE3E,IAAI,CAAC,uBAAuB,IAAI,CAAC,aAAa,CAAC,IAAI,uBAAuB,EAAE,CAAC;QAC3E,0DAA0D;QAC1D,MAAM,gBAAgB,CAAC,gBAAqB,CAAC,CAAC;QAE9C,mCAAmC;QACnC,IAAI,SAAS,EAAE,CAAC;YACd,OAAO,CAAC,oBAAoB,CAAC;gBAC3B,MAAM,EAAE,SAAS,CAAC,MAAM;gBACxB,SAAS,EAAE,SAAS,CAAC,SAAS;aAC/B,CAAC,CAAC;QACL,CAAC;QAED,OAAO,KAAK,CAAC,CAAC,0DAA0D;IAC1E,CAAC;IAED,OAAO,IAAI,CAAC,CAAC,+EAA+E;AAC9F,CAAC;AArDD,wDAqDC","sourcesContent":["import deepEqual from 'fast-deep-equal';\n\nimport type { BackupAndSyncAnalyticsAction } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport type { BackupAndSyncContext } from '../types';\n\n/**\n * Compares metadata between local and user storage, applying the most recent version.\n *\n * @param options - Configuration object for metadata comparison.\n * @param options.context - The backup and sync context containing controller and messenger.\n * @param options.localMetadata - The local metadata object.\n * @param options.localMetadata.value - The local metadata value.\n * @param options.localMetadata.lastUpdatedAt - The local metadata timestamp.\n * @param options.userStorageMetadata - The user storage metadata object.\n * @param options.userStorageMetadata.value - The user storage metadata value.\n * @param options.userStorageMetadata.lastUpdatedAt - The user storage metadata timestamp.\n * @param options.applyLocalUpdate - Function to apply the user storage value locally.\n * @param options.validateUserStorageValue - Function to validate user storage data.\n * @param options.analytics - Optional analytics configuration for tracking updates.\n * @param options.analytics.action - The analytics action for the event.\n * @param options.analytics.profileId - The profile ID for analytics.\n * @returns Promise resolving to true if local data should be pushed to user storage.\n */\nexport async function compareAndSyncMetadata<T>({\n context,\n localMetadata,\n userStorageMetadata,\n applyLocalUpdate,\n validateUserStorageValue,\n analytics,\n}: {\n context: BackupAndSyncContext;\n localMetadata?: { value?: T; lastUpdatedAt?: number };\n userStorageMetadata?: { value?: T; lastUpdatedAt?: number };\n applyLocalUpdate: (value: T) => Promise<void> | void;\n validateUserStorageValue: (value: T | undefined) => boolean;\n analytics?: {\n action: BackupAndSyncAnalyticsAction;\n profileId: ProfileId;\n };\n}): Promise<boolean> {\n const localValue = localMetadata?.value;\n const localTimestamp = localMetadata?.lastUpdatedAt;\n const userStorageValue = userStorageMetadata?.value;\n const userStorageTimestamp = userStorageMetadata?.lastUpdatedAt;\n\n const isSameValue = deepEqual(localValue, userStorageValue);\n\n if (isSameValue) {\n return false; // No sync needed, values are the same\n }\n\n const isUserStorageMoreRecent =\n localTimestamp !== undefined &&\n userStorageTimestamp !== undefined &&\n localTimestamp < userStorageTimestamp;\n\n // Validate user storage value using the provided validator\n const isUserStorageValueValid = validateUserStorageValue(userStorageValue);\n\n if ((isUserStorageMoreRecent || !localMetadata) && isUserStorageValueValid) {\n // User storage is more recent and valid, apply it locally\n await applyLocalUpdate(userStorageValue as T);\n\n // Emit analytics event if provided\n if (analytics) {\n context.emitAnalyticsEventFn({\n action: analytics.action,\n profileId: analytics.profileId,\n });\n }\n\n return false; // Don't push to user storage since we just pulled from it\n }\n\n return true; // Local is more recent or user storage is invalid, should push to user storage\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"metadata.mjs","sourceRoot":"","sources":["../../../src/backup-and-sync/syncing/metadata.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,UAAS,wBAAwB;;AAMxC;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAAI,EAC9C,OAAO,EACP,aAAa,EACb,mBAAmB,EACnB,gBAAgB,EAChB,wBAAwB,EACxB,SAAS,GAWV;IACC,MAAM,UAAU,GAAG,aAAa,EAAE,KAAK,CAAC;IACxC,MAAM,cAAc,GAAG,aAAa,EAAE,aAAa,CAAC;IACpD,MAAM,gBAAgB,GAAG,mBAAmB,EAAE,KAAK,CAAC;IACpD,MAAM,oBAAoB,GAAG,mBAAmB,EAAE,aAAa,CAAC;IAEhE,MAAM,WAAW,GAAG,SAAS,CAAC,UAAU,EAAE,gBAAgB,CAAC,CAAC;IAE5D,IAAI,WAAW,EAAE;QACf,OAAO,KAAK,CAAC,CAAC,sCAAsC;KACrD;IAED,MAAM,uBAAuB,GAC3B,cAAc,KAAK,SAAS;QAC5B,oBAAoB,KAAK,SAAS;QAClC,cAAc,GAAG,oBAAoB,CAAC;IAExC,2DAA2D;IAC3D,MAAM,uBAAuB,GAAG,wBAAwB,CAAC,gBAAgB,CAAC,CAAC;IAE3E,IAAI,CAAC,uBAAuB,IAAI,CAAC,aAAa,CAAC,IAAI,uBAAuB,EAAE;QAC1E,0DAA0D;QAC1D,MAAM,gBAAgB,CAAC,gBAAqB,CAAC,CAAC;QAE9C,mCAAmC;QACnC,IAAI,SAAS,EAAE;YACb,OAAO,CAAC,oBAAoB,CAAC;gBAC3B,MAAM,EAAE,SAAS,CAAC,MAAM;gBACxB,SAAS,EAAE,SAAS,CAAC,SAAS;aAC/B,CAAC,CAAC;SACJ;QAED,OAAO,KAAK,CAAC,CAAC,0DAA0D;KACzE;IAED,OAAO,IAAI,CAAC,CAAC,+EAA+E;AAC9F,CAAC","sourcesContent":["import deepEqual from 'fast-deep-equal';\n\nimport type { BackupAndSyncAnalyticsAction } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport type { BackupAndSyncContext } from '../types';\n\n/**\n * Compares metadata between local and user storage, applying the most recent version.\n *\n * @param options - Configuration object for metadata comparison.\n * @param options.context - The backup and sync context containing controller and messenger.\n * @param options.localMetadata - The local metadata object.\n * @param options.localMetadata.value - The local metadata value.\n * @param options.localMetadata.lastUpdatedAt - The local metadata timestamp.\n * @param options.userStorageMetadata - The user storage metadata object.\n * @param options.userStorageMetadata.value - The user storage metadata value.\n * @param options.userStorageMetadata.lastUpdatedAt - The user storage metadata timestamp.\n * @param options.applyLocalUpdate - Function to apply the user storage value locally.\n * @param options.validateUserStorageValue - Function to validate user storage data.\n * @param options.analytics - Optional analytics configuration for tracking updates.\n * @param options.analytics.action - The analytics action for the event.\n * @param options.analytics.profileId - The profile ID for analytics.\n * @returns Promise resolving to true if local data should be pushed to user storage.\n */\nexport async function compareAndSyncMetadata<T>({\n context,\n localMetadata,\n userStorageMetadata,\n applyLocalUpdate,\n validateUserStorageValue,\n analytics,\n}: {\n context: BackupAndSyncContext;\n localMetadata?: { value?: T; lastUpdatedAt?: number };\n userStorageMetadata?: { value?: T; lastUpdatedAt?: number };\n applyLocalUpdate: (value: T) => Promise<void> | void;\n validateUserStorageValue: (value: T | undefined) => boolean;\n analytics?: {\n action: BackupAndSyncAnalyticsAction;\n profileId: ProfileId;\n };\n}): Promise<boolean> {\n const localValue = localMetadata?.value;\n const localTimestamp = localMetadata?.lastUpdatedAt;\n const userStorageValue = userStorageMetadata?.value;\n const userStorageTimestamp = userStorageMetadata?.lastUpdatedAt;\n\n const isSameValue = deepEqual(localValue, userStorageValue);\n\n if (isSameValue) {\n return false; // No sync needed, values are the same\n }\n\n const isUserStorageMoreRecent =\n localTimestamp !== undefined &&\n userStorageTimestamp !== undefined &&\n localTimestamp < userStorageTimestamp;\n\n // Validate user storage value using the provided validator\n const isUserStorageValueValid = validateUserStorageValue(userStorageValue);\n\n if ((isUserStorageMoreRecent || !localMetadata) && isUserStorageValueValid) {\n // User storage is more recent and valid, apply it locally\n await applyLocalUpdate(userStorageValue as T);\n\n // Emit analytics event if provided\n if (analytics) {\n context.emitAnalyticsEventFn({\n action: analytics.action,\n profileId: analytics.profileId,\n });\n }\n\n return false; // Don't push to user storage since we just pulled from it\n }\n\n return true; // Local is more recent or user storage is invalid, should push to user storage\n}\n"]}
1
+ {"version":3,"file":"metadata.mjs","sourceRoot":"","sources":["../../../src/backup-and-sync/syncing/metadata.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,UAAS,wBAAwB;;AAMxC;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAAI,EAC9C,OAAO,EACP,aAAa,EACb,mBAAmB,EACnB,gBAAgB,EAChB,wBAAwB,EACxB,SAAS,GAWV;IACC,MAAM,UAAU,GAAG,aAAa,EAAE,KAAK,CAAC;IACxC,MAAM,cAAc,GAAG,aAAa,EAAE,aAAa,CAAC;IACpD,MAAM,gBAAgB,GAAG,mBAAmB,EAAE,KAAK,CAAC;IACpD,MAAM,oBAAoB,GAAG,mBAAmB,EAAE,aAAa,CAAC;IAEhE,MAAM,WAAW,GAAG,SAAS,CAAC,UAAU,EAAE,gBAAgB,CAAC,CAAC;IAE5D,IAAI,WAAW,EAAE,CAAC;QAChB,OAAO,KAAK,CAAC,CAAC,sCAAsC;IACtD,CAAC;IAED,MAAM,uBAAuB,GAC3B,cAAc,KAAK,SAAS;QAC5B,oBAAoB,KAAK,SAAS;QAClC,cAAc,GAAG,oBAAoB,CAAC;IAExC,2DAA2D;IAC3D,MAAM,uBAAuB,GAAG,wBAAwB,CAAC,gBAAgB,CAAC,CAAC;IAE3E,IAAI,CAAC,uBAAuB,IAAI,CAAC,aAAa,CAAC,IAAI,uBAAuB,EAAE,CAAC;QAC3E,0DAA0D;QAC1D,MAAM,gBAAgB,CAAC,gBAAqB,CAAC,CAAC;QAE9C,mCAAmC;QACnC,IAAI,SAAS,EAAE,CAAC;YACd,OAAO,CAAC,oBAAoB,CAAC;gBAC3B,MAAM,EAAE,SAAS,CAAC,MAAM;gBACxB,SAAS,EAAE,SAAS,CAAC,SAAS;aAC/B,CAAC,CAAC;QACL,CAAC;QAED,OAAO,KAAK,CAAC,CAAC,0DAA0D;IAC1E,CAAC;IAED,OAAO,IAAI,CAAC,CAAC,+EAA+E;AAC9F,CAAC","sourcesContent":["import deepEqual from 'fast-deep-equal';\n\nimport type { BackupAndSyncAnalyticsAction } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport type { BackupAndSyncContext } from '../types';\n\n/**\n * Compares metadata between local and user storage, applying the most recent version.\n *\n * @param options - Configuration object for metadata comparison.\n * @param options.context - The backup and sync context containing controller and messenger.\n * @param options.localMetadata - The local metadata object.\n * @param options.localMetadata.value - The local metadata value.\n * @param options.localMetadata.lastUpdatedAt - The local metadata timestamp.\n * @param options.userStorageMetadata - The user storage metadata object.\n * @param options.userStorageMetadata.value - The user storage metadata value.\n * @param options.userStorageMetadata.lastUpdatedAt - The user storage metadata timestamp.\n * @param options.applyLocalUpdate - Function to apply the user storage value locally.\n * @param options.validateUserStorageValue - Function to validate user storage data.\n * @param options.analytics - Optional analytics configuration for tracking updates.\n * @param options.analytics.action - The analytics action for the event.\n * @param options.analytics.profileId - The profile ID for analytics.\n * @returns Promise resolving to true if local data should be pushed to user storage.\n */\nexport async function compareAndSyncMetadata<T>({\n context,\n localMetadata,\n userStorageMetadata,\n applyLocalUpdate,\n validateUserStorageValue,\n analytics,\n}: {\n context: BackupAndSyncContext;\n localMetadata?: { value?: T; lastUpdatedAt?: number };\n userStorageMetadata?: { value?: T; lastUpdatedAt?: number };\n applyLocalUpdate: (value: T) => Promise<void> | void;\n validateUserStorageValue: (value: T | undefined) => boolean;\n analytics?: {\n action: BackupAndSyncAnalyticsAction;\n profileId: ProfileId;\n };\n}): Promise<boolean> {\n const localValue = localMetadata?.value;\n const localTimestamp = localMetadata?.lastUpdatedAt;\n const userStorageValue = userStorageMetadata?.value;\n const userStorageTimestamp = userStorageMetadata?.lastUpdatedAt;\n\n const isSameValue = deepEqual(localValue, userStorageValue);\n\n if (isSameValue) {\n return false; // No sync needed, values are the same\n }\n\n const isUserStorageMoreRecent =\n localTimestamp !== undefined &&\n userStorageTimestamp !== undefined &&\n localTimestamp < userStorageTimestamp;\n\n // Validate user storage value using the provided validator\n const isUserStorageValueValid = validateUserStorageValue(userStorageValue);\n\n if ((isUserStorageMoreRecent || !localMetadata) && isUserStorageValueValid) {\n // User storage is more recent and valid, apply it locally\n await applyLocalUpdate(userStorageValue as T);\n\n // Emit analytics event if provided\n if (analytics) {\n context.emitAnalyticsEventFn({\n action: analytics.action,\n profileId: analytics.profileId,\n });\n }\n\n return false; // Don't push to user storage since we just pulled from it\n }\n\n return true; // Local is more recent or user storage is invalid, should push to user storage\n}\n"]}
@@ -1 +1 @@
1
- {"version":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MAAM,+BAA+B,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAC3E,qBAAqB,EACrB,gCAAgC,CACjC,CAAC;IAEF,gBAAgB,KAAhB,gBAAgB,GAAK,+BAA+B,EAAC;IAErD,OAAO,gBAAgB,CAAC;AAC1B,CAAC;AAjDD,wFAiDC;AAED;;;;;;;GAOG;AACI,KAAK,UAAU,kBAAkB,CACtC,OAA6B,EAC7B,WAAuC,EACvC,qBAAiE,EACjE,SAAoB;IAEpB,MAAM,uBAAuB,GAAG,MAAM,sCAAsC,CAC1E,OAAO,EACP,WAAW,EACX,qBAAqB,EACrB,SAAS,CACV,CAAC;IAEF,IAAI,uBAAuB,EAAE;QAC3B,MAAM,IAAA,4CAAuB,EAAC,OAAO,EAAE,WAAW,CAAC,CAAC;KACrD;AACH,CAAC;AAhBD,gDAgBC","sourcesContent":["import { compareAndSyncMetadata } from './metadata';\nimport { backupAndSyncLogger } from '../../logger';\nimport type { AccountWalletEntropyObject } from '../../wallet';\nimport { BackupAndSyncAnalyticsEvent } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport {\n UserStorageSyncedWalletSchema,\n type BackupAndSyncContext,\n type UserStorageSyncedWallet,\n} from '../types';\nimport { pushWalletToUserStorage } from '../user-storage/network-operations';\n\n/**\n * Syncs wallet metadata fields and determines if the wallet needs to be pushed to user storage.\n *\n * @param context - The sync context containing controller and messenger.\n * @param localWallet - The local wallet to sync.\n * @param walletFromUserStorage - The wallet data from user storage, if any.\n * @param profileId - The profile ID for analytics.\n * @returns Promise resolving to true if the wallet should be pushed to user storage.\n */\nexport async function syncWalletMetadataAndCheckIfPushNeeded(\n context: BackupAndSyncContext,\n localWallet: AccountWalletEntropyObject,\n walletFromUserStorage: UserStorageSyncedWallet | null | undefined,\n profileId: ProfileId,\n): Promise<boolean> {\n const walletPersistedMetadata =\n context.controller.state.accountWalletsMetadata[localWallet.id];\n\n if (\n !walletFromUserStorage ||\n Object.keys(walletFromUserStorage).length === 0\n ) {\n backupAndSyncLogger(\n `Wallet ${localWallet.id} did not exist in user storage, pushing to user storage...`,\n );\n return true;\n }\n // Track if we need to push this wallet to user storage\n let shouldPushWallet = false;\n\n // Compare and sync name metadata\n const shouldPushForName = await compareAndSyncMetadata({\n context,\n localMetadata: walletPersistedMetadata?.name,\n userStorageMetadata: walletFromUserStorage.name,\n validateUserStorageValue: (value) =>\n UserStorageSyncedWalletSchema.schema.name.schema.value.is(value),\n applyLocalUpdate: (name: string) => {\n context.controller.setAccountWalletName(localWallet.id, name);\n },\n analytics: {\n action: BackupAndSyncAnalyticsEvent.WalletRenamed,\n profileId,\n },\n });\n\n shouldPushWallet ||= shouldPushForName;\n\n // Avoid re-triggering legacy-syncing (in case this field is missing on the remote\n // wallet object).\n const shouldPushForMissingLegacyField = !Object.prototype.hasOwnProperty.call(\n walletFromUserStorage,\n 'isLegacyAccountSyncingDisabled',\n );\n\n shouldPushWallet ||= shouldPushForMissingLegacyField;\n\n return shouldPushWallet;\n}\n\n/**\n * Syncs wallet metadata and pushes it to user storage if needed.\n *\n * @param context - The sync context containing controller and messenger.\n * @param localWallet - The local wallet to sync.\n * @param walletFromUserStorage - The wallet data from user storage, if any.\n * @param profileId - The profile ID for analytics.\n */\nexport async function syncWalletMetadata(\n context: BackupAndSyncContext,\n localWallet: AccountWalletEntropyObject,\n walletFromUserStorage: UserStorageSyncedWallet | null | undefined,\n profileId: ProfileId,\n): Promise<void> {\n const shouldPushToUserStorage = await syncWalletMetadataAndCheckIfPushNeeded(\n context,\n localWallet,\n walletFromUserStorage,\n profileId,\n );\n\n if (shouldPushToUserStorage) {\n await pushWalletToUserStorage(context, localWallet);\n }\n}\n"]}
1
+ {"version":3,"file":"wallet.cjs","sourceRoot":"","sources":["../../../src/backup-and-sync/syncing/wallet.ts"],"names":[],"mappings":";;;AAAA,6CAAoD;AACpD,6CAAmD;AAEnD,sDAA2D;AAE3D,wCAIkB;AAClB,+EAA6E;AAE7E;;;;;;;;GAQG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IAErD,OAAO,gBAAgB,CAAC;AAC1B,CAAC;AAjDD,wFAiDC;AAED;;;;;;;GAOG;AACI,KAAK,UAAU,kBAAkB,CACtC,OAA6B,EAC7B,WAAuC,EACvC,qBAAiE,EACjE,SAAoB;IAEpB,MAAM,uBAAuB,GAAG,MAAM,sCAAsC,CAC1E,OAAO,EACP,WAAW,EACX,qBAAqB,EACrB,SAAS,CACV,CAAC;IAEF,IAAI,uBAAuB,EAAE,CAAC;QAC5B,MAAM,IAAA,4CAAuB,EAAC,OAAO,EAAE,WAAW,CAAC,CAAC;IACtD,CAAC;AACH,CAAC;AAhBD,gDAgBC","sourcesContent":["import { compareAndSyncMetadata } from './metadata';\nimport { backupAndSyncLogger } from '../../logger';\nimport type { AccountWalletEntropyObject } from '../../wallet';\nimport { BackupAndSyncAnalyticsEvent } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport {\n UserStorageSyncedWalletSchema,\n type BackupAndSyncContext,\n type UserStorageSyncedWallet,\n} from '../types';\nimport { pushWalletToUserStorage } from '../user-storage/network-operations';\n\n/**\n * Syncs wallet metadata fields and determines if the wallet needs to be pushed to user storage.\n *\n * @param context - The sync context containing controller and messenger.\n * @param localWallet - The local wallet to sync.\n * @param walletFromUserStorage - The wallet data from user storage, if any.\n * @param profileId - The profile ID for analytics.\n * @returns Promise resolving to true if the wallet should be pushed to user storage.\n */\nexport async function syncWalletMetadataAndCheckIfPushNeeded(\n context: BackupAndSyncContext,\n localWallet: AccountWalletEntropyObject,\n walletFromUserStorage: UserStorageSyncedWallet | null | undefined,\n profileId: ProfileId,\n): Promise<boolean> {\n const walletPersistedMetadata =\n context.controller.state.accountWalletsMetadata[localWallet.id];\n\n if (\n !walletFromUserStorage ||\n Object.keys(walletFromUserStorage).length === 0\n ) {\n backupAndSyncLogger(\n `Wallet ${localWallet.id} did not exist in user storage, pushing to user storage...`,\n );\n return true;\n }\n // Track if we need to push this wallet to user storage\n let shouldPushWallet = false;\n\n // Compare and sync name metadata\n const shouldPushForName = await compareAndSyncMetadata({\n context,\n localMetadata: walletPersistedMetadata?.name,\n userStorageMetadata: walletFromUserStorage.name,\n validateUserStorageValue: (value) =>\n UserStorageSyncedWalletSchema.schema.name.schema.value.is(value),\n applyLocalUpdate: (name: string) => {\n context.controller.setAccountWalletName(localWallet.id, name);\n },\n analytics: {\n action: BackupAndSyncAnalyticsEvent.WalletRenamed,\n profileId,\n },\n });\n\n shouldPushWallet ||= shouldPushForName;\n\n // Avoid re-triggering legacy-syncing (in case this field is missing on the remote\n // wallet object).\n const shouldPushForMissingLegacyField = !Object.prototype.hasOwnProperty.call(\n walletFromUserStorage,\n 'isLegacyAccountSyncingDisabled',\n );\n\n shouldPushWallet ||= shouldPushForMissingLegacyField;\n\n return shouldPushWallet;\n}\n\n/**\n * Syncs wallet metadata and pushes it to user storage if needed.\n *\n * @param context - The sync context containing controller and messenger.\n * @param localWallet - The local wallet to sync.\n * @param walletFromUserStorage - The wallet data from user storage, if any.\n * @param profileId - The profile ID for analytics.\n */\nexport async function syncWalletMetadata(\n context: BackupAndSyncContext,\n localWallet: AccountWalletEntropyObject,\n walletFromUserStorage: UserStorageSyncedWallet | null | undefined,\n profileId: ProfileId,\n): Promise<void> {\n const shouldPushToUserStorage = await syncWalletMetadataAndCheckIfPushNeeded(\n context,\n localWallet,\n walletFromUserStorage,\n profileId,\n );\n\n if (shouldPushToUserStorage) {\n await pushWalletToUserStorage(context, localWallet);\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"wallet.mjs","sourceRoot":"","sources":["../../../src/backup-and-sync/syncing/wallet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,uBAAmB;AACpD,OAAO,EAAE,mBAAmB,EAAE,yBAAqB;AAEnD,OAAO,EAAE,2BAA2B,EAAE,+BAAqB;AAE3D,OAAO,EACL,6BAA6B,EAG9B,qBAAiB;AAClB,OAAO,EAAE,uBAAuB,EAAE,+CAA2C;AAE7E;;;;;;;;GAQG;AACH,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B,CAAC;AAC1B,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,OAA6B,EAC7B,WAAuC,EACvC,qBAAiE,EACjE,SAAoB;IAEpB,MAAM,uBAAuB,GAAG,MAAM,sCAAsC,CAC1E,OAAO,EACP,WAAW,EACX,qBAAqB,EACrB,SAAS,CACV,CAAC;IAEF,IAAI,uBAAuB,EAAE;QAC3B,MAAM,uBAAuB,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;KACrD;AACH,CAAC","sourcesContent":["import { compareAndSyncMetadata } from './metadata';\nimport { backupAndSyncLogger } from '../../logger';\nimport type { AccountWalletEntropyObject } from '../../wallet';\nimport { BackupAndSyncAnalyticsEvent } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport {\n UserStorageSyncedWalletSchema,\n type BackupAndSyncContext,\n type UserStorageSyncedWallet,\n} from '../types';\nimport { pushWalletToUserStorage } from '../user-storage/network-operations';\n\n/**\n * Syncs wallet metadata fields and determines if the wallet needs to be pushed to user storage.\n *\n * @param context - The sync context containing controller and messenger.\n * @param localWallet - The local wallet to sync.\n * @param walletFromUserStorage - The wallet data from user storage, if any.\n * @param profileId - The profile ID for analytics.\n * @returns Promise resolving to true if the wallet should be pushed to user storage.\n */\nexport async function syncWalletMetadataAndCheckIfPushNeeded(\n context: BackupAndSyncContext,\n localWallet: AccountWalletEntropyObject,\n walletFromUserStorage: UserStorageSyncedWallet | null | undefined,\n profileId: ProfileId,\n): Promise<boolean> {\n const walletPersistedMetadata =\n context.controller.state.accountWalletsMetadata[localWallet.id];\n\n if (\n !walletFromUserStorage ||\n Object.keys(walletFromUserStorage).length === 0\n ) {\n backupAndSyncLogger(\n `Wallet ${localWallet.id} did not exist in user storage, pushing to user storage...`,\n );\n return true;\n }\n // Track if we need to push this wallet to user storage\n let shouldPushWallet = false;\n\n // Compare and sync name metadata\n const shouldPushForName = await compareAndSyncMetadata({\n context,\n localMetadata: walletPersistedMetadata?.name,\n userStorageMetadata: walletFromUserStorage.name,\n validateUserStorageValue: (value) =>\n UserStorageSyncedWalletSchema.schema.name.schema.value.is(value),\n applyLocalUpdate: (name: string) => {\n context.controller.setAccountWalletName(localWallet.id, name);\n },\n analytics: {\n action: BackupAndSyncAnalyticsEvent.WalletRenamed,\n profileId,\n },\n });\n\n shouldPushWallet ||= shouldPushForName;\n\n // Avoid re-triggering legacy-syncing (in case this field is missing on the remote\n // wallet object).\n const shouldPushForMissingLegacyField = !Object.prototype.hasOwnProperty.call(\n walletFromUserStorage,\n 'isLegacyAccountSyncingDisabled',\n );\n\n shouldPushWallet ||= shouldPushForMissingLegacyField;\n\n return shouldPushWallet;\n}\n\n/**\n * Syncs wallet metadata and pushes it to user storage if needed.\n *\n * @param context - The sync context containing controller and messenger.\n * @param localWallet - The local wallet to sync.\n * @param walletFromUserStorage - The wallet data from user storage, if any.\n * @param profileId - The profile ID for analytics.\n */\nexport async function syncWalletMetadata(\n context: BackupAndSyncContext,\n localWallet: AccountWalletEntropyObject,\n walletFromUserStorage: UserStorageSyncedWallet | null | undefined,\n profileId: ProfileId,\n): Promise<void> {\n const shouldPushToUserStorage = await syncWalletMetadataAndCheckIfPushNeeded(\n context,\n localWallet,\n walletFromUserStorage,\n profileId,\n );\n\n if (shouldPushToUserStorage) {\n await pushWalletToUserStorage(context, localWallet);\n }\n}\n"]}
1
+ {"version":3,"file":"wallet.mjs","sourceRoot":"","sources":["../../../src/backup-and-sync/syncing/wallet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,uBAAmB;AACpD,OAAO,EAAE,mBAAmB,EAAE,yBAAqB;AAEnD,OAAO,EAAE,2BAA2B,EAAE,+BAAqB;AAE3D,OAAO,EACL,6BAA6B,EAG9B,qBAAiB;AAClB,OAAO,EAAE,uBAAuB,EAAE,+CAA2C;AAE7E;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,sCAAsC,CAC1D,OAA6B,EAC7B,WAAuC,EACvC,q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KAAK,UAAU,kBAAkB,CACtC,OAA6B,EAC7B,WAAuC,EACvC,qBAAiE,EACjE,SAAoB;IAEpB,MAAM,uBAAuB,GAAG,MAAM,sCAAsC,CAC1E,OAAO,EACP,WAAW,EACX,qBAAqB,EACrB,SAAS,CACV,CAAC;IAEF,IAAI,uBAAuB,EAAE,CAAC;QAC5B,MAAM,uBAAuB,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;IACtD,CAAC;AACH,CAAC","sourcesContent":["import { compareAndSyncMetadata } from './metadata';\nimport { backupAndSyncLogger } from '../../logger';\nimport type { AccountWalletEntropyObject } from '../../wallet';\nimport { BackupAndSyncAnalyticsEvent } from '../analytics';\nimport type { ProfileId } from '../authentication';\nimport {\n UserStorageSyncedWalletSchema,\n type BackupAndSyncContext,\n type UserStorageSyncedWallet,\n} from '../types';\nimport { pushWalletToUserStorage } from '../user-storage/network-operations';\n\n/**\n * Syncs wallet metadata fields and determines if the wallet needs to be pushed to user storage.\n *\n * @param context - The sync context containing controller and messenger.\n * @param localWallet - The local wallet to sync.\n * @param walletFromUserStorage - The wallet data from user storage, if any.\n * @param profileId - The profile ID for analytics.\n * @returns Promise resolving to true if the wallet should be pushed to user storage.\n */\nexport async function syncWalletMetadataAndCheckIfPushNeeded(\n context: BackupAndSyncContext,\n localWallet: AccountWalletEntropyObject,\n walletFromUserStorage: UserStorageSyncedWallet | null | undefined,\n profileId: ProfileId,\n): Promise<boolean> {\n const walletPersistedMetadata =\n context.controller.state.accountWalletsMetadata[localWallet.id];\n\n if (\n !walletFromUserStorage ||\n Object.keys(walletFromUserStorage).length === 0\n ) {\n backupAndSyncLogger(\n `Wallet ${localWallet.id} did not exist in user storage, pushing to user storage...`,\n );\n return true;\n }\n // Track if we need to push this wallet to user storage\n let shouldPushWallet = false;\n\n // Compare and sync name metadata\n const shouldPushForName = await compareAndSyncMetadata({\n context,\n localMetadata: walletPersistedMetadata?.name,\n userStorageMetadata: walletFromUserStorage.name,\n validateUserStorageValue: (value) =>\n UserStorageSyncedWalletSchema.schema.name.schema.value.is(value),\n applyLocalUpdate: (name: string) => {\n context.controller.setAccountWalletName(localWallet.id, name);\n },\n analytics: {\n action: BackupAndSyncAnalyticsEvent.WalletRenamed,\n profileId,\n },\n });\n\n shouldPushWallet ||= shouldPushForName;\n\n // Avoid re-triggering legacy-syncing (in case this field is missing on the remote\n // wallet object).\n const shouldPushForMissingLegacyField = !Object.prototype.hasOwnProperty.call(\n walletFromUserStorage,\n 'isLegacyAccountSyncingDisabled',\n );\n\n shouldPushWallet ||= shouldPushForMissingLegacyField;\n\n return shouldPushWallet;\n}\n\n/**\n * Syncs wallet metadata and pushes it to user storage if needed.\n *\n * @param context - The sync context containing controller and messenger.\n * @param localWallet - The local wallet to sync.\n * @param walletFromUserStorage - The wallet data from user storage, if any.\n * @param profileId - The profile ID for analytics.\n */\nexport async function syncWalletMetadata(\n context: BackupAndSyncContext,\n localWallet: AccountWalletEntropyObject,\n walletFromUserStorage: UserStorageSyncedWallet | null | undefined,\n profileId: ProfileId,\n): Promise<void> {\n const shouldPushToUserStorage = await syncWalletMetadataAndCheckIfPushNeeded(\n context,\n localWallet,\n walletFromUserStorage,\n profileId,\n );\n\n if (shouldPushToUserStorage) {\n await pushWalletToUserStorage(context, localWallet);\n }\n}\n"]}