Skip to content

Commit

Permalink
Merge branch 'main' into chore/12435-mvp-handle-engine-does-not-exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Cal-L authored Dec 4, 2024
2 parents 6953422 + 4ead541 commit 92edb61
Show file tree
Hide file tree
Showing 53 changed files with 3,033 additions and 395 deletions.
5 changes: 5 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ app/selectors/notification @MetaMask/notifications
app/util/notifications @MetaMask/notifications
app/store/util/notifications @MetaMask/notifications

# Identity Team
app/actions/identity @MetaMask/identity
app/util/identity @MetaMask/identity
app/components/UI/ProfileSyncing @MetaMask/identity

# LavaMoat Team
ses.cjs @MetaMask/supply-chain
patches/react-native+0.*.patch @MetaMask/supply-chain
Expand Down
8 changes: 8 additions & 0 deletions app/actions/identity/constants/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export enum identityErrors {
PERFORM_SIGN_IN = 'Error while trying to sign in',
PERFORM_SIGN_OUT = 'Error while trying to sign out',
ENABLE_PROFILE_SYNCING = 'Error while trying to enable profile syncing',
DISABLE_PROFILE_SYNCING = 'Error while trying to disable profile syncing',
}

export default identityErrors;
51 changes: 51 additions & 0 deletions app/actions/identity/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { performSignIn, performSignOut } from '.';
import Engine from '../../core/Engine';

jest.mock('../../core/Engine', () => ({
resetState: jest.fn(),
context: {
AuthenticationController: {
performSignIn: jest.fn(),
performSignOut: jest.fn(),
getSessionProfile: jest.fn(),
},
},
}));

describe('Identity actions', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('signs in successfully and obtain profile', async () => {
(
Engine.context.AuthenticationController.performSignIn as jest.Mock
).mockResolvedValue('valid-access-token');
(
Engine.context.AuthenticationController.getSessionProfile as jest.Mock
).mockResolvedValue('valid-profile');

const result = await performSignIn();

expect(
Engine.context.AuthenticationController.performSignIn,
).toHaveBeenCalled();
expect(
Engine.context.AuthenticationController.getSessionProfile,
).toHaveBeenCalled();
expect(result).toBeUndefined();
});

it('signs out successfully', async () => {
(
Engine.context.AuthenticationController.performSignOut as jest.Mock
).mockResolvedValue(undefined);

const result = await performSignOut();

expect(
Engine.context.AuthenticationController.performSignOut,
).toHaveBeenCalled();
expect(result).toBeUndefined();
});
});
53 changes: 53 additions & 0 deletions app/actions/identity/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { getErrorMessage } from '@metamask/utils';
import Engine from '../../core/Engine';
import identityErrors from './constants/errors';

export const performSignIn = async () => {
try {
const accessToken =
await Engine.context.AuthenticationController.performSignIn();
if (!accessToken) {
return getErrorMessage(identityErrors.PERFORM_SIGN_IN);
}

const profile =
await Engine.context.AuthenticationController.getSessionProfile();
if (!profile) {
return getErrorMessage(identityErrors.PERFORM_SIGN_IN);
}
} catch (error) {
return getErrorMessage(error);
}
};

export const performSignOut = async () => {
try {
await Engine.context.AuthenticationController.performSignOut();
} catch (error) {
return getErrorMessage(error);
}
};

export const enableProfileSyncing = async () => {
try {
await Engine.context.UserStorageController.enableProfileSyncing();
} catch (error) {
return getErrorMessage(error);
}
};

export const disableProfileSyncing = async () => {
try {
await Engine.context.UserStorageController.disableProfileSyncing();
} catch (error) {
return getErrorMessage(error);
}
};

export const syncInternalAccountsWithUserStorage = async () => {
try {
await Engine.context.UserStorageController.syncInternalAccountsWithUserStorage();
} catch (error) {
return getErrorMessage(error);
}
};
4 changes: 0 additions & 4 deletions app/actions/notification/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
export enum notificationsErrors {
PERFORM_SIGN_IN = 'Error while trying to sign in',
PERFORM_SIGN_OUT = 'Error while trying to sign out',
ENABLE_PROFILE_SYNCING = 'Error while trying to enable profile syncing',
DISABLE_PROFILE_SYNCING = 'Error while trying to disable profile syncing',
ENABLE_PUSH_NOTIFICATIONS = 'Error while trying to enable push notifications',
DISABLE_PUSH_NOTIFICATIONS = 'Error while trying to disable push notifications',
CHECK_ACCOUNTS_PRESENCE = 'Error while trying to check accounts presence',
Expand Down
51 changes: 18 additions & 33 deletions app/actions/notification/helpers/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
// Import necessary libraries and modules
import { signIn, signOut, enableNotificationServices, disableNotificationServices } from '.';
import { enableNotificationServices, disableNotificationServices } from '.';
import Engine from '../../../core/Engine';

jest.mock('../../../core/Engine', () => ({
resetState: jest.fn(),
context: {
AuthenticationController: {
performSignIn: jest.fn(),
performSignOut: jest.fn(),
getSessionProfile: jest.fn(),
},
NotificationServicesController: {
enableMetamaskNotifications:jest.fn(),
disableNotificationServices:jest.fn(),
enableMetamaskNotifications: jest.fn(),
disableNotificationServices: jest.fn(),
checkAccountsPresence: jest.fn(),
}
},
},
}));

