|
| 1 | +import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine'; |
| 2 | +import type { PermittedHandlerExport } from '@metamask/permission-controller'; |
| 3 | +import { rpcErrors } from '@metamask/rpc-errors'; |
| 4 | +import type { |
| 5 | + JsonRpcRequest, |
| 6 | + StartTraceParams, |
| 7 | + StartTraceResult, |
| 8 | + TraceRequest, |
| 9 | +} from '@metamask/snaps-sdk'; |
| 10 | +import type { InferMatching, Snap } from '@metamask/snaps-utils'; |
| 11 | +import { |
| 12 | + boolean, |
| 13 | + number, |
| 14 | + record, |
| 15 | + union, |
| 16 | + create, |
| 17 | + object, |
| 18 | + string, |
| 19 | + StructError, |
| 20 | + exactOptional, |
| 21 | +} from '@metamask/superstruct'; |
| 22 | +import type { PendingJsonRpcResponse } from '@metamask/utils'; |
| 23 | +import { JsonStruct } from '@metamask/utils'; |
| 24 | + |
| 25 | +import type { MethodHooksObject } from '../utils'; |
| 26 | + |
| 27 | +const hookNames: MethodHooksObject<StartTraceMethodHooks> = { |
| 28 | + startTrace: true, |
| 29 | + getSnap: true, |
| 30 | +}; |
| 31 | + |
| 32 | +export type StartTraceMethodHooks = { |
| 33 | + /** |
| 34 | + * Start a performance trace in Sentry. |
| 35 | + * |
| 36 | + * @param request - The trace request object. |
| 37 | + * @returns The performance trace context. |
| 38 | + */ |
| 39 | + startTrace: (request: TraceRequest) => string; |
| 40 | + |
| 41 | + /** |
| 42 | + * Get Snap metadata. |
| 43 | + * |
| 44 | + * @param snapId - The ID of a Snap. |
| 45 | + */ |
| 46 | + getSnap: (snapId: string) => Snap | undefined; |
| 47 | +}; |
| 48 | + |
| 49 | +const StartTraceParametersStruct = object({ |
| 50 | + data: exactOptional(record(string(), union([string(), number(), boolean()]))), |
| 51 | + id: exactOptional(string()), |
| 52 | + name: string(), |
| 53 | + parentContext: exactOptional(JsonStruct), |
| 54 | + startTime: exactOptional(number()), |
| 55 | + tags: exactOptional(record(string(), union([string(), number(), boolean()]))), |
| 56 | +}); |
| 57 | + |
| 58 | +export type StartTraceParameters = InferMatching< |
| 59 | + typeof StartTraceParametersStruct, |
| 60 | + StartTraceParams |
| 61 | +>; |
| 62 | + |
| 63 | +/** |
| 64 | + * Handler for the `snap_startTrace` method. |
| 65 | + */ |
| 66 | +export const startTraceHandler: PermittedHandlerExport< |
| 67 | + StartTraceMethodHooks, |
| 68 | + StartTraceParameters, |
| 69 | + StartTraceResult |
| 70 | +> = { |
| 71 | + methodNames: ['snap_startTrace'], |
| 72 | + implementation: getStartTraceImplementation, |
| 73 | + hookNames, |
| 74 | +}; |
| 75 | + |
| 76 | +/** |
| 77 | + * The `snap_startTrace` method implementation. This method is used to start a |
| 78 | + * performance trace in Sentry. It is only available to preinstalled Snaps. |
| 79 | + * |
| 80 | + * @param request - The JSON-RPC request object. |
| 81 | + * @param response - The JSON-RPC response object. |
| 82 | + * @param _next - The `json-rpc-engine` "next" callback. Not used by this |
| 83 | + * function. |
| 84 | + * @param end - The `json-rpc-engine` "end" callback. |
| 85 | + * @param hooks - The RPC method hooks. |
| 86 | + * @param hooks.startTrace - The hook function to start a performance trace. |
| 87 | + * @param hooks.getSnap - The hook function to get Snap metadata. |
| 88 | + * @returns Nothing. |
| 89 | + */ |
| 90 | +function getStartTraceImplementation( |
| 91 | + request: JsonRpcRequest<StartTraceParameters>, |
| 92 | + response: PendingJsonRpcResponse, |
| 93 | + _next: unknown, |
| 94 | + end: JsonRpcEngineEndCallback, |
| 95 | + { startTrace, getSnap }: StartTraceMethodHooks, |
| 96 | +): void { |
| 97 | + const snap = getSnap( |
| 98 | + (request as JsonRpcRequest<StartTraceParams> & { origin: string }).origin, |
| 99 | + ); |
| 100 | + |
| 101 | + if (!snap?.preinstalled) { |
| 102 | + return end(rpcErrors.methodNotFound()); |
| 103 | + } |
| 104 | + |
| 105 | + const { params } = request; |
| 106 | + |
| 107 | + try { |
| 108 | + const validatedParams = getValidatedParams(params); |
| 109 | + response.result = startTrace(validatedParams); |
| 110 | + } catch (error) { |
| 111 | + return end(error); |
| 112 | + } |
| 113 | + |
| 114 | + return end(); |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Validate the parameters for the `snap_startTrace` method. |
| 119 | + * |
| 120 | + * @param params - Parameters to validate. |
| 121 | + * @returns Validated parameters. |
| 122 | + * @throws Throws RPC error if validation fails. |
| 123 | + */ |
| 124 | +function getValidatedParams(params: unknown): StartTraceParameters { |
| 125 | + try { |
| 126 | + return create(params, StartTraceParametersStruct); |
| 127 | + } catch (error) { |
| 128 | + if (error instanceof StructError) { |
| 129 | + throw rpcErrors.invalidParams({ |
| 130 | + message: `Invalid params: ${error.message}.`, |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + /* istanbul ignore next */ |
| 135 | + throw rpcErrors.internal(); |
| 136 | + } |
| 137 | +} |
0 commit comments