Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove supported chains check #13047

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ jest.mock('../../../../../util/ENSUtils', () => ({
jest.mock('../../../../../lib/ppom/ppom-util', () => ({
...jest.requireActual('../../../../../lib/ppom/ppom-util'),
validateRequest: jest.fn(),
isChainSupported: jest.fn(),
}));

jest.mock('../../../../../core/Engine', () => {
Expand Down
20 changes: 10 additions & 10 deletions app/core/NotificationsManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe('NotificationManager', () => {
expect(message).toBe(strings('notifications.default_message_description'));
});
});

describe('smartTransactionListener', () => {
const mockTransactionController = {
getTransactions: jest.fn(),
Expand Down Expand Up @@ -172,7 +172,7 @@ describe('NotificationManager', () => {
beforeEach(() => {
// Clear all mock interactions before each test
jest.clearAllMocks();

// Reset the notification manager before each test
notificationManager = NotificationManager.init({
navigation: mockNavigation,
Expand All @@ -197,14 +197,14 @@ describe('NotificationManager', () => {
transactionId: '0x123',
};

notificationManager.watchSubmittedTransaction({
notificationManager.watchSubmittedTransaction({
id: '0x123',
txParams: {
nonce: '0x1'
},
silent: false
});

// Get the subscriber callback
const subscriberCallback = mockControllerMessenger.subscribe.mock.calls[0][1];
await subscriberCallback(transaction);
Expand All @@ -217,7 +217,7 @@ describe('NotificationManager', () => {
});

it('shows a cancelled notification for cancelled smart transactions', async () => {
const mockTransaction = {
const mockTransaction = {
id: '0x123',
txParams: {
nonce: '0x1'
Expand All @@ -230,14 +230,14 @@ describe('NotificationManager', () => {
transactionId: '0x123',
};

notificationManager.watchSubmittedTransaction({
notificationManager.watchSubmittedTransaction({
id: '0x123',
txParams: {
nonce: '0x1'
},
silent: false
});

// Get the subscriber callback
const subscriberCallback = mockControllerMessenger.subscribe.mock.calls[0][1];
await subscriberCallback(smartTransaction);
Expand All @@ -256,14 +256,14 @@ describe('NotificationManager', () => {
transactionId: '0x123',
};

notificationManager.watchSubmittedTransaction({
notificationManager.watchSubmittedTransaction({
id: '0x123',
txParams: {
nonce: '0x1'
},
silent: false
});

// Get the subscriber callback
const subscriberCallback = mockControllerMessenger.subscribe.mock.calls[0][1];
await subscriberCallback(transaction);
Expand All @@ -275,7 +275,7 @@ describe('NotificationManager', () => {
});

it('sets up transaction event listeners correctly', () => {
const transaction = {
const transaction = {
id: '0x123',
txParams: {
nonce: '0x1'
Expand Down
2 changes: 1 addition & 1 deletion app/core/SDKConnect/utils/isUUID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export const isUUID = (str: string) => {
const uuidRegex =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
return uuidRegex.test(str);
};
};
24 changes: 2 additions & 22 deletions app/lib/ppom/ppom-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ import {
import { WALLET_CONNECT_ORIGIN } from '../../util/walletconnect';
import AppConstants from '../../core/AppConstants';
import {
getSecurityAlertsAPISupportedChainIds,
isSecurityAlertsAPIEnabled,
validateWithSecurityAlertsAPI,
} from './security-alerts-api';
import { PPOMController } from '@metamask/ppom-validator';
import { Hex } from '@metamask/utils';
import { SECURITY_PROVIDER_SUPPORTED_CHAIN_IDS_FALLBACK_LIST } from '../../util/networks';

export interface PPOMRequest {
method: string;
Expand Down Expand Up @@ -72,13 +69,11 @@ async function validateRequest(req: PPOMRequest, transactionId?: string) {
NetworkController.state?.selectedNetworkClientId,
);
const isConfirmationMethod = CONFIRMATION_METHODS.includes(req.method);
const isSupportedChain = await isChainSupported(chainId);
const isBlockaidFeatEnabled = await isBlockaidFeatureEnabled();
if (
!ppomController ||
!isBlockaidFeatEnabled ||
!isConfirmationMethod ||
!isSupportedChain
!isConfirmationMethod
) {
return;
}
Expand Down Expand Up @@ -138,21 +133,6 @@ async function validateRequest(req: PPOMRequest, transactionId?: string) {
}
}

async function isChainSupported(chainId: Hex): Promise<boolean> {
let supportedChainIds = SECURITY_PROVIDER_SUPPORTED_CHAIN_IDS_FALLBACK_LIST;
try {
if (isSecurityAlertsAPIEnabled()) {
supportedChainIds = await getSecurityAlertsAPISupportedChainIds();
}
} catch (e) {
Logger.log(
`Error fetching supported chains from security alerts API: ${e}`,
);
}

return supportedChainIds.includes(chainId);
}

async function validateWithController(
ppomController: PPOMController,
request: PPOMRequest,
Expand Down Expand Up @@ -251,4 +231,4 @@ function normalizeRequest(request: PPOMRequest): PPOMRequest {
};
}

export default { validateRequest, isChainSupported };
export default { validateRequest };
28 changes: 0 additions & 28 deletions app/lib/ppom/security-alerts-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
ResultType,
} from '../../components/Views/confirmations/components/BlockaidBanner/BlockaidBanner.types';
import {
getSecurityAlertsAPISupportedChainIds,
validateWithSecurityAlertsAPI,
} from './security-alerts-api';

Expand Down Expand Up @@ -69,31 +68,4 @@ describe('Security Alerts API', () => {
);
});
});

describe('getSecurityAlertsAPISupportedChainIds', () => {
it('sends GET request', async () => {
const SUPPORTED_CHAIN_IDS_MOCK = ['0x1', '0x2'];
fetchMock.mockResolvedValue({
ok: true,
json: async () => SUPPORTED_CHAIN_IDS_MOCK,
});
const response = await getSecurityAlertsAPISupportedChainIds();

expect(response).toEqual(SUPPORTED_CHAIN_IDS_MOCK);

expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith(
`https://example.com/supportedChains`,
undefined,
);
});

it('throws an error if response is not ok', async () => {
fetchMock.mockResolvedValue({ ok: false, status: 404 });

await expect(getSecurityAlertsAPISupportedChainIds()).rejects.toThrow(
'Security alerts API request failed with status: 404',
);
});
});
});
6 changes: 0 additions & 6 deletions app/lib/ppom/security-alerts-api.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Hex } from '@metamask/utils';
import { SecurityAlertResponse } from '../../components/Views/confirmations/components/BlockaidBanner/BlockaidBanner.types';
import AppConstants from '../../core/AppConstants';

const ENDPOINT_VALIDATE = 'validate';
const ENDPOINT_SUPPORTED_CHAINS = 'supportedChains';

export interface SecurityAlertsAPIRequest {
method: string;
Expand All @@ -28,10 +26,6 @@ export async function validateWithSecurityAlertsAPI(
});
}

export async function getSecurityAlertsAPISupportedChainIds(): Promise<Hex[]> {
return request(ENDPOINT_SUPPORTED_CHAINS);
}

async function request(endpoint: string, options?: RequestInit) {
const url = getUrl(endpoint);

Expand Down
30 changes: 0 additions & 30 deletions app/util/blockaid/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import {
import * as NetworkControllerMock from '../../selectors/networkController';
import { NETWORKS_CHAIN_ID } from '../../constants/network';
import Engine from '../../core/Engine';
import ppomUtil from '../../lib/ppom/ppom-util';

import {
getBlockaidMetricsParams,
isBlockaidSupportedOnCurrentChain,
getBlockaidTransactionMetricsParams,
isBlockaidFeatureEnabled,
TransactionType,
Expand All @@ -30,18 +28,12 @@ jest.mock('../../core/Engine', () => ({
},
}));

const mockIsChainSupported = jest.fn().mockResolvedValue(true);

describe('Blockaid util', () => {
describe('getBlockaidTransactionMetricsParams', () => {
beforeEach(() => {
jest
.spyOn(NetworkControllerMock, 'selectChainId')
.mockReturnValue(NETWORKS_CHAIN_ID.MAINNET);

jest
.spyOn(ppomUtil, 'isChainSupported')
.mockImplementation(mockIsChainSupported);
});

afterEach(() => {
Expand Down Expand Up @@ -194,28 +186,6 @@ describe('Blockaid util', () => {
});
});

describe('isBlockaidSupportedOnCurrentChain', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('return true if blockaid is supported on current network', async () => {
jest
.spyOn(NetworkControllerMock, 'selectChainId')
.mockReturnValue(NETWORKS_CHAIN_ID.MAINNET);
const result = await isBlockaidSupportedOnCurrentChain();
expect(result).toEqual(true);
});

it('return false if blockaid is not on current network', async () => {
jest
.spyOn(NetworkControllerMock, 'selectChainId')
.mockReturnValue(NETWORKS_CHAIN_ID.GOERLI);
const result = await isBlockaidSupportedOnCurrentChain();
expect(result).toEqual(false);
});
});

describe('isBlockaidFeatureEnabled', () => {
afterEach(() => {
jest.clearAllMocks();
Expand Down
10 changes: 1 addition & 9 deletions app/util/blockaid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import {
ResultType,
SecurityAlertResponse,
} from '../../components/Views/confirmations/components/BlockaidBanner/BlockaidBanner.types';
import { store } from '../../store';
import { selectChainId } from '../../selectors/networkController';
import type { TransactionMeta } from '@metamask/transaction-controller';
import PPOMUtils from '../../lib/ppom/ppom-util';

interface TransactionSecurityAlertResponseType {
securityAlertResponses: Record<string, SecurityAlertResponse>;
Expand All @@ -15,18 +12,13 @@ interface TransactionSecurityAlertResponseType {
export type TransactionType = TransactionMeta &
TransactionSecurityAlertResponseType;

export const isBlockaidSupportedOnCurrentChain = async (): Promise<boolean> => {
const chainId = selectChainId(store.getState());
return await PPOMUtils.isChainSupported(chainId);
};

export const isBlockaidPreferenceEnabled = (): boolean => {
const { PreferencesController } = Engine.context;
return PreferencesController.state.securityAlertsEnabled;
};

export const isBlockaidFeatureEnabled = async (): Promise<boolean> =>
(await isBlockaidSupportedOnCurrentChain()) && isBlockaidPreferenceEnabled();
isBlockaidPreferenceEnabled();

export const getBlockaidMetricsParams = (
securityAlertResponse?: SecurityAlertResponse,
Expand Down
17 changes: 0 additions & 17 deletions app/util/networks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,6 @@ export const NetworkList = {

const NetworkListKeys = Object.keys(NetworkList);

export const SECURITY_PROVIDER_SUPPORTED_CHAIN_IDS_FALLBACK_LIST = [
NETWORKS_CHAIN_ID.MAINNET,
NETWORKS_CHAIN_ID.BSC,
NETWORKS_CHAIN_ID.BASE,
NETWORKS_CHAIN_ID.POLYGON,
NETWORKS_CHAIN_ID.ARBITRUM,
NETWORKS_CHAIN_ID.OPTIMISM,
NETWORKS_CHAIN_ID.AVAXCCHAIN,
NETWORKS_CHAIN_ID.LINEA_MAINNET,
NETWORKS_CHAIN_ID.SEPOLIA,
NETWORKS_CHAIN_ID.OPBNB,
NETWORKS_CHAIN_ID.ZKSYNC_ERA,
NETWORKS_CHAIN_ID.SCROLL,
NETWORKS_CHAIN_ID.BERACHAIN,
NETWORKS_CHAIN_ID.METACHAIN_ONE,
];

export const BLOCKAID_SUPPORTED_NETWORK_NAMES = {
[NETWORKS_CHAIN_ID.MAINNET]: 'Ethereum Mainnet',
[NETWORKS_CHAIN_ID.BSC]: 'Binance Smart Chain',
Expand Down
21 changes: 0 additions & 21 deletions e2e/api-mocking/mock-config/mock-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,6 @@ export const mockEvents = {
responseCode: 200,
},

securityAlertApiSupportedChains: {
urlEndpoint: 'https://security-alerts.api.cx.metamask.io/supportedChains',
response: [
'0xa4b1',
'0xa86a',
'0x2105',
'0x138d5',
'0x38',
'0xe708',
'0x1',
'0x1b6e6',
'0xcc',
'0xa',
'0x89',
'0x82750',
'0xaa36a7',
'0x144',
],
responseCode: 200,
},

remoteFeatureFlags: {
urlEndpoint:
'https://client-config.api.cx.metamask.io/v1/flags?client=mobile&distribution=main&environment=dev',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ describe(SmokeConfirmations('Security Alert API - Send flow'), () => {

it('should not show security alerts for benign requests', async () => {
const testSpecificMock = {
GET: [mockEvents.GET.securityAlertApiSupportedChains],
GET: [
mockEvents.GET.remoteFeatureFlags,
],
POST: [mockEvents.POST.securityAlertApiValidate],
};

Expand All @@ -67,7 +69,9 @@ describe(SmokeConfirmations('Security Alert API - Send flow'), () => {

it('should show security alerts for malicious request', async () => {
const testSpecificMock = {
GET: [mockEvents.GET.securityAlertApiSupportedChains],
GET: [
mockEvents.GET.remoteFeatureFlags,
],
POST: [
{
...mockEvents.POST.securityAlertApiValidate,
Expand All @@ -92,7 +96,7 @@ describe(SmokeConfirmations('Security Alert API - Send flow'), () => {
it('should show security alerts for error when validating request fails', async () => {
const testSpecificMock = {
GET: [
mockEvents.GET.securityAlertApiSupportedChains,
mockEvents.GET.remoteFeatureFlags,
{
urlEndpoint:
'https://static.cx.metamask.io/api/v1/confirmations/ppom/ppom_version.json',
Expand Down
Loading
Loading