Skip to content

Commit

Permalink
refactor: move and organize shared profile sync dependencies (#4717)
Browse files Browse the repository at this point in the history
## Explanation

This decouples dependencies between profile sync SDK and profile sync
Controllers.
It creates a shared section that both can use instead of the "cyclic
dependencies". This should also help with tree shaking and reducing
bundle size if using a subpath export.

## References

https://consensyssoftware.atlassian.net/browse/NOTIFY-1129

## 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 `encryption` folder into shared folder
- **CHANGED**: Moved `storage-schema` into shared folder
- **CHANGED**: Moved `env` into shared folder
- **REMOVED**: Removed old `encryption` file in SDK to instead use
shared encryption file.

## 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
  • Loading branch information
Prithpal-Sooriya committed Sep 19, 2024
1 parent add8e5f commit 5e30573
Show file tree
Hide file tree
Showing 29 changed files with 53 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Env, Platform, getEnvUrls, getOidcClientId } from '../../sdk/env';
import { Env, Platform, getEnvUrls, getOidcClientId } from '../../shared/env';

const ENV_URLS = getEnvUrls(Env.PRD);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ControllerMessenger } from '@metamask/base-controller';
import type { InternalAccount } from '@metamask/keyring-api';
import type nock from 'nock';

import encryption from '../../shared/encryption';
import type {
AuthenticationControllerGetBearerToken,
AuthenticationControllerGetSessionProfile,
Expand All @@ -23,7 +24,6 @@ import {
MOCK_STORAGE_KEY_SIGNATURE,
} from './__fixtures__/mockStorage';
import * as AccountsUserStorageModule from './accounts/user-storage';
import encryption from './encryption/encryption';
import type {
GetUserStorageAllFeatureEntriesResponse,
GetUserStorageResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import type {
import type { NetworkConfiguration } from '@metamask/network-controller';
import type { HandleSnapRequest } from '@metamask/snaps-controllers';

import { createSHA256Hash } from '../../shared/encryption';
import type {
UserStoragePathWithFeatureAndKey,
UserStoragePathWithFeatureOnly,
} from '../../shared/storage-schema';
import type { NativeScrypt } from '../../shared/types/encryption';
import { createSnapSignMessageRequest } from '../authentication/auth-snap-requests';
import type {
AuthenticationControllerGetBearerToken,
Expand All @@ -34,12 +40,7 @@ import {
isNameDefaultAccountName,
mapInternalAccountToUserStorageAccount,
} from './accounts/user-storage';
import { createSHA256Hash } from './encryption';
import { startNetworkSyncing } from './network-syncing/controller-integration';
import type {
UserStoragePathWithFeatureAndKey,
UserStoragePathWithFeatureOnly,
} from './schema';
import {
getUserStorage,
getUserStorageAllFeatureEntries,
Expand Down Expand Up @@ -80,15 +81,6 @@ export declare type NotificationServicesControllerSelectIsNotificationServicesEn
handler: () => boolean;
};

export declare type NativeScrypt = (
passwd: string,
salt: Uint8Array,
N: number,
r: number,
p: number,
size: number,
) => Promise<Uint8Array>;

const controllerName = 'UserStorageController';

// State
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {
UserStoragePathWithFeatureAndKey,
UserStoragePathWithFeatureOnly,
} from '../schema';
import { createEntryPath } from '../schema';
} from '../../../shared/storage-schema';
import { createEntryPath } from '../../../shared/storage-schema';
import type {
GetUserStorageAllFeatureEntriesResponse,
GetUserStorageResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import nock from 'nock';
import type {
UserStoragePathWithFeatureAndKey,
UserStoragePathWithFeatureOnly,
} from '../schema';
} from '../../../shared/storage-schema';
import {
getMockUserStorageGetResponse,
getMockUserStoragePutResponse,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import encryption, { createSHA256Hash } from '../encryption';
import encryption, { createSHA256Hash } from '../../../shared/encryption';

export const MOCK_STORAGE_KEY_SIGNATURE = 'mockStorageKey';
export const MOCK_STORAGE_KEY = createSHA256Hash(MOCK_STORAGE_KEY_SIGNATURE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ const UserStorageController = Controller;
export { Controller };
export default UserStorageController;
export * from './UserStorageController';
export * from './encryption';
export * as Mocks from './__fixtures__';
export * from '../../shared/encryption';
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import log from 'loglevel';

import { Env, getEnvUrls } from '../../sdk/env';
import encryption from './encryption';
import encryption from '../../shared/encryption';
import { Env, getEnvUrls } from '../../shared/env';
import type {
UserStoragePathWithFeatureAndKey,
UserStoragePathWithFeatureOnly,
} from './schema';
import { createEntryPath } from './schema';
import type { NativeScrypt } from './UserStorageController';
} from '../../shared/storage-schema';
import { createEntryPath } from '../../shared/storage-schema';
import type { NativeScrypt } from '../../shared/types/encryption';

const ENV_URLS = getEnvUrls(Env.PRD);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import nock from 'nock';

import { Env } from '../../shared/env';
import {
NONCE_URL,
SIWE_LOGIN_URL,
SRP_LOGIN_URL,
OIDC_TOKEN_URL,
PAIR_IDENTIFIERS,
} from '../authentication-jwt-bearer/services';
import { Env } from '../env';

type MockReply = {
status: nock.StatusCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import nock from 'nock';

import encryption from '../encryption';
import { Env } from '../env';
import encryption from '../../shared/encryption';
import { Env } from '../../shared/env';
import { STORAGE_URL } from '../user-storage';

type MockReply = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Env, Platform } from '../../shared/env';
import { JwtBearerAuth } from '../authentication';
import type {
AuthSigningOptions,
AuthStorageOptions,
} from '../authentication-jwt-bearer/types';
import { AuthType } from '../authentication-jwt-bearer/types';
import { Env, Platform } from '../env';

// Alias mocking variables with ANY to test runtime safety.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Env, Platform } from '../env';
import { getEnvUrls, getOidcClientId } from '../env';
import type { Env, Platform } from '../../shared/env';
import { getEnvUrls, getOidcClientId } from '../../shared/env';
import {
NonceRetrievalError,
PairError,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Env, Platform } from '../env';
import type { Env, Platform } from '../../shared/env';

export enum AuthType {
/* sign in using a private key derived from your secret recovery phrase (SRP).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Env, Platform } from '../shared/env';
import {
MOCK_ACCESS_JWT,
MOCK_SRP_LOGIN_RESPONSE,
Expand All @@ -7,7 +8,6 @@ import type { MockVariable } from './__fixtures__/test-utils';
import { arrangeAuth } from './__fixtures__/test-utils';
import { JwtBearerAuth } from './authentication';
import type { LoginResponse, Pair } from './authentication-jwt-bearer/types';
import { Env, Platform } from './env';
import {
NonceRetrievalError,
PairError,
Expand Down
2 changes: 1 addition & 1 deletion packages/profile-sync-controller/src/sdk/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Env } from '../shared/env';
import { SIWEJwtBearerAuth } from './authentication-jwt-bearer/flow-siwe';
import { SRPJwtBearerAuth } from './authentication-jwt-bearer/flow-srp';
import {
Expand All @@ -6,7 +7,6 @@ import {
} from './authentication-jwt-bearer/services';
import type { UserProfile, Pair } from './authentication-jwt-bearer/types';
import { AuthType } from './authentication-jwt-bearer/types';
import type { Env } from './env';
import { PairError, UnsupportedAuthTypeError } from './errors';

// Computing the Classes, so we only get back the public methods for the interface.
Expand Down
4 changes: 0 additions & 4 deletions packages/profile-sync-controller/src/sdk/encryption.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/profile-sync-controller/src/sdk/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './authentication';
export * from './user-storage';
export * from './env';
export * from './errors';
export * from './encryption';
export * from './utils/messaging-signing-snap-requests';
export * from '../shared/encryption';
export * from '../shared/env';
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Env } from '../shared/env';
import { arrangeAuthAPIs } from './__fixtures__/mock-auth';
import {
MOCK_NOTIFICATIONS_DATA,
Expand All @@ -8,7 +9,6 @@ import {
} from './__fixtures__/mock-userstorage';
import { arrangeAuth, typedMockFn } from './__fixtures__/test-utils';
import type { IBaseAuth } from './authentication-jwt-bearer/types';
import { Env } from './env';
import { NotFoundError, UserStorageError } from './errors';
import type { StorageOptions } from './user-storage';
import { STORAGE_URL, UserStorage } from './user-storage';
Expand Down
18 changes: 12 additions & 6 deletions packages/profile-sync-controller/src/sdk/user-storage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import encryption, { createSHA256Hash } from '../shared/encryption';
import type { Env } from '../shared/env';
import { getEnvUrls } from '../shared/env';
import type {
UserStoragePathWithFeatureAndKey,
UserStoragePathWithFeatureOnly,
} from '../controllers/user-storage/schema';
import { createEntryPath } from '../controllers/user-storage/schema';
import type { GetUserStorageAllFeatureEntriesResponse } from '../controllers/user-storage/services';
} from '../shared/storage-schema';
import { createEntryPath } from '../shared/storage-schema';
import type { IBaseAuth } from './authentication-jwt-bearer/types';
import encryption, { createSHA256Hash } from './encryption';
import type { Env } from './env';
import { getEnvUrls } from './env';
import { NotFoundError, UserStorageError } from './errors';

export const STORAGE_URL = (env: Env, encryptedPath: string) =>
Expand All @@ -27,6 +26,13 @@ export type UserStorageOptions = {
storage?: StorageOptions;
};

type GetUserStorageAllFeatureEntriesResponse = {
// eslint-disable-next-line @typescript-eslint/naming-convention
HashedKey: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
Data: string;
}[];

type ErrorMessage = {
message: string;
error: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { scryptAsync } from '@noble/hashes/scrypt';
import { sha256 } from '@noble/hashes/sha256';
import { utf8ToBytes, concatBytes, bytesToHex } from '@noble/hashes/utils';

import type { NativeScrypt } from '../UserStorageController';
import type { NativeScrypt } from '../types/encryption';
import { getAnyCachedKey, getCachedKeyBySalt, setCachedKey } from './cache';
import { base64ToByteArray, byteArrayToBase64, bytesToUtf8 } from './utils';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { MockVariable } from './__fixtures__/test-utils';
import type { MockVariable } from '../sdk/__fixtures__/test-utils';
import { getEnvUrls, Env, Platform, getOidcClientId } from './env';

describe('getEnvUrls', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
createEntryPath,
getFeatureAndKeyFromPath,
USER_STORAGE_SCHEMA,
} from './schema';
} from './storage-schema';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ErroneousUserStoragePath = any;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export declare type NativeScrypt = (
passwd: string,
salt: Uint8Array,
N: number,
r: number,
p: number,
size: number,
) => Promise<Uint8Array>;

0 comments on commit 5e30573

Please sign in to comment.