Skip to content

Commit

Permalink
Fix disabled 'eth_sign' event & add 'Request Disabled' event type (#1…
Browse files Browse the repository at this point in the history
…8007)

* Fix #17974 / add REQUEST_DISABLED event

* RPCMiddleware: add eth_sign REQUEST_DISABLED test

* don't call Signature Request for disabled eth_sign

* clean: alphabetize

* clean: rn isDisabledRequest -> isDisabledRPCMethod

* fix test

* rpc middeware: add eth_sign comment

* fix eth_sign events and re-add Signature Request

* send 'Signature Failed' for disabled eth_sigh
  • Loading branch information
digiwand authored Mar 14, 2023
1 parent c477e29 commit 5468c2c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
25 changes: 20 additions & 5 deletions app/scripts/lib/createRPCMethodTrackingMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { errorCodes } from 'eth-rpc-errors';
import { MESSAGE_TYPE, ORIGIN_METAMASK } from '../../../shared/constants/app';
import { SECOND } from '../../../shared/constants/time';
import { detectSIWE } from '../../../shared/modules/siwe';
Expand Down Expand Up @@ -46,6 +47,7 @@ const RATE_LIMIT_MAP = {
const EVENT_NAME_MAP = {
[MESSAGE_TYPE.ETH_SIGN]: {
APPROVED: EVENT_NAMES.SIGNATURE_APPROVED,
FAILED: EVENT_NAMES.SIGNATURE_FAILED,
REJECTED: EVENT_NAMES.SIGNATURE_REJECTED,
REQUESTED: EVENT_NAMES.SIGNATURE_REQUESTED,
},
Expand Down Expand Up @@ -195,13 +197,25 @@ export default function createRPCMethodTrackingMiddleware({
return callback();
}

// An error code of 4001 means the user rejected the request, which we
// can use here to determine which event to track.
const event =
res.error?.code === 4001 ? eventType.REJECTED : eventType.APPROVED;

const properties = {};

// 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;
properties.error = res.error;
} else if (res.error?.code === 4001) {
event = eventType.REJECTED;
} else {
event = eventType.APPROVED;
}

if (eventType.REQUESTED === EVENT_NAMES.SIGNATURE_REQUESTED) {
properties.signature_type = method;
} else {
Expand All @@ -226,6 +240,7 @@ export default function createRPCMethodTrackingMiddleware({
},
properties,
});

return callback();
});
};
Expand Down
30 changes: 30 additions & 0 deletions app/scripts/lib/createRPCMethodTrackingMiddleware.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { errorCodes } from 'eth-rpc-errors';
import { MESSAGE_TYPE } from '../../../shared/constants/app';
import { EVENT_NAMES } from '../../../shared/constants/metametrics';
import { SECOND } from '../../../shared/constants/time';
Expand Down Expand Up @@ -213,5 +214,34 @@ describe('createRPCMethodTrackingMiddleware', () => {
expect(trackEvent.mock.calls[0][0].properties.method).toBe('eth_chainId');
expect(trackEvent.mock.calls[1][0].properties.method).toBe('eth_chainId');
});

describe(`when '${MESSAGE_TYPE.ETH_SIGN}' is disabled in advanced settings`, () => {
it(`should track ${EVENT_NAMES.SIGNATURE_FAILED} and include error property`, async () => {
const mockError = { code: errorCodes.rpc.methodNotFound };
const req = {
method: MESSAGE_TYPE.ETH_SIGN,
origin: 'some.dapp',
};
const res = {
error: mockError,
};
const { next, executeMiddlewareStack } = getNext();

handler(req, res, next);
await executeMiddlewareStack();

expect(trackEvent).toHaveBeenCalledTimes(2);

expect(trackEvent.mock.calls[1][0]).toMatchObject({
category: 'inpage_provider',
event: EVENT_NAMES.SIGNATURE_FAILED,
properties: {
signature_type: MESSAGE_TYPE.ETH_SIGN,
error: mockError,
},
referrer: { url: 'some.dapp' },
});
});
});
});
});
1 change: 1 addition & 0 deletions shared/constants/metametrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ export const EVENT_NAMES = {
PUBLIC_ADDRESS_COPIED: 'Public Address Copied',
PROVIDER_METHOD_CALLED: 'Provider Method Called',
SIGNATURE_APPROVED: 'Signature Approved',
SIGNATURE_FAILED: 'Signature Failed',
SIGNATURE_REJECTED: 'Signature Rejected',
SIGNATURE_REQUESTED: 'Signature Requested',
TOKEN_IMPORT_BUTTON_CLICKED: 'Import Token Button Clicked',
Expand Down

0 comments on commit 5468c2c

Please sign in to comment.