From 4e52fe2494d077fdee80fd895192eaa6ce55ae46 Mon Sep 17 00:00:00 2001 From: Jyoti Puri Date: Tue, 1 Aug 2023 16:59:41 +0530 Subject: [PATCH] Adding metrics events for blockaid (#20091) --- app/scripts/controllers/transactions/index.js | 39 ++++++++++++++++++- .../controllers/transactions/index.test.js | 26 +++++++++++++ app/scripts/metamask-controller.js | 3 ++ shared/constants/security-provider.ts | 5 +++ 4 files changed, 71 insertions(+), 2 deletions(-) diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js index d030fd4dc..84847b645 100644 --- a/app/scripts/controllers/transactions/index.js +++ b/app/scripts/controllers/transactions/index.js @@ -56,6 +56,12 @@ import { isEIP1559Transaction, } from '../../../../shared/modules/transaction.utils'; import { ORIGIN_METAMASK } from '../../../../shared/constants/app'; +///: BEGIN:ONLY_INCLUDE_IN(blockaid) +import { + BlockaidReason, + BlockaidResultType, +} from '../../../../shared/constants/security-provider'; +///: END:ONLY_INCLUDE_IN import { calcGasTotal, getSwapsTokensReceivedFromTxMeta, @@ -327,6 +333,7 @@ export default class TransactionController extends EventEmitter { this.txStateManager.wipeTransactions(address); } + /* eslint-disable jsdoc/require-param, jsdoc/check-param-names */ /** * Add a new unapproved transaction * @@ -342,7 +349,7 @@ export default class TransactionController extends EventEmitter { * @param {boolean} opts.swaps.meta - Additional metadata to store for the transaction * @param {TransactionType} opts.type - Type of transaction to add, such as 'cancel' or 'swap' * @returns {Promise<{transactionMeta: TransactionMeta, result: Promise}>} An object containing the transaction metadata, and a promise that resolves to the transaction hash after being submitted to the network - */ + */ /* eslint-enable jsdoc/require-param, jsdoc/check-param-names */ async addTransaction( txParams, { @@ -353,6 +360,9 @@ export default class TransactionController extends EventEmitter { sendFlowHistory, swaps: { hasApproveTx, meta } = {}, type, + ///: BEGIN:ONLY_INCLUDE_IN(blockaid) + securityAlertResponse, + ///: END:ONLY_INCLUDE_IN } = {}, ) { log.debug(`MetaMaskController addTransaction ${JSON.stringify(txParams)}`); @@ -364,6 +374,9 @@ export default class TransactionController extends EventEmitter { sendFlowHistory, swaps: { hasApproveTx, meta }, type, + ///: BEGIN:ONLY_INCLUDE_IN(blockaid) + securityAlertResponse, + ///: END:ONLY_INCLUDE_IN }); return { @@ -1469,7 +1482,17 @@ export default class TransactionController extends EventEmitter { async _createTransaction( txParams, - { actionId, method, origin, sendFlowHistory = [], swaps, type }, + { + actionId, + method, + origin, + sendFlowHistory = [], + swaps, + type, + ///: BEGIN:ONLY_INCLUDE_IN(blockaid) + securityAlertResponse, + ///: END:ONLY_INCLUDE_IN + }, ) { if ( type !== undefined && @@ -1504,6 +1527,9 @@ export default class TransactionController extends EventEmitter { txParams: normalizedTxParams, origin, sendFlowHistory, + ///: BEGIN:ONLY_INCLUDE_IN(blockaid) + securityAlertResponse, + ///: END:ONLY_INCLUDE_IN }); // Add actionId to txMeta to check if same actionId is seen again @@ -2233,6 +2259,9 @@ export default class TransactionController extends EventEmitter { finalApprovalAmount, contractMethodName, securityProviderResponse, + ///: BEGIN:ONLY_INCLUDE_IN(blockaid) + securityAlertResponse, + ///: END:ONLY_INCLUDE_IN } = txMeta; const source = referrer === ORIGIN_METAMASK ? 'user' : 'dapp'; @@ -2411,6 +2440,12 @@ export default class TransactionController extends EventEmitter { transaction_type: transactionType, transaction_speed_up: type === TransactionType.retry, ui_customizations: uiCustomizations, + ///: BEGIN:ONLY_INCLUDE_IN(blockaid) + security_alert_response: + securityAlertResponse?.result_type ?? BlockaidResultType.NotApplicable, + security_alert_reason: + securityAlertResponse?.reason ?? BlockaidReason.notApplicable, + ///: END:ONLY_INCLUDE_IN }; if (transactionContractMethod === contractMethodNames.APPROVE) { diff --git a/app/scripts/controllers/transactions/index.test.js b/app/scripts/controllers/transactions/index.test.js index be2366578..fb759f1af 100644 --- a/app/scripts/controllers/transactions/index.test.js +++ b/app/scripts/controllers/transactions/index.test.js @@ -8,6 +8,10 @@ import { ApprovalType } from '@metamask/controller-utils'; import sinon from 'sinon'; import { errorCodes, ethErrors } from 'eth-rpc-errors'; +import { + BlockaidReason, + BlockaidResultType, +} from '../../../../shared/constants/security-provider'; import { createTestProviderTools, getTestAccounts, @@ -2163,6 +2167,8 @@ describe('Transaction Controller', function () { device_model: 'N/A', transaction_speed_up: false, ui_customizations: null, + security_alert_reason: BlockaidReason.notApplicable, + security_alert_response: BlockaidResultType.NotApplicable, status: 'unapproved', }, sensitiveProperties: { @@ -2250,6 +2256,8 @@ describe('Transaction Controller', function () { device_model: 'N/A', transaction_speed_up: false, ui_customizations: null, + security_alert_reason: BlockaidReason.notApplicable, + security_alert_response: BlockaidResultType.NotApplicable, status: 'unapproved', }, sensitiveProperties: { @@ -2349,6 +2357,8 @@ describe('Transaction Controller', function () { device_model: 'N/A', transaction_speed_up: false, ui_customizations: null, + security_alert_reason: BlockaidReason.notApplicable, + security_alert_response: BlockaidResultType.NotApplicable, status: 'unapproved', }, sensitiveProperties: { @@ -2438,6 +2448,8 @@ describe('Transaction Controller', function () { device_model: 'N/A', transaction_speed_up: false, ui_customizations: null, + security_alert_reason: BlockaidReason.notApplicable, + security_alert_response: BlockaidResultType.NotApplicable, status: 'unapproved', }, sensitiveProperties: { @@ -2505,6 +2517,10 @@ describe('Transaction Controller', function () { securityProviderResponse: { flagAsDangerous: 0, }, + securityAlertResponse: { + security_alert_reason: BlockaidReason.notApplicable, + security_alert_response: BlockaidResultType.NotApplicable, + }, }; const expectedPayload = { @@ -2529,6 +2545,8 @@ describe('Transaction Controller', function () { device_model: 'N/A', transaction_speed_up: false, ui_customizations: null, + security_alert_reason: BlockaidReason.notApplicable, + security_alert_response: BlockaidResultType.NotApplicable, status: 'unapproved', }, sensitiveProperties: { @@ -2601,6 +2619,8 @@ describe('Transaction Controller', function () { device_model: 'N/A', transaction_speed_up: false, ui_customizations: null, + security_alert_reason: BlockaidReason.notApplicable, + security_alert_response: BlockaidResultType.NotApplicable, status: 'unapproved', }, sensitiveProperties: { @@ -2675,6 +2695,8 @@ describe('Transaction Controller', function () { device_model: 'N/A', transaction_speed_up: false, ui_customizations: ['flagged_as_malicious'], + security_alert_reason: BlockaidReason.notApplicable, + security_alert_response: BlockaidResultType.NotApplicable, status: 'unapproved', }, sensitiveProperties: { @@ -2749,6 +2771,8 @@ describe('Transaction Controller', function () { device_model: 'N/A', transaction_speed_up: false, ui_customizations: ['flagged_as_safety_unknown'], + security_alert_reason: BlockaidReason.notApplicable, + security_alert_response: BlockaidResultType.NotApplicable, status: 'unapproved', }, sensitiveProperties: { @@ -2831,6 +2855,8 @@ describe('Transaction Controller', function () { device_model: 'N/A', transaction_speed_up: false, ui_customizations: null, + security_alert_reason: BlockaidReason.notApplicable, + security_alert_response: BlockaidResultType.NotApplicable, status: 'unapproved', }, sensitiveProperties: { diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 7cf31bdb5..04f2ce22e 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -3595,6 +3595,9 @@ export default class MetamaskController extends EventEmitter { origin: req.origin, // This is the default behaviour but specified here for clarity requireApproval: true, + ///: BEGIN:ONLY_INCLUDE_IN(blockaid) + securityAlertResponse: req.securityAlertResponse, + ///: END:ONLY_INCLUDE_IN }); return await result; diff --git a/shared/constants/security-provider.ts b/shared/constants/security-provider.ts index b956a8e75..5dba77dae 100644 --- a/shared/constants/security-provider.ts +++ b/shared/constants/security-provider.ts @@ -47,12 +47,17 @@ export enum BlockaidReason { unfairTrade = 'unfair_trade', other = 'other', + + // Locally defined + notApplicable = 'NotApplicable', } export enum BlockaidResultType { Malicious = 'Malicious', Warning = 'Warning', Benign = 'Benign', + // Locally defined + NotApplicable = 'NotApplicable', } /**