From 1b9539ab310d05ec7635c99fd7e03adc9a93bd39 Mon Sep 17 00:00:00 2001 From: Jyoti Puri Date: Wed, 19 Jul 2023 17:15:51 +0530 Subject: [PATCH] Adding metrics events for blockaid --- app/scripts/controllers/transactions/index.js | 17 ++++++++++++++ .../controllers/transactions/index.test.js | 22 +++++++++++++++++++ shared/constants/security-provider.ts | 3 +++ 3 files changed, 42 insertions(+) diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js index 37de2830e610..de293cccb3c3 100644 --- a/app/scripts/controllers/transactions/index.js +++ b/app/scripts/controllers/transactions/index.js @@ -53,6 +53,10 @@ import { isEIP1559Transaction, } from '../../../../shared/modules/transaction.utils'; import { ORIGIN_METAMASK } from '../../../../shared/constants/app'; +import { + BlockaidReason, + BlockaidResultType, +} from '../../../../shared/constants/security-provider'; import { calcGasTotal, getSwapsTokensReceivedFromTxMeta, @@ -359,6 +363,7 @@ export default class TransactionController extends EventEmitter { undefined, undefined, opts.id, + { securityAlertResponse: opts.securityAlertResponse }, ); const txId = initialTxMeta.id; @@ -1636,6 +1641,9 @@ export default class TransactionController extends EventEmitter { txParams: normalizedTxParams, origin, sendFlowHistory, + ///: BEGIN:ONLY_INCLUDE_IN(blockaid) + securityAlertResponse: options.securityAlertResponse, + ///: END:ONLY_INCLUDE_IN }); // Add actionId to txMeta to check if same actionId is seen again @@ -2322,6 +2330,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'; @@ -2498,6 +2509,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 8600f08dc4b5..8303fd63de93 100644 --- a/app/scripts/controllers/transactions/index.test.js +++ b/app/scripts/controllers/transactions/index.test.js @@ -2423,6 +2423,8 @@ describe('Transaction Controller', function () { device_model: 'N/A', transaction_speed_up: false, ui_customizations: null, + security_alert_reason: 'not_applicable', + security_alert_response: 'not_applicable', }, sensitiveProperties: { default_gas: '0.000031501', @@ -2510,6 +2512,8 @@ describe('Transaction Controller', function () { device_model: 'N/A', transaction_speed_up: false, ui_customizations: null, + security_alert_reason: 'not_applicable', + security_alert_response: 'not_applicable', }, sensitiveProperties: { default_gas: '0.000031501', @@ -2609,6 +2613,8 @@ describe('Transaction Controller', function () { device_model: 'N/A', transaction_speed_up: false, ui_customizations: null, + security_alert_reason: 'not_applicable', + security_alert_response: 'not_applicable', }, sensitiveProperties: { default_gas: '0.000031501', @@ -2698,6 +2704,8 @@ describe('Transaction Controller', function () { device_model: 'N/A', transaction_speed_up: false, ui_customizations: null, + security_alert_reason: 'not_applicable', + security_alert_response: 'not_applicable', }, sensitiveProperties: { default_gas: '0.000031501', @@ -2765,6 +2773,10 @@ describe('Transaction Controller', function () { securityProviderResponse: { flagAsDangerous: 0, }, + securityAlertResponse: { + result_type: 'Benign', + reason: 'none', + }, }; const expectedPayload = { @@ -2789,6 +2801,8 @@ describe('Transaction Controller', function () { device_model: 'N/A', transaction_speed_up: false, ui_customizations: null, + security_alert_response: 'Benign', + security_alert_reason: 'none', }, sensitiveProperties: { gas_price: '2', @@ -2861,6 +2875,8 @@ describe('Transaction Controller', function () { device_model: 'N/A', transaction_speed_up: false, ui_customizations: null, + security_alert_reason: 'not_applicable', + security_alert_response: 'not_applicable', }, sensitiveProperties: { baz: 3.0, @@ -2935,6 +2951,8 @@ describe('Transaction Controller', function () { device_model: 'N/A', transaction_speed_up: false, ui_customizations: ['flagged_as_malicious'], + security_alert_reason: 'not_applicable', + security_alert_response: 'not_applicable', }, sensitiveProperties: { baz: 3.0, @@ -3009,6 +3027,8 @@ describe('Transaction Controller', function () { device_model: 'N/A', transaction_speed_up: false, ui_customizations: ['flagged_as_safety_unknown'], + security_alert_reason: 'not_applicable', + security_alert_response: 'not_applicable', }, sensitiveProperties: { baz: 3.0, @@ -3091,6 +3111,8 @@ describe('Transaction Controller', function () { device_model: 'N/A', transaction_speed_up: false, ui_customizations: null, + security_alert_reason: 'not_applicable', + security_alert_response: 'not_applicable', }, sensitiveProperties: { baz: 3.0, diff --git a/shared/constants/security-provider.ts b/shared/constants/security-provider.ts index b956a8e7529b..94bbd9f06356 100644 --- a/shared/constants/security-provider.ts +++ b/shared/constants/security-provider.ts @@ -46,6 +46,8 @@ export enum BlockaidReason { /** Malicious trade that results in the victim being rained */ unfairTrade = 'unfair_trade', + notApplicable = 'not_applicable', + other = 'other', } @@ -53,6 +55,7 @@ export enum BlockaidResultType { Malicious = 'Malicious', Warning = 'Warning', Benign = 'Benign', + NotApplicable = 'not_applicable', } /**