Expand All @@ -23,41 +18,31 @@ describe('Notification Helpers', () => {
jest.clearAllMocks();
});

it('signs in successfully and obtain profile', async () => {
(Engine.context.AuthenticationController.performSignIn as jest.Mock).mockResolvedValue('valid-access-token');
(Engine.context.AuthenticationController.getSessionProfile as jest.Mock).mockResolvedValue('valid-profile');

const result = await signIn();

expect(Engine.context.AuthenticationController.performSignIn).toHaveBeenCalled();
expect(Engine.context.AuthenticationController.getSessionProfile).toHaveBeenCalled();
expect(result).toBeUndefined();
});

it('signs out successfully', async () => {
(Engine.context.AuthenticationController.performSignOut as jest.Mock).mockResolvedValue(undefined);

const result = await signOut();

expect(Engine.context.AuthenticationController.performSignOut).toHaveBeenCalled();
expect(result).toBeUndefined();
});

it('enables notification services successfully', async () => {
(Engine.context.NotificationServicesController.enableMetamaskNotifications as jest.Mock).mockResolvedValue(undefined);
(
Engine.context.NotificationServicesController
.enableMetamaskNotifications as jest.Mock
).mockResolvedValue(undefined);

const result = await enableNotificationServices();

expect(Engine.context.NotificationServicesController.enableMetamaskNotifications).toHaveBeenCalled();
expect(
Engine.context.NotificationServicesController.enableMetamaskNotifications,
).toHaveBeenCalled();
expect(result).toBeUndefined();
});

it('disables notification services successfully', async () => {
(Engine.context.NotificationServicesController.disableNotificationServices as jest.Mock).mockResolvedValue(undefined);
(
Engine.context.NotificationServicesController
.disableNotificationServices as jest.Mock
).mockResolvedValue(undefined);

const result = await disableNotificationServices();

expect(Engine.context.NotificationServicesController.disableNotificationServices).toHaveBeenCalled();
expect(
Engine.context.NotificationServicesController.disableNotificationServices,
).toHaveBeenCalled();
expect(result).toBeUndefined();
});
});
66 changes: 13 additions & 53 deletions app/actions/notification/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,18 @@ import { getErrorMessage } from '@metamask/utils';

import { notificationsErrors } from '../constants';
import Engine from '../../../core/Engine';
import { Notification, mmStorage, getAllUUIDs } from '../../../util/notifications';
import {
Notification,
mmStorage,
getAllUUIDs,
} from '../../../util/notifications';
import { UserStorage } from '@metamask/notification-services-controller/dist/NotificationServicesController/types/user-storage/index.cjs';

export type MarkAsReadNotificationsParam = Pick<
Notification,
'id' | 'type' | 'isRead'
>[];

export const signIn = async () => {
try {
const accessToken =
await Engine.context.AuthenticationController.performSignIn();
if (!accessToken) {
return getErrorMessage(notificationsErrors.PERFORM_SIGN_IN);
}

const profile =
await Engine.context.AuthenticationController.getSessionProfile();
if (!profile) {
return getErrorMessage(notificationsErrors.PERFORM_SIGN_IN);
}
} catch (error) {
return getErrorMessage(error);
}
};

export const signOut = async () => {
try {
await Engine.context.AuthenticationController.performSignOut();
} catch (error) {
return getErrorMessage(error);
}
};

export const enableProfileSyncing = async () => {
try {
await Engine.context.UserStorageController.enableProfileSyncing();
} catch (error) {
return getErrorMessage(error);
}
};

export const disableProfileSyncing = async () => {
try {
await Engine.context.UserStorageController.disableProfileSyncing();
} catch (error) {
return getErrorMessage(error);
}
};

