Skip to content
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
5 changes: 5 additions & 0 deletions app/scripts/lib/transaction/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ const mockTransactionMetricsRequest = {
getNetworkRpcUrl: jest.fn(),
getFeatureFlags: jest.fn(),
getPna25Acknowledged: jest.fn(),
getAddressSecurityAlertResponse: jest.fn(),
getSecurityAlertsEnabled: jest.fn(),
} as TransactionMetricsRequest;

describe('Transaction metrics', () => {
Expand Down Expand Up @@ -184,6 +186,9 @@ describe('Transaction metrics', () => {
account_type: undefined,
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860
// eslint-disable-next-line @typescript-eslint/naming-convention
address_alert_response: 'not_applicable',
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860
// eslint-disable-next-line @typescript-eslint/naming-convention
api_method: MESSAGE_TYPE.ETH_SEND_TRANSACTION,
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down
53 changes: 53 additions & 0 deletions app/scripts/lib/transaction/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ import { shouldUseRedesignForTransactions } from '../../../../shared/lib/confirm
import { getMaximumGasTotalInHexWei } from '../../../../shared/modules/gas.utils';
import { Numeric } from '../../../../shared/modules/Numeric';
import { extractRpcDomain } from '../util';
import {
createCacheKey,
mapChainIdToSupportedEVMChain,
ResultType,
} from '../../../../shared/lib/trust-signals';

const log = createProjectLogger('transaction-metrics');

Expand Down Expand Up @@ -988,6 +993,11 @@ async function buildEventFragmentProperties({
hd_entropy_index: transactionMetricsRequest.getHDEntropyIndex(),
};

const addressAlertProperties = getAddressAlertMetricsProperties(
transactionMeta,
transactionMetricsRequest,
);

let accountType;
try {
accountType = await transactionMetricsRequest.getAccountType(
Expand Down Expand Up @@ -1029,6 +1039,7 @@ async function buildEventFragmentProperties({
...smartTransactionMetricsProperties,
...swapAndSendMetricsProperties,
...hdEntropyProperties,
...addressAlertProperties,

// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31973
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -1437,3 +1448,45 @@ function addHashProperty(
properties.transaction_hash = transactionMeta.hash;
}
}

/**
* Returns what the address_alert_response metrics property should be set to
*
* @param transactionMeta - The transaction metadata
* @param transactionMetricsRequest - The transaction metrics request
* @returns ResultType or 'not_applicable' if address alert scanning cannot be performed:
* - Security alerts feature is disabled by the user
* - Transaction has no 'to' address
* - Chain is not supported by the Security Alerts API
*/
function getAddressAlertMetricsProperties(
transactionMeta: TransactionMeta,
transactionMetricsRequest: TransactionMetricsRequest,
): { address_alert_response?: ResultType | 'not_applicable' } {
const securityAlertsEnabled =
transactionMetricsRequest.getSecurityAlertsEnabled();
if (!securityAlertsEnabled) {
return { address_alert_response: 'not_applicable' };
}

const { to } = transactionMeta.txParams;
if (typeof to !== 'string') {
return { address_alert_response: 'not_applicable' };
}

const { chainId } = transactionMeta;
const supportedEVMChain = mapChainIdToSupportedEVMChain(chainId);
if (!supportedEVMChain) {
return { address_alert_response: 'not_applicable' };
}

const cacheKey = createCacheKey(supportedEVMChain, to);
const cachedResponse =
transactionMetricsRequest.getAddressSecurityAlertResponse(cacheKey);

if (cachedResponse) {
return { address_alert_response: cachedResponse.result_type };
}

return { address_alert_response: ResultType.Loading };
}
8 changes: 8 additions & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8218,6 +8218,14 @@ export default class MetamaskController extends EventEmitter {
getPna25Acknowledged: () => {
return this.appStateController?.state?.pna25Acknowledged;
},
getAddressSecurityAlertResponse: (cacheKey) => {
return this.appStateController?.getAddressSecurityAlertResponse(
cacheKey,
);
},
getSecurityAlertsEnabled: () => {
return this.preferencesController?.state?.securityAlertsEnabled;
},
};

const snapAndHardwareMessenger = new Messenger({
Expand Down
2 changes: 2 additions & 0 deletions shared/modules/metametrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const createTransactionMetricsRequest = (customProps = {}) => {
getNetworkRpcUrl: jest.fn(),
getFeatureFlags: jest.fn(),
getPna25Acknowledged: jest.fn(),
getAddressSecurityAlertResponse: jest.fn(),
getSecurityAlertsEnabled: jest.fn(),
...customProps,
} as TransactionMetricsRequest;
};
Expand Down
5 changes: 5 additions & 0 deletions shared/types/metametrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { HardwareKeyringType } from '../constants/hardware-wallets';
// eslint-disable-next-line import/no-restricted-paths
import type { SnapAndHardwareMessenger } from '../../app/scripts/lib/snap-keyring/metrics';
import { EntryModalSourceEnum } from '../constants/subscriptions';
import type { ScanAddressResponse } from '../lib/trust-signals';

export type TransactionMetricsRequest = {
createEventFragment: (
Expand Down Expand Up @@ -74,6 +75,10 @@ export type TransactionMetricsRequest = {
getNetworkRpcUrl: (chainId: Hex) => string;
getFeatureFlags: () => Record<string, unknown>;
getPna25Acknowledged: () => boolean;
getAddressSecurityAlertResponse: (
cacheKey: string,
) => ScanAddressResponse | undefined;
getSecurityAlertsEnabled: () => boolean;
};

export type TransactionEventPayload = {
Expand Down
Loading