@metamask-previews/profile-sync-controller 3.1.1-preview-209dbcc → 3.1.1-preview-e218ba33
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/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 +6 -1
- package/dist/controllers/user-storage/UserStorageController.d.cts.map +1 -1
- package/dist/controllers/user-storage/UserStorageController.d.mts +6 -1
- package/dist/controllers/user-storage/UserStorageController.d.mts.map +1 -1
- package/dist/controllers/user-storage/UserStorageController.mjs +36 -215
- package/dist/controllers/user-storage/UserStorageController.mjs.map +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 +2 -2
- 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/{accounts/user-storage.d.cts → account-syncing/utils.d.cts} +8 -17
- package/dist/controllers/user-storage/account-syncing/utils.d.cts.map +1 -0
- package/dist/controllers/user-storage/{accounts/user-storage.d.mts → account-syncing/utils.d.mts} +8 -17
- 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/package.json +1 -1
- 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.map +0 -1
- 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,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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C,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,CAC3D,CAAC;YACF,+BAA+B,EAAE,CAC/B,yHAAyH,CAC1H,CAAC;SACH;QAED,qFAAqF;QACrF,mFAAmF;QACnF,MAAM,gCAAgC,EAAE,CAAC,qCAAqC,CAC5E,IAAI,CACL,CAAC;KACH;IAAC,OAAO,CAAC,EAAE;QACV,uBAAuB;QACvB,MAAM,YAAY,GAAG,CAAC,YAAY,KAAK,CAAC,CAAC,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","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"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AccountSyncingConfig, AccountSyncingOptions } from "./types.cjs";
|
|
2
|
+
/**
|
|
3
|
+
* Initialize and setup events to listen to for account syncing
|
|
4
|
+
* @param config - configuration parameters
|
|
5
|
+
* @param options - parameters used for initializing and enabling account syncing
|
|
6
|
+
*/
|
|
7
|
+
export declare function setupAccountSyncingSubscriptions(config: AccountSyncingConfig, options: AccountSyncingOptions): void;
|
|
8
|
+
//# sourceMappingURL=setup-subscriptions.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setup-subscriptions.d.cts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/setup-subscriptions.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,oBAAgB;AAE3E;;;;GAIG;AACH,wBAAgB,gCAAgC,CAC9C,MAAM,EAAE,oBAAoB,EAC5B,OAAO,EAAE,qBAAqB,QAmC/B"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AccountSyncingConfig, AccountSyncingOptions } from "./types.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Initialize and setup events to listen to for account syncing
|
|
4
|
+
* @param config - configuration parameters
|
|
5
|
+
* @param options - parameters used for initializing and enabling account syncing
|
|
6
|
+
*/
|
|
7
|
+
export declare function setupAccountSyncingSubscriptions(config: AccountSyncingConfig, options: AccountSyncingOptions): void;
|
|
8
|
+
//# sourceMappingURL=setup-subscriptions.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setup-subscriptions.d.mts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/setup-subscriptions.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,oBAAgB;AAE3E;;;;GAIG;AACH,wBAAgB,gCAAgC,CAC9C,MAAM,EAAE,oBAAoB,EAC5B,OAAO,EAAE,qBAAqB,QAmC/B"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { saveInternalAccountToUserStorage } from "./controller-integration.mjs";
|
|
2
|
+
import { canPerformAccountSyncing } from "./sync-utils.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Initialize and setup events to listen to for account syncing
|
|
5
|
+
* @param config - configuration parameters
|
|
6
|
+
* @param options - parameters used for initializing and enabling account syncing
|
|
7
|
+
*/
|
|
8
|
+
export function setupAccountSyncingSubscriptions(config, options) {
|
|
9
|
+
const { getMessenger, getUserStorageControllerInstance } = options;
|
|
10
|
+
getMessenger().subscribe('AccountsController:accountAdded',
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
12
|
+
async (account) => {
|
|
13
|
+
if (!canPerformAccountSyncing(config, options) ||
|
|
14
|
+
!getUserStorageControllerInstance().state
|
|
15
|
+
.hasAccountSyncingSyncedAtLeastOnce) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
await saveInternalAccountToUserStorage(account, config, options);
|
|
19
|
+
});
|
|
20
|
+
getMessenger().subscribe('AccountsController:accountRenamed',
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
22
|
+
async (account) => {
|
|
23
|
+
if (!canPerformAccountSyncing(config, options) ||
|
|
24
|
+
!getUserStorageControllerInstance().state
|
|
25
|
+
.hasAccountSyncingSyncedAtLeastOnce) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
await saveInternalAccountToUserStorage(account, config, options);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=setup-subscriptions.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setup-subscriptions.mjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/setup-subscriptions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gCAAgC,EAAE,qCAAiC;AAC5E,OAAO,EAAE,wBAAwB,EAAE,yBAAqB;AAGxD;;;;GAIG;AACH,MAAM,UAAU,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,wBAAwB,CAAC,MAAM,EAAE,OAAO,CAAC;YAC1C,CAAC,gCAAgC,EAAE,CAAC,KAAK;iBACtC,kCAAkC,EACrC;YACA,OAAO;SACR;QAED,MAAM,gCAAgC,CAAC,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,wBAAwB,CAAC,MAAM,EAAE,OAAO,CAAC;YAC1C,CAAC,gCAAgC,EAAE,CAAC,KAAK;iBACtC,kCAAkC,EACrC;YACA,OAAO;SACR;QAED,MAAM,gCAAgC,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IACnE,CAAC,CACF,CAAC;AACJ,CAAC","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"]}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getUserStorageAccountsList = exports.getInternalAccountsList = exports.canPerformAccountSyncing = void 0;
|
|
4
|
+
const storage_schema_1 = require("../../../shared/storage-schema.cjs");
|
|
5
|
+
const utils_1 = require("./utils.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Checks if account syncing can be performed based on a set of conditions
|
|
8
|
+
* @param config - configuration parameters
|
|
9
|
+
* @param options - parameters used for checking if account syncing can be performed
|
|
10
|
+
* @returns Returns true if account syncing can be performed, false otherwise.
|
|
11
|
+
*/
|
|
12
|
+
function canPerformAccountSyncing(config, options) {
|
|
13
|
+
const { isAccountSyncingEnabled } = config;
|
|
14
|
+
const { getMessenger, getUserStorageControllerInstance } = options;
|
|
15
|
+
const { isProfileSyncingEnabled, isAccountSyncingInProgress } = getUserStorageControllerInstance().state;
|
|
16
|
+
const isAuthEnabled = getMessenger().call('AuthenticationController:isSignedIn');
|
|
17
|
+
if (!isProfileSyncingEnabled ||
|
|
18
|
+
!isAuthEnabled ||
|
|
19
|
+
!isAccountSyncingEnabled ||
|
|
20
|
+
isAccountSyncingInProgress) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
exports.canPerformAccountSyncing = canPerformAccountSyncing;
|
|
26
|
+
/**
|
|
27
|
+
* Get the list of internal accounts
|
|
28
|
+
* @param options - parameters used for getting the list of internal accounts
|
|
29
|
+
*/
|
|
30
|
+
async function getInternalAccountsList(options) {
|
|
31
|
+
const { getMessenger } = options;
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/await-thenable
|
|
33
|
+
const internalAccountsList = await getMessenger().call('AccountsController:listAccounts');
|
|
34
|
+
return internalAccountsList?.filter(utils_1.doesInternalAccountHaveCorrectKeyringType);
|
|
35
|
+
}
|
|
36
|
+
exports.getInternalAccountsList = getInternalAccountsList;
|
|
37
|
+
/**
|
|
38
|
+
* Get the list of user storage accounts
|
|
39
|
+
* @param options - parameters used for getting the list of user storage accounts
|
|
40
|
+
*/
|
|
41
|
+
async function getUserStorageAccountsList(options) {
|
|
42
|
+
const { getUserStorageControllerInstance } = options;
|
|
43
|
+
const rawAccountsListResponse = await getUserStorageControllerInstance().performGetStorageAllFeatureEntries(storage_schema_1.USER_STORAGE_FEATURE_NAMES.accounts);
|
|
44
|
+
return (rawAccountsListResponse?.map((rawAccount) => JSON.parse(rawAccount)) ?? null);
|
|
45
|
+
}
|
|
46
|
+
exports.getUserStorageAccountsList = getUserStorageAccountsList;
|
|
47
|
+
//# sourceMappingURL=sync-utils.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync-utils.cjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/sync-utils.ts"],"names":[],"mappings":";;;AAEA,uEAA4E;AAM5E,uCAAoE;AAEpE;;;;;GAKG;AACH,SAAgB,wBAAwB,CACtC,MAA4B,EAC5B,OAA8B;IAE9B,MAAM,EAAE,uBAAuB,EAAE,GAAG,MAAM,CAAC;IAC3C,MAAM,EAAE,YAAY,EAAE,gCAAgC,EAAE,GAAG,OAAO,CAAC;IAEnE,MAAM,EAAE,uBAAuB,EAAE,0BAA0B,EAAE,GAC3D,gCAAgC,EAAE,CAAC,KAAK,CAAC;IAC3C,MAAM,aAAa,GAAG,YAAY,EAAE,CAAC,IAAI,CACvC,qCAAqC,CACtC,CAAC;IAEF,IACE,CAAC,uBAAuB;QACxB,CAAC,aAAa;QACd,CAAC,uBAAuB;QACxB,0BAA0B,EAC1B;QACA,OAAO,KAAK,CAAC;KACd;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAvBD,4DAuBC;AAED;;;GAGG;AACI,KAAK,UAAU,uBAAuB,CAC3C,OAA8B;IAE9B,MAAM,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC;IAEjC,6DAA6D;IAC7D,MAAM,oBAAoB,GAAG,MAAM,YAAY,EAAE,CAAC,IAAI,CACpD,iCAAiC,CAClC,CAAC;IAEF,OAAO,oBAAoB,EAAE,MAAM,CACjC,iDAAyC,CAC1C,CAAC;AACJ,CAAC;AAbD,0DAaC;AAED;;;GAGG;AACI,KAAK,UAAU,0BAA0B,CAC9C,OAA8B;IAE9B,MAAM,EAAE,gCAAgC,EAAE,GAAG,OAAO,CAAC;IAErD,MAAM,uBAAuB,GAC3B,MAAM,gCAAgC,EAAE,CAAC,kCAAkC,CACzE,2CAA0B,CAAC,QAAQ,CACpC,CAAC;IAEJ,OAAO,CACL,uBAAuB,EAAE,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,IAAI,IAAI,CAC7E,CAAC;AACJ,CAAC;AAbD,gEAaC","sourcesContent":["import type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport { USER_STORAGE_FEATURE_NAMES } from '../../../shared/storage-schema';\nimport type {\n AccountSyncingConfig,\n AccountSyncingOptions,\n UserStorageAccount,\n} from './types';\nimport { doesInternalAccountHaveCorrectKeyringType } from './utils';\n\n/**\n * Checks if account syncing can be performed based on a set of conditions\n * @param config - configuration parameters\n * @param options - parameters used for checking if account syncing can be performed\n * @returns Returns true if account syncing can be performed, false otherwise.\n */\nexport function canPerformAccountSyncing(\n config: AccountSyncingConfig,\n options: AccountSyncingOptions,\n): boolean {\n const { isAccountSyncingEnabled } = config;\n const { getMessenger, getUserStorageControllerInstance } = options;\n\n const { isProfileSyncingEnabled, isAccountSyncingInProgress } =\n getUserStorageControllerInstance().state;\n const isAuthEnabled = getMessenger().call(\n 'AuthenticationController:isSignedIn',\n );\n\n if (\n !isProfileSyncingEnabled ||\n !isAuthEnabled ||\n !isAccountSyncingEnabled ||\n isAccountSyncingInProgress\n ) {\n return false;\n }\n\n return true;\n}\n\n/**\n * Get the list of internal accounts\n * @param options - parameters used for getting the list of internal accounts\n */\nexport async function getInternalAccountsList(\n options: AccountSyncingOptions,\n): Promise<InternalAccount[]> {\n const { getMessenger } = options;\n\n // eslint-disable-next-line @typescript-eslint/await-thenable\n const internalAccountsList = await getMessenger().call(\n 'AccountsController:listAccounts',\n );\n\n return internalAccountsList?.filter(\n doesInternalAccountHaveCorrectKeyringType,\n );\n}\n\n/**\n * Get the list of user storage accounts\n * @param options - parameters used for getting the list of user storage accounts\n */\nexport async function getUserStorageAccountsList(\n options: AccountSyncingOptions,\n): Promise<UserStorageAccount[] | null> {\n const { getUserStorageControllerInstance } = options;\n\n const rawAccountsListResponse =\n await getUserStorageControllerInstance().performGetStorageAllFeatureEntries(\n USER_STORAGE_FEATURE_NAMES.accounts,\n );\n\n return (\n rawAccountsListResponse?.map((rawAccount) => JSON.parse(rawAccount)) ?? null\n );\n}\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { InternalAccount } from "@metamask/keyring-internal-api";
|
|
2
|
+
import type { AccountSyncingConfig, AccountSyncingOptions, UserStorageAccount } from "./types.cjs";
|
|
3
|
+
/**
|
|
4
|
+
* Checks if account syncing can be performed based on a set of conditions
|
|
5
|
+
* @param config - configuration parameters
|
|
6
|
+
* @param options - parameters used for checking if account syncing can be performed
|
|
7
|
+
* @returns Returns true if account syncing can be performed, false otherwise.
|
|
8
|
+
*/
|
|
9
|
+
export declare function canPerformAccountSyncing(config: AccountSyncingConfig, options: AccountSyncingOptions): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Get the list of internal accounts
|
|
12
|
+
* @param options - parameters used for getting the list of internal accounts
|
|
13
|
+
*/
|
|
14
|
+
export declare function getInternalAccountsList(options: AccountSyncingOptions): Promise<InternalAccount[]>;
|
|
15
|
+
/**
|
|
16
|
+
* Get the list of user storage accounts
|
|
17
|
+
* @param options - parameters used for getting the list of user storage accounts
|
|
18
|
+
*/
|
|
19
|
+
export declare function getUserStorageAccountsList(options: AccountSyncingOptions): Promise<UserStorageAccount[] | null>;
|
|
20
|
+
//# sourceMappingURL=sync-utils.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync-utils.d.cts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/sync-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAGtE,OAAO,KAAK,EACV,oBAAoB,EACpB,qBAAqB,EACrB,kBAAkB,EACnB,oBAAgB;AAGjB;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,oBAAoB,EAC5B,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAoBT;AAED;;;GAGG;AACH,wBAAsB,uBAAuB,CAC3C,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,eAAe,EAAE,CAAC,CAW5B;AAED;;;GAGG;AACH,wBAAsB,0BAA0B,CAC9C,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,kBAAkB,EAAE,GAAG,IAAI,CAAC,CAWtC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { InternalAccount } from "@metamask/keyring-internal-api";
|
|
2
|
+
import type { AccountSyncingConfig, AccountSyncingOptions, UserStorageAccount } from "./types.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Checks if account syncing can be performed based on a set of conditions
|
|
5
|
+
* @param config - configuration parameters
|
|
6
|
+
* @param options - parameters used for checking if account syncing can be performed
|
|
7
|
+
* @returns Returns true if account syncing can be performed, false otherwise.
|
|
8
|
+
*/
|
|
9
|
+
export declare function canPerformAccountSyncing(config: AccountSyncingConfig, options: AccountSyncingOptions): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Get the list of internal accounts
|
|
12
|
+
* @param options - parameters used for getting the list of internal accounts
|
|
13
|
+
*/
|
|
14
|
+
export declare function getInternalAccountsList(options: AccountSyncingOptions): Promise<InternalAccount[]>;
|
|
15
|
+
/**
|
|
16
|
+
* Get the list of user storage accounts
|
|
17
|
+
* @param options - parameters used for getting the list of user storage accounts
|
|
18
|
+
*/
|
|
19
|
+
export declare function getUserStorageAccountsList(options: AccountSyncingOptions): Promise<UserStorageAccount[] | null>;
|
|
20
|
+
//# sourceMappingURL=sync-utils.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync-utils.d.mts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/sync-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAGtE,OAAO,KAAK,EACV,oBAAoB,EACpB,qBAAqB,EACrB,kBAAkB,EACnB,oBAAgB;AAGjB;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,oBAAoB,EAC5B,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAoBT;AAED;;;GAGG;AACH,wBAAsB,uBAAuB,CAC3C,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,eAAe,EAAE,CAAC,CAW5B;AAED;;;GAGG;AACH,wBAAsB,0BAA0B,CAC9C,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,kBAAkB,EAAE,GAAG,IAAI,CAAC,CAWtC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { USER_STORAGE_FEATURE_NAMES } from "../../../shared/storage-schema.mjs";
|
|
2
|
+
import { doesInternalAccountHaveCorrectKeyringType } from "./utils.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Checks if account syncing can be performed based on a set of conditions
|
|
5
|
+
* @param config - configuration parameters
|
|
6
|
+
* @param options - parameters used for checking if account syncing can be performed
|
|
7
|
+
* @returns Returns true if account syncing can be performed, false otherwise.
|
|
8
|
+
*/
|
|
9
|
+
export function canPerformAccountSyncing(config, options) {
|
|
10
|
+
const { isAccountSyncingEnabled } = config;
|
|
11
|
+
const { getMessenger, getUserStorageControllerInstance } = options;
|
|
12
|
+
const { isProfileSyncingEnabled, isAccountSyncingInProgress } = getUserStorageControllerInstance().state;
|
|
13
|
+
const isAuthEnabled = getMessenger().call('AuthenticationController:isSignedIn');
|
|
14
|
+
if (!isProfileSyncingEnabled ||
|
|
15
|
+
!isAuthEnabled ||
|
|
16
|
+
!isAccountSyncingEnabled ||
|
|
17
|
+
isAccountSyncingInProgress) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Get the list of internal accounts
|
|
24
|
+
* @param options - parameters used for getting the list of internal accounts
|
|
25
|
+
*/
|
|
26
|
+
export async function getInternalAccountsList(options) {
|
|
27
|
+
const { getMessenger } = options;
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/await-thenable
|
|
29
|
+
const internalAccountsList = await getMessenger().call('AccountsController:listAccounts');
|
|
30
|
+
return internalAccountsList?.filter(doesInternalAccountHaveCorrectKeyringType);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Get the list of user storage accounts
|
|
34
|
+
* @param options - parameters used for getting the list of user storage accounts
|
|
35
|
+
*/
|
|
36
|
+
export async function getUserStorageAccountsList(options) {
|
|
37
|
+
const { getUserStorageControllerInstance } = options;
|
|
38
|
+
const rawAccountsListResponse = await getUserStorageControllerInstance().performGetStorageAllFeatureEntries(USER_STORAGE_FEATURE_NAMES.accounts);
|
|
39
|
+
return (rawAccountsListResponse?.map((rawAccount) => JSON.parse(rawAccount)) ?? null);
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=sync-utils.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync-utils.mjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/sync-utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,0BAA0B,EAAE,2CAAuC;AAM5E,OAAO,EAAE,yCAAyC,EAAE,oBAAgB;AAEpE;;;;;GAKG;AACH,MAAM,UAAU,wBAAwB,CACtC,MAA4B,EAC5B,OAA8B;IAE9B,MAAM,EAAE,uBAAuB,EAAE,GAAG,MAAM,CAAC;IAC3C,MAAM,EAAE,YAAY,EAAE,gCAAgC,EAAE,GAAG,OAAO,CAAC;IAEnE,MAAM,EAAE,uBAAuB,EAAE,0BAA0B,EAAE,GAC3D,gCAAgC,EAAE,CAAC,KAAK,CAAC;IAC3C,MAAM,aAAa,GAAG,YAAY,EAAE,CAAC,IAAI,CACvC,qCAAqC,CACtC,CAAC;IAEF,IACE,CAAC,uBAAuB;QACxB,CAAC,aAAa;QACd,CAAC,uBAAuB;QACxB,0BAA0B,EAC1B;QACA,OAAO,KAAK,CAAC;KACd;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,OAA8B;IAE9B,MAAM,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC;IAEjC,6DAA6D;IAC7D,MAAM,oBAAoB,GAAG,MAAM,YAAY,EAAE,CAAC,IAAI,CACpD,iCAAiC,CAClC,CAAC;IAEF,OAAO,oBAAoB,EAAE,MAAM,CACjC,yCAAyC,CAC1C,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAC9C,OAA8B;IAE9B,MAAM,EAAE,gCAAgC,EAAE,GAAG,OAAO,CAAC;IAErD,MAAM,uBAAuB,GAC3B,MAAM,gCAAgC,EAAE,CAAC,kCAAkC,CACzE,0BAA0B,CAAC,QAAQ,CACpC,CAAC;IAEJ,OAAO,CACL,uBAAuB,EAAE,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,IAAI,IAAI,CAC7E,CAAC;AACJ,CAAC","sourcesContent":["import type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport { USER_STORAGE_FEATURE_NAMES } from '../../../shared/storage-schema';\nimport type {\n AccountSyncingConfig,\n AccountSyncingOptions,\n UserStorageAccount,\n} from './types';\nimport { doesInternalAccountHaveCorrectKeyringType } from './utils';\n\n/**\n * Checks if account syncing can be performed based on a set of conditions\n * @param config - configuration parameters\n * @param options - parameters used for checking if account syncing can be performed\n * @returns Returns true if account syncing can be performed, false otherwise.\n */\nexport function canPerformAccountSyncing(\n config: AccountSyncingConfig,\n options: AccountSyncingOptions,\n): boolean {\n const { isAccountSyncingEnabled } = config;\n const { getMessenger, getUserStorageControllerInstance } = options;\n\n const { isProfileSyncingEnabled, isAccountSyncingInProgress } =\n getUserStorageControllerInstance().state;\n const isAuthEnabled = getMessenger().call(\n 'AuthenticationController:isSignedIn',\n );\n\n if (\n !isProfileSyncingEnabled ||\n !isAuthEnabled ||\n !isAccountSyncingEnabled ||\n isAccountSyncingInProgress\n ) {\n return false;\n }\n\n return true;\n}\n\n/**\n * Get the list of internal accounts\n * @param options - parameters used for getting the list of internal accounts\n */\nexport async function getInternalAccountsList(\n options: AccountSyncingOptions,\n): Promise<InternalAccount[]> {\n const { getMessenger } = options;\n\n // eslint-disable-next-line @typescript-eslint/await-thenable\n const internalAccountsList = await getMessenger().call(\n 'AccountsController:listAccounts',\n );\n\n return internalAccountsList?.filter(\n doesInternalAccountHaveCorrectKeyringType,\n );\n}\n\n/**\n * Get the list of user storage accounts\n * @param options - parameters used for getting the list of user storage accounts\n */\nexport async function getUserStorageAccountsList(\n options: AccountSyncingOptions,\n): Promise<UserStorageAccount[] | null> {\n const { getUserStorageControllerInstance } = options;\n\n const rawAccountsListResponse =\n await getUserStorageControllerInstance().performGetStorageAllFeatureEntries(\n USER_STORAGE_FEATURE_NAMES.accounts,\n );\n\n return (\n rawAccountsListResponse?.map((rawAccount) => JSON.parse(rawAccount)) ?? null\n );\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.cjs","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { UserStorageControllerMessenger } from '../UserStorageController';\nimport type UserStorageController from '../UserStorageController';\nimport type {\n USER_STORAGE_VERSION_KEY,\n USER_STORAGE_VERSION,\n} from './constants';\n\nexport type UserStorageAccount = {\n /**\n * The Version 'v' of the User Storage.\n * NOTE - will allow us to support upgrade/downgrades in the future\n */\n [USER_STORAGE_VERSION_KEY]: typeof USER_STORAGE_VERSION;\n /** the id 'i' of the account */\n i: string;\n /** the address 'a' of the account */\n a: string;\n /** the name 'n' of the account */\n n: string;\n /** the nameLastUpdatedAt timestamp 'nlu' of the account */\n nlu?: number;\n};\n\nexport type AccountSyncingConfig = {\n isAccountSyncingEnabled: boolean;\n};\n\nexport type AccountSyncingOptions = {\n getUserStorageControllerInstance: () => UserStorageController;\n getMessenger: () => UserStorageControllerMessenger;\n};\n"]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { UserStorageControllerMessenger } from "../UserStorageController.cjs";
|
|
2
|
+
import type UserStorageController from "../UserStorageController.cjs";
|
|
3
|
+
import type { USER_STORAGE_VERSION_KEY, USER_STORAGE_VERSION } from "./constants.cjs";
|
|
4
|
+
export type UserStorageAccount = {
|
|
5
|
+
/**
|
|
6
|
+
* The Version 'v' of the User Storage.
|
|
7
|
+
* NOTE - will allow us to support upgrade/downgrades in the future
|
|
8
|
+
*/
|
|
9
|
+
[USER_STORAGE_VERSION_KEY]: typeof USER_STORAGE_VERSION;
|
|
10
|
+
/** the id 'i' of the account */
|
|
11
|
+
i: string;
|
|
12
|
+
/** the address 'a' of the account */
|
|
13
|
+
a: string;
|
|
14
|
+
/** the name 'n' of the account */
|
|
15
|
+
n: string;
|
|
16
|
+
/** the nameLastUpdatedAt timestamp 'nlu' of the account */
|
|
17
|
+
nlu?: number;
|
|
18
|
+
};
|
|
19
|
+
export type AccountSyncingConfig = {
|
|
20
|
+
isAccountSyncingEnabled: boolean;
|
|
21
|
+
};
|
|
22
|
+
export type AccountSyncingOptions = {
|
|
23
|
+
getUserStorageControllerInstance: () => UserStorageController;
|
|
24
|
+
getMessenger: () => UserStorageControllerMessenger;
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=types.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.cts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,8BAA8B,EAAE,qCAAiC;AAC/E,OAAO,KAAK,qBAAqB,qCAAiC;AAClE,OAAO,KAAK,EACV,wBAAwB,EACxB,oBAAoB,EACrB,wBAAoB;AAErB,MAAM,MAAM,kBAAkB,GAAG;IAC/B;;;OAGG;IACH,CAAC,wBAAwB,CAAC,EAAE,OAAO,oBAAoB,CAAC;IACxD,gCAAgC;IAChC,CAAC,EAAE,MAAM,CAAC;IACV,qCAAqC;IACrC,CAAC,EAAE,MAAM,CAAC;IACV,kCAAkC;IAClC,CAAC,EAAE,MAAM,CAAC;IACV,2DAA2D;IAC3D,GAAG,CAAC,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,uBAAuB,EAAE,OAAO,CAAC;CAClC,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,gCAAgC,EAAE,MAAM,qBAAqB,CAAC;IAC9D,YAAY,EAAE,MAAM,8BAA8B,CAAC;CACpD,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { UserStorageControllerMessenger } from "../UserStorageController.mjs";
|
|
2
|
+
import type UserStorageController from "../UserStorageController.mjs";
|
|
3
|
+
import type { USER_STORAGE_VERSION_KEY, USER_STORAGE_VERSION } from "./constants.mjs";
|
|
4
|
+
export type UserStorageAccount = {
|
|
5
|
+
/**
|
|
6
|
+
* The Version 'v' of the User Storage.
|
|
7
|
+
* NOTE - will allow us to support upgrade/downgrades in the future
|
|
8
|
+
*/
|
|
9
|
+
[USER_STORAGE_VERSION_KEY]: typeof USER_STORAGE_VERSION;
|
|
10
|
+
/** the id 'i' of the account */
|
|
11
|
+
i: string;
|
|
12
|
+
/** the address 'a' of the account */
|
|
13
|
+
a: string;
|
|
14
|
+
/** the name 'n' of the account */
|
|
15
|
+
n: string;
|
|
16
|
+
/** the nameLastUpdatedAt timestamp 'nlu' of the account */
|
|
17
|
+
nlu?: number;
|
|
18
|
+
};
|
|
19
|
+
export type AccountSyncingConfig = {
|
|
20
|
+
isAccountSyncingEnabled: boolean;
|
|
21
|
+
};
|
|
22
|
+
export type AccountSyncingOptions = {
|
|
23
|
+
getUserStorageControllerInstance: () => UserStorageController;
|
|
24
|
+
getMessenger: () => UserStorageControllerMessenger;
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=types.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../../../../src/controllers/user-storage/account-syncing/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,8BAA8B,EAAE,qCAAiC;AAC/E,OAAO,KAAK,qBAAqB,qCAAiC;AAClE,OAAO,KAAK,EACV,wBAAwB,EACxB,oBAAoB,EACrB,wBAAoB;AAErB,MAAM,MAAM,kBAAkB,GAAG;IAC/B;;;OAGG;IACH,CAAC,wBAAwB,CAAC,EAAE,OAAO,oBAAoB,CAAC;IACxD,gCAAgC;IAChC,CAAC,EAAE,MAAM,CAAC;IACV,qCAAqC;IACrC,CAAC,EAAE,MAAM,CAAC;IACV,kCAAkC;IAClC,CAAC,EAAE,MAAM,CAAC;IACV,2DAA2D;IAC3D,GAAG,CAAC,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,uBAAuB,EAAE,OAAO,CAAC;CAClC,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,gCAAgC,EAAE,MAAM,qBAAqB,CAAC;IAC9D,YAAY,EAAE,MAAM,8BAA8B,CAAC;CACpD,CAAC"}
|