diff --git a/app/components/UI/ApproveTransactionReview/index.js b/app/components/UI/ApproveTransactionReview/index.js index 2e486ef15f9..4303be78622 100644 --- a/app/components/UI/ApproveTransactionReview/index.js +++ b/app/components/UI/ApproveTransactionReview/index.js @@ -53,10 +53,7 @@ import TransactionReviewDetailsCard from '../../UI/TransactionReview/Transaction import AppConstants from '../../../core/AppConstants'; import { UINT256_HEX_MAX_VALUE } from '../../../constants/transaction'; import { WALLET_CONNECT_ORIGIN } from '../../../util/walletconnect'; -import { - isBlockaidFeatureEnabled, - getBlockaidMetricsParams, -} from '../../../util/blockaid'; +import { getBlockaidMetricsParams } from '../../../util/blockaid'; import { withNavigation } from '@react-navigation/compat'; import { isTestNet, @@ -822,13 +819,11 @@ class ApproveTransactionReview extends PureComponent { style={styles.accountApprovalWrapper} onStartShouldSetResponder={() => true} > - {isBlockaidFeatureEnabled() && ( - - )} + {strings( `spend_limit_edition.${ diff --git a/app/components/UI/BlockaidBanner/BlockaidBanner.test.tsx b/app/components/UI/BlockaidBanner/BlockaidBanner.test.tsx index 069bbd806ff..da07dc8291b 100644 --- a/app/components/UI/BlockaidBanner/BlockaidBanner.test.tsx +++ b/app/components/UI/BlockaidBanner/BlockaidBanner.test.tsx @@ -15,6 +15,7 @@ import renderWithProvider from '../../../util/test/renderWithProvider'; jest.mock('../../../util/blockaid', () => ({ isBlockaidFeatureEnabled: jest.fn().mockReturnValue(true), + isBlockaidSupportedOnCurrentChain: jest.fn().mockReturnValue(true), })); jest.mock('react-native-gzip', () => ({ diff --git a/app/components/UI/BlockaidBanner/BlockaidBanner.tsx b/app/components/UI/BlockaidBanner/BlockaidBanner.tsx index b91e9ccd95c..6df2cea596a 100644 --- a/app/components/UI/BlockaidBanner/BlockaidBanner.tsx +++ b/app/components/UI/BlockaidBanner/BlockaidBanner.tsx @@ -19,7 +19,10 @@ import { import Icon from '../../../component-library/components/Icons/Icon/Icon'; import Text from '../../../component-library/components/Texts/Text/Text'; import { useStyles } from '../../../component-library/hooks/useStyles'; -import { isBlockaidFeatureEnabled } from '../../../util/blockaid'; +import { + isBlockaidFeatureEnabled, + isBlockaidSupportedOnCurrentChain, +} from '../../../util/blockaid'; import { ATTRIBUTION_LINE_TEST_ID, FALSE_POSITIVE_REPOST_LINE_TEST_ID, @@ -38,11 +41,7 @@ import { FALSE_POSITIVE_REPORT_BASE_URL, UTM_SOURCE, } from '../../../constants/urls'; -import { - BLOCKAID_SUPPORTED_CHAIN_IDS, - BLOCKAID_SUPPORTED_NETWORK_NAMES, -} from '../../../util/networks'; -import { selectChainId } from '../../../selectors/networkController'; +import { BLOCKAID_SUPPORTED_NETWORK_NAMES } from '../../../util/networks'; import { selectIsSecurityAlertsEnabled } from '../../../selectors/preferencesController'; import BlockaidVersionInfo from '../../../lib/ppom/blockaid-version'; @@ -99,7 +98,6 @@ const BlockaidBanner = (bannerProps: BlockaidBannerProps) => { } = bannerProps; const { styles, theme } = useStyles(styleSheet, { style }); const [displayPositiveResponse, setDisplayPositiveResponse] = useState(false); - const networkChainId = useSelector(selectChainId); const [reportUrl, setReportUrl] = useState(''); const isSecurityAlertsEnabled = useSelector(selectIsSecurityAlertsEnabled); @@ -141,7 +139,7 @@ const BlockaidBanner = (bannerProps: BlockaidBannerProps) => { if ( !securityAlertResponse || !isBlockaidFeatureEnabled() || - !BLOCKAID_SUPPORTED_CHAIN_IDS.includes(networkChainId) || + !isBlockaidSupportedOnCurrentChain() || !isSecurityAlertsEnabled ) { return null; diff --git a/app/components/UI/SignatureRequest/index.js b/app/components/UI/SignatureRequest/index.js index 0a2f195ebf9..babd4738610 100644 --- a/app/components/UI/SignatureRequest/index.js +++ b/app/components/UI/SignatureRequest/index.js @@ -12,7 +12,6 @@ import { selectProviderType } from '../../../selectors/networkController'; import { fontStyles } from '../../../styles/common'; import { isHardwareAccount } from '../../../util/address'; import AnalyticsV2 from '../../../util/analyticsV2'; -import { isBlockaidFeatureEnabled } from '../../../util/blockaid'; import { getHost } from '../../../util/browser'; import { getAnalyticsParams } from '../../../util/confirmation/signatureUtils'; import Device from '../../../util/device'; @@ -367,13 +366,11 @@ class SignatureRequest extends PureComponent { ) : null} - {isBlockaidFeatureEnabled() && ( - - )} + {this.renderActionViewChildren()} diff --git a/app/components/UI/TransactionBlockaidBanner/TransactionBlockaidBanner.test.tsx b/app/components/UI/TransactionBlockaidBanner/TransactionBlockaidBanner.test.tsx index 444e42bb5c7..deda858f061 100644 --- a/app/components/UI/TransactionBlockaidBanner/TransactionBlockaidBanner.test.tsx +++ b/app/components/UI/TransactionBlockaidBanner/TransactionBlockaidBanner.test.tsx @@ -9,6 +9,7 @@ import TransactionBlockaidBanner from './TransactionBlockaidBanner'; jest.mock('../../../util/blockaid', () => ({ isBlockaidFeatureEnabled: jest.fn().mockReturnValue(true), + isBlockaidSupportedOnCurrentChain: jest.fn().mockReturnValue(true), })); jest.mock('react-native-gzip', () => ({ diff --git a/app/components/UI/TransactionReview/index.js b/app/components/UI/TransactionReview/index.js index 1df6a91de8c..12bb0cd52eb 100644 --- a/app/components/UI/TransactionReview/index.js +++ b/app/components/UI/TransactionReview/index.js @@ -34,10 +34,7 @@ import { } from '../../../util/number'; import { safeToChecksumAddress } from '../../../util/address'; import Device from '../../../util/device'; -import { - isBlockaidFeatureEnabled, - getBlockaidMetricsParams, -} from '../../../util/blockaid'; +import { getBlockaidMetricsParams } from '../../../util/blockaid'; import TransactionReviewInformation from './TransactionReviewInformation'; import TransactionReviewSummary from './TransactionReviewSummary'; import TransactionReviewData from './TransactionReviewData'; @@ -518,13 +515,11 @@ class TransactionReview extends PureComponent { style={styles.accountTransactionWrapper} onStartShouldSetResponder={() => true} > - {isBlockaidFeatureEnabled() && ( - - )} + ({ jest.mock('../../../../util/blockaid', () => ({ isBlockaidFeatureEnabled: jest.fn().mockReturnValue(true), + isBlockaidSupportedOnCurrentChain: jest.fn().mockReturnValue(true), })); jest.mock('../../../../core/Engine', () => ({ diff --git a/app/core/RPCMethods/RPCMethodMiddleware.ts b/app/core/RPCMethods/RPCMethodMiddleware.ts index 30d00a5643b..3aef41d4333 100644 --- a/app/core/RPCMethods/RPCMethodMiddleware.ts +++ b/app/core/RPCMethods/RPCMethodMiddleware.ts @@ -19,7 +19,6 @@ import Networks, { blockTagParamIndex, getAllNetworks, } from '../../util/networks'; -import { isBlockaidFeatureEnabled } from '../../util/blockaid'; import { polyfillGasPrice } from './utils'; import ImportedEngine from '../Engine'; import { strings } from '../../../locales/i18n'; @@ -550,9 +549,7 @@ export const getRpcMethodMiddleware = ({ address: req.params[0].from, checkSelectedAddress: isMMSDK || isWalletConnect, }); - if (isBlockaidFeatureEnabled()) { - PPOMUtil.validateRequest(req); - } + PPOMUtil.validateRequest(req); const rawSig = await SignatureController.newUnsignedMessage({ data: req.params[1], from: req.params[0], @@ -600,9 +597,7 @@ export const getRpcMethodMiddleware = ({ checkSelectedAddress: isMMSDK || isWalletConnect, }); - if (isBlockaidFeatureEnabled()) { - PPOMUtil.validateRequest(req); - } + PPOMUtil.validateRequest(req); const rawSig = await SignatureController.newUnsignedPersonalMessage({ ...params, @@ -649,9 +644,7 @@ export const getRpcMethodMiddleware = ({ checkSelectedAddress: isMMSDK || isWalletConnect, }); - if (isBlockaidFeatureEnabled()) { - PPOMUtil.validateRequest(req); - } + PPOMUtil.validateRequest(req); const rawSig = await SignatureController.newUnsignedTypedMessage( { @@ -673,9 +666,7 @@ export const getRpcMethodMiddleware = ({ ? JSON.parse(req.params[1]) : req.params[1]; const chainId = data.domain.chainId; - if (isBlockaidFeatureEnabled()) { - PPOMUtil.validateRequest(req); - } + PPOMUtil.validateRequest(req); res.result = await generateRawSignature({ version: 'V3', req, @@ -695,9 +686,7 @@ export const getRpcMethodMiddleware = ({ eth_signTypedData_v4: async () => { const data = JSON.parse(req.params[1]); const chainId = data.domain.chainId; - if (isBlockaidFeatureEnabled()) { - PPOMUtil.validateRequest(req); - } + PPOMUtil.validateRequest(req); res.result = await generateRawSignature({ version: 'V4', req, diff --git a/app/core/RPCMethods/eth_sendTransaction.ts b/app/core/RPCMethods/eth_sendTransaction.ts index 6755c0103bb..154e4c8deb6 100644 --- a/app/core/RPCMethods/eth_sendTransaction.ts +++ b/app/core/RPCMethods/eth_sendTransaction.ts @@ -5,7 +5,6 @@ import { } from '@metamask/transaction-controller'; import { ethErrors } from 'eth-json-rpc-errors'; import ppomUtil from '../../lib/ppom/ppom-util'; -import { isBlockaidFeatureEnabled } from '../../util/blockaid'; /** * A JavaScript object that is not `null`, a function, or an array. @@ -99,9 +98,7 @@ async function eth_sendTransaction({ origin: hostname, }); - if (isBlockaidFeatureEnabled()) { - ppomUtil.validateRequest(req, transactionMeta?.id); - } + ppomUtil.validateRequest(req, transactionMeta?.id); res.result = await result; } diff --git a/app/util/blockaid/index.test.ts b/app/util/blockaid/index.test.ts index 60fe4fed253..663ebbef4b5 100644 --- a/app/util/blockaid/index.test.ts +++ b/app/util/blockaid/index.test.ts @@ -1,4 +1,3 @@ -import { getBlockaidMetricsParams } from '.'; import { Reason, ResultType, @@ -8,78 +7,104 @@ import { import * as NetworkControllerMock from '../../selectors/networkController'; import { NETWORKS_CHAIN_ID } from '../../constants/network'; -describe('getBlockaidMetricsParams', () => { - beforeEach(() => { - jest - .spyOn(NetworkControllerMock, 'selectChainId') - .mockReturnValue(NETWORKS_CHAIN_ID.MAINNET); - }); +import { getBlockaidMetricsParams, isBlockaidSupportedOnCurrentChain } from '.'; - afterEach(() => { - jest.clearAllMocks(); - }); +describe('Blockaid util', () => { + describe('getBlockaidMetricsParams', () => { + beforeEach(() => { + jest + .spyOn(NetworkControllerMock, 'selectChainId') + .mockReturnValue(NETWORKS_CHAIN_ID.MAINNET); + }); - it('returns empty object when securityAlertResponse is not defined', () => { - const result = getBlockaidMetricsParams(undefined); - expect(result).toStrictEqual({}); - }); + afterEach(() => { + jest.clearAllMocks(); + }); - it('returns enpty object when chain id is not in supported chain ids list', () => { - jest.spyOn(NetworkControllerMock, 'selectChainId').mockReturnValue('10'); - const result = getBlockaidMetricsParams(undefined); - expect(result).toStrictEqual({}); - }); + it('returns empty object when securityAlertResponse is not defined', () => { + const result = getBlockaidMetricsParams(undefined); + expect(result).toStrictEqual({}); + }); + + it('returns enpty object when chain id is not in supported chain ids list', () => { + jest.spyOn(NetworkControllerMock, 'selectChainId').mockReturnValue('10'); + const result = getBlockaidMetricsParams(undefined); + expect(result).toStrictEqual({}); + }); - it('should return additionalParams object when securityAlertResponse is defined', () => { - const securityAlertResponse: SecurityAlertResponse = { - result_type: ResultType.Malicious, - reason: Reason.notApplicable, - providerRequestsCount: { - eth_call: 5, - eth_getCode: 3, - }, - features: [], - }; + it('should return additionalParams object when securityAlertResponse is defined', () => { + const securityAlertResponse: SecurityAlertResponse = { + result_type: ResultType.Malicious, + reason: Reason.notApplicable, + providerRequestsCount: { + eth_call: 5, + eth_getCode: 3, + }, + features: [], + }; - const result = getBlockaidMetricsParams(securityAlertResponse); - expect(result).toEqual({ - ui_customizations: ['flagged_as_malicious'], - security_alert_response: ResultType.Malicious, - security_alert_reason: Reason.notApplicable, - ppom_eth_call_count: 5, - ppom_eth_getCode_count: 3, + const result = getBlockaidMetricsParams(securityAlertResponse); + expect(result).toEqual({ + ui_customizations: ['flagged_as_malicious'], + security_alert_response: ResultType.Malicious, + security_alert_reason: Reason.notApplicable, + ppom_eth_call_count: 5, + ppom_eth_getCode_count: 3, + }); + }); + + it('should not return eth call counts if providerRequestsCount is empty', () => { + const securityAlertResponse: SecurityAlertResponse = { + result_type: ResultType.Malicious, + reason: Reason.notApplicable, + features: [], + providerRequestsCount: {}, + }; + + const result = getBlockaidMetricsParams(securityAlertResponse); + expect(result).toEqual({ + ui_customizations: ['flagged_as_malicious'], + security_alert_response: ResultType.Malicious, + security_alert_reason: Reason.notApplicable, + }); }); - }); - it('should not return eth call counts if providerRequestsCount is empty', () => { - const securityAlertResponse: SecurityAlertResponse = { - result_type: ResultType.Malicious, - reason: Reason.notApplicable, - features: [], - providerRequestsCount: {}, - }; + it('should not return eth call counts if providerRequestsCount is undefined', () => { + const securityAlertResponse: SecurityAlertResponse = { + result_type: ResultType.Malicious, + reason: Reason.notApplicable, + features: [], + providerRequestsCount: undefined, + }; - const result = getBlockaidMetricsParams(securityAlertResponse); - expect(result).toEqual({ - ui_customizations: ['flagged_as_malicious'], - security_alert_response: ResultType.Malicious, - security_alert_reason: Reason.notApplicable, + const result = getBlockaidMetricsParams(securityAlertResponse); + expect(result).toEqual({ + ui_customizations: ['flagged_as_malicious'], + security_alert_response: ResultType.Malicious, + security_alert_reason: Reason.notApplicable, + }); }); }); - it('should not return eth call counts if providerRequestsCount is undefined', () => { - const securityAlertResponse: SecurityAlertResponse = { - result_type: ResultType.Malicious, - reason: Reason.notApplicable, - features: [], - providerRequestsCount: undefined, - }; + describe('isBlockaidSupportedOnCurrentChain', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + it('return true if blockaid is supported on current network', () => { + jest + .spyOn(NetworkControllerMock, 'selectChainId') + .mockReturnValue(NETWORKS_CHAIN_ID.MAINNET); + const result = isBlockaidSupportedOnCurrentChain(); + expect(result).toEqual(true); + }); - const result = getBlockaidMetricsParams(securityAlertResponse); - expect(result).toEqual({ - ui_customizations: ['flagged_as_malicious'], - security_alert_response: ResultType.Malicious, - security_alert_reason: Reason.notApplicable, + it('return false if blockaid is not on current network', () => { + jest + .spyOn(NetworkControllerMock, 'selectChainId') + .mockReturnValue(NETWORKS_CHAIN_ID.GOERLI); + const result = isBlockaidSupportedOnCurrentChain(); + expect(result).toEqual(false); }); }); }); diff --git a/app/util/blockaid/index.ts b/app/util/blockaid/index.ts index 967f727baaf..41404905105 100644 --- a/app/util/blockaid/index.ts +++ b/app/util/blockaid/index.ts @@ -24,19 +24,25 @@ export const isSupportedChainId = (chainId: string) => { }; // eslint-disable-next-line import/prefer-default-export -export const isBlockaidFeatureEnabled = () => { +export const isBlockaidSupportedOnCurrentChain = () => { const chainId = selectChainId(store.getState()); - return ( - process.env.MM_BLOCKAID_UI_ENABLED === 'true' && isSupportedChainId(chainId) - ); + return isSupportedChainId(chainId); }; +// eslint-disable-next-line import/prefer-default-export +export const isBlockaidFeatureEnabled = () => + process.env.MM_BLOCKAID_UI_ENABLED; + export const getBlockaidMetricsParams = ( securityAlertResponse?: SecurityAlertResponse, ) => { const additionalParams: Record = {}; - if (securityAlertResponse && isBlockaidFeatureEnabled()) { + if ( + securityAlertResponse && + isBlockaidFeatureEnabled() && + isBlockaidSupportedOnCurrentChain() + ) { const { result_type, reason, providerRequestsCount } = securityAlertResponse;