-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add signature tracing (#11453)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> - Adding `createTracingMiddleware` to start tracing for defined type of messages. - Adding `trace` callback to `SignatureController`. - End `Notification Display` trace after signature confirmation landed on users screen. ## **Related issues** Fixes: MetaMask/mobile-planning#1883 ## **Manual testing steps** 1. Set `MM_SENTRY_DSN_DEV` to developer Sentry dsn, see the value here https://github.com/MetaMask/metamask-extension/blob/develop/.metamaskrc.dist 2. Go test-dapp in app, trigger and personal signature 3. If you used the mentioned dsn above, your `Signature` trace will appear on this link: https://metamask.sentry.io/traces/?project=273496&query=signature%3ATransaction&source=traces&statsPeriod=1h ## **Screenshots/Recordings** ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [X] I’ve followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [X] I've completed the PR template to the best of my ability - [X] I’ve included tests if applicable - [X] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [X] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [X] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [X] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
- Loading branch information
Showing
9 changed files
with
138 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { JsonRpcRequest, PendingJsonRpcResponse } from 'json-rpc-engine'; | ||
import { default as createTracingMiddleware, MESSAGE_TYPE } from './index'; | ||
|
||
const REQUEST_MOCK = { | ||
id: 'testId', | ||
method: MESSAGE_TYPE.PERSONAL_SIGN, | ||
} as JsonRpcRequest<unknown>; | ||
const RESPONSE_MOCK = {} as PendingJsonRpcResponse<unknown>; | ||
const NEXT_MOCK = jest.fn(); | ||
|
||
jest.mock('../../util/trace', () => ({ | ||
...jest.requireActual('../../util/trace'), | ||
trace: jest.fn().mockResolvedValue({}), | ||
})); | ||
|
||
describe('createTracingMiddleware', () => { | ||
let request: JsonRpcRequest<unknown> & { traceContext?: unknown }; | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
request = { ...REQUEST_MOCK }; | ||
}); | ||
|
||
it('adds trace context to request if method is send transaction', async () => { | ||
await createTracingMiddleware()(request, RESPONSE_MOCK, NEXT_MOCK); | ||
expect(request.traceContext).toBeDefined(); | ||
}); | ||
|
||
it('does not add trace context to request if method not supported', async () => { | ||
request.method = 'unsupportedMethod'; | ||
|
||
await createTracingMiddleware()(request, RESPONSE_MOCK, NEXT_MOCK); | ||
|
||
expect(request.traceContext).toBeUndefined(); | ||
}); | ||
it('calls next', async () => { | ||
await createTracingMiddleware()(request, RESPONSE_MOCK, NEXT_MOCK); | ||
expect(NEXT_MOCK).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { JsonRpcRequest, PendingJsonRpcResponse } from 'json-rpc-engine'; | ||
import { trace, TraceName } from '../../util/trace'; | ||
|
||
export const MESSAGE_TYPE = { | ||
ETH_SIGN_TYPED_DATA: 'eth_signTypedData', | ||
ETH_SIGN_TYPED_DATA_V1: 'eth_signTypedData_v1', | ||
ETH_SIGN_TYPED_DATA_V3: 'eth_signTypedData_v3', | ||
ETH_SIGN_TYPED_DATA_V4: 'eth_signTypedData_v4', | ||
PERSONAL_SIGN: 'personal_sign', | ||
}; | ||
|
||
const METHOD_TYPE_TO_TRACE_NAME: Record<string, TraceName> = { | ||
[MESSAGE_TYPE.ETH_SIGN_TYPED_DATA]: TraceName.Signature, | ||
[MESSAGE_TYPE.ETH_SIGN_TYPED_DATA_V1]: TraceName.Signature, | ||
[MESSAGE_TYPE.ETH_SIGN_TYPED_DATA_V3]: TraceName.Signature, | ||
[MESSAGE_TYPE.ETH_SIGN_TYPED_DATA_V4]: TraceName.Signature, | ||
[MESSAGE_TYPE.PERSONAL_SIGN]: TraceName.Signature, | ||
}; | ||
|
||
export default function createTracingMiddleware() { | ||
return async function tracingMiddleware( | ||
req: JsonRpcRequest<unknown> & { traceContext?: unknown }, | ||
_res: PendingJsonRpcResponse<unknown>, | ||
next: () => void, | ||
) { | ||
const { id, method } = req; | ||
|
||
const traceName = METHOD_TYPE_TO_TRACE_NAME[method]; | ||
|
||
if (traceName) { | ||
req.traceContext = await trace({ | ||
name: traceName, | ||
id: id as string, | ||
}); | ||
|
||
await trace({ | ||
name: TraceName.Middleware, | ||
id: id as string, | ||
parentContext: req.traceContext, | ||
}); | ||
} | ||
|
||
next(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters