@metamask/snaps-rpc-methods 12.4.0 → 13.1.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 +18 -1
- package/dist/endowments/index.cjs +2 -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 +2 -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/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 +8 -0
- package/dist/permitted/handlers.cjs.map +1 -1
- package/dist/permitted/handlers.d.cts +6 -2
- package/dist/permitted/handlers.d.cts.map +1 -1
- package/dist/permitted/handlers.d.mts +6 -2
- package/dist/permitted/handlers.d.mts.map +1 -1
- package/dist/permitted/handlers.mjs +8 -0
- package/dist/permitted/handlers.mjs.map +1 -1
- package/dist/permitted/index.cjs.map +1 -1
- package/dist/permitted/index.d.cts +5 -1
- package/dist/permitted/index.d.cts.map +1 -1
- package/dist/permitted/index.d.mts +5 -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/scheduleBackgroundEvent.cjs +13 -15
- package/dist/permitted/scheduleBackgroundEvent.cjs.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.d.cts +1 -1
- package/dist/permitted/scheduleBackgroundEvent.d.cts.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.d.mts +1 -1
- package/dist/permitted/scheduleBackgroundEvent.d.mts.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.mjs +15 -17
- package/dist/permitted/scheduleBackgroundEvent.mjs.map +1 -1
- 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/package.json +7 -9
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scheduleBackgroundEvent.d.cts","sourceRoot":"","sources":["../../src/permitted/scheduleBackgroundEvent.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,EAGL,KAAK,6BAA6B,EAClC,KAAK,6BAA6B,EACnC,4BAA4B;AAC7B,OAAO,KAAK,EAAE,iBAAiB,EAAE,aAAa,EAAE,8BAA8B;
|
|
1
|
+
{"version":3,"file":"scheduleBackgroundEvent.d.cts","sourceRoot":"","sources":["../../src/permitted/scheduleBackgroundEvent.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,EAGL,KAAK,6BAA6B,EAClC,KAAK,6BAA6B,EACnC,4BAA4B;AAC7B,OAAO,KAAK,EAAE,iBAAiB,EAAE,aAAa,EAAE,8BAA8B;AAmB9E,KAAK,iCAAiC,GAAG;IACvC,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,iBAAiB,CAAC;CAC5B,CAAC;AAEF,MAAM,MAAM,kCAAkC,GAAG;IAC/C,uBAAuB,EAAE,CACvB,SAAS,EAAE,iCAAiC,KACzC,MAAM,CAAC;IAEZ,aAAa,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,OAAO,CAAC;CACpD,CAAC;AAEF,eAAO,MAAM,8BAA8B,EAAE,sBAAsB,CACjE,kCAAkC,EAClC,iCAAiC,EACjC,6BAA6B,CAK9B,CAAC;AAYF,QAAA,MAAM,uCAAuC;;;;;;;;;;;;;;;;QAK3C,CAAC;AAEH,MAAM,MAAM,iCAAiC,GAAG,aAAa,CAC3D,OAAO,uCAAuC,EAC9C,6BAA6B,CAC9B,CAAC"}
|
|
@@ -2,7 +2,7 @@ import type { PermittedHandlerExport } from "@metamask/permission-controller";
|
|
|
2
2
|
import { type ScheduleBackgroundEventParams, type ScheduleBackgroundEventResult } from "@metamask/snaps-sdk";
|
|
3
3
|
import type { CronjobRpcRequest, InferMatching } from "@metamask/snaps-utils";
|
|
4
4
|
type ScheduleBackgroundEventHookParams = {
|
|
5
|
-
|
|
5
|
+
schedule: string;
|
|
6
6
|
request: CronjobRpcRequest;
|
|
7
7
|
};
|
|
8
8
|
export type ScheduleBackgroundEventMethodHooks = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scheduleBackgroundEvent.d.mts","sourceRoot":"","sources":["../../src/permitted/scheduleBackgroundEvent.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,EAGL,KAAK,6BAA6B,EAClC,KAAK,6BAA6B,EACnC,4BAA4B;AAC7B,OAAO,KAAK,EAAE,iBAAiB,EAAE,aAAa,EAAE,8BAA8B;
|
|
1
|
+
{"version":3,"file":"scheduleBackgroundEvent.d.mts","sourceRoot":"","sources":["../../src/permitted/scheduleBackgroundEvent.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,EAGL,KAAK,6BAA6B,EAClC,KAAK,6BAA6B,EACnC,4BAA4B;AAC7B,OAAO,KAAK,EAAE,iBAAiB,EAAE,aAAa,EAAE,8BAA8B;AAmB9E,KAAK,iCAAiC,GAAG;IACvC,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,iBAAiB,CAAC;CAC5B,CAAC;AAEF,MAAM,MAAM,kCAAkC,GAAG;IAC/C,uBAAuB,EAAE,CACvB,SAAS,EAAE,iCAAiC,KACzC,MAAM,CAAC;IAEZ,aAAa,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,OAAO,CAAC;CACpD,CAAC;AAEF,eAAO,MAAM,8BAA8B,EAAE,sBAAsB,CACjE,kCAAkC,EAClC,iCAAiC,EACjC,6BAA6B,CAK9B,CAAC;AAYF,QAAA,MAAM,uCAAuC;;;;;;;;;;;;;;;;QAK3C,CAAC;AAEH,MAAM,MAAM,iCAAiC,GAAG,aAAa,CAC3D,OAAO,uCAAuC,EAC9C,6BAA6B,CAC9B,CAAC"}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { providerErrors, rpcErrors } from "@metamask/rpc-errors";
|
|
2
2
|
import { selectiveUnion } from "@metamask/snaps-sdk";
|
|
3
|
-
import { CronjobRpcRequestStruct, ISO8601DateStruct, ISO8601DurationStruct
|
|
3
|
+
import { CronjobRpcRequestStruct, ISO8601DateStruct, ISO8601DurationStruct } from "@metamask/snaps-utils";
|
|
4
4
|
import { StructError, create, object } from "@metamask/superstruct";
|
|
5
|
-
import {
|
|
6
|
-
import { DateTime, Duration } from "luxon";
|
|
5
|
+
import { hasProperty } from "@metamask/utils";
|
|
7
6
|
import { SnapEndowments } from "../endowments/index.mjs";
|
|
8
7
|
const methodName = 'snap_scheduleBackgroundEvent';
|
|
9
8
|
const hookNames = {
|
|
@@ -30,20 +29,17 @@ const ScheduleBackgroundEventParametersStruct = selectiveUnion((val) => {
|
|
|
30
29
|
return ScheduleBackgroundEventParametersWithDurationStruct;
|
|
31
30
|
});
|
|
32
31
|
/**
|
|
33
|
-
*
|
|
32
|
+
* Get the schedule for a background event based on the provided parameters.
|
|
34
33
|
*
|
|
35
|
-
* @param params - The
|
|
36
|
-
* @returns
|
|
34
|
+
* @param params - The parameters for the background event.
|
|
35
|
+
* @returns The schedule parameters for the background event.
|
|
37
36
|
*/
|
|
38
|
-
function
|
|
39
|
-
if ('
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const duration = parsed.as('seconds') >= 1 ? parsed : Duration.fromObject({ seconds: 1 });
|
|
43
|
-
return DateTime.fromJSDate(new Date()).toUTC().plus(duration).toISO();
|
|
37
|
+
function getSchedule(params) {
|
|
38
|
+
if (hasProperty(params, 'date')) {
|
|
39
|
+
// TODO: Check why `params.date` is not a string.
|
|
40
|
+
return params.date;
|
|
44
41
|
}
|
|
45
|
-
|
|
46
|
-
return toCensoredISO8601String(params.date);
|
|
42
|
+
return params.duration;
|
|
47
43
|
}
|
|
48
44
|
/**
|
|
49
45
|
* The `snap_scheduleBackgroundEvent` method implementation.
|
|
@@ -66,9 +62,11 @@ async function getScheduleBackgroundEventImplementation(req, res, _next, end, {
|
|
|
66
62
|
try {
|
|
67
63
|
const validatedParams = getValidatedParams(params);
|
|
68
64
|
const { request } = validatedParams;
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
65
|
+
const schedule = getSchedule(validatedParams);
|
|
66
|
+
const id = scheduleBackgroundEvent({
|
|
67
|
+
schedule,
|
|
68
|
+
request,
|
|
69
|
+
});
|
|
72
70
|
res.result = id;
|
|
73
71
|
}
|
|
74
72
|
catch (error) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scheduleBackgroundEvent.mjs","sourceRoot":"","sources":["../../src/permitted/scheduleBackgroundEvent.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,6BAA6B;AACjE,OAAO,EACL,cAAc,EAIf,4BAA4B;AAE7B,OAAO,EACL,uBAAuB,EACvB,iBAAiB,EACjB,qBAAqB,
|
|
1
|
+
{"version":3,"file":"scheduleBackgroundEvent.mjs","sourceRoot":"","sources":["../../src/permitted/scheduleBackgroundEvent.ts"],"names":[],"mappings":"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","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 selectiveUnion,\n type JsonRpcRequest,\n type ScheduleBackgroundEventParams,\n type ScheduleBackgroundEventResult,\n} from '@metamask/snaps-sdk';\nimport type { CronjobRpcRequest, InferMatching } from '@metamask/snaps-utils';\nimport {\n CronjobRpcRequestStruct,\n ISO8601DateStruct,\n ISO8601DurationStruct,\n} from '@metamask/snaps-utils';\nimport { StructError, create, object } from '@metamask/superstruct';\nimport { hasProperty, type PendingJsonRpcResponse } from '@metamask/utils';\n\nimport { SnapEndowments } from '../endowments';\nimport type { MethodHooksObject } from '../utils';\n\nconst methodName = 'snap_scheduleBackgroundEvent';\n\nconst hookNames: MethodHooksObject<ScheduleBackgroundEventMethodHooks> = {\n scheduleBackgroundEvent: true,\n hasPermission: true,\n};\n\ntype ScheduleBackgroundEventHookParams = {\n schedule: string;\n request: CronjobRpcRequest;\n};\n\nexport type ScheduleBackgroundEventMethodHooks = {\n scheduleBackgroundEvent: (\n snapEvent: ScheduleBackgroundEventHookParams,\n ) => string;\n\n hasPermission: (permissionName: string) => boolean;\n};\n\nexport const scheduleBackgroundEventHandler: PermittedHandlerExport<\n ScheduleBackgroundEventMethodHooks,\n ScheduleBackgroundEventParameters,\n ScheduleBackgroundEventResult\n> = {\n methodNames: [methodName],\n implementation: getScheduleBackgroundEventImplementation,\n hookNames,\n};\n\nconst ScheduleBackgroundEventParametersWithDateStruct = object({\n date: ISO8601DateStruct,\n request: CronjobRpcRequestStruct,\n});\n\nconst ScheduleBackgroundEventParametersWithDurationStruct = object({\n duration: ISO8601DurationStruct,\n request: CronjobRpcRequestStruct,\n});\n\nconst ScheduleBackgroundEventParametersStruct = selectiveUnion((val) => {\n if (hasProperty(val, 'date')) {\n return ScheduleBackgroundEventParametersWithDateStruct;\n }\n return ScheduleBackgroundEventParametersWithDurationStruct;\n});\n\nexport type ScheduleBackgroundEventParameters = InferMatching<\n typeof ScheduleBackgroundEventParametersStruct,\n ScheduleBackgroundEventParams\n>;\n\n/**\n * Get the schedule for a background event based on the provided parameters.\n *\n * @param params - The parameters for the background event.\n * @returns The schedule parameters for the background event.\n */\nfunction getSchedule(params: ScheduleBackgroundEventParameters): string {\n if (hasProperty(params, 'date')) {\n // TODO: Check why `params.date` is not a string.\n return params.date as string;\n }\n\n return params.duration;\n}\n\n/**\n * The `snap_scheduleBackgroundEvent` 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\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.scheduleBackgroundEvent - The function to schedule a background event.\n * @param hooks.hasPermission - The function to check if a snap has the `endowment:cronjob` permission.\n * @returns An id representing the background event.\n */\nasync function getScheduleBackgroundEventImplementation(\n req: JsonRpcRequest<ScheduleBackgroundEventParameters>,\n res: PendingJsonRpcResponse<ScheduleBackgroundEventResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n {\n scheduleBackgroundEvent,\n hasPermission,\n }: ScheduleBackgroundEventMethodHooks,\n): Promise<void> {\n const { params } = req;\n\n if (!hasPermission(SnapEndowments.Cronjob)) {\n return end(providerErrors.unauthorized());\n }\n\n try {\n const validatedParams = getValidatedParams(params);\n const { request } = validatedParams;\n const schedule = getSchedule(validatedParams);\n\n const id = scheduleBackgroundEvent({\n schedule,\n request,\n });\n\n res.result = id;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validate the scheduleBackgroundEvent method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @returns The validated resolveInterface method parameter object.\n */\nfunction getValidatedParams(\n params: unknown,\n): ScheduleBackgroundEventParameters {\n try {\n return create(params, ScheduleBackgroundEventParametersStruct);\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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/snaps-rpc-methods",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "13.1.0",
|
|
4
4
|
"description": "MetaMask Snaps JSON-RPC method implementations",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -58,21 +58,19 @@
|
|
|
58
58
|
"@metamask/key-tree": "^10.1.1",
|
|
59
59
|
"@metamask/permission-controller": "^11.0.6",
|
|
60
60
|
"@metamask/rpc-errors": "^7.0.2",
|
|
61
|
-
"@metamask/snaps-sdk": "^
|
|
62
|
-
"@metamask/snaps-utils": "^
|
|
61
|
+
"@metamask/snaps-sdk": "^8.1.0",
|
|
62
|
+
"@metamask/snaps-utils": "^10.1.0",
|
|
63
63
|
"@metamask/superstruct": "^3.2.1",
|
|
64
64
|
"@metamask/utils": "^11.4.0",
|
|
65
|
-
"@noble/hashes": "^1.7.1"
|
|
66
|
-
"luxon": "^3.5.0"
|
|
65
|
+
"@noble/hashes": "^1.7.1"
|
|
67
66
|
},
|
|
68
67
|
"devDependencies": {
|
|
69
68
|
"@lavamoat/allow-scripts": "^3.3.3",
|
|
70
69
|
"@metamask/auto-changelog": "^5.0.2",
|
|
71
70
|
"@metamask/json-rpc-engine": "^10.0.2",
|
|
72
|
-
"@swc/core": "1.
|
|
73
|
-
"@swc/jest": "^0.2.
|
|
71
|
+
"@swc/core": "1.11.31",
|
|
72
|
+
"@swc/jest": "^0.2.38",
|
|
74
73
|
"@ts-bridge/cli": "^0.6.1",
|
|
75
|
-
"@types/luxon": "^3",
|
|
76
74
|
"@types/node": "18.14.2",
|
|
77
75
|
"deepmerge": "^4.2.2",
|
|
78
76
|
"depcheck": "^1.4.7",
|
|
@@ -84,7 +82,7 @@
|
|
|
84
82
|
"typescript": "~5.3.3"
|
|
85
83
|
},
|
|
86
84
|
"engines": {
|
|
87
|
-
"node": "^
|
|
85
|
+
"node": "^20 || >=22"
|
|
88
86
|
},
|
|
89
87
|
"publishConfig": {
|
|
90
88
|
"access": "public",
|