Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Send signature metrics as event fragments in middleware & feat: Add signature alert metrics to events #26597

Merged
merged 15 commits into from
Sep 12, 2024
Merged
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
108 changes: 82 additions & 26 deletions app/scripts/lib/createRPCMethodTrackingMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import {
PRIMARY_TYPES_PERMIT,
} from '../../../shared/constants/signatures';
import { SIGNING_METHODS } from '../../../shared/constants/transaction';
import { getBlockaidMetricsProps } from '../../../ui/helpers/utils/metrics';
import {
generateSignatureUniqueId,
getBlockaidMetricsProps,
} from '../../../ui/helpers/utils/metrics';
import { REDESIGN_APPROVAL_TYPES } from '../../../ui/pages/confirmations/utils/confirm';
import { getSnapAndHardwareInfoForMetrics } from './snap-keyring/metrics';

Expand Down Expand Up @@ -123,14 +126,58 @@ const TRANSFORM_PARAMS_MAP = {
const rateLimitTimeoutsByMethod = {};
let globalRateLimitCount = 0;

/**
* Create signature request event fragment with an assigned unique identifier
*
* @param {MetaMetricsController} metaMetricsController
* @param {OriginalRequest} req
* @param {object} properties
*/
function createSignatureFragment(metaMetricsController, req, properties) {
metaMetricsController.createEventFragment({
category: MetaMetricsEventCategory.InpageProvider,
initialEvent: MetaMetricsEventName.SignatureRequested,
successEvent: MetaMetricsEventName.SignatureApproved,
failureEvent: MetaMetricsEventName.SignatureRejected,
uniqueIdentifier: generateSignatureUniqueId(req.id),
persist: true,
referrer: {
url: req.origin,
},
properties,
});
}

/**
* Updates and finalizes event fragment for signature requests
*
* @param {MetaMetricsController} metaMetricsController
* @param {OriginalRequest} req
* @param {object} options
* @param {boolean} options.abandoned
* @param {object} options.properties
*/
function finalizeSignatureFragment(
metaMetricsController,
req,
{ abandoned, properties },
) {
const signatureUniqueId = generateSignatureUniqueId(req.id);

metaMetricsController.updateEventFragment(signatureUniqueId, {
properties,
});
metaMetricsController.finalizeEventFragment(signatureUniqueId, {
abandoned,
});
}

/**
* 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
* @param {Function} opts.trackEvent - trackEvent method from
* MetaMetricsController
* @param {Function} opts.getMetricsState - get the state of
* MetaMetricsController
* @param {number} [opts.rateLimitTimeout] - time, in milliseconds, to wait before
Expand All @@ -145,11 +192,12 @@ let globalRateLimitCount = 0;
* time window that should limit the number of method calls tracked to globalRateLimitMaxAmount.
* @param {number} [opts.globalRateLimitMaxAmount] - max number of method calls that should
* tracked within the globalRateLimitTimeout time window.
* @param {AppStateController} [opts.appStateController]
* @param {MetaMetricsController} [opts.metaMetricsController]
* @returns {Function}
*/

export default function createRPCMethodTrackingMiddleware({
trackEvent,
getMetricsState,
rateLimitTimeout = 60 * 5 * 1000, // 5 minutes
rateLimitSamplePercent = 0.001, // 0.1%
Expand All @@ -160,6 +208,7 @@ export default function createRPCMethodTrackingMiddleware({
isConfirmationRedesignEnabled,
snapAndHardwareMessenger,
appStateController,
metaMetricsController,
}) {
return async function rpcMethodTrackingMiddleware(
/** @type {any} */ req,
Expand Down Expand Up @@ -216,6 +265,8 @@ export default function createRPCMethodTrackingMiddleware({
// Don't track if the user isn't participating in metametrics
userParticipatingInMetaMetrics === true;

let signatureUniqueId;

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
Expand Down Expand Up @@ -316,14 +367,18 @@ export default function createRPCMethodTrackingMiddleware({
eventProperties.params = transformParams(params);
}

trackEvent({
event,
category: MetaMetricsEventCategory.InpageProvider,
referrer: {
url: origin,
},
properties: eventProperties,
});
if (event === MetaMetricsEventName.SignatureRequested) {
digiwand marked this conversation as resolved.
Show resolved Hide resolved
createSignatureFragment(metaMetricsController, req, eventProperties);
} else {
metaMetricsController.trackEvent({
event,
category: MetaMetricsEventCategory.InpageProvider,
referrer: {
url: origin,
},
properties: eventProperties,
});
}

if (rateLimitType === RATE_LIMIT_TYPES.TIMEOUT) {
rateLimitTimeoutsByMethod[method] = setTimeout(() => {
Expand Down Expand Up @@ -372,22 +427,23 @@ export default function createRPCMethodTrackingMiddleware({
...eventProperties,
...blockaidMetricProps,
location,
// if security_alert_response from blockaidMetricProps is Benign, force set security_alert_reason to empty string
security_alert_reason:
blockaidMetricProps.security_alert_response ===
BlockaidResultType.Benign
? ''
: blockaidMetricProps.security_alert_reason,
};

trackEvent({
event,
category: MetaMetricsEventCategory.InpageProvider,
referrer: {
url: origin,
},
properties,
});
if (signatureUniqueId) {
finalizeSignatureFragment(metaMetricsController, req, {
abandoned: event === eventType.REJECTED,
properties,
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @digiwand is event of type REJECTED received in this middleware ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @jpuri, yep! Within this next() node.js callback, the event is set based on the response above. This may include eventType.REJECTED

} else {
metaMetricsController.trackEvent({
event,
category: MetaMetricsEventCategory.InpageProvider,
referrer: {
url: origin,
},
properties,
});
}

return callback();
});
Expand Down
Loading