1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-21 17:37:01 +01:00

Adding metrics events for blockaid (#20091)

This commit is contained in:
Jyoti Puri 2023-08-01 16:59:41 +05:30 committed by GitHub
parent 2b5c112a02
commit 4e52fe2494
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 2 deletions

View File

@ -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<string>}>} 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) {

View File

@ -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: {

View File

@ -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;

View File

@ -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',
}
/**