2023-03-14 19:31:19 +01:00
|
|
|
import { errorCodes } from 'eth-rpc-errors';
|
2023-04-04 17:25:57 +02:00
|
|
|
import { detectSIWE } from '@metamask/controller-utils';
|
2023-04-17 06:24:25 +02:00
|
|
|
import { isValidAddress } from 'ethereumjs-util';
|
|
|
|
|
2022-07-20 15:25:04 +02:00
|
|
|
import { MESSAGE_TYPE, ORIGIN_METAMASK } from '../../../shared/constants/app';
|
2023-03-29 20:25:01 +02:00
|
|
|
import { TransactionStatus } from '../../../shared/constants/transaction';
|
2022-04-04 21:26:13 +02:00
|
|
|
import { SECOND } from '../../../shared/constants/time';
|
2023-04-04 17:25:57 +02:00
|
|
|
|
2023-02-08 16:16:17 +01:00
|
|
|
import {
|
2023-04-03 17:31:04 +02:00
|
|
|
MetaMetricsEventCategory,
|
|
|
|
MetaMetricsEventName,
|
|
|
|
MetaMetricsEventUiCustomization,
|
2023-02-08 16:16:17 +01:00
|
|
|
} from '../../../shared/constants/metametrics';
|
2022-04-04 21:26:13 +02:00
|
|
|
|
2022-07-20 15:25:04 +02:00
|
|
|
/**
|
|
|
|
* These types determine how the method tracking middleware handles incoming
|
|
|
|
* requests based on the method name. There are three options right now but
|
|
|
|
* the types could be expanded to cover other options in the future.
|
|
|
|
*/
|
|
|
|
const RATE_LIMIT_TYPES = {
|
|
|
|
RATE_LIMITED: 'rate_limited',
|
|
|
|
BLOCKED: 'blocked',
|
|
|
|
NON_RATE_LIMITED: 'non_rate_limited',
|
2022-04-04 21:26:13 +02:00
|
|
|
};
|
|
|
|
|
2022-07-20 15:25:04 +02:00
|
|
|
/**
|
|
|
|
* This object maps a method name to a RATE_LIMIT_TYPE. If not in this map the
|
|
|
|
* default is 'RATE_LIMITED'
|
|
|
|
*/
|
|
|
|
const RATE_LIMIT_MAP = {
|
|
|
|
[MESSAGE_TYPE.ETH_SIGN]: RATE_LIMIT_TYPES.NON_RATE_LIMITED,
|
|
|
|
[MESSAGE_TYPE.ETH_SIGN_TYPED_DATA]: RATE_LIMIT_TYPES.NON_RATE_LIMITED,
|
|
|
|
[MESSAGE_TYPE.ETH_SIGN_TYPED_DATA_V3]: RATE_LIMIT_TYPES.NON_RATE_LIMITED,
|
|
|
|
[MESSAGE_TYPE.ETH_SIGN_TYPED_DATA_V4]: RATE_LIMIT_TYPES.NON_RATE_LIMITED,
|
|
|
|
[MESSAGE_TYPE.PERSONAL_SIGN]: RATE_LIMIT_TYPES.NON_RATE_LIMITED,
|
|
|
|
[MESSAGE_TYPE.ETH_DECRYPT]: RATE_LIMIT_TYPES.NON_RATE_LIMITED,
|
|
|
|
[MESSAGE_TYPE.ETH_GET_ENCRYPTION_PUBLIC_KEY]:
|
|
|
|
RATE_LIMIT_TYPES.NON_RATE_LIMITED,
|
|
|
|
[MESSAGE_TYPE.ETH_REQUEST_ACCOUNTS]: RATE_LIMIT_TYPES.RATE_LIMITED,
|
|
|
|
[MESSAGE_TYPE.WALLET_REQUEST_PERMISSIONS]: RATE_LIMIT_TYPES.RATE_LIMITED,
|
|
|
|
[MESSAGE_TYPE.SEND_METADATA]: RATE_LIMIT_TYPES.BLOCKED,
|
|
|
|
[MESSAGE_TYPE.GET_PROVIDER_STATE]: RATE_LIMIT_TYPES.BLOCKED,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For events with user interaction (approve / reject | cancel) this map will
|
2023-03-29 20:25:01 +02:00
|
|
|
* return an object with APPROVED, REJECTED, REQUESTED, and FAILED keys that map to the
|
2022-07-20 15:25:04 +02:00
|
|
|
* appropriate event names.
|
|
|
|
*/
|
|
|
|
const EVENT_NAME_MAP = {
|
|
|
|
[MESSAGE_TYPE.ETH_SIGN]: {
|
2023-04-03 17:31:04 +02:00
|
|
|
APPROVED: MetaMetricsEventName.SignatureApproved,
|
|
|
|
FAILED: MetaMetricsEventName.SignatureFailed,
|
|
|
|
REJECTED: MetaMetricsEventName.SignatureRejected,
|
|
|
|
REQUESTED: MetaMetricsEventName.SignatureRequested,
|
2022-07-20 15:25:04 +02:00
|
|
|
},
|
|
|
|
[MESSAGE_TYPE.ETH_SIGN_TYPED_DATA]: {
|
2023-04-03 17:31:04 +02:00
|
|
|
APPROVED: MetaMetricsEventName.SignatureApproved,
|
|
|
|
REJECTED: MetaMetricsEventName.SignatureRejected,
|
|
|
|
REQUESTED: MetaMetricsEventName.SignatureRequested,
|
2022-07-20 15:25:04 +02:00
|
|
|
},
|
|
|
|
[MESSAGE_TYPE.ETH_SIGN_TYPED_DATA_V3]: {
|
2023-04-03 17:31:04 +02:00
|
|
|
APPROVED: MetaMetricsEventName.SignatureApproved,
|
|
|
|
REJECTED: MetaMetricsEventName.SignatureRejected,
|
|
|
|
REQUESTED: MetaMetricsEventName.SignatureRequested,
|
2022-07-20 15:25:04 +02:00
|
|
|
},
|
|
|
|
[MESSAGE_TYPE.ETH_SIGN_TYPED_DATA_V4]: {
|
2023-04-03 17:31:04 +02:00
|
|
|
APPROVED: MetaMetricsEventName.SignatureApproved,
|
|
|
|
REJECTED: MetaMetricsEventName.SignatureRejected,
|
|
|
|
REQUESTED: MetaMetricsEventName.SignatureRequested,
|
2022-07-20 15:25:04 +02:00
|
|
|
},
|
|
|
|
[MESSAGE_TYPE.PERSONAL_SIGN]: {
|
2023-04-03 17:31:04 +02:00
|
|
|
APPROVED: MetaMetricsEventName.SignatureApproved,
|
|
|
|
REJECTED: MetaMetricsEventName.SignatureRejected,
|
|
|
|
REQUESTED: MetaMetricsEventName.SignatureRequested,
|
2022-07-20 15:25:04 +02:00
|
|
|
},
|
|
|
|
[MESSAGE_TYPE.ETH_DECRYPT]: {
|
2023-04-03 17:31:04 +02:00
|
|
|
APPROVED: MetaMetricsEventName.DecryptionApproved,
|
|
|
|
REJECTED: MetaMetricsEventName.DecryptionRejected,
|
|
|
|
REQUESTED: MetaMetricsEventName.DecryptionRequested,
|
2022-07-20 15:25:04 +02:00
|
|
|
},
|
|
|
|
[MESSAGE_TYPE.ETH_GET_ENCRYPTION_PUBLIC_KEY]: {
|
2023-04-03 17:31:04 +02:00
|
|
|
APPROVED: MetaMetricsEventName.EncryptionPublicKeyApproved,
|
|
|
|
REJECTED: MetaMetricsEventName.EncryptionPublicKeyRejected,
|
|
|
|
REQUESTED: MetaMetricsEventName.EncryptionPublicKeyRequested,
|
2022-07-20 15:25:04 +02:00
|
|
|
},
|
|
|
|
[MESSAGE_TYPE.ETH_REQUEST_ACCOUNTS]: {
|
2023-04-03 17:31:04 +02:00
|
|
|
APPROVED: MetaMetricsEventName.PermissionsApproved,
|
|
|
|
REJECTED: MetaMetricsEventName.PermissionsRejected,
|
|
|
|
REQUESTED: MetaMetricsEventName.PermissionsRequested,
|
2022-07-20 15:25:04 +02:00
|
|
|
},
|
|
|
|
[MESSAGE_TYPE.WALLET_REQUEST_PERMISSIONS]: {
|
2023-04-03 17:31:04 +02:00
|
|
|
APPROVED: MetaMetricsEventName.PermissionsApproved,
|
|
|
|
REJECTED: MetaMetricsEventName.PermissionsRejected,
|
|
|
|
REQUESTED: MetaMetricsEventName.PermissionsRequested,
|
2022-07-20 15:25:04 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const rateLimitTimeouts = {};
|
2022-04-04 21:26:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a middleware that tracks inpage_provider usage using sampling for
|
|
|
|
* each type of event except those that require user interaction, such as
|
|
|
|
* signature requests
|
|
|
|
*
|
|
|
|
* @param {object} opts - options for the rpc method tracking middleware
|
2022-07-20 15:25:04 +02:00
|
|
|
* @param {Function} opts.trackEvent - trackEvent method from
|
|
|
|
* MetaMetricsController
|
|
|
|
* @param {Function} opts.getMetricsState - get the state of
|
|
|
|
* MetaMetricsController
|
|
|
|
* @param {number} [opts.rateLimitSeconds] - number of seconds to wait before
|
|
|
|
* allowing another set of events to be tracked.
|
2023-03-23 18:01:51 +01:00
|
|
|
* @param opts.securityProviderRequest
|
2022-04-04 21:26:13 +02:00
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
export default function createRPCMethodTrackingMiddleware({
|
|
|
|
trackEvent,
|
|
|
|
getMetricsState,
|
2022-10-31 18:54:01 +01:00
|
|
|
rateLimitSeconds = 60 * 5,
|
2023-03-23 18:01:51 +01:00
|
|
|
securityProviderRequest,
|
2022-04-04 21:26:13 +02:00
|
|
|
}) {
|
2023-03-23 18:01:51 +01:00
|
|
|
return async function rpcMethodTrackingMiddleware(
|
2022-04-04 21:26:13 +02:00
|
|
|
/** @type {any} */ req,
|
|
|
|
/** @type {any} */ res,
|
|
|
|
/** @type {Function} */ next,
|
|
|
|
) {
|
2022-07-20 15:25:04 +02:00
|
|
|
const { origin, method } = req;
|
|
|
|
|
|
|
|
// Determine what type of rate limit to apply based on method
|
|
|
|
const rateLimitType =
|
|
|
|
RATE_LIMIT_MAP[method] ?? RATE_LIMIT_TYPES.RATE_LIMITED;
|
|
|
|
|
|
|
|
// If the rateLimitType is RATE_LIMITED check the rateLimitTimeouts
|
|
|
|
const rateLimited =
|
|
|
|
rateLimitType === RATE_LIMIT_TYPES.RATE_LIMITED &&
|
|
|
|
typeof rateLimitTimeouts[method] !== 'undefined';
|
|
|
|
|
|
|
|
// Get the participateInMetaMetrics state to determine if we should track
|
|
|
|
// anything. This is extra redundancy because this value is checked in
|
|
|
|
// the metametrics controller's trackEvent method as well.
|
|
|
|
const userParticipatingInMetaMetrics =
|
|
|
|
getMetricsState().participateInMetaMetrics === true;
|
|
|
|
|
|
|
|
// Get the event type, each of which has APPROVED, REJECTED and REQUESTED
|
|
|
|
// keys for the various events in the flow.
|
|
|
|
const eventType = EVENT_NAME_MAP[method];
|
|
|
|
|
2023-03-29 20:25:01 +02:00
|
|
|
const eventProperties = {};
|
|
|
|
|
2022-07-20 15:25:04 +02:00
|
|
|
// Boolean variable that reduces code duplication and increases legibility
|
|
|
|
const shouldTrackEvent =
|
|
|
|
// Don't track if the request came from our own UI or background
|
|
|
|
origin !== ORIGIN_METAMASK &&
|
|
|
|
// Don't track if this is a blocked method
|
|
|
|
rateLimitType !== RATE_LIMIT_TYPES.BLOCKED &&
|
|
|
|
// Don't track if the rate limit has been hit
|
|
|
|
rateLimited === false &&
|
|
|
|
// Don't track if the user isn't participating in metametrics
|
|
|
|
userParticipatingInMetaMetrics === true;
|
|
|
|
|
|
|
|
if (shouldTrackEvent) {
|
|
|
|
// We track an initial "requested" event as soon as the dapp calls the
|
|
|
|
// provider method. For the events not special cased this is the only
|
|
|
|
// event that will be fired and the event name will be
|
|
|
|
// 'Provider Method Called'.
|
|
|
|
const event = eventType
|
|
|
|
? eventType.REQUESTED
|
2023-04-03 17:31:04 +02:00
|
|
|
: MetaMetricsEventName.ProviderMethodCalled;
|
2022-07-20 15:25:04 +02:00
|
|
|
|
2023-04-03 17:31:04 +02:00
|
|
|
if (event === MetaMetricsEventName.SignatureRequested) {
|
2023-03-29 20:25:01 +02:00
|
|
|
eventProperties.signature_type = method;
|
2022-07-20 15:25:04 +02:00
|
|
|
|
2023-04-17 06:24:25 +02:00
|
|
|
// In personal messages the first param is data while in typed messages second param is data
|
|
|
|
// if condition below is added to ensure that the right params are captured as data and address.
|
|
|
|
let data;
|
|
|
|
let from;
|
|
|
|
if (isValidAddress(req?.params?.[1])) {
|
|
|
|
data = req?.params?.[0];
|
|
|
|
from = req?.params?.[1];
|
|
|
|
} else {
|
|
|
|
data = req?.params?.[1];
|
|
|
|
from = req?.params?.[0];
|
|
|
|
}
|
2023-03-23 18:01:51 +01:00
|
|
|
const paramsExamplePassword = req?.params?.[2];
|
|
|
|
|
|
|
|
const msgData = {
|
2023-03-29 20:25:01 +02:00
|
|
|
msgParams: {
|
|
|
|
...paramsExamplePassword,
|
|
|
|
from,
|
|
|
|
data,
|
|
|
|
origin,
|
|
|
|
},
|
|
|
|
status: TransactionStatus.unapproved,
|
2023-03-23 18:01:51 +01:00
|
|
|
type: req.method,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const securityProviderResponse = await securityProviderRequest(
|
|
|
|
msgData,
|
|
|
|
req.method,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (securityProviderResponse?.flagAsDangerous === 1) {
|
2023-03-29 20:25:01 +02:00
|
|
|
eventProperties.ui_customizations = [
|
2023-04-03 17:31:04 +02:00
|
|
|
MetaMetricsEventUiCustomization.FlaggedAsMalicious,
|
2023-03-29 20:25:01 +02:00
|
|
|
];
|
2023-03-23 18:01:51 +01:00
|
|
|
} else if (securityProviderResponse?.flagAsDangerous === 2) {
|
2023-03-29 20:25:01 +02:00
|
|
|
eventProperties.ui_customizations = [
|
2023-04-03 17:31:04 +02:00
|
|
|
MetaMetricsEventUiCustomization.FlaggedAsSafetyUnknown,
|
2023-03-29 20:25:01 +02:00
|
|
|
];
|
2023-03-23 18:01:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (method === MESSAGE_TYPE.PERSONAL_SIGN) {
|
|
|
|
const { isSIWEMessage } = detectSIWE({ data });
|
|
|
|
if (isSIWEMessage) {
|
2023-03-29 20:25:01 +02:00
|
|
|
eventProperties.ui_customizations = (
|
|
|
|
eventProperties.ui_customizations || []
|
2023-04-03 17:31:04 +02:00
|
|
|
).concat(MetaMetricsEventUiCustomization.Siwe);
|
2023-03-23 18:01:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(
|
|
|
|
`createRPCMethodTrackingMiddleware: Error calling securityProviderRequest - ${e}`,
|
|
|
|
);
|
2023-02-08 16:16:17 +01:00
|
|
|
}
|
2023-03-23 18:01:51 +01:00
|
|
|
} else {
|
2023-03-29 20:25:01 +02:00
|
|
|
eventProperties.method = method;
|
2023-02-08 16:16:17 +01:00
|
|
|
}
|
|
|
|
|
2022-07-20 15:25:04 +02:00
|
|
|
trackEvent({
|
|
|
|
event,
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.InpageProvider,
|
2022-07-20 15:25:04 +02:00
|
|
|
referrer: {
|
|
|
|
url: origin,
|
|
|
|
},
|
2023-03-29 20:25:01 +02:00
|
|
|
properties: eventProperties,
|
2022-07-20 15:25:04 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
rateLimitTimeouts[method] = setTimeout(() => {
|
|
|
|
delete rateLimitTimeouts[method];
|
|
|
|
}, SECOND * rateLimitSeconds);
|
|
|
|
}
|
2022-04-04 21:26:13 +02:00
|
|
|
|
2023-03-23 18:01:51 +01:00
|
|
|
next(async (callback) => {
|
2022-07-20 15:25:04 +02:00
|
|
|
if (shouldTrackEvent === false || typeof eventType === 'undefined') {
|
2022-04-04 21:26:13 +02:00
|
|
|
return callback();
|
|
|
|
}
|
2022-07-20 15:25:04 +02:00
|
|
|
|
2023-03-14 19:31:19 +01:00
|
|
|
// The rpc error methodNotFound implies that 'eth_sign' is disabled in Advanced Settings
|
|
|
|
const isDisabledEthSignAdvancedSetting =
|
|
|
|
method === MESSAGE_TYPE.ETH_SIGN &&
|
|
|
|
res.error?.code === errorCodes.rpc.methodNotFound;
|
|
|
|
|
|
|
|
const isDisabledRPCMethod = isDisabledEthSignAdvancedSetting;
|
|
|
|
|
|
|
|
let event;
|
|
|
|
if (isDisabledRPCMethod) {
|
|
|
|
event = eventType.FAILED;
|
2023-03-29 20:25:01 +02:00
|
|
|
eventProperties.error = res.error;
|
|
|
|
} else if (res.error?.code === errorCodes.provider.userRejectedRequest) {
|
2023-03-14 19:31:19 +01:00
|
|
|
event = eventType.REJECTED;
|
|
|
|
} else {
|
|
|
|
event = eventType.APPROVED;
|
|
|
|
}
|
|
|
|
|
2022-07-20 15:25:04 +02:00
|
|
|
trackEvent({
|
|
|
|
event,
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.InpageProvider,
|
2022-07-20 15:25:04 +02:00
|
|
|
referrer: {
|
|
|
|
url: origin,
|
|
|
|
},
|
2023-03-29 20:25:01 +02:00
|
|
|
properties: eventProperties,
|
2022-07-20 15:25:04 +02:00
|
|
|
});
|
2023-03-14 19:31:19 +01:00
|
|
|
|
2022-04-04 21:26:13 +02:00
|
|
|
return callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|