export const enableNotificationServices = async () => {
try {
await Engine.context.NotificationServicesController.enableMetamaskNotifications();
Expand Down Expand Up @@ -176,14 +138,6 @@ export const markMetamaskNotificationsAsRead = async (
}
};

export const syncInternalAccountsWithUserStorage = async () => {
try {
await Engine.context.UserStorageController.syncInternalAccountsWithUserStorage();
} catch (error) {
return getErrorMessage(error);
}
};

/**
* Perform the deletion of the notifications storage key and the creation of on chain triggers to reset the notifications.
*
Expand All @@ -201,7 +155,11 @@ export const performDeleteStorage = async (): Promise<string | undefined> => {
return getErrorMessage(error);
}
};
export const enablePushNotifications = async (userStorage: UserStorage, fcmToken?: string) => {

export const enablePushNotifications = async (
userStorage: UserStorage,
fcmToken?: string,
) => {
try {
const uuids = getAllUUIDs(userStorage);
await Engine.context.NotificationServicesPushController.enablePushNotifications(
Expand All @@ -224,7 +182,9 @@ export const disablePushNotifications = async (userStorage: UserStorage) => {
}
};

export const updateTriggerPushNotifications = async (userStorage: UserStorage) => {
export const updateTriggerPushNotifications = async (
userStorage: UserStorage,
) => {
try {
const uuids = getAllUUIDs(userStorage);
await Engine.context.NotificationServicesPushController.updateTriggerPushNotifications(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ import NotificationsService from '../../../../util/notifications/services/Notifi
import { MetaMetricsEvents } from '../../../../core/Analytics';
import { useEnableNotifications } from '../../../../util/notifications/hooks/useNotifications';
import { useMetrics } from '../../../hooks/useMetrics';
import {
selectIsProfileSyncingEnabled,
selectIsMetamaskNotificationsEnabled,
} from '../../../../selectors/notifications';
import { selectIsMetamaskNotificationsEnabled } from '../../../../selectors/notifications';
import { selectIsProfileSyncingEnabled } from '../../../../selectors/identity';

interface Props {
route: {
Expand Down
6 changes: 2 additions & 4 deletions app/components/UI/NetworkModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import Text from '../../Base/Text';
import NetworkDetails from './NetworkDetails';
import NetworkAdded from './NetworkAdded';
import Engine from '../../../core/Engine';
import {
isPrivateConnection,
toggleUseSafeChainsListValidation,
} from '../../../util/networks';
import { isPrivateConnection } from '../../../util/networks';
import { toggleUseSafeChainsListValidation } from '../../../util/networks/engineNetworkUtils';
import getDecimalChainId from '../../../util/networks/getDecimalChainId';
import URLPARSE from 'url-parse';
import { isWebUri } from 'valid-url';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ import BottomSheetFooter, {
import BottomSheetHeader from '../../../component-library/components/BottomSheets/BottomSheetHeader';
import {
getNetworkImageSource,
toggleUseSafeChainsListValidation,
isMultichainVersion1Enabled,
} from '../../../util/networks';
import { toggleUseSafeChainsListValidation } from '../../../util/networks/engineNetworkUtils';
import { NetworkApprovalBottomSheetSelectorsIDs } from '../../../../e2e/selectors/Network/NetworkApprovalBottomSheet.selectors';
import hideKeyFromUrl from '../../../util/hideKeyFromUrl';
import { convertHexToDecimal } from '@metamask/controller-utils';
Expand Down
2 changes: 1 addition & 1 deletion app/components/UI/ProfileSyncing/ProfileSyncing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { strings } from '../../../../locales/i18n';
import styles from './ProfileSyncing.styles';
import { ProfileSyncingComponentProps } from './ProfileSyncing.types';
import AppConstants from '../../../core/AppConstants';
import { useProfileSyncing } from '../../../util/notifications/hooks/useProfileSyncing';
import { useProfileSyncing } from '../../../util/identity/hooks/useProfileSyncing';
import { isNotificationsFeatureEnabled } from '../../../util/notifications';

function ProfileSyncingComponent({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ import {
IconName,
IconSize,
} from '../../../../component-library/components/Icons/Icon';
import {
selectIsProfileSyncingEnabled,
selectIsMetamaskNotificationsEnabled,
} from '../../../../selectors/notifications';
import { useProfileSyncing } from '../../../../util/notifications/hooks/useProfileSyncing';
import { selectIsMetamaskNotificationsEnabled } from '../../../../selectors/notifications';
import { selectIsProfileSyncingEnabled } from '../../../../selectors/identity';
import { useProfileSyncing } from '../../../../util/identity/hooks/useProfileSyncing';
import { MetaMetricsEvents } from '../../../../core/Analytics';
import ModalContent from '../../Notification/Modal';

Expand Down
Loading

0 comments on commit 92edb61

Please sign in to comment.