Skip to content

Commit 62b836b

Browse files
mathieuartuPatrykLucka
authored andcommitted
feat: decouple account sync logic from UserStorageController (#5078)
## Explanation This PR moves the logic related to account syncing from `UserStorageController` to separated files in the `account-syncing` folder. It also improves test coverage related to account syncing to 100%. ## References Related to #4923 - Extension draft PR: MetaMask/metamask-extension#29316 - CI & E2E Passes ✅ - I needed to add the new `isAccountSyncingInProgress` state key at various places to make it pass CI (as expected) - Account syncing is enabled on extension - Mobile draft PR: MetaMask/metamask-mobile#12755 - CI passes BUT ✅ ☝️ - I needed to add the new isAccountSyncingInProgress state key at various places to make it pass CI (as expected) - UTs were broken. It seems to be linked to the latest version of `NetworkController` and its `NetworkController:networkDidChange` event. This latest version is requested by our controller as part of the upcoming network syncing feature. - This will require a separate PR that bumps `NetworkController` to `v22.1.1` (or another incriminated dependency TBD) - This has started to be addressed here: MetaMask/metamask-mobile#12765 - This was fixed by this PR: #5116 - In any case, account syncing is **NOT** enabled on mobile yet ## Changelog <!-- If you're making any consumer-facing changes, list those changes here as if you were updating a changelog, using the template below as a guide. (CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or FIXED. For security-related issues, follow the Security Advisory process.) Please take care to name the exact pieces of the API you've added or changed (e.g. types, interfaces, functions, or methods). If there are any breaking changes, make sure to offer a solution for consumers to follow once they upgrade to the changes. Finally, if you're only making changes to development scripts or tests, you may replace the template below with "None". --> ### `@metamask/profile-sync-controller` - **CHANGED**: moved account syncing logic to its own files - **BREAKING**: added a new `isAccountSyncingInProgress` state key ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've highlighted breaking changes using the "BREAKING" category above as appropriate - [x] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes
1 parent 0f16606 commit 62b836b

File tree

16 files changed

+2150
-1625
lines changed

16 files changed

+2150
-1625
lines changed

packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.test.ts

Lines changed: 90 additions & 1252 deletions
Large diffs are not rendered by default.

packages/profile-sync-controller/src/controllers/user-storage/UserStorageController.ts

Lines changed: 63 additions & 334 deletions
Large diffs are not rendered by default.

packages/profile-sync-controller/src/controllers/user-storage/__fixtures__/test-utils.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,65 @@
1+
import type nock from 'nock';
2+
3+
import encryption from '../../../shared/encryption/encryption';
4+
import type {
5+
GetUserStorageAllFeatureEntriesResponse,
6+
GetUserStorageResponse,
7+
} from '../services';
8+
import { MOCK_STORAGE_KEY } from './mockStorage';
9+
10+
/**
11+
* Test Utility - creates a realistic mock user-storage entry
12+
* @param data - data to encrypt
13+
* @returns user storage entry
14+
*/
15+
export async function createMockUserStorageEntry(
16+
data: unknown,
17+
): Promise<GetUserStorageResponse> {
18+
return {
19+
HashedKey: 'HASHED_KEY',
20+
Data: await encryption.encryptString(
21+
JSON.stringify(data),
22+
MOCK_STORAGE_KEY,
23+
),
24+
};
25+
}
26+
27+
/**
28+
* Test Utility - creates a realistic mock user-storage get-all entry
29+
* @param data - data array to encrypt
30+
* @returns user storage entry
31+
*/
32+
export async function createMockUserStorageEntries(
33+
data: unknown[],
34+
): Promise<GetUserStorageAllFeatureEntriesResponse> {
35+
return await Promise.all(data.map((d) => createMockUserStorageEntry(d)));
36+
}
37+
38+
/**
39+
* Test Utility - decrypts a realistic batch upsert payload
40+
* @param requestBody - nock body
41+
* @param storageKey - storage key
42+
* @returns decrypted body
43+
*/
44+
export async function decryptBatchUpsertBody(
45+
requestBody: nock.Body,
46+
storageKey: string,
47+
) {
48+
if (typeof requestBody === 'string') {
49+
return requestBody;
50+
}
51+
return await Promise.all(
52+
Object.entries<string>(requestBody.data).map(
53+
async ([entryKey, entryValue]) => {
54+
return [
55+
entryKey,
56+
await encryption.decryptString(entryValue, storageKey),
57+
];
58+
},
59+
),
60+
);
61+
}
62+
163
type WaitForOptions = {
264
intervalMs?: number;
365
timeoutMs?: number;

packages/profile-sync-controller/src/controllers/user-storage/__fixtures__/mockAccounts.ts renamed to packages/profile-sync-controller/src/controllers/user-storage/account-syncing/__fixtures__/mockAccounts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { EthAccountType } from '@metamask/keyring-api';
22
import { KeyringTypes } from '@metamask/keyring-controller';
33
import type { InternalAccount } from '@metamask/keyring-internal-api';
44

5-
import { LOCALIZED_DEFAULT_ACCOUNT_NAMES } from '../accounts/constants';
6-
import { mapInternalAccountToUserStorageAccount } from '../accounts/user-storage';
5+
import { LOCALIZED_DEFAULT_ACCOUNT_NAMES } from '../constants';
6+
import { mapInternalAccountToUserStorageAccount } from '../utils';
77

88
/**
99
* Map an array of internal accounts to an array of user storage accounts
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import type { InternalAccount } from '@metamask/keyring-internal-api';
2+
3+
import { createSHA256Hash } from '../../../../shared/encryption';
4+
import { mockUserStorageMessenger } from '../../__fixtures__/mockMessenger';
5+
import { mapInternalAccountToUserStorageAccount } from '../utils';
6+
import { MOCK_INTERNAL_ACCOUNTS } from './mockAccounts';
7+
8+
/**
9+
* Test Utility - create a mock user storage messenger for account syncing tests
10+
*
11+
* @param options - options for the mock messenger
12+
* @param options.accounts - options for the accounts part of the controller
13+
* @param options.accounts.accountsList - list of accounts to return for the 'AccountsController:listAccounts' action
14+
* @returns Mock User Storage Messenger
15+
*/
16+
export function mockUserStorageMessengerForAccountSyncing(options?: {
17+
accounts?: {
18+
accountsList?: InternalAccount[];
19+
};
20+
}) {
21+
const messengerMocks = mockUserStorageMessenger();
22+
23+
messengerMocks.mockKeyringAddNewAccount.mockImplementation(async () => {
24+
messengerMocks.baseMessenger.publish(
25+
'AccountsController:accountAdded',
26+
MOCK_INTERNAL_ACCOUNTS.ONE[0] as InternalAccount,
27+
);
28+
return MOCK_INTERNAL_ACCOUNTS.ONE[0].address;
29+
});
30+
31+
messengerMocks.mockAccountsListAccounts.mockReturnValue(
32+
(options?.accounts?.accountsList ??
33+
MOCK_INTERNAL_ACCOUNTS.ALL) as InternalAccount[],
34+
);
35+
36+
return messengerMocks;
37+
}
38+
39+
/**
40+
* Test Utility - creates a realistic expected batch upsert payload
41+
* @param data - data supposed to be upserted
42+
* @param storageKey - storage key
43+
* @returns expected body
44+
*/
45+
export function createExpectedAccountSyncBatchUpsertBody(
46+
data: [string, InternalAccount][],
47+
storageKey: string,
48+
) {
49+
return data.map(([entryKey, entryValue]) => [
50+
createSHA256Hash(String(entryKey) + storageKey),
51+
JSON.stringify(mapInternalAccountToUserStorageAccount(entryValue)),
52+
]);
53+
}
54+
55+
/**
56+
* Test Utility - creates a realistic expected batch delete payload
57+
* @param data - data supposed to be deleted
58+
* @param storageKey - storage key
59+
* @returns expected body
60+
*/
61+
export function createExpectedAccountSyncBatchDeleteBody(
62+
data: string[],
63+
storageKey: string,
64+
) {
65+
return data.map((entryKey) =>
66+
createSHA256Hash(String(entryKey) + storageKey),
67+
);
68+
}

packages/profile-sync-controller/src/controllers/user-storage/accounts/constants.ts renamed to packages/profile-sync-controller/src/controllers/user-storage/account-syncing/constants.ts

File renamed without changes.

0 commit comments

Comments
 (0)