@metamask/profile-sync-controller 3.1.0 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -1
- package/dist/controllers/authentication/services.cjs +10 -3
- package/dist/controllers/authentication/services.cjs.map +1 -1
- package/dist/controllers/authentication/services.d.cts.map +1 -1
- package/dist/controllers/authentication/services.d.mts.map +1 -1
- package/dist/controllers/authentication/services.mjs +14 -3
- package/dist/controllers/authentication/services.mjs.map +1 -1
- package/dist/controllers/user-storage/UserStorageController.cjs +39 -218
- package/dist/controllers/user-storage/UserStorageController.cjs.map +1 -1
- package/dist/controllers/user-storage/UserStorageController.d.cts +7 -2
- package/dist/controllers/user-storage/UserStorageController.d.cts.map +1 -1
- package/dist/controllers/user-storage/UserStorageController.d.mts +7 -2
- package/dist/controllers/user-storage/UserStorageController.d.mts.map +1 -1
- package/dist/controllers/user-storage/UserStorageController.mjs +36 -216
- package/dist/controllers/user-storage/UserStorageController.mjs.map +1 -1
- package/dist/controllers/user-storage/__fixtures__/mockMessenger.d.cts +1 -1
- package/dist/controllers/user-storage/__fixtures__/mockMessenger.d.mts +1 -1
- package/dist/controllers/user-storage/__fixtures__/test-utils.cjs +45 -1
- package/dist/controllers/user-storage/__fixtures__/test-utils.cjs.map +1 -1
- package/dist/controllers/user-storage/__fixtures__/test-utils.d.cts +21 -0
- package/dist/controllers/user-storage/__fixtures__/test-utils.d.cts.map +1 -1
- package/dist/controllers/user-storage/__fixtures__/test-utils.d.mts +21 -0
- package/dist/controllers/user-storage/__fixtures__/test-utils.d.mts.map +1 -1
- package/dist/controllers/user-storage/__fixtures__/test-utils.mjs +38 -0
- package/dist/controllers/user-storage/__fixtures__/test-utils.mjs.map +1 -1
- package/dist/controllers/user-storage/{__fixtures__ → account-syncing/__fixtures__}/mockAccounts.cjs +3 -3
- package/dist/controllers/user-storage/account-syncing/__fixtures__/mockAccounts.cjs.map +1 -0
- package/dist/controllers/user-storage/{__fixtures__ → account-syncing/__fixtures__}/mockAccounts.d.cts +6 -6
- package/dist/controllers/user-storage/account-syncing/__fixtures__/mockAccounts.d.cts.map +1 -0
- package/dist/controllers/user-storage/{__fixtures__ → account-syncing/__fixtures__}/mockAccounts.d.mts +6 -6
- package/dist/controllers/user-storage/account-syncing/__fixtures__/mockAccounts.d.mts.map +1 -0
- package/dist/controllers/user-storage/{__fixtures__ → account-syncing/__fixtures__}/mockAccounts.mjs +3 -4
- package/dist/controllers/user-storage/account-syncing/__fixtures__/mockAccounts.mjs.map +1 -0
- package/dist/controllers/user-storage/account-syncing/__fixtures__/test-utils.cjs +50 -0
- package/dist/controllers/user-storage/account-syncing/__fixtures__/test-utils.cjs.map +1 -0
- package/dist/controllers/user-storage/account-syncing/__fixtures__/test-utils.d.cts +85 -0
- package/dist/controllers/user-storage/account-syncing/__fixtures__/test-utils.d.cts.map +1 -0
- package/dist/controllers/user-storage/account-syncing/__fixtures__/test-utils.d.mts +85 -0
- package/dist/controllers/user-storage/account-syncing/__fixtures__/test-utils.d.mts.map +1 -0
- package/dist/controllers/user-storage/account-syncing/__fixtures__/test-utils.mjs +44 -0
- package/dist/controllers/user-storage/account-syncing/__fixtures__/test-utils.mjs.map +1 -0
- package/dist/controllers/user-storage/account-syncing/constants.cjs.map +1 -0
- package/dist/controllers/user-storage/account-syncing/constants.d.cts.map +1 -0
- package/dist/controllers/user-storage/account-syncing/constants.d.mts.map +1 -0
- package/dist/controllers/user-storage/account-syncing/constants.mjs.map +1 -0
- package/dist/controllers/user-storage/account-syncing/controller-integration.cjs +194 -0
- package/dist/controllers/user-storage/account-syncing/controller-integration.cjs.map +1 -0
- package/dist/controllers/user-storage/account-syncing/controller-integration.d.cts +31 -0
- package/dist/controllers/user-storage/account-syncing/controller-integration.d.cts.map +1 -0
- package/dist/controllers/user-storage/account-syncing/controller-integration.d.mts +31 -0
- package/dist/controllers/user-storage/account-syncing/controller-integration.d.mts.map +1 -0
- package/dist/controllers/user-storage/account-syncing/controller-integration.mjs +188 -0
- package/dist/controllers/user-storage/account-syncing/controller-integration.mjs.map +1 -0
- package/dist/controllers/user-storage/account-syncing/setup-subscriptions.cjs +35 -0
- package/dist/controllers/user-storage/account-syncing/setup-subscriptions.cjs.map +1 -0
- package/dist/controllers/user-storage/account-syncing/setup-subscriptions.d.cts +8 -0
- package/dist/controllers/user-storage/account-syncing/setup-subscriptions.d.cts.map +1 -0
- package/dist/controllers/user-storage/account-syncing/setup-subscriptions.d.mts +8 -0
- package/dist/controllers/user-storage/account-syncing/setup-subscriptions.d.mts.map +1 -0
- package/dist/controllers/user-storage/account-syncing/setup-subscriptions.mjs +31 -0
- package/dist/controllers/user-storage/account-syncing/setup-subscriptions.mjs.map +1 -0
- package/dist/controllers/user-storage/account-syncing/sync-utils.cjs +47 -0
- package/dist/controllers/user-storage/account-syncing/sync-utils.cjs.map +1 -0
- package/dist/controllers/user-storage/account-syncing/sync-utils.d.cts +20 -0
- package/dist/controllers/user-storage/account-syncing/sync-utils.d.cts.map +1 -0
- package/dist/controllers/user-storage/account-syncing/sync-utils.d.mts +20 -0
- package/dist/controllers/user-storage/account-syncing/sync-utils.d.mts.map +1 -0
- package/dist/controllers/user-storage/account-syncing/sync-utils.mjs +41 -0
- package/dist/controllers/user-storage/account-syncing/sync-utils.mjs.map +1 -0
- package/dist/controllers/user-storage/account-syncing/types.cjs +3 -0
- package/dist/controllers/user-storage/account-syncing/types.cjs.map +1 -0
- package/dist/controllers/user-storage/account-syncing/types.d.cts +26 -0
- package/dist/controllers/user-storage/account-syncing/types.d.cts.map +1 -0
- package/dist/controllers/user-storage/account-syncing/types.d.mts +26 -0
- package/dist/controllers/user-storage/account-syncing/types.d.mts.map +1 -0
- package/dist/controllers/user-storage/account-syncing/types.mjs +2 -0
- package/dist/controllers/user-storage/account-syncing/types.mjs.map +1 -0
- package/dist/controllers/user-storage/{accounts/user-storage.cjs → account-syncing/utils.cjs} +12 -2
- package/dist/controllers/user-storage/account-syncing/utils.cjs.map +1 -0
- package/dist/controllers/user-storage/account-syncing/utils.d.cts +23 -0
- package/dist/controllers/user-storage/account-syncing/utils.d.cts.map +1 -0
- package/dist/controllers/user-storage/account-syncing/utils.d.mts +23 -0
- package/dist/controllers/user-storage/account-syncing/utils.d.mts.map +1 -0
- package/dist/controllers/user-storage/{accounts/user-storage.mjs → account-syncing/utils.mjs} +10 -1
- package/dist/controllers/user-storage/account-syncing/utils.mjs.map +1 -0
- package/dist/controllers/user-storage/services.cjs +10 -10
- package/dist/controllers/user-storage/services.cjs.map +1 -1
- package/dist/controllers/user-storage/services.d.cts.map +1 -1
- package/dist/controllers/user-storage/services.d.mts.map +1 -1
- package/dist/controllers/user-storage/services.mjs +10 -10
- package/dist/controllers/user-storage/services.mjs.map +1 -1
- package/package.json +6 -5
- package/dist/controllers/user-storage/__fixtures__/mockAccounts.cjs.map +0 -1
- package/dist/controllers/user-storage/__fixtures__/mockAccounts.d.cts.map +0 -1
- package/dist/controllers/user-storage/__fixtures__/mockAccounts.d.mts.map +0 -1
- package/dist/controllers/user-storage/__fixtures__/mockAccounts.mjs.map +0 -1
- package/dist/controllers/user-storage/accounts/constants.cjs.map +0 -1
- package/dist/controllers/user-storage/accounts/constants.d.cts.map +0 -1
- package/dist/controllers/user-storage/accounts/constants.d.mts.map +0 -1
- package/dist/controllers/user-storage/accounts/constants.mjs.map +0 -1
- package/dist/controllers/user-storage/accounts/user-storage.cjs.map +0 -1
- package/dist/controllers/user-storage/accounts/user-storage.d.cts +0 -32
- package/dist/controllers/user-storage/accounts/user-storage.d.cts.map +0 -1
- package/dist/controllers/user-storage/accounts/user-storage.d.mts +0 -32
- package/dist/controllers/user-storage/accounts/user-storage.d.mts.map +0 -1
- package/dist/controllers/user-storage/accounts/user-storage.mjs.map +0 -1
- /package/dist/controllers/user-storage/{accounts → account-syncing}/constants.cjs +0 -0
- /package/dist/controllers/user-storage/{accounts → account-syncing}/constants.d.cts +0 -0
- /package/dist/controllers/user-storage/{accounts → account-syncing}/constants.d.mts +0 -0
- /package/dist/controllers/user-storage/{accounts → account-syncing}/constants.mjs +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-utils.mjs","sourceRoot":"","sources":["../../../../../src/controllers/user-storage/account-syncing/__fixtures__/test-utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,gDAAsC;AACjE,OAAO,EAAE,wBAAwB,EAAE,6CAAyC;AAC5E,OAAO,EAAE,sCAAsC,EAAE,qBAAiB;AAClE,OAAO,EAAE,sBAAsB,EAAE,2BAAuB;AAExD;;;;;;;GAOG;AACH,MAAM,UAAU,yCAAyC,CAAC,OAIzD;IACC,MAAM,cAAc,GAAG,wBAAwB,EAAE,CAAC;IAElD,cAAc,CAAC,wBAAwB,CAAC,kBAAkB,CAAC,KAAK,IAAI,EAAE;QACpE,cAAc,CAAC,aAAa,CAAC,OAAO,CAClC,iCAAiC,EACjC,sBAAsB,CAAC,GAAG,CAAC,CAAC,CAAoB,CACjD,CAAC;QACF,OAAO,sBAAsB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IAC/C,CAAC,CAAC,CAAC;IAEH,cAAc,CAAC,wBAAwB,CAAC,eAAe,CACrD,CAAC,OAAO,EAAE,QAAQ,EAAE,YAAY;QAC9B,sBAAsB,CAAC,GAAG,CAAsB,CACnD,CAAC;IAEF,OAAO,cAAc,CAAC;AACxB,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,wCAAwC,CACtD,IAAiC,EACjC,UAAkB;IAElB,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,EAAE,UAAU,CAAC,EAAE,EAAE,CAAC;QAC1C,gBAAgB,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,UAAU,CAAC;QAC/C,IAAI,CAAC,SAAS,CAAC,sCAAsC,CAAC,UAAU,CAAC,CAAC;KACnE,CAAC,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,wCAAwC,CACtD,IAAc,EACd,UAAkB;IAElB,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAC3B,gBAAgB,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,UAAU,CAAC,CAChD,CAAC;AACJ,CAAC","sourcesContent":["import type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport { createSHA256Hash } from '../../../../shared/encryption';\nimport { mockUserStorageMessenger } from '../../__fixtures__/mockMessenger';\nimport { mapInternalAccountToUserStorageAccount } from '../utils';\nimport { MOCK_INTERNAL_ACCOUNTS } from './mockAccounts';\n\n/**\n * Test Utility - create a mock user storage messenger for account syncing tests\n *\n * @param options - options for the mock messenger\n * @param options.accounts - options for the accounts part of the controller\n * @param options.accounts.accountsList - list of accounts to return for the 'AccountsController:listAccounts' action\n * @returns Mock User Storage Messenger\n */\nexport function mockUserStorageMessengerForAccountSyncing(options?: {\n accounts?: {\n accountsList?: InternalAccount[];\n };\n}) {\n const messengerMocks = mockUserStorageMessenger();\n\n messengerMocks.mockKeyringAddNewAccount.mockImplementation(async () => {\n messengerMocks.baseMessenger.publish(\n 'AccountsController:accountAdded',\n MOCK_INTERNAL_ACCOUNTS.ONE[0] as InternalAccount,\n );\n return MOCK_INTERNAL_ACCOUNTS.ONE[0].address;\n });\n\n messengerMocks.mockAccountsListAccounts.mockReturnValue(\n (options?.accounts?.accountsList ??\n MOCK_INTERNAL_ACCOUNTS.ALL) as InternalAccount[],\n );\n\n return messengerMocks;\n}\n\n/**\n * Test Utility - creates a realistic expected batch upsert payload\n * @param data - data supposed to be upserted\n * @param storageKey - storage key\n * @returns expected body\n */\nexport function createExpectedAccountSyncBatchUpsertBody(\n data: [string, InternalAccount][],\n storageKey: string,\n) {\n return data.map(([entryKey, entryValue]) => [\n createSHA256Hash(String(entryKey) + storageKey),\n JSON.stringify(mapInternalAccountToUserStorageAccount(entryValue)),\n ]);\n}\n\n/**\n * Test Utility - creates a realistic expected batch delete payload\n * @param data - data supposed to be deleted\n * @param storageKey - storage key\n * @returns expected body\n */\nexport function createExpectedAccountSyncBatchDeleteBody(\n data: string[],\n storageKey: string,\n) {\n return data.map((entryKey) =>\n createSHA256Hash(String(entryKey) + storageKey),\n );\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.cjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,oBAAoB,GAAG,GAAG,CAAC;AAExC,4FAA4F;AAC/E,QAAA,wBAAwB,GAAkB,GAAY,CAAC;AAEpE,qGAAqG;AACxF,QAAA,+BAA+B,GAAG;IAC7C,SAAS;IACT,KAAK;IACL,QAAQ;IACR,QAAQ;IACR,YAAY;IACZ,QAAQ;IACR,MAAM;IACN,OAAO;IACP,aAAa;IACb,QAAQ;IACR,MAAM;IACN,MAAM;IACN,SAAS;IACT,OAAO;IACP,QAAQ;IACR,MAAM;IACN,OAAO;IACP,MAAM;IACN,MAAM;IACN,MAAM;IACN,OAAO;IACP,MAAM;IACN,IAAI;IACJ,SAAS;IACT,OAAO;IACP,WAAW;IACX,MAAM;IACN,OAAO;IACP,OAAO;IACP,MAAM;IACN,MAAM;IACN,OAAO;IACP,SAAS;IACT,QAAQ;IACR,MAAM;IACN,OAAO;IACP,OAAO;IACP,iBAAiB;IACjB,WAAW;IACX,IAAI;IACJ,IAAI;CACI,CAAC","sourcesContent":["export const USER_STORAGE_VERSION = '1';\n\n// Force cast. We don't really care about the type here since we treat it as a unique symbol\nexport const USER_STORAGE_VERSION_KEY: unique symbol = 'v' as never;\n\n// We need this in order to know if an account is a default account or not when we do account syncing\nexport const LOCALIZED_DEFAULT_ACCOUNT_NAMES = [\n 'Account',\n 'መለያ',\n 'الحساب',\n 'Профил',\n 'অ্যাকাউন্ট',\n 'Compte',\n 'Účet',\n 'Konto',\n 'Λογαριασμός',\n 'Cuenta',\n 'حساب',\n 'Tili',\n 'એકાઉન્ટ',\n 'חשבון',\n 'अकाउंट',\n 'खाता',\n 'Račun',\n 'Kont',\n 'Fiók',\n 'Akun',\n 'アカウント',\n 'ಖಾತೆ',\n '계정',\n 'Paskyra',\n 'Konts',\n 'അക്കൗണ്ട്',\n 'खाते',\n 'Akaun',\n 'Conta',\n 'Cont',\n 'Счет',\n 'налог',\n 'Akaunti',\n 'கணக்கு',\n 'ఖాతా',\n 'บัญชี',\n 'Hesap',\n 'Обліковий запис',\n 'Tài khoản',\n '账户',\n '帳戶',\n] as const;\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.cts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,oBAAoB,MAAM,CAAC;AAGxC,eAAO,MAAM,wBAAwB,EAAE,OAAO,MAAqB,CAAC;AAGpE,eAAO,MAAM,+BAA+B,gZA0ClC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.mts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,oBAAoB,MAAM,CAAC;AAGxC,eAAO,MAAM,wBAAwB,EAAE,OAAO,MAAqB,CAAC;AAGpE,eAAO,MAAM,+BAA+B,gZA0ClC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.mjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,oBAAoB,GAAG,GAAG,CAAC;AAExC,4FAA4F;AAC5F,MAAM,CAAC,MAAM,wBAAwB,GAAkB,GAAY,CAAC;AAEpE,qGAAqG;AACrG,MAAM,CAAC,MAAM,+BAA+B,GAAG;IAC7C,SAAS;IACT,KAAK;IACL,QAAQ;IACR,QAAQ;IACR,YAAY;IACZ,QAAQ;IACR,MAAM;IACN,OAAO;IACP,aAAa;IACb,QAAQ;IACR,MAAM;IACN,MAAM;IACN,SAAS;IACT,OAAO;IACP,QAAQ;IACR,MAAM;IACN,OAAO;IACP,MAAM;IACN,MAAM;IACN,MAAM;IACN,OAAO;IACP,MAAM;IACN,IAAI;IACJ,SAAS;IACT,OAAO;IACP,WAAW;IACX,MAAM;IACN,OAAO;IACP,OAAO;IACP,MAAM;IACN,MAAM;IACN,OAAO;IACP,SAAS;IACT,QAAQ;IACR,MAAM;IACN,OAAO;IACP,OAAO;IACP,iBAAiB;IACjB,WAAW;IACX,IAAI;IACJ,IAAI;CACI,CAAC","sourcesContent":["export const USER_STORAGE_VERSION = '1';\n\n// Force cast. We don't really care about the type here since we treat it as a unique symbol\nexport const USER_STORAGE_VERSION_KEY: unique symbol = 'v' as never;\n\n// We need this in order to know if an account is a default account or not when we do account syncing\nexport const LOCALIZED_DEFAULT_ACCOUNT_NAMES = [\n 'Account',\n 'መለያ',\n 'الحساب',\n 'Профил',\n 'অ্যাকাউন্ট',\n 'Compte',\n 'Účet',\n 'Konto',\n 'Λογαριασμός',\n 'Cuenta',\n 'حساب',\n 'Tili',\n 'એકાઉન્ટ',\n 'חשבון',\n 'अकाउंट',\n 'खाता',\n 'Račun',\n 'Kont',\n 'Fiók',\n 'Akun',\n 'アカウント',\n 'ಖಾತೆ',\n '계정',\n 'Paskyra',\n 'Konts',\n 'അക്കൗണ്ട്',\n 'खाते',\n 'Akaun',\n 'Conta',\n 'Cont',\n 'Счет',\n 'налог',\n 'Akaunti',\n 'கணக்கு',\n 'ఖాతా',\n 'บัญชี',\n 'Hesap',\n 'Обліковий запис',\n 'Tài khoản',\n '账户',\n '帳戶',\n] as const;\n"]}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.syncInternalAccountsWithUserStorage = exports.saveInternalAccountsListToUserStorage = exports.saveInternalAccountToUserStorage = void 0;
|
|
4
|
+
const keyring_api_1 = require("@metamask/keyring-api");
|
|
5
|
+
const storage_schema_1 = require("../../../shared/storage-schema.cjs");
|
|
6
|
+
const sync_utils_1 = require("./sync-utils.cjs");
|
|
7
|
+
const utils_1 = require("./utils.cjs");
|
|
8
|
+
/**
|
|
9
|
+
* Saves an individual internal account to the user storage.
|
|
10
|
+
* @param internalAccount - The internal account to save
|
|
11
|
+
* @param config - parameters used for saving the internal account
|
|
12
|
+
* @param options - parameters used for saving the internal account
|
|
13
|
+
*/
|
|
14
|
+
async function saveInternalAccountToUserStorage(internalAccount, config, options) {
|
|
15
|
+
const { isAccountSyncingEnabled } = config;
|
|
16
|
+
const { getUserStorageControllerInstance } = options;
|
|
17
|
+
if (!isAccountSyncingEnabled ||
|
|
18
|
+
!(0, sync_utils_1.canPerformAccountSyncing)(config, options) ||
|
|
19
|
+
!(0, keyring_api_1.isEvmAccountType)(internalAccount.type) ||
|
|
20
|
+
!(0, utils_1.doesInternalAccountHaveCorrectKeyringType)(internalAccount)) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
// Map the internal account to the user storage account schema
|
|
25
|
+
const mappedAccount = (0, utils_1.mapInternalAccountToUserStorageAccount)(internalAccount);
|
|
26
|
+
await getUserStorageControllerInstance().performSetStorage(
|
|
27
|
+
// ESLint is confused here.
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
29
|
+
`${storage_schema_1.USER_STORAGE_FEATURE_NAMES.accounts}.${internalAccount.address}`, JSON.stringify(mappedAccount));
|
|
30
|
+
}
|
|
31
|
+
catch (e) {
|
|
32
|
+
// istanbul ignore next
|
|
33
|
+
const errorMessage = e instanceof Error ? e.message : JSON.stringify(e);
|
|
34
|
+
throw new Error(`UserStorageController - failed to save account to user storage - ${errorMessage}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.saveInternalAccountToUserStorage = saveInternalAccountToUserStorage;
|
|
38
|
+
/**
|
|
39
|
+
* Saves the list of internal accounts to the user storage.
|
|
40
|
+
* @param config - parameters used for saving the list of internal accounts
|
|
41
|
+
* @param options - parameters used for saving the list of internal accounts
|
|
42
|
+
*/
|
|
43
|
+
async function saveInternalAccountsListToUserStorage(config, options) {
|
|
44
|
+
const { isAccountSyncingEnabled } = config;
|
|
45
|
+
const { getUserStorageControllerInstance } = options;
|
|
46
|
+
if (!isAccountSyncingEnabled) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const internalAccountsList = await (0, sync_utils_1.getInternalAccountsList)(options);
|
|
50
|
+
if (!internalAccountsList?.length) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const internalAccountsListFormattedForUserStorage = internalAccountsList.map(utils_1.mapInternalAccountToUserStorageAccount);
|
|
54
|
+
await getUserStorageControllerInstance().performBatchSetStorage(storage_schema_1.USER_STORAGE_FEATURE_NAMES.accounts, internalAccountsListFormattedForUserStorage.map((account) => [
|
|
55
|
+
account.a,
|
|
56
|
+
JSON.stringify(account),
|
|
57
|
+
]));
|
|
58
|
+
}
|
|
59
|
+
exports.saveInternalAccountsListToUserStorage = saveInternalAccountsListToUserStorage;
|
|
60
|
+
/**
|
|
61
|
+
* Syncs the internal accounts list with the user storage accounts list.
|
|
62
|
+
* This method is used to make sure that the internal accounts list is up-to-date with the user storage accounts list and vice-versa.
|
|
63
|
+
* It will add new accounts to the internal accounts list, update/merge conflicting names and re-upload the results in some cases to the user storage.
|
|
64
|
+
* @param config - parameters used for syncing the internal accounts list with the user storage accounts list
|
|
65
|
+
* @param options - parameters used for syncing the internal accounts list with the user storage accounts list
|
|
66
|
+
*/
|
|
67
|
+
async function syncInternalAccountsWithUserStorage(config, options) {
|
|
68
|
+
const { isAccountSyncingEnabled } = config;
|
|
69
|
+
if (!(0, sync_utils_1.canPerformAccountSyncing)(config, options) || !isAccountSyncingEnabled) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const { maxNumberOfAccountsToAdd = 100, onAccountAdded, onAccountNameUpdated, onAccountSyncErroneousSituation, } = config;
|
|
73
|
+
const { getMessenger, getUserStorageControllerInstance } = options;
|
|
74
|
+
try {
|
|
75
|
+
await getUserStorageControllerInstance().setIsAccountSyncingInProgress(true);
|
|
76
|
+
const userStorageAccountsList = await (0, sync_utils_1.getUserStorageAccountsList)(options);
|
|
77
|
+
if (!userStorageAccountsList || !userStorageAccountsList.length) {
|
|
78
|
+
await saveInternalAccountsListToUserStorage({ isAccountSyncingEnabled }, options);
|
|
79
|
+
await getUserStorageControllerInstance().setHasAccountSyncingSyncedAtLeastOnce(true);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
// Prepare an array of internal accounts to be saved to the user storage
|
|
83
|
+
const internalAccountsToBeSavedToUserStorage = [];
|
|
84
|
+
// Compare internal accounts list with user storage accounts list
|
|
85
|
+
// First step: compare lengths
|
|
86
|
+
const internalAccountsList = await (0, sync_utils_1.getInternalAccountsList)(options);
|
|
87
|
+
if (!internalAccountsList || !internalAccountsList.length) {
|
|
88
|
+
throw new Error(`Failed to get internal accounts list`);
|
|
89
|
+
}
|
|
90
|
+
const hasMoreUserStorageAccountsThanInternalAccounts = userStorageAccountsList.length > internalAccountsList.length;
|
|
91
|
+
// We don't want to remove existing accounts for a user
|
|
92
|
+
// so we only add new accounts if the user has more accounts in user storage than internal accounts
|
|
93
|
+
if (hasMoreUserStorageAccountsThanInternalAccounts) {
|
|
94
|
+
const numberOfAccountsToAdd = Math.min(userStorageAccountsList.length, maxNumberOfAccountsToAdd) -
|
|
95
|
+
internalAccountsList.length;
|
|
96
|
+
// Create new accounts to match the user storage accounts list
|
|
97
|
+
for (let i = 0; i < numberOfAccountsToAdd; i++) {
|
|
98
|
+
await getMessenger().call('KeyringController:addNewAccount');
|
|
99
|
+
onAccountAdded?.();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// Second step: compare account names
|
|
103
|
+
// Get the internal accounts list again since new accounts might have been added in the previous step
|
|
104
|
+
const refreshedInternalAccountsList = await (0, sync_utils_1.getInternalAccountsList)(options);
|
|
105
|
+
const newlyAddedAccounts = refreshedInternalAccountsList.filter((account) => !internalAccountsList.find((a) => a.address === account.address));
|
|
106
|
+
for (const internalAccount of refreshedInternalAccountsList) {
|
|
107
|
+
const userStorageAccount = userStorageAccountsList.find((account) => account.a === internalAccount.address);
|
|
108
|
+
// If the account is not present in user storage
|
|
109
|
+
// istanbul ignore next
|
|
110
|
+
if (!userStorageAccount) {
|
|
111
|
+
// If the account was just added in the previous step, skip saving it, it's likely to be a bogus account
|
|
112
|
+
if (newlyAddedAccounts.includes(internalAccount)) {
|
|
113
|
+
onAccountSyncErroneousSituation?.('An account was added to the internal accounts list but was not present in the user storage accounts list');
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
// Otherwise, it means that this internal account was present before the sync, and needs to be saved to the user storage
|
|
117
|
+
internalAccountsToBeSavedToUserStorage.push(internalAccount);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
// From this point on, we know that the account is present in
|
|
121
|
+
// both the internal accounts list and the user storage accounts list
|
|
122
|
+
// One or both accounts have default names
|
|
123
|
+
const isInternalAccountNameDefault = (0, utils_1.isNameDefaultAccountName)(internalAccount.metadata.name);
|
|
124
|
+
const isUserStorageAccountNameDefault = (0, utils_1.isNameDefaultAccountName)(userStorageAccount.n);
|
|
125
|
+
// Internal account has default name
|
|
126
|
+
if (isInternalAccountNameDefault) {
|
|
127
|
+
if (!isUserStorageAccountNameDefault) {
|
|
128
|
+
getMessenger().call('AccountsController:updateAccountMetadata', internalAccount.id, {
|
|
129
|
+
name: userStorageAccount.n,
|
|
130
|
+
});
|
|
131
|
+
onAccountNameUpdated?.();
|
|
132
|
+
}
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
// Internal account has custom name but user storage account has default name
|
|
136
|
+
if (isUserStorageAccountNameDefault) {
|
|
137
|
+
internalAccountsToBeSavedToUserStorage.push(internalAccount);
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
// Both accounts have custom names
|
|
141
|
+
// User storage account has a nameLastUpdatedAt timestamp
|
|
142
|
+
// Note: not storing the undefined checks in constants to act as a type guard
|
|
143
|
+
if (userStorageAccount.nlu !== undefined) {
|
|
144
|
+
if (internalAccount.metadata.nameLastUpdatedAt !== undefined) {
|
|
145
|
+
const isInternalAccountNameNewer = internalAccount.metadata.nameLastUpdatedAt > userStorageAccount.nlu;
|
|
146
|
+
if (isInternalAccountNameNewer) {
|
|
147
|
+
internalAccountsToBeSavedToUserStorage.push(internalAccount);
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
getMessenger().call('AccountsController:updateAccountMetadata', internalAccount.id, {
|
|
152
|
+
name: userStorageAccount.n,
|
|
153
|
+
nameLastUpdatedAt: userStorageAccount.nlu,
|
|
154
|
+
});
|
|
155
|
+
const areInternalAndUserStorageAccountNamesEqual = internalAccount.metadata.name === userStorageAccount.n;
|
|
156
|
+
if (!areInternalAndUserStorageAccountNamesEqual) {
|
|
157
|
+
onAccountNameUpdated?.();
|
|
158
|
+
}
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
else if (internalAccount.metadata.nameLastUpdatedAt !== undefined) {
|
|
162
|
+
internalAccountsToBeSavedToUserStorage.push(internalAccount);
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// Save the internal accounts list to the user storage
|
|
167
|
+
if (internalAccountsToBeSavedToUserStorage.length) {
|
|
168
|
+
await getUserStorageControllerInstance().performBatchSetStorage(storage_schema_1.USER_STORAGE_FEATURE_NAMES.accounts, internalAccountsToBeSavedToUserStorage.map((account) => [
|
|
169
|
+
account.address,
|
|
170
|
+
JSON.stringify((0, utils_1.mapInternalAccountToUserStorageAccount)(account)),
|
|
171
|
+
]));
|
|
172
|
+
}
|
|
173
|
+
// In case we have corrupted user storage with accounts that don't exist in the internal accounts list
|
|
174
|
+
// Delete those accounts from the user storage
|
|
175
|
+
const userStorageAccountsToBeDeleted = userStorageAccountsList.filter((account) => !refreshedInternalAccountsList.find((a) => a.address === account.a));
|
|
176
|
+
if (userStorageAccountsToBeDeleted.length) {
|
|
177
|
+
await getUserStorageControllerInstance().performBatchDeleteStorage(storage_schema_1.USER_STORAGE_FEATURE_NAMES.accounts, userStorageAccountsToBeDeleted.map((account) => account.a));
|
|
178
|
+
onAccountSyncErroneousSituation?.('An account was present in the user storage accounts list but was not found in the internal accounts list after the sync');
|
|
179
|
+
}
|
|
180
|
+
// We do this here and not in the finally statement because we want to make sure that
|
|
181
|
+
// the accounts are saved / updated / deleted at least once before we set this flag
|
|
182
|
+
await getUserStorageControllerInstance().setHasAccountSyncingSyncedAtLeastOnce(true);
|
|
183
|
+
}
|
|
184
|
+
catch (e) {
|
|
185
|
+
// istanbul ignore next
|
|
186
|
+
const errorMessage = e instanceof Error ? e.message : JSON.stringify(e);
|
|
187
|
+
throw new Error(`UserStorageController - failed to sync user storage accounts list - ${errorMessage}`);
|
|
188
|
+
}
|
|
189
|
+
finally {
|
|
190
|
+
await getUserStorageControllerInstance().setIsAccountSyncingInProgress(false);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
exports.syncInternalAccountsWithUserStorage = syncInternalAccountsWithUserStorage;
|
|
194
|
+
//# sourceMappingURL=controller-integration.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"controller-integration.cjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/controller-integration.ts"],"names":[],"mappings":";;;AAAA,uDAAyD;AAGzD,uEAA4E;AAC5E,iDAIsB;AAEtB,uCAIiB;AAEjB;;;;;GAKG;AACI,KAAK,UAAU,gCAAgC,CACpD,eAAgC,EAChC,MAA4B,EAC5B,OAA8B;IAE9B,MAAM,EAAE,uBAAuB,EAAE,GAAG,MAAM,CAAC;IAC3C,MAAM,EAAE,gCAAgC,EAAE,GAAG,OAAO,CAAC;IAErD,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AAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;QACxE,MAAM,IAAI,KAAK,CACb,uEAAuE,YAAY,EAAE,CACtF,CAAC;KACH;YAAS;QACR,MAAM,gCAAgC,EAAE,CAAC,6BAA6B,CACpE,KAAK,CACN,CAAC;KACH;AACH,CAAC;AAlND,kFAkNC","sourcesContent":["import { isEvmAccountType } from '@metamask/keyring-api';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport { USER_STORAGE_FEATURE_NAMES } from '../../../shared/storage-schema';\nimport {\n canPerformAccountSyncing,\n getInternalAccountsList,\n getUserStorageAccountsList,\n} from './sync-utils';\nimport type { AccountSyncingConfig, AccountSyncingOptions } from './types';\nimport {\n doesInternalAccountHaveCorrectKeyringType,\n isNameDefaultAccountName,\n mapInternalAccountToUserStorageAccount,\n} from './utils';\n\n/**\n * Saves an individual internal account to the user storage.\n * @param internalAccount - The internal account to save\n * @param config - parameters used for saving the internal account\n * @param options - parameters used for saving the internal account\n */\nexport async function saveInternalAccountToUserStorage(\n internalAccount: InternalAccount,\n config: AccountSyncingConfig,\n options: AccountSyncingOptions,\n): Promise<void> {\n const { isAccountSyncingEnabled } = config;\n const { getUserStorageControllerInstance } = options;\n\n if (\n !isAccountSyncingEnabled ||\n !canPerformAccountSyncing(config, options) ||\n !isEvmAccountType(internalAccount.type) ||\n !doesInternalAccountHaveCorrectKeyringType(internalAccount)\n ) {\n return;\n }\n\n try {\n // Map the internal account to the user storage account schema\n const mappedAccount =\n mapInternalAccountToUserStorageAccount(internalAccount);\n\n await getUserStorageControllerInstance().performSetStorage(\n // ESLint is confused here.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `${USER_STORAGE_FEATURE_NAMES.accounts}.${internalAccount.address}`,\n JSON.stringify(mappedAccount),\n );\n } catch (e) {\n // istanbul ignore next\n const errorMessage = e instanceof Error ? e.message : JSON.stringify(e);\n throw new Error(\n `UserStorageController - failed to save account to user storage - ${errorMessage}`,\n );\n }\n}\n\n/**\n * Saves the list of internal accounts to the user storage.\n * @param config - parameters used for saving the list of internal accounts\n * @param options - parameters used for saving the list of internal accounts\n */\nexport async function saveInternalAccountsListToUserStorage(\n config: AccountSyncingConfig,\n options: AccountSyncingOptions,\n): Promise<void> {\n const { isAccountSyncingEnabled } = config;\n const { getUserStorageControllerInstance } = options;\n\n if (!isAccountSyncingEnabled) {\n return;\n }\n\n const internalAccountsList = await getInternalAccountsList(options);\n\n if (!internalAccountsList?.length) {\n return;\n }\n\n const internalAccountsListFormattedForUserStorage = internalAccountsList.map(\n mapInternalAccountToUserStorageAccount,\n );\n\n await getUserStorageControllerInstance().performBatchSetStorage(\n USER_STORAGE_FEATURE_NAMES.accounts,\n internalAccountsListFormattedForUserStorage.map((account) => [\n account.a,\n JSON.stringify(account),\n ]),\n );\n}\n\ntype SyncInternalAccountsWithUserStorageConfig = AccountSyncingConfig & {\n maxNumberOfAccountsToAdd?: number;\n onAccountAdded?: () => void;\n onAccountNameUpdated?: () => void;\n onAccountSyncErroneousSituation?: (errorMessage: string) => void;\n};\n\n/**\n * Syncs the internal accounts list with the user storage accounts list.\n * This method is used to make sure that the internal accounts list is up-to-date with the user storage accounts list and vice-versa.\n * It will add new accounts to the internal accounts list, update/merge conflicting names and re-upload the results in some cases to the user storage.\n * @param config - parameters used for syncing the internal accounts list with the user storage accounts list\n * @param options - parameters used for syncing the internal accounts list with the user storage accounts list\n */\nexport async function syncInternalAccountsWithUserStorage(\n config: SyncInternalAccountsWithUserStorageConfig,\n options: AccountSyncingOptions,\n): Promise<void> {\n const { isAccountSyncingEnabled } = config;\n\n if (!canPerformAccountSyncing(config, options) || !isAccountSyncingEnabled) {\n return;\n }\n\n const {\n maxNumberOfAccountsToAdd = 100,\n onAccountAdded,\n onAccountNameUpdated,\n onAccountSyncErroneousSituation,\n } = config;\n const { getMessenger, getUserStorageControllerInstance } = options;\n\n try {\n await getUserStorageControllerInstance().setIsAccountSyncingInProgress(\n true,\n );\n\n const userStorageAccountsList = await getUserStorageAccountsList(options);\n\n if (!userStorageAccountsList || !userStorageAccountsList.length) {\n await saveInternalAccountsListToUserStorage(\n { isAccountSyncingEnabled },\n options,\n );\n await getUserStorageControllerInstance().setHasAccountSyncingSyncedAtLeastOnce(\n true,\n );\n return;\n }\n\n // Prepare an array of internal accounts to be saved to the user storage\n const internalAccountsToBeSavedToUserStorage: InternalAccount[] = [];\n\n // Compare internal accounts list with user storage accounts list\n // First step: compare lengths\n const internalAccountsList = await getInternalAccountsList(options);\n\n if (!internalAccountsList || !internalAccountsList.length) {\n throw new Error(`Failed to get internal accounts list`);\n }\n\n const hasMoreUserStorageAccountsThanInternalAccounts =\n userStorageAccountsList.length > internalAccountsList.length;\n\n // We don't want to remove existing accounts for a user\n // so we only add new accounts if the user has more accounts in user storage than internal accounts\n if (hasMoreUserStorageAccountsThanInternalAccounts) {\n const numberOfAccountsToAdd =\n Math.min(userStorageAccountsList.length, maxNumberOfAccountsToAdd) -\n internalAccountsList.length;\n\n // Create new accounts to match the user storage accounts list\n for (let i = 0; i < numberOfAccountsToAdd; i++) {\n await getMessenger().call('KeyringController:addNewAccount');\n onAccountAdded?.();\n }\n }\n\n // Second step: compare account names\n // Get the internal accounts list again since new accounts might have been added in the previous step\n const refreshedInternalAccountsList = await getInternalAccountsList(\n options,\n );\n\n const newlyAddedAccounts = refreshedInternalAccountsList.filter(\n (account) =>\n !internalAccountsList.find((a) => a.address === account.address),\n );\n\n for (const internalAccount of refreshedInternalAccountsList) {\n const userStorageAccount = userStorageAccountsList.find(\n (account) => account.a === internalAccount.address,\n );\n\n // If the account is not present in user storage\n // istanbul ignore next\n if (!userStorageAccount) {\n // If the account was just added in the previous step, skip saving it, it's likely to be a bogus account\n if (newlyAddedAccounts.includes(internalAccount)) {\n onAccountSyncErroneousSituation?.(\n 'An account was added to the internal accounts list but was not present in the user storage accounts list',\n );\n continue;\n }\n // Otherwise, it means that this internal account was present before the sync, and needs to be saved to the user storage\n internalAccountsToBeSavedToUserStorage.push(internalAccount);\n continue;\n }\n\n // From this point on, we know that the account is present in\n // both the internal accounts list and the user storage accounts list\n\n // One or both accounts have default names\n const isInternalAccountNameDefault = isNameDefaultAccountName(\n internalAccount.metadata.name,\n );\n const isUserStorageAccountNameDefault = isNameDefaultAccountName(\n userStorageAccount.n,\n );\n\n // Internal account has default name\n if (isInternalAccountNameDefault) {\n if (!isUserStorageAccountNameDefault) {\n getMessenger().call(\n 'AccountsController:updateAccountMetadata',\n internalAccount.id,\n {\n name: userStorageAccount.n,\n },\n );\n\n onAccountNameUpdated?.();\n }\n continue;\n }\n\n // Internal account has custom name but user storage account has default name\n if (isUserStorageAccountNameDefault) {\n internalAccountsToBeSavedToUserStorage.push(internalAccount);\n continue;\n }\n\n // Both accounts have custom names\n\n // User storage account has a nameLastUpdatedAt timestamp\n // Note: not storing the undefined checks in constants to act as a type guard\n if (userStorageAccount.nlu !== undefined) {\n if (internalAccount.metadata.nameLastUpdatedAt !== undefined) {\n const isInternalAccountNameNewer =\n internalAccount.metadata.nameLastUpdatedAt > userStorageAccount.nlu;\n\n if (isInternalAccountNameNewer) {\n internalAccountsToBeSavedToUserStorage.push(internalAccount);\n continue;\n }\n }\n\n getMessenger().call(\n 'AccountsController:updateAccountMetadata',\n internalAccount.id,\n {\n name: userStorageAccount.n,\n nameLastUpdatedAt: userStorageAccount.nlu,\n },\n );\n\n const areInternalAndUserStorageAccountNamesEqual =\n internalAccount.metadata.name === userStorageAccount.n;\n\n if (!areInternalAndUserStorageAccountNamesEqual) {\n onAccountNameUpdated?.();\n }\n\n continue;\n } else if (internalAccount.metadata.nameLastUpdatedAt !== undefined) {\n internalAccountsToBeSavedToUserStorage.push(internalAccount);\n continue;\n }\n }\n\n // Save the internal accounts list to the user storage\n if (internalAccountsToBeSavedToUserStorage.length) {\n await getUserStorageControllerInstance().performBatchSetStorage(\n USER_STORAGE_FEATURE_NAMES.accounts,\n internalAccountsToBeSavedToUserStorage.map((account) => [\n account.address,\n JSON.stringify(mapInternalAccountToUserStorageAccount(account)),\n ]),\n );\n }\n\n // In case we have corrupted user storage with accounts that don't exist in the internal accounts list\n // Delete those accounts from the user storage\n const userStorageAccountsToBeDeleted = userStorageAccountsList.filter(\n (account) =>\n !refreshedInternalAccountsList.find((a) => a.address === account.a),\n );\n\n if (userStorageAccountsToBeDeleted.length) {\n await getUserStorageControllerInstance().performBatchDeleteStorage(\n USER_STORAGE_FEATURE_NAMES.accounts,\n userStorageAccountsToBeDeleted.map((account) => account.a),\n );\n onAccountSyncErroneousSituation?.(\n 'An account was present in the user storage accounts list but was not found in the internal accounts list after the sync',\n );\n }\n\n // We do this here and not in the finally statement because we want to make sure that\n // the accounts are saved / updated / deleted at least once before we set this flag\n await getUserStorageControllerInstance().setHasAccountSyncingSyncedAtLeastOnce(\n true,\n );\n } catch (e) {\n // istanbul ignore next\n const errorMessage = e instanceof Error ? e.message : JSON.stringify(e);\n throw new Error(\n `UserStorageController - failed to sync user storage accounts list - ${errorMessage}`,\n );\n } finally {\n await getUserStorageControllerInstance().setIsAccountSyncingInProgress(\n false,\n );\n }\n}\n"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { InternalAccount } from "@metamask/keyring-internal-api";
|
|
2
|
+
import type { AccountSyncingConfig, AccountSyncingOptions } from "./types.cjs";
|
|
3
|
+
/**
|
|
4
|
+
* Saves an individual internal account to the user storage.
|
|
5
|
+
* @param internalAccount - The internal account to save
|
|
6
|
+
* @param config - parameters used for saving the internal account
|
|
7
|
+
* @param options - parameters used for saving the internal account
|
|
8
|
+
*/
|
|
9
|
+
export declare function saveInternalAccountToUserStorage(internalAccount: InternalAccount, config: AccountSyncingConfig, options: AccountSyncingOptions): Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* Saves the list of internal accounts to the user storage.
|
|
12
|
+
* @param config - parameters used for saving the list of internal accounts
|
|
13
|
+
* @param options - parameters used for saving the list of internal accounts
|
|
14
|
+
*/
|
|
15
|
+
export declare function saveInternalAccountsListToUserStorage(config: AccountSyncingConfig, options: AccountSyncingOptions): Promise<void>;
|
|
16
|
+
type SyncInternalAccountsWithUserStorageConfig = AccountSyncingConfig & {
|
|
17
|
+
maxNumberOfAccountsToAdd?: number;
|
|
18
|
+
onAccountAdded?: () => void;
|
|
19
|
+
onAccountNameUpdated?: () => void;
|
|
20
|
+
onAccountSyncErroneousSituation?: (errorMessage: string) => void;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Syncs the internal accounts list with the user storage accounts list.
|
|
24
|
+
* This method is used to make sure that the internal accounts list is up-to-date with the user storage accounts list and vice-versa.
|
|
25
|
+
* It will add new accounts to the internal accounts list, update/merge conflicting names and re-upload the results in some cases to the user storage.
|
|
26
|
+
* @param config - parameters used for syncing the internal accounts list with the user storage accounts list
|
|
27
|
+
* @param options - parameters used for syncing the internal accounts list with the user storage accounts list
|
|
28
|
+
*/
|
|
29
|
+
export declare function syncInternalAccountsWithUserStorage(config: SyncInternalAccountsWithUserStorageConfig, options: AccountSyncingOptions): Promise<void>;
|
|
30
|
+
export {};
|
|
31
|
+
//# sourceMappingURL=controller-integration.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"controller-integration.d.cts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/controller-integration.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAQtE,OAAO,KAAK,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,oBAAgB;AAO3E;;;;;GAKG;AACH,wBAAsB,gCAAgC,CACpD,eAAe,EAAE,eAAe,EAChC,MAAM,EAAE,oBAAoB,EAC5B,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CA+Bf;AAED;;;;GAIG;AACH,wBAAsB,qCAAqC,CACzD,MAAM,EAAE,oBAAoB,EAC5B,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CAyBf;AAED,KAAK,yCAAyC,GAAG,oBAAoB,GAAG;IACtE,wBAAwB,CAAC,EAAE,MAAM,CAAC;IAClC,cAAc,CAAC,EAAE,MAAM,IAAI,CAAC;IAC5B,oBAAoB,CAAC,EAAE,MAAM,IAAI,CAAC;IAClC,+BAA+B,CAAC,EAAE,CAAC,YAAY,EAAE,MAAM,KAAK,IAAI,CAAC;CAClE,CAAC;AAEF;;;;;;GAMG;AACH,wBAAsB,mCAAmC,CACvD,MAAM,EAAE,yCAAyC,EACjD,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CA+Mf"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { InternalAccount } from "@metamask/keyring-internal-api";
|
|
2
|
+
import type { AccountSyncingConfig, AccountSyncingOptions } from "./types.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Saves an individual internal account to the user storage.
|
|
5
|
+
* @param internalAccount - The internal account to save
|
|
6
|
+
* @param config - parameters used for saving the internal account
|
|
7
|
+
* @param options - parameters used for saving the internal account
|
|
8
|
+
*/
|
|
9
|
+
export declare function saveInternalAccountToUserStorage(internalAccount: InternalAccount, config: AccountSyncingConfig, options: AccountSyncingOptions): Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* Saves the list of internal accounts to the user storage.
|
|
12
|
+
* @param config - parameters used for saving the list of internal accounts
|
|
13
|
+
* @param options - parameters used for saving the list of internal accounts
|
|
14
|
+
*/
|
|
15
|
+
export declare function saveInternalAccountsListToUserStorage(config: AccountSyncingConfig, options: AccountSyncingOptions): Promise<void>;
|
|
16
|
+
type SyncInternalAccountsWithUserStorageConfig = AccountSyncingConfig & {
|
|
17
|
+
maxNumberOfAccountsToAdd?: number;
|
|
18
|
+
onAccountAdded?: () => void;
|
|
19
|
+
onAccountNameUpdated?: () => void;
|
|
20
|
+
onAccountSyncErroneousSituation?: (errorMessage: string) => void;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Syncs the internal accounts list with the user storage accounts list.
|
|
24
|
+
* This method is used to make sure that the internal accounts list is up-to-date with the user storage accounts list and vice-versa.
|
|
25
|
+
* It will add new accounts to the internal accounts list, update/merge conflicting names and re-upload the results in some cases to the user storage.
|
|
26
|
+
* @param config - parameters used for syncing the internal accounts list with the user storage accounts list
|
|
27
|
+
* @param options - parameters used for syncing the internal accounts list with the user storage accounts list
|
|
28
|
+
*/
|
|
29
|
+
export declare function syncInternalAccountsWithUserStorage(config: SyncInternalAccountsWithUserStorageConfig, options: AccountSyncingOptions): Promise<void>;
|
|
30
|
+
export {};
|
|
31
|
+
//# sourceMappingURL=controller-integration.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"controller-integration.d.mts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/controller-integration.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAQtE,OAAO,KAAK,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,oBAAgB;AAO3E;;;;;GAKG;AACH,wBAAsB,gCAAgC,CACpD,eAAe,EAAE,eAAe,EAChC,MAAM,EAAE,oBAAoB,EAC5B,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CA+Bf;AAED;;;;GAIG;AACH,wBAAsB,qCAAqC,CACzD,MAAM,EAAE,oBAAoB,EAC5B,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CAyBf;AAED,KAAK,yCAAyC,GAAG,oBAAoB,GAAG;IACtE,wBAAwB,CAAC,EAAE,MAAM,CAAC;IAClC,cAAc,CAAC,EAAE,MAAM,IAAI,CAAC;IAC5B,oBAAoB,CAAC,EAAE,MAAM,IAAI,CAAC;IAClC,+BAA+B,CAAC,EAAE,CAAC,YAAY,EAAE,MAAM,KAAK,IAAI,CAAC;CAClE,CAAC;AAEF;;;;;;GAMG;AACH,wBAAsB,mCAAmC,CACvD,MAAM,EAAE,yCAAyC,EACjD,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,IAAI,CAAC,CA+Mf"}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { isEvmAccountType } from "@metamask/keyring-api";
|
|
2
|
+
import { USER_STORAGE_FEATURE_NAMES } from "../../../shared/storage-schema.mjs";
|
|
3
|
+
import { canPerformAccountSyncing, getInternalAccountsList, getUserStorageAccountsList } from "./sync-utils.mjs";
|
|
4
|
+
import { doesInternalAccountHaveCorrectKeyringType, isNameDefaultAccountName, mapInternalAccountToUserStorageAccount } from "./utils.mjs";
|
|
5
|
+
/**
|
|
6
|
+
* Saves an individual internal account to the user storage.
|
|
7
|
+
* @param internalAccount - The internal account to save
|
|
8
|
+
* @param config - parameters used for saving the internal account
|
|
9
|
+
* @param options - parameters used for saving the internal account
|
|
10
|
+
*/
|
|
11
|
+
export async function saveInternalAccountToUserStorage(internalAccount, config, options) {
|
|
12
|
+
const { isAccountSyncingEnabled } = config;
|
|
13
|
+
const { getUserStorageControllerInstance } = options;
|
|
14
|
+
if (!isAccountSyncingEnabled ||
|
|
15
|
+
!canPerformAccountSyncing(config, options) ||
|
|
16
|
+
!isEvmAccountType(internalAccount.type) ||
|
|
17
|
+
!doesInternalAccountHaveCorrectKeyringType(internalAccount)) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
// Map the internal account to the user storage account schema
|
|
22
|
+
const mappedAccount = mapInternalAccountToUserStorageAccount(internalAccount);
|
|
23
|
+
await getUserStorageControllerInstance().performSetStorage(
|
|
24
|
+
// ESLint is confused here.
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
26
|
+
`${USER_STORAGE_FEATURE_NAMES.accounts}.${internalAccount.address}`, JSON.stringify(mappedAccount));
|
|
27
|
+
}
|
|
28
|
+
catch (e) {
|
|
29
|
+
// istanbul ignore next
|
|
30
|
+
const errorMessage = e instanceof Error ? e.message : JSON.stringify(e);
|
|
31
|
+
throw new Error(`UserStorageController - failed to save account to user storage - ${errorMessage}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Saves the list of internal accounts to the user storage.
|
|
36
|
+
* @param config - parameters used for saving the list of internal accounts
|
|
37
|
+
* @param options - parameters used for saving the list of internal accounts
|
|
38
|
+
*/
|
|
39
|
+
export async function saveInternalAccountsListToUserStorage(config, options) {
|
|
40
|
+
const { isAccountSyncingEnabled } = config;
|
|
41
|
+
const { getUserStorageControllerInstance } = options;
|
|
42
|
+
if (!isAccountSyncingEnabled) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const internalAccountsList = await getInternalAccountsList(options);
|
|
46
|
+
if (!internalAccountsList?.length) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const internalAccountsListFormattedForUserStorage = internalAccountsList.map(mapInternalAccountToUserStorageAccount);
|
|
50
|
+
await getUserStorageControllerInstance().performBatchSetStorage(USER_STORAGE_FEATURE_NAMES.accounts, internalAccountsListFormattedForUserStorage.map((account) => [
|
|
51
|
+
account.a,
|
|
52
|
+
JSON.stringify(account),
|
|
53
|
+
]));
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Syncs the internal accounts list with the user storage accounts list.
|
|
57
|
+
* This method is used to make sure that the internal accounts list is up-to-date with the user storage accounts list and vice-versa.
|
|
58
|
+
* It will add new accounts to the internal accounts list, update/merge conflicting names and re-upload the results in some cases to the user storage.
|
|
59
|
+
* @param config - parameters used for syncing the internal accounts list with the user storage accounts list
|
|
60
|
+
* @param options - parameters used for syncing the internal accounts list with the user storage accounts list
|
|
61
|
+
*/
|
|
62
|
+
export async function syncInternalAccountsWithUserStorage(config, options) {
|
|
63
|
+
const { isAccountSyncingEnabled } = config;
|
|
64
|
+
if (!canPerformAccountSyncing(config, options) || !isAccountSyncingEnabled) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const { maxNumberOfAccountsToAdd = 100, onAccountAdded, onAccountNameUpdated, onAccountSyncErroneousSituation, } = config;
|
|
68
|
+
const { getMessenger, getUserStorageControllerInstance } = options;
|
|
69
|
+
try {
|
|
70
|
+
await getUserStorageControllerInstance().setIsAccountSyncingInProgress(true);
|
|
71
|
+
const userStorageAccountsList = await getUserStorageAccountsList(options);
|
|
72
|
+
if (!userStorageAccountsList || !userStorageAccountsList.length) {
|
|
73
|
+
await saveInternalAccountsListToUserStorage({ isAccountSyncingEnabled }, options);
|
|
74
|
+
await getUserStorageControllerInstance().setHasAccountSyncingSyncedAtLeastOnce(true);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
// Prepare an array of internal accounts to be saved to the user storage
|
|
78
|
+
const internalAccountsToBeSavedToUserStorage = [];
|
|
79
|
+
// Compare internal accounts list with user storage accounts list
|
|
80
|
+
// First step: compare lengths
|
|
81
|
+
const internalAccountsList = await getInternalAccountsList(options);
|
|
82
|
+
if (!internalAccountsList || !internalAccountsList.length) {
|
|
83
|
+
throw new Error(`Failed to get internal accounts list`);
|
|
84
|
+
}
|
|
85
|
+
const hasMoreUserStorageAccountsThanInternalAccounts = userStorageAccountsList.length > internalAccountsList.length;
|
|
86
|
+
// We don't want to remove existing accounts for a user
|
|
87
|
+
// so we only add new accounts if the user has more accounts in user storage than internal accounts
|
|
88
|
+
if (hasMoreUserStorageAccountsThanInternalAccounts) {
|
|
89
|
+
const numberOfAccountsToAdd = Math.min(userStorageAccountsList.length, maxNumberOfAccountsToAdd) -
|
|
90
|
+
internalAccountsList.length;
|
|
91
|
+
// Create new accounts to match the user storage accounts list
|
|
92
|
+
for (let i = 0; i < numberOfAccountsToAdd; i++) {
|
|
93
|
+
await getMessenger().call('KeyringController:addNewAccount');
|
|
94
|
+
onAccountAdded?.();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// Second step: compare account names
|
|
98
|
+
// Get the internal accounts list again since new accounts might have been added in the previous step
|
|
99
|
+
const refreshedInternalAccountsList = await getInternalAccountsList(options);
|
|
100
|
+
const newlyAddedAccounts = refreshedInternalAccountsList.filter((account) => !internalAccountsList.find((a) => a.address === account.address));
|
|
101
|
+
for (const internalAccount of refreshedInternalAccountsList) {
|
|
102
|
+
const userStorageAccount = userStorageAccountsList.find((account) => account.a === internalAccount.address);
|
|
103
|
+
// If the account is not present in user storage
|
|
104
|
+
// istanbul ignore next
|
|
105
|
+
if (!userStorageAccount) {
|
|
106
|
+
// If the account was just added in the previous step, skip saving it, it's likely to be a bogus account
|
|
107
|
+
if (newlyAddedAccounts.includes(internalAccount)) {
|
|
108
|
+
onAccountSyncErroneousSituation?.('An account was added to the internal accounts list but was not present in the user storage accounts list');
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
// Otherwise, it means that this internal account was present before the sync, and needs to be saved to the user storage
|
|
112
|
+
internalAccountsToBeSavedToUserStorage.push(internalAccount);
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
// From this point on, we know that the account is present in
|
|
116
|
+
// both the internal accounts list and the user storage accounts list
|
|
117
|
+
// One or both accounts have default names
|
|
118
|
+
const isInternalAccountNameDefault = isNameDefaultAccountName(internalAccount.metadata.name);
|
|
119
|
+
const isUserStorageAccountNameDefault = isNameDefaultAccountName(userStorageAccount.n);
|
|
120
|
+
// Internal account has default name
|
|
121
|
+
if (isInternalAccountNameDefault) {
|
|
122
|
+
if (!isUserStorageAccountNameDefault) {
|
|
123
|
+
getMessenger().call('AccountsController:updateAccountMetadata', internalAccount.id, {
|
|
124
|
+
name: userStorageAccount.n,
|
|
125
|
+
});
|
|
126
|
+
onAccountNameUpdated?.();
|
|
127
|
+
}
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
// Internal account has custom name but user storage account has default name
|
|
131
|
+
if (isUserStorageAccountNameDefault) {
|
|
132
|
+
internalAccountsToBeSavedToUserStorage.push(internalAccount);
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
// Both accounts have custom names
|
|
136
|
+
// User storage account has a nameLastUpdatedAt timestamp
|
|
137
|
+
// Note: not storing the undefined checks in constants to act as a type guard
|
|
138
|
+
if (userStorageAccount.nlu !== undefined) {
|
|
139
|
+
if (internalAccount.metadata.nameLastUpdatedAt !== undefined) {
|
|
140
|
+
const isInternalAccountNameNewer = internalAccount.metadata.nameLastUpdatedAt > userStorageAccount.nlu;
|
|
141
|
+
if (isInternalAccountNameNewer) {
|
|
142
|
+
internalAccountsToBeSavedToUserStorage.push(internalAccount);
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
getMessenger().call('AccountsController:updateAccountMetadata', internalAccount.id, {
|
|
147
|
+
name: userStorageAccount.n,
|
|
148
|
+
nameLastUpdatedAt: userStorageAccount.nlu,
|
|
149
|
+
});
|
|
150
|
+
const areInternalAndUserStorageAccountNamesEqual = internalAccount.metadata.name === userStorageAccount.n;
|
|
151
|
+
if (!areInternalAndUserStorageAccountNamesEqual) {
|
|
152
|
+
onAccountNameUpdated?.();
|
|
153
|
+
}
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
else if (internalAccount.metadata.nameLastUpdatedAt !== undefined) {
|
|
157
|
+
internalAccountsToBeSavedToUserStorage.push(internalAccount);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
// Save the internal accounts list to the user storage
|
|
162
|
+
if (internalAccountsToBeSavedToUserStorage.length) {
|
|
163
|
+
await getUserStorageControllerInstance().performBatchSetStorage(USER_STORAGE_FEATURE_NAMES.accounts, internalAccountsToBeSavedToUserStorage.map((account) => [
|
|
164
|
+
account.address,
|
|
165
|
+
JSON.stringify(mapInternalAccountToUserStorageAccount(account)),
|
|
166
|
+
]));
|
|
167
|
+
}
|
|
168
|
+
// In case we have corrupted user storage with accounts that don't exist in the internal accounts list
|
|
169
|
+
// Delete those accounts from the user storage
|
|
170
|
+
const userStorageAccountsToBeDeleted = userStorageAccountsList.filter((account) => !refreshedInternalAccountsList.find((a) => a.address === account.a));
|
|
171
|
+
if (userStorageAccountsToBeDeleted.length) {
|
|
172
|
+
await getUserStorageControllerInstance().performBatchDeleteStorage(USER_STORAGE_FEATURE_NAMES.accounts, userStorageAccountsToBeDeleted.map((account) => account.a));
|
|
173
|
+
onAccountSyncErroneousSituation?.('An account was present in the user storage accounts list but was not found in the internal accounts list after the sync');
|
|
174
|
+
}
|
|
175
|
+
// We do this here and not in the finally statement because we want to make sure that
|
|
176
|
+
// the accounts are saved / updated / deleted at least once before we set this flag
|
|
177
|
+
await getUserStorageControllerInstance().setHasAccountSyncingSyncedAtLeastOnce(true);
|
|
178
|
+
}
|
|
179
|
+
catch (e) {
|
|
180
|
+
// istanbul ignore next
|
|
181
|
+
const errorMessage = e instanceof Error ? e.message : JSON.stringify(e);
|
|
182
|
+
throw new Error(`UserStorageController - failed to sync user storage accounts list - ${errorMessage}`);
|
|
183
|
+
}
|
|
184
|
+
finally {
|
|
185
|
+
await getUserStorageControllerInstance().setIsAccountSyncingInProgress(false);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
//# sourceMappingURL=controller-integration.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"controller-integration.mjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/controller-integration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,8BAA8B;AAGzD,OAAO,EAAE,0BAA0B,EAAE,2CAAuC;AAC5E,OAAO,EACL,wBAAwB,EACxB,uBAAuB,EACvB,0BAA0B,EAC3B,yBAAqB;AAEtB,OAAO,EACL,yCAAyC,EACzC,wBAAwB,EACxB,sCAAsC,EACvC,oBAAgB;AAEjB;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,gCAAgC,CACpD,eAAgC,EAChC,MAA4B,EAC5B,OAA8B;IAE9B,MAAM,EAAE,uBAAuB,EAAE,GAAG,MAAM,CAAC;IAC3C,MAAM,EAAE,gCA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C,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;QACxE,MAAM,IAAI,KAAK,CACb,uEAAuE,YAAY,EAAE,CACtF,CAAC;KACH;YAAS;QACR,MAAM,gCAAgC,EAAE,CAAC,6BAA6B,CACpE,KAAK,CACN,CAAC;KACH;AACH,CAAC","sourcesContent":["import { isEvmAccountType } from '@metamask/keyring-api';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport { USER_STORAGE_FEATURE_NAMES } from '../../../shared/storage-schema';\nimport {\n canPerformAccountSyncing,\n getInternalAccountsList,\n getUserStorageAccountsList,\n} from './sync-utils';\nimport type { AccountSyncingConfig, AccountSyncingOptions } from './types';\nimport {\n doesInternalAccountHaveCorrectKeyringType,\n isNameDefaultAccountName,\n mapInternalAccountToUserStorageAccount,\n} from './utils';\n\n/**\n * Saves an individual internal account to the user storage.\n * @param internalAccount - The internal account to save\n * @param config - parameters used for saving the internal account\n * @param options - parameters used for saving the internal account\n */\nexport async function saveInternalAccountToUserStorage(\n internalAccount: InternalAccount,\n config: AccountSyncingConfig,\n options: AccountSyncingOptions,\n): Promise<void> {\n const { isAccountSyncingEnabled } = config;\n const { getUserStorageControllerInstance } = options;\n\n if (\n !isAccountSyncingEnabled ||\n !canPerformAccountSyncing(config, options) ||\n !isEvmAccountType(internalAccount.type) ||\n !doesInternalAccountHaveCorrectKeyringType(internalAccount)\n ) {\n return;\n }\n\n try {\n // Map the internal account to the user storage account schema\n const mappedAccount =\n mapInternalAccountToUserStorageAccount(internalAccount);\n\n await getUserStorageControllerInstance().performSetStorage(\n // ESLint is confused here.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `${USER_STORAGE_FEATURE_NAMES.accounts}.${internalAccount.address}`,\n JSON.stringify(mappedAccount),\n );\n } catch (e) {\n // istanbul ignore next\n const errorMessage = e instanceof Error ? e.message : JSON.stringify(e);\n throw new Error(\n `UserStorageController - failed to save account to user storage - ${errorMessage}`,\n );\n }\n}\n\n/**\n * Saves the list of internal accounts to the user storage.\n * @param config - parameters used for saving the list of internal accounts\n * @param options - parameters used for saving the list of internal accounts\n */\nexport async function saveInternalAccountsListToUserStorage(\n config: AccountSyncingConfig,\n options: AccountSyncingOptions,\n): Promise<void> {\n const { isAccountSyncingEnabled } = config;\n const { getUserStorageControllerInstance } = options;\n\n if (!isAccountSyncingEnabled) {\n return;\n }\n\n const internalAccountsList = await getInternalAccountsList(options);\n\n if (!internalAccountsList?.length) {\n return;\n }\n\n const internalAccountsListFormattedForUserStorage = internalAccountsList.map(\n mapInternalAccountToUserStorageAccount,\n );\n\n await getUserStorageControllerInstance().performBatchSetStorage(\n USER_STORAGE_FEATURE_NAMES.accounts,\n internalAccountsListFormattedForUserStorage.map((account) => [\n account.a,\n JSON.stringify(account),\n ]),\n );\n}\n\ntype SyncInternalAccountsWithUserStorageConfig = AccountSyncingConfig & {\n maxNumberOfAccountsToAdd?: number;\n onAccountAdded?: () => void;\n onAccountNameUpdated?: () => void;\n onAccountSyncErroneousSituation?: (errorMessage: string) => void;\n};\n\n/**\n * Syncs the internal accounts list with the user storage accounts list.\n * This method is used to make sure that the internal accounts list is up-to-date with the user storage accounts list and vice-versa.\n * It will add new accounts to the internal accounts list, update/merge conflicting names and re-upload the results in some cases to the user storage.\n * @param config - parameters used for syncing the internal accounts list with the user storage accounts list\n * @param options - parameters used for syncing the internal accounts list with the user storage accounts list\n */\nexport async function syncInternalAccountsWithUserStorage(\n config: SyncInternalAccountsWithUserStorageConfig,\n options: AccountSyncingOptions,\n): Promise<void> {\n const { isAccountSyncingEnabled } = config;\n\n if (!canPerformAccountSyncing(config, options) || !isAccountSyncingEnabled) {\n return;\n }\n\n const {\n maxNumberOfAccountsToAdd = 100,\n onAccountAdded,\n onAccountNameUpdated,\n onAccountSyncErroneousSituation,\n } = config;\n const { getMessenger, getUserStorageControllerInstance } = options;\n\n try {\n await getUserStorageControllerInstance().setIsAccountSyncingInProgress(\n true,\n );\n\n const userStorageAccountsList = await getUserStorageAccountsList(options);\n\n if (!userStorageAccountsList || !userStorageAccountsList.length) {\n await saveInternalAccountsListToUserStorage(\n { isAccountSyncingEnabled },\n options,\n );\n await getUserStorageControllerInstance().setHasAccountSyncingSyncedAtLeastOnce(\n true,\n );\n return;\n }\n\n // Prepare an array of internal accounts to be saved to the user storage\n const internalAccountsToBeSavedToUserStorage: InternalAccount[] = [];\n\n // Compare internal accounts list with user storage accounts list\n // First step: compare lengths\n const internalAccountsList = await getInternalAccountsList(options);\n\n if (!internalAccountsList || !internalAccountsList.length) {\n throw new Error(`Failed to get internal accounts list`);\n }\n\n const hasMoreUserStorageAccountsThanInternalAccounts =\n userStorageAccountsList.length > internalAccountsList.length;\n\n // We don't want to remove existing accounts for a user\n // so we only add new accounts if the user has more accounts in user storage than internal accounts\n if (hasMoreUserStorageAccountsThanInternalAccounts) {\n const numberOfAccountsToAdd =\n Math.min(userStorageAccountsList.length, maxNumberOfAccountsToAdd) -\n internalAccountsList.length;\n\n // Create new accounts to match the user storage accounts list\n for (let i = 0; i < numberOfAccountsToAdd; i++) {\n await getMessenger().call('KeyringController:addNewAccount');\n onAccountAdded?.();\n }\n }\n\n // Second step: compare account names\n // Get the internal accounts list again since new accounts might have been added in the previous step\n const refreshedInternalAccountsList = await getInternalAccountsList(\n options,\n );\n\n const newlyAddedAccounts = refreshedInternalAccountsList.filter(\n (account) =>\n !internalAccountsList.find((a) => a.address === account.address),\n );\n\n for (const internalAccount of refreshedInternalAccountsList) {\n const userStorageAccount = userStorageAccountsList.find(\n (account) => account.a === internalAccount.address,\n );\n\n // If the account is not present in user storage\n // istanbul ignore next\n if (!userStorageAccount) {\n // If the account was just added in the previous step, skip saving it, it's likely to be a bogus account\n if (newlyAddedAccounts.includes(internalAccount)) {\n onAccountSyncErroneousSituation?.(\n 'An account was added to the internal accounts list but was not present in the user storage accounts list',\n );\n continue;\n }\n // Otherwise, it means that this internal account was present before the sync, and needs to be saved to the user storage\n internalAccountsToBeSavedToUserStorage.push(internalAccount);\n continue;\n }\n\n // From this point on, we know that the account is present in\n // both the internal accounts list and the user storage accounts list\n\n // One or both accounts have default names\n const isInternalAccountNameDefault = isNameDefaultAccountName(\n internalAccount.metadata.name,\n );\n const isUserStorageAccountNameDefault = isNameDefaultAccountName(\n userStorageAccount.n,\n );\n\n // Internal account has default name\n if (isInternalAccountNameDefault) {\n if (!isUserStorageAccountNameDefault) {\n getMessenger().call(\n 'AccountsController:updateAccountMetadata',\n internalAccount.id,\n {\n name: userStorageAccount.n,\n },\n );\n\n onAccountNameUpdated?.();\n }\n continue;\n }\n\n // Internal account has custom name but user storage account has default name\n if (isUserStorageAccountNameDefault) {\n internalAccountsToBeSavedToUserStorage.push(internalAccount);\n continue;\n }\n\n // Both accounts have custom names\n\n // User storage account has a nameLastUpdatedAt timestamp\n // Note: not storing the undefined checks in constants to act as a type guard\n if (userStorageAccount.nlu !== undefined) {\n if (internalAccount.metadata.nameLastUpdatedAt !== undefined) {\n const isInternalAccountNameNewer =\n internalAccount.metadata.nameLastUpdatedAt > userStorageAccount.nlu;\n\n if (isInternalAccountNameNewer) {\n internalAccountsToBeSavedToUserStorage.push(internalAccount);\n continue;\n }\n }\n\n getMessenger().call(\n 'AccountsController:updateAccountMetadata',\n internalAccount.id,\n {\n name: userStorageAccount.n,\n nameLastUpdatedAt: userStorageAccount.nlu,\n },\n );\n\n const areInternalAndUserStorageAccountNamesEqual =\n internalAccount.metadata.name === userStorageAccount.n;\n\n if (!areInternalAndUserStorageAccountNamesEqual) {\n onAccountNameUpdated?.();\n }\n\n continue;\n } else if (internalAccount.metadata.nameLastUpdatedAt !== undefined) {\n internalAccountsToBeSavedToUserStorage.push(internalAccount);\n continue;\n }\n }\n\n // Save the internal accounts list to the user storage\n if (internalAccountsToBeSavedToUserStorage.length) {\n await getUserStorageControllerInstance().performBatchSetStorage(\n USER_STORAGE_FEATURE_NAMES.accounts,\n internalAccountsToBeSavedToUserStorage.map((account) => [\n account.address,\n JSON.stringify(mapInternalAccountToUserStorageAccount(account)),\n ]),\n );\n }\n\n // In case we have corrupted user storage with accounts that don't exist in the internal accounts list\n // Delete those accounts from the user storage\n const userStorageAccountsToBeDeleted = userStorageAccountsList.filter(\n (account) =>\n !refreshedInternalAccountsList.find((a) => a.address === account.a),\n );\n\n if (userStorageAccountsToBeDeleted.length) {\n await getUserStorageControllerInstance().performBatchDeleteStorage(\n USER_STORAGE_FEATURE_NAMES.accounts,\n userStorageAccountsToBeDeleted.map((account) => account.a),\n );\n onAccountSyncErroneousSituation?.(\n 'An account was present in the user storage accounts list but was not found in the internal accounts list after the sync',\n );\n }\n\n // We do this here and not in the finally statement because we want to make sure that\n // the accounts are saved / updated / deleted at least once before we set this flag\n await getUserStorageControllerInstance().setHasAccountSyncingSyncedAtLeastOnce(\n true,\n );\n } catch (e) {\n // istanbul ignore next\n const errorMessage = e instanceof Error ? e.message : JSON.stringify(e);\n throw new Error(\n `UserStorageController - failed to sync user storage accounts list - ${errorMessage}`,\n );\n } finally {\n await getUserStorageControllerInstance().setIsAccountSyncingInProgress(\n false,\n );\n }\n}\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setupAccountSyncingSubscriptions = void 0;
|
|
4
|
+
const controller_integration_1 = require("./controller-integration.cjs");
|
|
5
|
+
const sync_utils_1 = require("./sync-utils.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Initialize and setup events to listen to for account syncing
|
|
8
|
+
* @param config - configuration parameters
|
|
9
|
+
* @param options - parameters used for initializing and enabling account syncing
|
|
10
|
+
*/
|
|
11
|
+
function setupAccountSyncingSubscriptions(config, options) {
|
|
12
|
+
const { getMessenger, getUserStorageControllerInstance } = options;
|
|
13
|
+
getMessenger().subscribe('AccountsController:accountAdded',
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
15
|
+
async (account) => {
|
|
16
|
+
if (!(0, sync_utils_1.canPerformAccountSyncing)(config, options) ||
|
|
17
|
+
!getUserStorageControllerInstance().state
|
|
18
|
+
.hasAccountSyncingSyncedAtLeastOnce) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
await (0, controller_integration_1.saveInternalAccountToUserStorage)(account, config, options);
|
|
22
|
+
});
|
|
23
|
+
getMessenger().subscribe('AccountsController:accountRenamed',
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
25
|
+
async (account) => {
|
|
26
|
+
if (!(0, sync_utils_1.canPerformAccountSyncing)(config, options) ||
|
|
27
|
+
!getUserStorageControllerInstance().state
|
|
28
|
+
.hasAccountSyncingSyncedAtLeastOnce) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
await (0, controller_integration_1.saveInternalAccountToUserStorage)(account, config, options);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
exports.setupAccountSyncingSubscriptions = setupAccountSyncingSubscriptions;
|
|
35
|
+
//# sourceMappingURL=setup-subscriptions.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setup-subscriptions.cjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/setup-subscriptions.ts"],"names":[],"mappings":";;;AAAA,yEAA4E;AAC5E,iDAAwD;AAGxD;;;;GAIG;AACH,SAAgB,gCAAgC,CAC9C,MAA4B,EAC5B,OAA8B;IAE9B,MAAM,EAAE,YAAY,EAAE,gCAAgC,EAAE,GAAG,OAAO,CAAC;IAEnE,YAAY,EAAE,CAAC,SAAS,CACtB,iCAAiC;IACjC,kEAAkE;IAClE,KAAK,EAAE,OAAO,EAAE,EAAE;QAChB,IACE,CAAC,IAAA,qCAAwB,EAAC,MAAM,EAAE,OAAO,CAAC;YAC1C,CAAC,gCAAgC,EAAE,CAAC,KAAK;iBACtC,kCAAkC,EACrC;YACA,OAAO;SACR;QAED,MAAM,IAAA,yDAAgC,EAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IACnE,CAAC,CACF,CAAC;IAEF,YAAY,EAAE,CAAC,SAAS,CACtB,mCAAmC;IACnC,kEAAkE;IAClE,KAAK,EAAE,OAAO,EAAE,EAAE;QAChB,IACE,CAAC,IAAA,qCAAwB,EAAC,MAAM,EAAE,OAAO,CAAC;YAC1C,CAAC,gCAAgC,EAAE,CAAC,KAAK;iBACtC,kCAAkC,EACrC;YACA,OAAO;SACR;QAED,MAAM,IAAA,yDAAgC,EAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IACnE,CAAC,CACF,CAAC;AACJ,CAAC;AArCD,4EAqCC","sourcesContent":["import { saveInternalAccountToUserStorage } from './controller-integration';\nimport { canPerformAccountSyncing } from './sync-utils';\nimport type { AccountSyncingConfig, AccountSyncingOptions } from './types';\n\n/**\n * Initialize and setup events to listen to for account syncing\n * @param config - configuration parameters\n * @param options - parameters used for initializing and enabling account syncing\n */\nexport function setupAccountSyncingSubscriptions(\n config: AccountSyncingConfig,\n options: AccountSyncingOptions,\n) {\n const { getMessenger, getUserStorageControllerInstance } = options;\n\n getMessenger().subscribe(\n 'AccountsController:accountAdded',\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (account) => {\n if (\n !canPerformAccountSyncing(config, options) ||\n !getUserStorageControllerInstance().state\n .hasAccountSyncingSyncedAtLeastOnce\n ) {\n return;\n }\n\n await saveInternalAccountToUserStorage(account, config, options);\n },\n );\n\n getMessenger().subscribe(\n 'AccountsController:accountRenamed',\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (account) => {\n if (\n !canPerformAccountSyncing(config, options) ||\n !getUserStorageControllerInstance().state\n .hasAccountSyncingSyncedAtLeastOnce\n ) {\n return;\n }\n\n await saveInternalAccountToUserStorage(account, config, options);\n },\n );\n}\n"]}
|