@metamask/snaps-rpc-methods 13.0.0 → 13.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -1
- package/dist/endowments/index.cjs +3 -0
- package/dist/endowments/index.cjs.map +1 -1
- package/dist/endowments/index.d.cts.map +1 -1
- package/dist/endowments/index.d.mts.map +1 -1
- package/dist/endowments/index.mjs +3 -0
- package/dist/endowments/index.mjs.map +1 -1
- package/dist/permitted/closeWebSocket.cjs +70 -0
- package/dist/permitted/closeWebSocket.cjs.map +1 -0
- package/dist/permitted/closeWebSocket.d.cts +19 -0
- package/dist/permitted/closeWebSocket.d.cts.map +1 -0
- package/dist/permitted/closeWebSocket.d.mts +19 -0
- package/dist/permitted/closeWebSocket.d.mts.map +1 -0
- package/dist/permitted/closeWebSocket.mjs +67 -0
- package/dist/permitted/closeWebSocket.mjs.map +1 -0
- package/dist/permitted/createInterface.d.cts +10 -10
- package/dist/permitted/createInterface.d.mts +10 -10
- package/dist/permitted/getWebSockets.cjs +42 -0
- package/dist/permitted/getWebSockets.cjs.map +1 -0
- package/dist/permitted/getWebSockets.d.cts +11 -0
- package/dist/permitted/getWebSockets.d.cts.map +1 -0
- package/dist/permitted/getWebSockets.d.mts +11 -0
- package/dist/permitted/getWebSockets.d.mts.map +1 -0
- package/dist/permitted/getWebSockets.mjs +39 -0
- package/dist/permitted/getWebSockets.mjs.map +1 -0
- package/dist/permitted/handlers.cjs +10 -0
- package/dist/permitted/handlers.cjs.map +1 -1
- package/dist/permitted/handlers.d.cts +7 -2
- package/dist/permitted/handlers.d.cts.map +1 -1
- package/dist/permitted/handlers.d.mts +7 -2
- package/dist/permitted/handlers.d.mts.map +1 -1
- package/dist/permitted/handlers.mjs +10 -0
- package/dist/permitted/handlers.mjs.map +1 -1
- package/dist/permitted/index.cjs.map +1 -1
- package/dist/permitted/index.d.cts +7 -1
- package/dist/permitted/index.d.cts.map +1 -1
- package/dist/permitted/index.d.mts +7 -1
- package/dist/permitted/index.d.mts.map +1 -1
- package/dist/permitted/index.mjs.map +1 -1
- package/dist/permitted/openWebSocket.cjs +72 -0
- package/dist/permitted/openWebSocket.cjs.map +1 -0
- package/dist/permitted/openWebSocket.d.cts +21 -0
- package/dist/permitted/openWebSocket.d.cts.map +1 -0
- package/dist/permitted/openWebSocket.d.mts +21 -0
- package/dist/permitted/openWebSocket.d.mts.map +1 -0
- package/dist/permitted/openWebSocket.mjs +69 -0
- package/dist/permitted/openWebSocket.mjs.map +1 -0
- package/dist/permitted/sendWebSocketMessage.cjs +72 -0
- package/dist/permitted/sendWebSocketMessage.cjs.map +1 -0
- package/dist/permitted/sendWebSocketMessage.d.cts +21 -0
- package/dist/permitted/sendWebSocketMessage.d.cts.map +1 -0
- package/dist/permitted/sendWebSocketMessage.d.mts +21 -0
- package/dist/permitted/sendWebSocketMessage.d.mts.map +1 -0
- package/dist/permitted/sendWebSocketMessage.mjs +69 -0
- package/dist/permitted/sendWebSocketMessage.mjs.map +1 -0
- package/dist/permitted/trackError.cjs +94 -0
- package/dist/permitted/trackError.cjs.map +1 -0
- package/dist/permitted/trackError.d.cts +32 -0
- package/dist/permitted/trackError.d.cts.map +1 -0
- package/dist/permitted/trackError.d.mts +32 -0
- package/dist/permitted/trackError.d.mts.map +1 -0
- package/dist/permitted/trackError.mjs +91 -0
- package/dist/permitted/trackError.mjs.map +1 -0
- package/dist/permitted/updateInterface.d.cts +10 -10
- package/dist/permitted/updateInterface.d.mts +10 -10
- package/dist/restricted/dialog.d.cts +20 -20
- package/dist/restricted/dialog.d.mts +20 -20
- package/dist/restricted/notify.d.cts +15 -15
- package/dist/restricted/notify.d.mts +15 -15
- package/package.json +4 -4
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { providerErrors, rpcErrors } from "@metamask/rpc-errors";
|
|
2
|
+
import { literal, union } from "@metamask/snaps-sdk";
|
|
3
|
+
import { uri } from "@metamask/snaps-utils";
|
|
4
|
+
import { create, object, array, string, optional, StructError } from "@metamask/superstruct";
|
|
5
|
+
import { SnapEndowments } from "../endowments/index.mjs";
|
|
6
|
+
const hookNames = {
|
|
7
|
+
hasPermission: true,
|
|
8
|
+
openWebSocket: true,
|
|
9
|
+
};
|
|
10
|
+
const OpenWebSocketParametersStruct = object({
|
|
11
|
+
url: uri({ protocol: union([literal('wss:'), literal('ws:')]) }),
|
|
12
|
+
protocols: optional(array(string())),
|
|
13
|
+
});
|
|
14
|
+
/**
|
|
15
|
+
* Handler for the `snap_openWebSocket` method.
|
|
16
|
+
*/
|
|
17
|
+
export const openWebSocketHandler = {
|
|
18
|
+
methodNames: ['snap_openWebSocket'],
|
|
19
|
+
implementation: openWebSocketImplementation,
|
|
20
|
+
hookNames,
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* The `snap_openWebSocket` method implementation.
|
|
24
|
+
*
|
|
25
|
+
* @param req - The JSON-RPC request object.
|
|
26
|
+
* @param res - The JSON-RPC response object.
|
|
27
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this function.
|
|
28
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
29
|
+
* @param hooks - The RPC method hooks.
|
|
30
|
+
* @param hooks.hasPermission - The function to check if a snap has the `endowment:network-access` permission.
|
|
31
|
+
* @param hooks.openWebSocket - The function to open a WebSocket.
|
|
32
|
+
* @returns Nothing.
|
|
33
|
+
*/
|
|
34
|
+
async function openWebSocketImplementation(req, res, _next, end, { hasPermission, openWebSocket }) {
|
|
35
|
+
if (!hasPermission(SnapEndowments.NetworkAccess)) {
|
|
36
|
+
return end(providerErrors.unauthorized());
|
|
37
|
+
}
|
|
38
|
+
const { params } = req;
|
|
39
|
+
try {
|
|
40
|
+
const { url, protocols } = getValidatedParams(params);
|
|
41
|
+
res.result = await openWebSocket(url, protocols);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
return end(error);
|
|
45
|
+
}
|
|
46
|
+
return end();
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Validates the parameters for the snap_openWebSocket method.
|
|
50
|
+
*
|
|
51
|
+
* @param params - Parameters to validate.
|
|
52
|
+
* @returns Validated parameters.
|
|
53
|
+
* @throws Throws RPC error if validation fails.
|
|
54
|
+
*/
|
|
55
|
+
function getValidatedParams(params) {
|
|
56
|
+
try {
|
|
57
|
+
return create(params, OpenWebSocketParametersStruct);
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
if (error instanceof StructError) {
|
|
61
|
+
throw rpcErrors.invalidParams({
|
|
62
|
+
message: `Invalid params: ${error.message}.`,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/* istanbul ignore next */
|
|
66
|
+
throw rpcErrors.internal();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=openWebSocket.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openWebSocket.mjs","sourceRoot":"","sources":["../../src/permitted/openWebSocket.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,6BAA6B;AACjE,OAAO,EACL,OAAO,EACP,KAAK,EAIN,4BAA4B;AAC7B,OAAO,EAAE,GAAG,EAAsB,8BAA8B;AAChE,OAAO,EACL,MAAM,EACN,MAAM,EACN,KAAK,EACL,MAAM,EACN,QAAQ,EACR,WAAW,EACZ,8BAA8B;AAG/B,OAAO,EAAE,cAAc,EAAE,gCAAsB;AAG/C,MAAM,SAAS,GAAgD;IAC7D,aAAa,EAAE,IAAI;IACnB,aAAa,EAAE,IAAI;CACpB,CAAC;AAOF,MAAM,6BAA6B,GAAG,MAAM,CAAC;IAC3C,GAAG,EAAE,GAAG,CAAC,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;IAChE,SAAS,EAAE,QAAQ,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC;CACrC,CAAC,CAAC;AAOH;;GAEG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAI7B;IACF,WAAW,EAAE,CAAC,oBAAoB,CAAC;IACnC,cAAc,EAAE,2BAA2B;IAC3C,SAAS;CACV,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,KAAK,UAAU,2BAA2B,CACxC,GAA4C,EAC5C,GAAgD,EAChD,KAAc,EACd,GAA6B,EAC7B,EAAE,aAAa,EAAE,aAAa,EAA4B;IAE1D,IAAI,CAAC,aAAa,CAAC,cAAc,CAAC,aAAa,CAAC,EAAE,CAAC;QACjD,OAAO,GAAG,CAAC,cAAc,CAAC,YAAY,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IAEvB,IAAI,CAAC;QACH,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;QACtD,GAAG,CAAC,MAAM,GAAG,MAAM,aAAa,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;IACnD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAED;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,MAAe;IACzC,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,MAAM,EAAE,6BAA6B,CAAC,CAAC;IACvD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,WAAW,EAAE,CAAC;YACjC,MAAM,SAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,mBAAmB,KAAK,CAAC,OAAO,GAAG;aAC7C,CAAC,CAAC;QACL,CAAC;QACD,0BAA0B;QAC1B,MAAM,SAAS,CAAC,QAAQ,EAAE,CAAC;IAC7B,CAAC;AACH,CAAC","sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport {\n literal,\n union,\n type JsonRpcRequest,\n type OpenWebSocketParams,\n type OpenWebSocketResult,\n} from '@metamask/snaps-sdk';\nimport { uri, type InferMatching } from '@metamask/snaps-utils';\nimport {\n create,\n object,\n array,\n string,\n optional,\n StructError,\n} from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport { SnapEndowments } from '../endowments';\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<OpenWebSocketMethodHooks> = {\n hasPermission: true,\n openWebSocket: true,\n};\n\nexport type OpenWebSocketMethodHooks = {\n hasPermission: (permissionName: string) => boolean;\n openWebSocket: (url: string, protocols?: string[]) => Promise<string>;\n};\n\nconst OpenWebSocketParametersStruct = object({\n url: uri({ protocol: union([literal('wss:'), literal('ws:')]) }),\n protocols: optional(array(string())),\n});\n\nexport type OpenWebSocketParameters = InferMatching<\n typeof OpenWebSocketParametersStruct,\n OpenWebSocketParams\n>;\n\n/**\n * Handler for the `snap_openWebSocket` method.\n */\nexport const openWebSocketHandler: PermittedHandlerExport<\n OpenWebSocketMethodHooks,\n OpenWebSocketParams,\n OpenWebSocketResult\n> = {\n methodNames: ['snap_openWebSocket'],\n implementation: openWebSocketImplementation,\n hookNames,\n};\n\n/**\n * The `snap_openWebSocket` method implementation.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.hasPermission - The function to check if a snap has the `endowment:network-access` permission.\n * @param hooks.openWebSocket - The function to open a WebSocket.\n * @returns Nothing.\n */\nasync function openWebSocketImplementation(\n req: JsonRpcRequest<OpenWebSocketParameters>,\n res: PendingJsonRpcResponse<OpenWebSocketResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { hasPermission, openWebSocket }: OpenWebSocketMethodHooks,\n): Promise<void> {\n if (!hasPermission(SnapEndowments.NetworkAccess)) {\n return end(providerErrors.unauthorized());\n }\n\n const { params } = req;\n\n try {\n const { url, protocols } = getValidatedParams(params);\n res.result = await openWebSocket(url, protocols);\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validates the parameters for the snap_openWebSocket method.\n *\n * @param params - Parameters to validate.\n * @returns Validated parameters.\n * @throws Throws RPC error if validation fails.\n */\nfunction getValidatedParams(params: unknown): OpenWebSocketParameters {\n try {\n return create(params, OpenWebSocketParametersStruct);\n } catch (error) {\n if (error instanceof StructError) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n"]}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sendWebSocketMessageHandler = void 0;
|
|
4
|
+
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
5
|
+
const snaps_sdk_1 = require("@metamask/snaps-sdk");
|
|
6
|
+
const superstruct_1 = require("@metamask/superstruct");
|
|
7
|
+
const endowments_1 = require("../endowments/index.cjs");
|
|
8
|
+
const hookNames = {
|
|
9
|
+
hasPermission: true,
|
|
10
|
+
sendWebSocketMessage: true,
|
|
11
|
+
};
|
|
12
|
+
const SendWebSocketMessageParametersStruct = (0, superstruct_1.object)({
|
|
13
|
+
id: (0, superstruct_1.string)(),
|
|
14
|
+
message: (0, snaps_sdk_1.union)([(0, superstruct_1.string)(), (0, superstruct_1.array)((0, superstruct_1.number)())]),
|
|
15
|
+
});
|
|
16
|
+
/**
|
|
17
|
+
* Handler for the `snap_sendWebSocketMessage` method.
|
|
18
|
+
*/
|
|
19
|
+
exports.sendWebSocketMessageHandler = {
|
|
20
|
+
methodNames: ['snap_sendWebSocketMessage'],
|
|
21
|
+
implementation: sendWebSocketMessageImplementation,
|
|
22
|
+
hookNames,
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* The `snap_sendWebSocketMessage` method implementation.
|
|
26
|
+
*
|
|
27
|
+
* @param req - The JSON-RPC request object.
|
|
28
|
+
* @param res - The JSON-RPC response object.
|
|
29
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this function.
|
|
30
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
31
|
+
* @param hooks - The RPC method hooks.
|
|
32
|
+
* @param hooks.hasPermission - The function to check if a snap has the `endowment:network-access` permission.
|
|
33
|
+
* @param hooks.sendWebSocketMessage - The function to send a WebSocket message.
|
|
34
|
+
* @returns Nothing.
|
|
35
|
+
*/
|
|
36
|
+
function sendWebSocketMessageImplementation(req, res, _next, end, { hasPermission, sendWebSocketMessage }) {
|
|
37
|
+
if (!hasPermission(endowments_1.SnapEndowments.NetworkAccess)) {
|
|
38
|
+
return end(rpc_errors_1.providerErrors.unauthorized());
|
|
39
|
+
}
|
|
40
|
+
const { params } = req;
|
|
41
|
+
try {
|
|
42
|
+
const { id, message } = getValidatedParams(params);
|
|
43
|
+
sendWebSocketMessage(id, message);
|
|
44
|
+
res.result = null;
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
return end(error);
|
|
48
|
+
}
|
|
49
|
+
return end();
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Validates the parameters for the snap_sendWebSocketMessage method.
|
|
53
|
+
*
|
|
54
|
+
* @param params - Parameters to validate.
|
|
55
|
+
* @returns Validated parameters.
|
|
56
|
+
* @throws Throws RPC error if validation fails.
|
|
57
|
+
*/
|
|
58
|
+
function getValidatedParams(params) {
|
|
59
|
+
try {
|
|
60
|
+
return (0, superstruct_1.create)(params, SendWebSocketMessageParametersStruct);
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
if (error instanceof superstruct_1.StructError) {
|
|
64
|
+
throw rpc_errors_1.rpcErrors.invalidParams({
|
|
65
|
+
message: `Invalid params: ${error.message}.`,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/* istanbul ignore next */
|
|
69
|
+
throw rpc_errors_1.rpcErrors.internal();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=sendWebSocketMessage.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sendWebSocketMessage.cjs","sourceRoot":"","sources":["../../src/permitted/sendWebSocketMessage.ts"],"names":[],"mappings":";;;AAEA,qDAAiE;AAMjE,mDAA4C;AAE5C,uDAO+B;AAG/B,wDAA+C;AAG/C,MAAM,SAAS,GAAuD;IACpE,aAAa,EAAE,IAAI;IACnB,oBAAoB,EAAE,IAAI;CAC3B,CAAC;AAOF,MAAM,oCAAoC,GAAG,IAAA,oBAAM,EAAC;IAClD,EAAE,EAAE,IAAA,oBAAM,GAAE;IACZ,OAAO,EAAE,IAAA,iBAAK,EAAC,CAAC,IAAA,oBAAM,GAAE,EAAE,IAAA,mBAAK,EAAC,IAAA,oBAAM,GAAE,CAAC,CAAC,CAAC;CAC5C,CAAC,CAAC;AAOH;;GAEG;AACU,QAAA,2BAA2B,GAIpC;IACF,WAAW,EAAE,CAAC,2BAA2B,CAAC;IAC1C,cAAc,EAAE,kCAAkC;IAClD,SAAS;CACV,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,SAAS,kCAAkC,CACzC,GAAmD,EACnD,GAAuD,EACvD,KAAc,EACd,GAA6B,EAC7B,EAAE,aAAa,EAAE,oBAAoB,EAAmC;IAExE,IAAI,CAAC,aAAa,CAAC,2BAAc,CAAC,aAAa,CAAC,EAAE,CAAC;QACjD,OAAO,GAAG,CAAC,2BAAc,CAAC,YAAY,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IAEvB,IAAI,CAAC;QACH,MAAM,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;QACnD,oBAAoB,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;QAClC,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC;IACpB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAED;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,MAAe;IACzC,IAAI,CAAC;QACH,OAAO,IAAA,oBAAM,EAAC,MAAM,EAAE,oCAAoC,CAAC,CAAC;IAC9D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,yBAAW,EAAE,CAAC;YACjC,MAAM,sBAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,mBAAmB,KAAK,CAAC,OAAO,GAAG;aAC7C,CAAC,CAAC;QACL,CAAC;QACD,0BAA0B;QAC1B,MAAM,sBAAS,CAAC,QAAQ,EAAE,CAAC;IAC7B,CAAC;AACH,CAAC","sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type {\n JsonRpcRequest,\n SendWebSocketMessageParams,\n SendWebSocketMessageResult,\n} from '@metamask/snaps-sdk';\nimport { union } from '@metamask/snaps-sdk';\nimport type { InferMatching } from '@metamask/snaps-utils';\nimport {\n create,\n object,\n number,\n string,\n array,\n StructError,\n} from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport { SnapEndowments } from '../endowments';\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<SendWebSocketMessageMethodHooks> = {\n hasPermission: true,\n sendWebSocketMessage: true,\n};\n\nexport type SendWebSocketMessageMethodHooks = {\n hasPermission: (permissionName: string) => boolean;\n sendWebSocketMessage: (id: string, data: string | number[]) => void;\n};\n\nconst SendWebSocketMessageParametersStruct = object({\n id: string(),\n message: union([string(), array(number())]),\n});\n\nexport type SendWebSocketMessageParameters = InferMatching<\n typeof SendWebSocketMessageParametersStruct,\n SendWebSocketMessageParams\n>;\n\n/**\n * Handler for the `snap_sendWebSocketMessage` method.\n */\nexport const sendWebSocketMessageHandler: PermittedHandlerExport<\n SendWebSocketMessageMethodHooks,\n SendWebSocketMessageParams,\n SendWebSocketMessageResult\n> = {\n methodNames: ['snap_sendWebSocketMessage'],\n implementation: sendWebSocketMessageImplementation,\n hookNames,\n};\n\n/**\n * The `snap_sendWebSocketMessage` method implementation.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.hasPermission - The function to check if a snap has the `endowment:network-access` permission.\n * @param hooks.sendWebSocketMessage - The function to send a WebSocket message.\n * @returns Nothing.\n */\nfunction sendWebSocketMessageImplementation(\n req: JsonRpcRequest<SendWebSocketMessageParameters>,\n res: PendingJsonRpcResponse<SendWebSocketMessageResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { hasPermission, sendWebSocketMessage }: SendWebSocketMessageMethodHooks,\n): void {\n if (!hasPermission(SnapEndowments.NetworkAccess)) {\n return end(providerErrors.unauthorized());\n }\n\n const { params } = req;\n\n try {\n const { id, message } = getValidatedParams(params);\n sendWebSocketMessage(id, message);\n res.result = null;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validates the parameters for the snap_sendWebSocketMessage method.\n *\n * @param params - Parameters to validate.\n * @returns Validated parameters.\n * @throws Throws RPC error if validation fails.\n */\nfunction getValidatedParams(params: unknown): SendWebSocketMessageParameters {\n try {\n return create(params, SendWebSocketMessageParametersStruct);\n } catch (error) {\n if (error instanceof StructError) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { PermittedHandlerExport } from "@metamask/permission-controller";
|
|
2
|
+
import type { SendWebSocketMessageParams, SendWebSocketMessageResult } from "@metamask/snaps-sdk";
|
|
3
|
+
import type { InferMatching } from "@metamask/snaps-utils";
|
|
4
|
+
export type SendWebSocketMessageMethodHooks = {
|
|
5
|
+
hasPermission: (permissionName: string) => boolean;
|
|
6
|
+
sendWebSocketMessage: (id: string, data: string | number[]) => void;
|
|
7
|
+
};
|
|
8
|
+
declare const SendWebSocketMessageParametersStruct: import("@metamask/superstruct").Struct<{
|
|
9
|
+
id: string;
|
|
10
|
+
message: string | number[];
|
|
11
|
+
}, {
|
|
12
|
+
id: import("@metamask/superstruct").Struct<string, null>;
|
|
13
|
+
message: import("@metamask/superstruct").Struct<string | number[], [head: import("@metamask/superstruct").Struct<string, null>, import("@metamask/superstruct").Struct<number[], import("@metamask/superstruct").Struct<number, null>>]>;
|
|
14
|
+
}>;
|
|
15
|
+
export type SendWebSocketMessageParameters = InferMatching<typeof SendWebSocketMessageParametersStruct, SendWebSocketMessageParams>;
|
|
16
|
+
/**
|
|
17
|
+
* Handler for the `snap_sendWebSocketMessage` method.
|
|
18
|
+
*/
|
|
19
|
+
export declare const sendWebSocketMessageHandler: PermittedHandlerExport<SendWebSocketMessageMethodHooks, SendWebSocketMessageParams, SendWebSocketMessageResult>;
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=sendWebSocketMessage.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sendWebSocketMessage.d.cts","sourceRoot":"","sources":["../../src/permitted/sendWebSocketMessage.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,KAAK,EAEV,0BAA0B,EAC1B,0BAA0B,EAC3B,4BAA4B;AAE7B,OAAO,KAAK,EAAE,aAAa,EAAE,8BAA8B;AAmB3D,MAAM,MAAM,+BAA+B,GAAG;IAC5C,aAAa,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,OAAO,CAAC;IACnD,oBAAoB,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,KAAK,IAAI,CAAC;CACrE,CAAC;AAEF,QAAA,MAAM,oCAAoC;;;;;;EAGxC,CAAC;AAEH,MAAM,MAAM,8BAA8B,GAAG,aAAa,CACxD,OAAO,oCAAoC,EAC3C,0BAA0B,CAC3B,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,2BAA2B,EAAE,sBAAsB,CAC9D,+BAA+B,EAC/B,0BAA0B,EAC1B,0BAA0B,CAK3B,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { PermittedHandlerExport } from "@metamask/permission-controller";
|
|
2
|
+
import type { SendWebSocketMessageParams, SendWebSocketMessageResult } from "@metamask/snaps-sdk";
|
|
3
|
+
import type { InferMatching } from "@metamask/snaps-utils";
|
|
4
|
+
export type SendWebSocketMessageMethodHooks = {
|
|
5
|
+
hasPermission: (permissionName: string) => boolean;
|
|
6
|
+
sendWebSocketMessage: (id: string, data: string | number[]) => void;
|
|
7
|
+
};
|
|
8
|
+
declare const SendWebSocketMessageParametersStruct: import("@metamask/superstruct").Struct<{
|
|
9
|
+
id: string;
|
|
10
|
+
message: string | number[];
|
|
11
|
+
}, {
|
|
12
|
+
id: import("@metamask/superstruct").Struct<string, null>;
|
|
13
|
+
message: import("@metamask/superstruct").Struct<string | number[], [head: import("@metamask/superstruct").Struct<string, null>, import("@metamask/superstruct").Struct<number[], import("@metamask/superstruct").Struct<number, null>>]>;
|
|
14
|
+
}>;
|
|
15
|
+
export type SendWebSocketMessageParameters = InferMatching<typeof SendWebSocketMessageParametersStruct, SendWebSocketMessageParams>;
|
|
16
|
+
/**
|
|
17
|
+
* Handler for the `snap_sendWebSocketMessage` method.
|
|
18
|
+
*/
|
|
19
|
+
export declare const sendWebSocketMessageHandler: PermittedHandlerExport<SendWebSocketMessageMethodHooks, SendWebSocketMessageParams, SendWebSocketMessageResult>;
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=sendWebSocketMessage.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sendWebSocketMessage.d.mts","sourceRoot":"","sources":["../../src/permitted/sendWebSocketMessage.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,KAAK,EAEV,0BAA0B,EAC1B,0BAA0B,EAC3B,4BAA4B;AAE7B,OAAO,KAAK,EAAE,aAAa,EAAE,8BAA8B;AAmB3D,MAAM,MAAM,+BAA+B,GAAG;IAC5C,aAAa,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,OAAO,CAAC;IACnD,oBAAoB,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,KAAK,IAAI,CAAC;CACrE,CAAC;AAEF,QAAA,MAAM,oCAAoC;;;;;;EAGxC,CAAC;AAEH,MAAM,MAAM,8BAA8B,GAAG,aAAa,CACxD,OAAO,oCAAoC,EAC3C,0BAA0B,CAC3B,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,2BAA2B,EAAE,sBAAsB,CAC9D,+BAA+B,EAC/B,0BAA0B,EAC1B,0BAA0B,CAK3B,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { providerErrors, rpcErrors } from "@metamask/rpc-errors";
|
|
2
|
+
import { union } from "@metamask/snaps-sdk";
|
|
3
|
+
import { create, object, number, string, array, StructError } from "@metamask/superstruct";
|
|
4
|
+
import { SnapEndowments } from "../endowments/index.mjs";
|
|
5
|
+
const hookNames = {
|
|
6
|
+
hasPermission: true,
|
|
7
|
+
sendWebSocketMessage: true,
|
|
8
|
+
};
|
|
9
|
+
const SendWebSocketMessageParametersStruct = object({
|
|
10
|
+
id: string(),
|
|
11
|
+
message: union([string(), array(number())]),
|
|
12
|
+
});
|
|
13
|
+
/**
|
|
14
|
+
* Handler for the `snap_sendWebSocketMessage` method.
|
|
15
|
+
*/
|
|
16
|
+
export const sendWebSocketMessageHandler = {
|
|
17
|
+
methodNames: ['snap_sendWebSocketMessage'],
|
|
18
|
+
implementation: sendWebSocketMessageImplementation,
|
|
19
|
+
hookNames,
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* The `snap_sendWebSocketMessage` method implementation.
|
|
23
|
+
*
|
|
24
|
+
* @param req - The JSON-RPC request object.
|
|
25
|
+
* @param res - The JSON-RPC response object.
|
|
26
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this function.
|
|
27
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
28
|
+
* @param hooks - The RPC method hooks.
|
|
29
|
+
* @param hooks.hasPermission - The function to check if a snap has the `endowment:network-access` permission.
|
|
30
|
+
* @param hooks.sendWebSocketMessage - The function to send a WebSocket message.
|
|
31
|
+
* @returns Nothing.
|
|
32
|
+
*/
|
|
33
|
+
function sendWebSocketMessageImplementation(req, res, _next, end, { hasPermission, sendWebSocketMessage }) {
|
|
34
|
+
if (!hasPermission(SnapEndowments.NetworkAccess)) {
|
|
35
|
+
return end(providerErrors.unauthorized());
|
|
36
|
+
}
|
|
37
|
+
const { params } = req;
|
|
38
|
+
try {
|
|
39
|
+
const { id, message } = getValidatedParams(params);
|
|
40
|
+
sendWebSocketMessage(id, message);
|
|
41
|
+
res.result = null;
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
return end(error);
|
|
45
|
+
}
|
|
46
|
+
return end();
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Validates the parameters for the snap_sendWebSocketMessage method.
|
|
50
|
+
*
|
|
51
|
+
* @param params - Parameters to validate.
|
|
52
|
+
* @returns Validated parameters.
|
|
53
|
+
* @throws Throws RPC error if validation fails.
|
|
54
|
+
*/
|
|
55
|
+
function getValidatedParams(params) {
|
|
56
|
+
try {
|
|
57
|
+
return create(params, SendWebSocketMessageParametersStruct);
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
if (error instanceof StructError) {
|
|
61
|
+
throw rpcErrors.invalidParams({
|
|
62
|
+
message: `Invalid params: ${error.message}.`,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/* istanbul ignore next */
|
|
66
|
+
throw rpcErrors.internal();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=sendWebSocketMessage.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sendWebSocketMessage.mjs","sourceRoot":"","sources":["../../src/permitted/sendWebSocketMessage.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,6BAA6B;AAMjE,OAAO,EAAE,KAAK,EAAE,4BAA4B;AAE5C,OAAO,EACL,MAAM,EACN,MAAM,EACN,MAAM,EACN,MAAM,EACN,KAAK,EACL,WAAW,EACZ,8BAA8B;AAG/B,OAAO,EAAE,cAAc,EAAE,gCAAsB;AAG/C,MAAM,SAAS,GAAuD;IACpE,aAAa,EAAE,IAAI;IACnB,oBAAoB,EAAE,IAAI;CAC3B,CAAC;AAOF,MAAM,oCAAoC,GAAG,MAAM,CAAC;IAClD,EAAE,EAAE,MAAM,EAAE;IACZ,OAAO,EAAE,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;CAC5C,CAAC,CAAC;AAOH;;GAEG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAIpC;IACF,WAAW,EAAE,CAAC,2BAA2B,CAAC;IAC1C,cAAc,EAAE,kCAAkC;IAClD,SAAS;CACV,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,SAAS,kCAAkC,CACzC,GAAmD,EACnD,GAAuD,EACvD,KAAc,EACd,GAA6B,EAC7B,EAAE,aAAa,EAAE,oBAAoB,EAAmC;IAExE,IAAI,CAAC,aAAa,CAAC,cAAc,CAAC,aAAa,CAAC,EAAE,CAAC;QACjD,OAAO,GAAG,CAAC,cAAc,CAAC,YAAY,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IAEvB,IAAI,CAAC;QACH,MAAM,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;QACnD,oBAAoB,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;QAClC,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC;IACpB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAED;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,MAAe;IACzC,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,MAAM,EAAE,oCAAoC,CAAC,CAAC;IAC9D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,WAAW,EAAE,CAAC;YACjC,MAAM,SAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,mBAAmB,KAAK,CAAC,OAAO,GAAG;aAC7C,CAAC,CAAC;QACL,CAAC;QACD,0BAA0B;QAC1B,MAAM,SAAS,CAAC,QAAQ,EAAE,CAAC;IAC7B,CAAC;AACH,CAAC","sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type {\n JsonRpcRequest,\n SendWebSocketMessageParams,\n SendWebSocketMessageResult,\n} from '@metamask/snaps-sdk';\nimport { union } from '@metamask/snaps-sdk';\nimport type { InferMatching } from '@metamask/snaps-utils';\nimport {\n create,\n object,\n number,\n string,\n array,\n StructError,\n} from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport { SnapEndowments } from '../endowments';\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<SendWebSocketMessageMethodHooks> = {\n hasPermission: true,\n sendWebSocketMessage: true,\n};\n\nexport type SendWebSocketMessageMethodHooks = {\n hasPermission: (permissionName: string) => boolean;\n sendWebSocketMessage: (id: string, data: string | number[]) => void;\n};\n\nconst SendWebSocketMessageParametersStruct = object({\n id: string(),\n message: union([string(), array(number())]),\n});\n\nexport type SendWebSocketMessageParameters = InferMatching<\n typeof SendWebSocketMessageParametersStruct,\n SendWebSocketMessageParams\n>;\n\n/**\n * Handler for the `snap_sendWebSocketMessage` method.\n */\nexport const sendWebSocketMessageHandler: PermittedHandlerExport<\n SendWebSocketMessageMethodHooks,\n SendWebSocketMessageParams,\n SendWebSocketMessageResult\n> = {\n methodNames: ['snap_sendWebSocketMessage'],\n implementation: sendWebSocketMessageImplementation,\n hookNames,\n};\n\n/**\n * The `snap_sendWebSocketMessage` method implementation.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.hasPermission - The function to check if a snap has the `endowment:network-access` permission.\n * @param hooks.sendWebSocketMessage - The function to send a WebSocket message.\n * @returns Nothing.\n */\nfunction sendWebSocketMessageImplementation(\n req: JsonRpcRequest<SendWebSocketMessageParameters>,\n res: PendingJsonRpcResponse<SendWebSocketMessageResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { hasPermission, sendWebSocketMessage }: SendWebSocketMessageMethodHooks,\n): void {\n if (!hasPermission(SnapEndowments.NetworkAccess)) {\n return end(providerErrors.unauthorized());\n }\n\n const { params } = req;\n\n try {\n const { id, message } = getValidatedParams(params);\n sendWebSocketMessage(id, message);\n res.result = null;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validates the parameters for the snap_sendWebSocketMessage method.\n *\n * @param params - Parameters to validate.\n * @returns Validated parameters.\n * @throws Throws RPC error if validation fails.\n */\nfunction getValidatedParams(params: unknown): SendWebSocketMessageParameters {\n try {\n return create(params, SendWebSocketMessageParametersStruct);\n } catch (error) {\n if (error instanceof StructError) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n"]}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.trackErrorHandler = void 0;
|
|
4
|
+
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
5
|
+
const superstruct_1 = require("@metamask/superstruct");
|
|
6
|
+
const hookNames = {
|
|
7
|
+
trackError: true,
|
|
8
|
+
getSnap: true,
|
|
9
|
+
};
|
|
10
|
+
const TrackableErrorStruct = (0, superstruct_1.object)({
|
|
11
|
+
name: (0, superstruct_1.string)(),
|
|
12
|
+
message: (0, superstruct_1.string)(),
|
|
13
|
+
stack: (0, superstruct_1.nullable)((0, superstruct_1.string)()),
|
|
14
|
+
cause: (0, superstruct_1.nullable)((0, superstruct_1.lazy)(() => TrackableErrorStruct)),
|
|
15
|
+
});
|
|
16
|
+
const TrackErrorParametersStruct = (0, superstruct_1.object)({
|
|
17
|
+
error: TrackableErrorStruct,
|
|
18
|
+
});
|
|
19
|
+
/**
|
|
20
|
+
* Handler for the `snap_trackError` method.
|
|
21
|
+
*/
|
|
22
|
+
exports.trackErrorHandler = {
|
|
23
|
+
methodNames: ['snap_trackError'],
|
|
24
|
+
implementation: getTrackErrorImplementation,
|
|
25
|
+
hookNames,
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* The `snap_trackError` method implementation. This method allows preinstalled
|
|
29
|
+
* Snaps to send errors to the Sentry instance in the client for tracking.
|
|
30
|
+
*
|
|
31
|
+
* @param request - The JSON-RPC request object.
|
|
32
|
+
* @param response - The JSON-RPC response object.
|
|
33
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
34
|
+
* function.
|
|
35
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
36
|
+
* @param hooks - The RPC method hooks.
|
|
37
|
+
* @param hooks.trackError - The hook function to track an error.
|
|
38
|
+
* @param hooks.getSnap - The hook function to get Snap metadata.
|
|
39
|
+
* @returns Nothing.
|
|
40
|
+
*/
|
|
41
|
+
function getTrackErrorImplementation(request, response, _next, end, { trackError, getSnap }) {
|
|
42
|
+
const snap = getSnap(request.origin);
|
|
43
|
+
if (!snap?.preinstalled) {
|
|
44
|
+
return end(rpc_errors_1.rpcErrors.methodNotFound());
|
|
45
|
+
}
|
|
46
|
+
const { params } = request;
|
|
47
|
+
try {
|
|
48
|
+
const validatedParams = getValidatedParams(params);
|
|
49
|
+
const error = deserializeError(validatedParams.error);
|
|
50
|
+
response.result = trackError(error);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
return end(error);
|
|
54
|
+
}
|
|
55
|
+
return end();
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Validates the parameters for the snap_trackEvent method.
|
|
59
|
+
*
|
|
60
|
+
* @param params - Parameters to validate.
|
|
61
|
+
* @returns Validated parameters.
|
|
62
|
+
* @throws Throws RPC error if validation fails.
|
|
63
|
+
*/
|
|
64
|
+
function getValidatedParams(params) {
|
|
65
|
+
try {
|
|
66
|
+
return (0, superstruct_1.create)(params, TrackErrorParametersStruct);
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (error instanceof superstruct_1.StructError) {
|
|
70
|
+
throw rpc_errors_1.rpcErrors.invalidParams({
|
|
71
|
+
message: `Invalid params: ${error.message}.`,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/* istanbul ignore next */
|
|
75
|
+
throw rpc_errors_1.rpcErrors.internal();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Deserialize a {@link TrackableError} into a standard {@link Error} object.
|
|
80
|
+
*
|
|
81
|
+
* @param error - The error to deserialize.
|
|
82
|
+
* @returns A standard {@link Error} object with the same properties as the
|
|
83
|
+
* original {@link TrackableError}.
|
|
84
|
+
*/
|
|
85
|
+
function deserializeError(error) {
|
|
86
|
+
const deserializedError = new Error(error.message);
|
|
87
|
+
deserializedError.name = error.name;
|
|
88
|
+
deserializedError.stack = error.stack ?? undefined;
|
|
89
|
+
deserializedError.cause = error.cause
|
|
90
|
+
? deserializeError(error.cause)
|
|
91
|
+
: undefined;
|
|
92
|
+
return deserializedError;
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=trackError.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trackError.cjs","sourceRoot":"","sources":["../../src/permitted/trackError.ts"],"names":[],"mappings":";;;AAEA,qDAAiD;AASjD,uDAO+B;AAK/B,MAAM,SAAS,GAA6C;IAC1D,UAAU,EAAE,IAAI;IAChB,OAAO,EAAE,IAAI;CACd,CAAC;AAoBF,MAAM,oBAAoB,GAA2B,IAAA,oBAAM,EAAC;IAC1D,IAAI,EAAE,IAAA,oBAAM,GAAE;IACd,OAAO,EAAE,IAAA,oBAAM,GAAE;IACjB,KAAK,EAAE,IAAA,sBAAQ,EAAC,IAAA,oBAAM,GAAE,CAAC;IACzB,KAAK,EAAE,IAAA,sBAAQ,EAAC,IAAA,kBAAI,EAAiB,GAAG,EAAE,CAAC,oBAAoB,CAAC,CAAC;CAClE,CAAC,CAAC;AAEH,MAAM,0BAA0B,GAAG,IAAA,oBAAM,EAAC;IACxC,KAAK,EAAE,oBAAoB;CAC5B,CAAC,CAAC;AAOH;;GAEG;AACU,QAAA,iBAAiB,GAI1B;IACF,WAAW,EAAE,CAAC,iBAAiB,CAAC;IAChC,cAAc,EAAE,2BAA2B;IAC3C,SAAS;CACV,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,SAAS,2BAA2B,CAClC,OAA6C,EAC7C,QAAkD,EAClD,KAAc,EACd,GAA6B,EAC7B,EAAE,UAAU,EAAE,OAAO,EAAyB;IAE9C,MAAM,IAAI,GAAG,OAAO,CACjB,OAAiE,CAAC,MAAM,CAC1E,CAAC;IAEF,IAAI,CAAC,IAAI,EAAE,YAAY,EAAE,CAAC;QACxB,OAAO,GAAG,CAAC,sBAAS,CAAC,cAAc,EAAE,CAAC,CAAC;IACzC,CAAC;IAED,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAE3B,IAAI,CAAC;QACH,MAAM,eAAe,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;QACnD,MAAM,KAAK,GAAG,gBAAgB,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;QAEtD,QAAQ,CAAC,MAAM,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC;IACtC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAED;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,MAAe;IACzC,IAAI,CAAC;QACH,OAAO,IAAA,oBAAM,EAAC,MAAM,EAAE,0BAA0B,CAAC,CAAC;IACpD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,yBAAW,EAAE,CAAC;YACjC,MAAM,sBAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,mBAAmB,KAAK,CAAC,OAAO,GAAG;aAC7C,CAAC,CAAC;QACL,CAAC;QAED,0BAA0B;QAC1B,MAAM,sBAAS,CAAC,QAAQ,EAAE,CAAC;IAC7B,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,SAAS,gBAAgB,CAAC,KAAqB;IAC7C,MAAM,iBAAiB,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACnD,iBAAiB,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;IACpC,iBAAiB,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,IAAI,SAAS,CAAC;IACnD,iBAAiB,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK;QACnC,CAAC,CAAC,gBAAgB,CAAC,KAAK,CAAC,KAAK,CAAC;QAC/B,CAAC,CAAC,SAAS,CAAC;IAEd,OAAO,iBAAiB,CAAC;AAC3B,CAAC","sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n JsonRpcRequest,\n TrackableError,\n TrackErrorParams,\n TrackErrorResult,\n} from '@metamask/snaps-sdk';\nimport type { InferMatching, Snap } from '@metamask/snaps-utils';\nimport type { Struct } from '@metamask/superstruct';\nimport {\n create,\n lazy,\n nullable,\n object,\n string,\n StructError,\n} from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<TrackErrorMethodHooks> = {\n trackError: true,\n getSnap: true,\n};\n\nexport type TrackErrorMethodHooks = {\n /**\n * Track an error.\n *\n * @param error - The error object to track.\n * @returns The ID of the tracked error, as returned by the Sentry instance\n * in the client.\n */\n trackError: (error: Error) => string;\n\n /**\n * Get Snap metadata.\n *\n * @param snapId - The ID of a Snap.\n */\n getSnap: (snapId: string) => Snap | undefined;\n};\n\nconst TrackableErrorStruct: Struct<TrackableError> = object({\n name: string(),\n message: string(),\n stack: nullable(string()),\n cause: nullable(lazy<TrackableError>(() => TrackableErrorStruct)),\n});\n\nconst TrackErrorParametersStruct = object({\n error: TrackableErrorStruct,\n});\n\nexport type TrackErrorParameters = InferMatching<\n typeof TrackErrorParametersStruct,\n TrackErrorParams\n>;\n\n/**\n * Handler for the `snap_trackError` method.\n */\nexport const trackErrorHandler: PermittedHandlerExport<\n TrackErrorMethodHooks,\n TrackErrorParameters,\n TrackErrorResult\n> = {\n methodNames: ['snap_trackError'],\n implementation: getTrackErrorImplementation,\n hookNames,\n};\n\n/**\n * The `snap_trackError` method implementation. This method allows preinstalled\n * Snaps to send errors to the Sentry instance in the client for tracking.\n *\n * @param request - The JSON-RPC request object.\n * @param response - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.trackError - The hook function to track an error.\n * @param hooks.getSnap - The hook function to get Snap metadata.\n * @returns Nothing.\n */\nfunction getTrackErrorImplementation(\n request: JsonRpcRequest<TrackErrorParameters>,\n response: PendingJsonRpcResponse<TrackErrorResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { trackError, getSnap }: TrackErrorMethodHooks,\n): void {\n const snap = getSnap(\n (request as JsonRpcRequest<TrackErrorParams> & { origin: string }).origin,\n );\n\n if (!snap?.preinstalled) {\n return end(rpcErrors.methodNotFound());\n }\n\n const { params } = request;\n\n try {\n const validatedParams = getValidatedParams(params);\n const error = deserializeError(validatedParams.error);\n\n response.result = trackError(error);\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validates the parameters for the snap_trackEvent method.\n *\n * @param params - Parameters to validate.\n * @returns Validated parameters.\n * @throws Throws RPC error if validation fails.\n */\nfunction getValidatedParams(params: unknown): TrackErrorParameters {\n try {\n return create(params, TrackErrorParametersStruct);\n } catch (error) {\n if (error instanceof StructError) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n\n/**\n * Deserialize a {@link TrackableError} into a standard {@link Error} object.\n *\n * @param error - The error to deserialize.\n * @returns A standard {@link Error} object with the same properties as the\n * original {@link TrackableError}.\n */\nfunction deserializeError(error: TrackableError): Error {\n const deserializedError = new Error(error.message);\n deserializedError.name = error.name;\n deserializedError.stack = error.stack ?? undefined;\n deserializedError.cause = error.cause\n ? deserializeError(error.cause)\n : undefined;\n\n return deserializedError;\n}\n"]}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { PermittedHandlerExport } from "@metamask/permission-controller";
|
|
2
|
+
import type { TrackableError, TrackErrorParams, TrackErrorResult } from "@metamask/snaps-sdk";
|
|
3
|
+
import type { InferMatching, Snap } from "@metamask/snaps-utils";
|
|
4
|
+
import type { Struct } from "@metamask/superstruct";
|
|
5
|
+
export type TrackErrorMethodHooks = {
|
|
6
|
+
/**
|
|
7
|
+
* Track an error.
|
|
8
|
+
*
|
|
9
|
+
* @param error - The error object to track.
|
|
10
|
+
* @returns The ID of the tracked error, as returned by the Sentry instance
|
|
11
|
+
* in the client.
|
|
12
|
+
*/
|
|
13
|
+
trackError: (error: Error) => string;
|
|
14
|
+
/**
|
|
15
|
+
* Get Snap metadata.
|
|
16
|
+
*
|
|
17
|
+
* @param snapId - The ID of a Snap.
|
|
18
|
+
*/
|
|
19
|
+
getSnap: (snapId: string) => Snap | undefined;
|
|
20
|
+
};
|
|
21
|
+
declare const TrackErrorParametersStruct: Struct<{
|
|
22
|
+
error: TrackableError;
|
|
23
|
+
}, {
|
|
24
|
+
error: Struct<TrackableError, unknown>;
|
|
25
|
+
}>;
|
|
26
|
+
export type TrackErrorParameters = InferMatching<typeof TrackErrorParametersStruct, TrackErrorParams>;
|
|
27
|
+
/**
|
|
28
|
+
* Handler for the `snap_trackError` method.
|
|
29
|
+
*/
|
|
30
|
+
export declare const trackErrorHandler: PermittedHandlerExport<TrackErrorMethodHooks, TrackErrorParameters, TrackErrorResult>;
|
|
31
|
+
export {};
|
|
32
|
+
//# sourceMappingURL=trackError.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trackError.d.cts","sourceRoot":"","sources":["../../src/permitted/trackError.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,KAAK,EAEV,cAAc,EACd,gBAAgB,EAChB,gBAAgB,EACjB,4BAA4B;AAC7B,OAAO,KAAK,EAAE,aAAa,EAAE,IAAI,EAAE,8BAA8B;AACjE,OAAO,KAAK,EAAE,MAAM,EAAE,8BAA8B;AAkBpD,MAAM,MAAM,qBAAqB,GAAG;IAClC;;;;;;OAMG;IACH,UAAU,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,MAAM,CAAC;IAErC;;;;OAIG;IACH,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,GAAG,SAAS,CAAC;CAC/C,CAAC;AASF,QAAA,MAAM,0BAA0B;;;;EAE9B,CAAC;AAEH,MAAM,MAAM,oBAAoB,GAAG,aAAa,CAC9C,OAAO,0BAA0B,EACjC,gBAAgB,CACjB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,EAAE,sBAAsB,CACpD,qBAAqB,EACrB,oBAAoB,EACpB,gBAAgB,CAKjB,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { PermittedHandlerExport } from "@metamask/permission-controller";
|
|
2
|
+
import type { TrackableError, TrackErrorParams, TrackErrorResult } from "@metamask/snaps-sdk";
|
|
3
|
+
import type { InferMatching, Snap } from "@metamask/snaps-utils";
|
|
4
|
+
import type { Struct } from "@metamask/superstruct";
|
|
5
|
+
export type TrackErrorMethodHooks = {
|
|
6
|
+
/**
|
|
7
|
+
* Track an error.
|
|
8
|
+
*
|
|
9
|
+
* @param error - The error object to track.
|
|
10
|
+
* @returns The ID of the tracked error, as returned by the Sentry instance
|
|
11
|
+
* in the client.
|
|
12
|
+
*/
|
|
13
|
+
trackError: (error: Error) => string;
|
|
14
|
+
/**
|
|
15
|
+
* Get Snap metadata.
|
|
16
|
+
*
|
|
17
|
+
* @param snapId - The ID of a Snap.
|
|
18
|
+
*/
|
|
19
|
+
getSnap: (snapId: string) => Snap | undefined;
|
|
20
|
+
};
|
|
21
|
+
declare const TrackErrorParametersStruct: Struct<{
|
|
22
|
+
error: TrackableError;
|
|
23
|
+
}, {
|
|
24
|
+
error: Struct<TrackableError, unknown>;
|
|
25
|
+
}>;
|
|
26
|
+
export type TrackErrorParameters = InferMatching<typeof TrackErrorParametersStruct, TrackErrorParams>;
|
|
27
|
+
/**
|
|
28
|
+
* Handler for the `snap_trackError` method.
|
|
29
|
+
*/
|
|
30
|
+
export declare const trackErrorHandler: PermittedHandlerExport<TrackErrorMethodHooks, TrackErrorParameters, TrackErrorResult>;
|
|
31
|
+
export {};
|
|
32
|
+
//# sourceMappingURL=trackError.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trackError.d.mts","sourceRoot":"","sources":["../../src/permitted/trackError.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,KAAK,EAEV,cAAc,EACd,gBAAgB,EAChB,gBAAgB,EACjB,4BAA4B;AAC7B,OAAO,KAAK,EAAE,aAAa,EAAE,IAAI,EAAE,8BAA8B;AACjE,OAAO,KAAK,EAAE,MAAM,EAAE,8BAA8B;AAkBpD,MAAM,MAAM,qBAAqB,GAAG;IAClC;;;;;;OAMG;IACH,UAAU,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,MAAM,CAAC;IAErC;;;;OAIG;IACH,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,GAAG,SAAS,CAAC;CAC/C,CAAC;AASF,QAAA,MAAM,0BAA0B;;;;EAE9B,CAAC;AAEH,MAAM,MAAM,oBAAoB,GAAG,aAAa,CAC9C,OAAO,0BAA0B,EACjC,gBAAgB,CACjB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,EAAE,sBAAsB,CACpD,qBAAqB,EACrB,oBAAoB,EACpB,gBAAgB,CAKjB,CAAC"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { rpcErrors } from "@metamask/rpc-errors";
|
|
2
|
+
import { create, lazy, nullable, object, string, StructError } from "@metamask/superstruct";
|
|
3
|
+
const hookNames = {
|
|
4
|
+
trackError: true,
|
|
5
|
+
getSnap: true,
|
|
6
|
+
};
|
|
7
|
+
const TrackableErrorStruct = object({
|
|
8
|
+
name: string(),
|
|
9
|
+
message: string(),
|
|
10
|
+
stack: nullable(string()),
|
|
11
|
+
cause: nullable(lazy(() => TrackableErrorStruct)),
|
|
12
|
+
});
|
|
13
|
+
const TrackErrorParametersStruct = object({
|
|
14
|
+
error: TrackableErrorStruct,
|
|
15
|
+
});
|
|
16
|
+
/**
|
|
17
|
+
* Handler for the `snap_trackError` method.
|
|
18
|
+
*/
|
|
19
|
+
export const trackErrorHandler = {
|
|
20
|
+
methodNames: ['snap_trackError'],
|
|
21
|
+
implementation: getTrackErrorImplementation,
|
|
22
|
+
hookNames,
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* The `snap_trackError` method implementation. This method allows preinstalled
|
|
26
|
+
* Snaps to send errors to the Sentry instance in the client for tracking.
|
|
27
|
+
*
|
|
28
|
+
* @param request - The JSON-RPC request object.
|
|
29
|
+
* @param response - The JSON-RPC response object.
|
|
30
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
31
|
+
* function.
|
|
32
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
33
|
+
* @param hooks - The RPC method hooks.
|
|
34
|
+
* @param hooks.trackError - The hook function to track an error.
|
|
35
|
+
* @param hooks.getSnap - The hook function to get Snap metadata.
|
|
36
|
+
* @returns Nothing.
|
|
37
|
+
*/
|
|
38
|
+
function getTrackErrorImplementation(request, response, _next, end, { trackError, getSnap }) {
|
|
39
|
+
const snap = getSnap(request.origin);
|
|
40
|
+
if (!snap?.preinstalled) {
|
|
41
|
+
return end(rpcErrors.methodNotFound());
|
|
42
|
+
}
|
|
43
|
+
const { params } = request;
|
|
44
|
+
try {
|
|
45
|
+
const validatedParams = getValidatedParams(params);
|
|
46
|
+
const error = deserializeError(validatedParams.error);
|
|
47
|
+
response.result = trackError(error);
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
return end(error);
|
|
51
|
+
}
|
|
52
|
+
return end();
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Validates the parameters for the snap_trackEvent method.
|
|
56
|
+
*
|
|
57
|
+
* @param params - Parameters to validate.
|
|
58
|
+
* @returns Validated parameters.
|
|
59
|
+
* @throws Throws RPC error if validation fails.
|
|
60
|
+
*/
|
|
61
|
+
function getValidatedParams(params) {
|
|
62
|
+
try {
|
|
63
|
+
return create(params, TrackErrorParametersStruct);
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
if (error instanceof StructError) {
|
|
67
|
+
throw rpcErrors.invalidParams({
|
|
68
|
+
message: `Invalid params: ${error.message}.`,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/* istanbul ignore next */
|
|
72
|
+
throw rpcErrors.internal();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Deserialize a {@link TrackableError} into a standard {@link Error} object.
|
|
77
|
+
*
|
|
78
|
+
* @param error - The error to deserialize.
|
|
79
|
+
* @returns A standard {@link Error} object with the same properties as the
|
|
80
|
+
* original {@link TrackableError}.
|
|
81
|
+
*/
|
|
82
|
+
function deserializeError(error) {
|
|
83
|
+
const deserializedError = new Error(error.message);
|
|
84
|
+
deserializedError.name = error.name;
|
|
85
|
+
deserializedError.stack = error.stack ?? undefined;
|
|
86
|
+
deserializedError.cause = error.cause
|
|
87
|
+
? deserializeError(error.cause)
|
|
88
|
+
: undefined;
|
|
89
|
+
return deserializedError;
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=trackError.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trackError.mjs","sourceRoot":"","sources":["../../src/permitted/trackError.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,6BAA6B;AASjD,OAAO,EACL,MAAM,EACN,IAAI,EACJ,QAAQ,EACR,MAAM,EACN,MAAM,EACN,WAAW,EACZ,8BAA8B;AAK/B,MAAM,SAAS,GAA6C;IAC1D,UAAU,EAAE,IAAI;IAChB,OAAO,EAAE,IAAI;CACd,CAAC;AAoBF,MAAM,oBAAoB,GAA2B,MAAM,CAAC;IAC1D,IAAI,EAAE,MAAM,EAAE;IACd,OAAO,EAAE,MAAM,EAAE;IACjB,KAAK,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC;IACzB,KAAK,EAAE,QAAQ,CAAC,IAAI,CAAiB,GAAG,EAAE,CAAC,oBAAoB,CAAC,CAAC;CAClE,CAAC,CAAC;AAEH,MAAM,0BAA0B,GAAG,MAAM,CAAC;IACxC,KAAK,EAAE,oBAAoB;CAC5B,CAAC,CAAC;AAOH;;GAEG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAI1B;IACF,WAAW,EAAE,CAAC,iBAAiB,CAAC;IAChC,cAAc,EAAE,2BAA2B;IAC3C,SAAS;CACV,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,SAAS,2BAA2B,CAClC,OAA6C,EAC7C,QAAkD,EAClD,KAAc,EACd,GAA6B,EAC7B,EAAE,UAAU,EAAE,OAAO,EAAyB;IAE9C,MAAM,IAAI,GAAG,OAAO,CACjB,OAAiE,CAAC,MAAM,CAC1E,CAAC;IAEF,IAAI,CAAC,IAAI,EAAE,YAAY,EAAE,CAAC;QACxB,OAAO,GAAG,CAAC,SAAS,CAAC,cAAc,EAAE,CAAC,CAAC;IACzC,CAAC;IAED,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAE3B,IAAI,CAAC;QACH,MAAM,eAAe,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;QACnD,MAAM,KAAK,GAAG,gBAAgB,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;QAEtD,QAAQ,CAAC,MAAM,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC;IACtC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAED;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,MAAe;IACzC,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,MAAM,EAAE,0BAA0B,CAAC,CAAC;IACpD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,WAAW,EAAE,CAAC;YACjC,MAAM,SAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,mBAAmB,KAAK,CAAC,OAAO,GAAG;aAC7C,CAAC,CAAC;QACL,CAAC;QAED,0BAA0B;QAC1B,MAAM,SAAS,CAAC,QAAQ,EAAE,CAAC;IAC7B,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,SAAS,gBAAgB,CAAC,KAAqB;IAC7C,MAAM,iBAAiB,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACnD,iBAAiB,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;IACpC,iBAAiB,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,IAAI,SAAS,CAAC;IACnD,iBAAiB,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK;QACnC,CAAC,CAAC,gBAAgB,CAAC,KAAK,CAAC,KAAK,CAAC;QAC/B,CAAC,CAAC,SAAS,CAAC;IAEd,OAAO,iBAAiB,CAAC;AAC3B,CAAC","sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n JsonRpcRequest,\n TrackableError,\n TrackErrorParams,\n TrackErrorResult,\n} from '@metamask/snaps-sdk';\nimport type { InferMatching, Snap } from '@metamask/snaps-utils';\nimport type { Struct } from '@metamask/superstruct';\nimport {\n create,\n lazy,\n nullable,\n object,\n string,\n StructError,\n} from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<TrackErrorMethodHooks> = {\n trackError: true,\n getSnap: true,\n};\n\nexport type TrackErrorMethodHooks = {\n /**\n * Track an error.\n *\n * @param error - The error object to track.\n * @returns The ID of the tracked error, as returned by the Sentry instance\n * in the client.\n */\n trackError: (error: Error) => string;\n\n /**\n * Get Snap metadata.\n *\n * @param snapId - The ID of a Snap.\n */\n getSnap: (snapId: string) => Snap | undefined;\n};\n\nconst TrackableErrorStruct: Struct<TrackableError> = object({\n name: string(),\n message: string(),\n stack: nullable(string()),\n cause: nullable(lazy<TrackableError>(() => TrackableErrorStruct)),\n});\n\nconst TrackErrorParametersStruct = object({\n error: TrackableErrorStruct,\n});\n\nexport type TrackErrorParameters = InferMatching<\n typeof TrackErrorParametersStruct,\n TrackErrorParams\n>;\n\n/**\n * Handler for the `snap_trackError` method.\n */\nexport const trackErrorHandler: PermittedHandlerExport<\n TrackErrorMethodHooks,\n TrackErrorParameters,\n TrackErrorResult\n> = {\n methodNames: ['snap_trackError'],\n implementation: getTrackErrorImplementation,\n hookNames,\n};\n\n/**\n * The `snap_trackError` method implementation. This method allows preinstalled\n * Snaps to send errors to the Sentry instance in the client for tracking.\n *\n * @param request - The JSON-RPC request object.\n * @param response - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.trackError - The hook function to track an error.\n * @param hooks.getSnap - The hook function to get Snap metadata.\n * @returns Nothing.\n */\nfunction getTrackErrorImplementation(\n request: JsonRpcRequest<TrackErrorParameters>,\n response: PendingJsonRpcResponse<TrackErrorResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { trackError, getSnap }: TrackErrorMethodHooks,\n): void {\n const snap = getSnap(\n (request as JsonRpcRequest<TrackErrorParams> & { origin: string }).origin,\n );\n\n if (!snap?.preinstalled) {\n return end(rpcErrors.methodNotFound());\n }\n\n const { params } = request;\n\n try {\n const validatedParams = getValidatedParams(params);\n const error = deserializeError(validatedParams.error);\n\n response.result = trackError(error);\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validates the parameters for the snap_trackEvent method.\n *\n * @param params - Parameters to validate.\n * @returns Validated parameters.\n * @throws Throws RPC error if validation fails.\n */\nfunction getValidatedParams(params: unknown): TrackErrorParameters {\n try {\n return create(params, TrackErrorParametersStruct);\n } catch (error) {\n if (error instanceof StructError) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n\n/**\n * Deserialize a {@link TrackableError} into a standard {@link Error} object.\n *\n * @param error - The error to deserialize.\n * @returns A standard {@link Error} object with the same properties as the\n * original {@link TrackableError}.\n */\nfunction deserializeError(error: TrackableError): Error {\n const deserializedError = new Error(error.message);\n deserializedError.name = error.name;\n deserializedError.stack = error.stack ?? undefined;\n deserializedError.cause = error.cause\n ? deserializeError(error.cause)\n : undefined;\n\n return deserializedError;\n}\n"]}
|