@metamask/snaps-rpc-methods 13.5.0 → 13.5.2
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 +19 -1
- package/dist/permitted/createInterface.cjs +2 -2
- package/dist/permitted/createInterface.cjs.map +1 -1
- package/dist/permitted/createInterface.d.cts +15 -3
- package/dist/permitted/createInterface.d.cts.map +1 -1
- package/dist/permitted/createInterface.d.mts +15 -3
- package/dist/permitted/createInterface.d.mts.map +1 -1
- package/dist/permitted/createInterface.mjs +2 -2
- package/dist/permitted/createInterface.mjs.map +1 -1
- package/dist/permitted/sendWebSocketMessage.cjs +2 -2
- package/dist/permitted/sendWebSocketMessage.cjs.map +1 -1
- package/dist/permitted/sendWebSocketMessage.d.cts +1 -1
- package/dist/permitted/sendWebSocketMessage.d.cts.map +1 -1
- package/dist/permitted/sendWebSocketMessage.d.mts +1 -1
- package/dist/permitted/sendWebSocketMessage.d.mts.map +1 -1
- package/dist/permitted/sendWebSocketMessage.mjs +2 -2
- package/dist/permitted/sendWebSocketMessage.mjs.map +1 -1
- package/dist/permitted/setState.cjs +13 -6
- package/dist/permitted/setState.cjs.map +1 -1
- package/dist/permitted/setState.d.cts +7 -1
- package/dist/permitted/setState.d.cts.map +1 -1
- package/dist/permitted/setState.d.mts +7 -1
- package/dist/permitted/setState.d.mts.map +1 -1
- package/dist/permitted/setState.mjs +14 -7
- package/dist/permitted/setState.mjs.map +1 -1
- package/dist/permitted/updateInterface.cjs +2 -2
- package/dist/permitted/updateInterface.cjs.map +1 -1
- package/dist/permitted/updateInterface.d.cts +1 -1
- package/dist/permitted/updateInterface.d.cts.map +1 -1
- package/dist/permitted/updateInterface.d.mts +1 -1
- package/dist/permitted/updateInterface.d.mts.map +1 -1
- package/dist/permitted/updateInterface.mjs +2 -2
- package/dist/permitted/updateInterface.mjs.map +1 -1
- package/dist/restricted/manageState.cjs +16 -14
- package/dist/restricted/manageState.cjs.map +1 -1
- package/dist/restricted/manageState.d.cts +10 -3
- package/dist/restricted/manageState.d.cts.map +1 -1
- package/dist/restricted/manageState.d.mts +10 -3
- package/dist/restricted/manageState.d.mts.map +1 -1
- package/dist/restricted/manageState.mjs +18 -16
- package/dist/restricted/manageState.mjs.map +1 -1
- package/package.json +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [13.5.2]
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- Reduce JSON validation during state updates ([#3660](https://github.com/MetaMask/snaps/pull/3660))
|
|
15
|
+
- As part of this change, JSON sizing limits are not longer enforced for preinstalled Snaps.
|
|
16
|
+
- Adjust hooks after interface actions were made synchronous ([#3361](https://github.com/MetaMask/snaps/pull/3361))
|
|
17
|
+
- Bump MetaMask dependencies ([#3651](https://github.com/MetaMask/snaps/pull/3651), [#3638](https://github.com/MetaMask/snaps/pull/3638), [#3648](https://github.com/MetaMask/snaps/pull/3648), [#3630](https://github.com/MetaMask/snaps/pull/3630), [#3628](https://github.com/MetaMask/snaps/pull/3628), [#3629](https://github.com/MetaMask/snaps/pull/3629), [#3607](https://github.com/MetaMask/snaps/pull/3607), [#3623](https://github.com/MetaMask/snaps/pull/3623), [#3612](https://github.com/MetaMask/snaps/pull/3612))
|
|
18
|
+
|
|
19
|
+
## [13.5.1]
|
|
20
|
+
|
|
21
|
+
### Fixed
|
|
22
|
+
|
|
23
|
+
- Await `sendWebSocketMessage` promise ([#3608](https://github.com/MetaMask/snaps/pull/3608))
|
|
24
|
+
- Errors were not previously being thrown when calling `snap_sendWebSocketMessage`, this also fixes that.
|
|
25
|
+
|
|
10
26
|
## [13.5.0]
|
|
11
27
|
|
|
12
28
|
### Changed
|
|
@@ -493,7 +509,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
493
509
|
- The version of the package no longer needs to match the version of all other
|
|
494
510
|
MetaMask Snaps packages.
|
|
495
511
|
|
|
496
|
-
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@13.5.
|
|
512
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@13.5.2...HEAD
|
|
513
|
+
[13.5.2]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@13.5.1...@metamask/snaps-rpc-methods@13.5.2
|
|
514
|
+
[13.5.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@13.5.0...@metamask/snaps-rpc-methods@13.5.1
|
|
497
515
|
[13.5.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@13.4.0...@metamask/snaps-rpc-methods@13.5.0
|
|
498
516
|
[13.4.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@13.3.0...@metamask/snaps-rpc-methods@13.4.0
|
|
499
517
|
[13.3.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@13.2.0...@metamask/snaps-rpc-methods@13.3.0
|
|
@@ -28,12 +28,12 @@ const CreateInterfaceParametersStruct = (0, superstruct_1.object)({
|
|
|
28
28
|
* @param hooks.createInterface - The function to create the interface.
|
|
29
29
|
* @returns Nothing.
|
|
30
30
|
*/
|
|
31
|
-
|
|
31
|
+
function getCreateInterfaceImplementation(req, res, _next, end, { createInterface }) {
|
|
32
32
|
const { params } = req;
|
|
33
33
|
try {
|
|
34
34
|
const validatedParams = getValidatedParams(params);
|
|
35
35
|
const { ui, context } = validatedParams;
|
|
36
|
-
res.result =
|
|
36
|
+
res.result = createInterface(ui, context);
|
|
37
37
|
}
|
|
38
38
|
catch (error) {
|
|
39
39
|
return end(error);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createInterface.cjs","sourceRoot":"","sources":["../../src/permitted/createInterface.ts"],"names":[],"mappings":";;;AAEA,q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C,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,+BAA+B,CAAC,CAAC;IACzD,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 { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n CreateInterfaceParams,\n CreateInterfaceResult,\n JsonRpcRequest,\n ComponentOrElement,\n InterfaceContext,\n ContentType,\n} from '@metamask/snaps-sdk';\nimport {\n ComponentOrElementStruct,\n InterfaceContextStruct,\n} from '@metamask/snaps-sdk';\nimport { type InferMatching } from '@metamask/snaps-utils';\nimport { StructError, create, object, optional } from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<CreateInterfaceMethodHooks> = {\n createInterface: true,\n};\n\nexport type CreateInterfaceMethodHooks = {\n /**\n * @param ui - The UI components.\n * @returns The unique identifier of the interface.\n */\n createInterface: (\n ui: ComponentOrElement,\n context?: InterfaceContext,\n contentType?: ContentType,\n ) => string;\n};\n\nexport const createInterfaceHandler: PermittedHandlerExport<\n CreateInterfaceMethodHooks,\n CreateInterfaceParameters,\n CreateInterfaceResult\n> = {\n methodNames: ['snap_createInterface'],\n implementation: getCreateInterfaceImplementation,\n hookNames,\n};\n\nconst CreateInterfaceParametersStruct = object({\n ui: ComponentOrElementStruct,\n context: optional(InterfaceContextStruct),\n});\n\nexport type CreateInterfaceParameters = InferMatching<\n typeof CreateInterfaceParametersStruct,\n CreateInterfaceParams\n>;\n\n/**\n * The `snap_createInterface` 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.createInterface - The function to create the interface.\n * @returns Nothing.\n */\nfunction getCreateInterfaceImplementation(\n req: JsonRpcRequest<CreateInterfaceParameters>,\n res: PendingJsonRpcResponse<CreateInterfaceResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { createInterface }: CreateInterfaceMethodHooks,\n): void {\n const { params } = req;\n\n try {\n const validatedParams = getValidatedParams(params);\n\n const { ui, context } = validatedParams;\n\n res.result = createInterface(ui, context);\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validate the createInterface 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 createInterface method parameter object.\n */\nfunction getValidatedParams(params: unknown): CreateInterfaceParameters {\n try {\n return create(params, CreateInterfaceParametersStruct);\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"]}
|
|
@@ -6,7 +6,7 @@ export type CreateInterfaceMethodHooks = {
|
|
|
6
6
|
* @param ui - The UI components.
|
|
7
7
|
* @returns The unique identifier of the interface.
|
|
8
8
|
*/
|
|
9
|
-
createInterface: (ui: ComponentOrElement, context?: InterfaceContext, contentType?: ContentType) =>
|
|
9
|
+
createInterface: (ui: ComponentOrElement, context?: InterfaceContext, contentType?: ContentType) => string;
|
|
10
10
|
};
|
|
11
11
|
export declare const createInterfaceHandler: PermittedHandlerExport<CreateInterfaceMethodHooks, CreateInterfaceParameters, CreateInterfaceResult>;
|
|
12
12
|
declare const CreateInterfaceParametersStruct: import("@metamask/superstruct").Struct<{
|
|
@@ -21,7 +21,13 @@ declare const CreateInterfaceParametersStruct: import("@metamask/superstruct").S
|
|
|
21
21
|
type: import("@metamask/snaps-sdk").NodeType.Heading;
|
|
22
22
|
} | {
|
|
23
23
|
value: string;
|
|
24
|
-
type: import("@metamask/snaps-sdk").NodeType.Image;
|
|
24
|
+
type: import("@metamask/snaps-sdk").NodeType.Image; /**
|
|
25
|
+
* Validate the createInterface method `params` and returns them cast to the correct
|
|
26
|
+
* type. Throws if validation fails.
|
|
27
|
+
*
|
|
28
|
+
* @param params - The unvalidated params object from the method request.
|
|
29
|
+
* @returns The validated createInterface method parameter object.
|
|
30
|
+
*/
|
|
25
31
|
} | {
|
|
26
32
|
type: import("@metamask/snaps-sdk").NodeType.Spinner;
|
|
27
33
|
} | {
|
|
@@ -108,7 +114,13 @@ declare const CreateInterfaceParametersStruct: import("@metamask/superstruct").S
|
|
|
108
114
|
type: import("@metamask/snaps-sdk").NodeType.Heading;
|
|
109
115
|
} | {
|
|
110
116
|
value: string;
|
|
111
|
-
type: import("@metamask/snaps-sdk").NodeType.Image;
|
|
117
|
+
type: import("@metamask/snaps-sdk").NodeType.Image; /**
|
|
118
|
+
* Validate the createInterface method `params` and returns them cast to the correct
|
|
119
|
+
* type. Throws if validation fails.
|
|
120
|
+
*
|
|
121
|
+
* @param params - The unvalidated params object from the method request.
|
|
122
|
+
* @returns The validated createInterface method parameter object.
|
|
123
|
+
*/
|
|
112
124
|
} | {
|
|
113
125
|
type: import("@metamask/snaps-sdk").NodeType.Spinner;
|
|
114
126
|
} | {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createInterface.d.cts","sourceRoot":"","sources":["../../src/permitted/createInterface.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,KAAK,EACV,qBAAqB,EACrB,qBAAqB,EAErB,kBAAkB,EAClB,gBAAgB,EAChB,WAAW,EACZ,4BAA4B;AAK7B,OAAO,EAAE,KAAK,aAAa,EAAE,8BAA8B;AAU3D,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;OAGG;IACH,eAAe,EAAE,CACf,EAAE,EAAE,kBAAkB,EACtB,OAAO,CAAC,EAAE,gBAAgB,EAC1B,WAAW,CAAC,EAAE,WAAW,KACtB,
|
|
1
|
+
{"version":3,"file":"createInterface.d.cts","sourceRoot":"","sources":["../../src/permitted/createInterface.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,KAAK,EACV,qBAAqB,EACrB,qBAAqB,EAErB,kBAAkB,EAClB,gBAAgB,EAChB,WAAW,EACZ,4BAA4B;AAK7B,OAAO,EAAE,KAAK,aAAa,EAAE,8BAA8B;AAU3D,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;OAGG;IACH,eAAe,EAAE,CACf,EAAE,EAAE,kBAAkB,EACtB,OAAO,CAAC,EAAE,gBAAgB,EAC1B,WAAW,CAAC,EAAE,WAAW,KACtB,MAAM,CAAC;CACb,CAAC;AAEF,eAAO,MAAM,sBAAsB,EAAE,sBAAsB,CACzD,0BAA0B,EAC1B,yBAAyB,EACzB,qBAAqB,CAKtB,CAAC;AAEF,QAAA,MAAM,+BAA+B;;;;;;;;;;;;4DA4CrC;;;;;;WAMG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;4DANH;;;;;;WAMG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA/CD,CAAC;AAEH,MAAM,MAAM,yBAAyB,GAAG,aAAa,CACnD,OAAO,+BAA+B,EACtC,qBAAqB,CACtB,CAAC"}
|
|
@@ -6,7 +6,7 @@ export type CreateInterfaceMethodHooks = {
|
|
|
6
6
|
* @param ui - The UI components.
|
|
7
7
|
* @returns The unique identifier of the interface.
|
|
8
8
|
*/
|
|
9
|
-
createInterface: (ui: ComponentOrElement, context?: InterfaceContext, contentType?: ContentType) =>
|
|
9
|
+
createInterface: (ui: ComponentOrElement, context?: InterfaceContext, contentType?: ContentType) => string;
|
|
10
10
|
};
|
|
11
11
|
export declare const createInterfaceHandler: PermittedHandlerExport<CreateInterfaceMethodHooks, CreateInterfaceParameters, CreateInterfaceResult>;
|
|
12
12
|
declare const CreateInterfaceParametersStruct: import("@metamask/superstruct").Struct<{
|
|
@@ -21,7 +21,13 @@ declare const CreateInterfaceParametersStruct: import("@metamask/superstruct").S
|
|
|
21
21
|
type: import("@metamask/snaps-sdk").NodeType.Heading;
|
|
22
22
|
} | {
|
|
23
23
|
value: string;
|
|
24
|
-
type: import("@metamask/snaps-sdk").NodeType.Image;
|
|
24
|
+
type: import("@metamask/snaps-sdk").NodeType.Image; /**
|
|
25
|
+
* Validate the createInterface method `params` and returns them cast to the correct
|
|
26
|
+
* type. Throws if validation fails.
|
|
27
|
+
*
|
|
28
|
+
* @param params - The unvalidated params object from the method request.
|
|
29
|
+
* @returns The validated createInterface method parameter object.
|
|
30
|
+
*/
|
|
25
31
|
} | {
|
|
26
32
|
type: import("@metamask/snaps-sdk").NodeType.Spinner;
|
|
27
33
|
} | {
|
|
@@ -108,7 +114,13 @@ declare const CreateInterfaceParametersStruct: import("@metamask/superstruct").S
|
|
|
108
114
|
type: import("@metamask/snaps-sdk").NodeType.Heading;
|
|
109
115
|
} | {
|
|
110
116
|
value: string;
|
|
111
|
-
type: import("@metamask/snaps-sdk").NodeType.Image;
|
|
117
|
+
type: import("@metamask/snaps-sdk").NodeType.Image; /**
|
|
118
|
+
* Validate the createInterface method `params` and returns them cast to the correct
|
|
119
|
+
* type. Throws if validation fails.
|
|
120
|
+
*
|
|
121
|
+
* @param params - The unvalidated params object from the method request.
|
|
122
|
+
* @returns The validated createInterface method parameter object.
|
|
123
|
+
*/
|
|
112
124
|
} | {
|
|
113
125
|
type: import("@metamask/snaps-sdk").NodeType.Spinner;
|
|
114
126
|
} | {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createInterface.d.mts","sourceRoot":"","sources":["../../src/permitted/createInterface.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,KAAK,EACV,qBAAqB,EACrB,qBAAqB,EAErB,kBAAkB,EAClB,gBAAgB,EAChB,WAAW,EACZ,4BAA4B;AAK7B,OAAO,EAAE,KAAK,aAAa,EAAE,8BAA8B;AAU3D,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;OAGG;IACH,eAAe,EAAE,CACf,EAAE,EAAE,kBAAkB,EACtB,OAAO,CAAC,EAAE,gBAAgB,EAC1B,WAAW,CAAC,EAAE,WAAW,KACtB,
|
|
1
|
+
{"version":3,"file":"createInterface.d.mts","sourceRoot":"","sources":["../../src/permitted/createInterface.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,KAAK,EACV,qBAAqB,EACrB,qBAAqB,EAErB,kBAAkB,EAClB,gBAAgB,EAChB,WAAW,EACZ,4BAA4B;AAK7B,OAAO,EAAE,KAAK,aAAa,EAAE,8BAA8B;AAU3D,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;OAGG;IACH,eAAe,EAAE,CACf,EAAE,EAAE,kBAAkB,EACtB,OAAO,CAAC,EAAE,gBAAgB,EAC1B,WAAW,CAAC,EAAE,WAAW,KACtB,MAAM,CAAC;CACb,CAAC;AAEF,eAAO,MAAM,sBAAsB,EAAE,sBAAsB,CACzD,0BAA0B,EAC1B,yBAAyB,EACzB,qBAAqB,CAKtB,CAAC;AAEF,QAAA,MAAM,+BAA+B;;;;;;;;;;;;4DA4CrC;;;;;;WAMG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;4DANH;;;;;;WAMG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA/CD,CAAC;AAEH,MAAM,MAAM,yBAAyB,GAAG,aAAa,CACnD,OAAO,+BAA+B,EACtC,qBAAqB,CACtB,CAAC"}
|
|
@@ -25,12 +25,12 @@ const CreateInterfaceParametersStruct = object({
|
|
|
25
25
|
* @param hooks.createInterface - The function to create the interface.
|
|
26
26
|
* @returns Nothing.
|
|
27
27
|
*/
|
|
28
|
-
|
|
28
|
+
function getCreateInterfaceImplementation(req, res, _next, end, { createInterface }) {
|
|
29
29
|
const { params } = req;
|
|
30
30
|
try {
|
|
31
31
|
const validatedParams = getValidatedParams(params);
|
|
32
32
|
const { ui, context } = validatedParams;
|
|
33
|
-
res.result =
|
|
33
|
+
res.result = createInterface(ui, context);
|
|
34
34
|
}
|
|
35
35
|
catch (error) {
|
|
36
36
|
return end(error);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createInterface.mjs","sourceRoot":"","sources":["../../src/permitted/createInterface.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,6BAA6B;AASjD,OAAO,EACL,wBAAwB,EACxB,sBAAsB,EACvB,4BAA4B;AAE7B,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,8BAA8B;AAK9E,MAAM,SAAS,GAAkD;IAC/D,eAAe,EAAE,IAAI;CACtB,CAAC;AAcF,MAAM,CAAC,MAAM,sBAAsB,GAI/B;IACF,WAAW,EAAE,CAAC,sBAAsB,CAAC;IACrC,cAAc,EAAE,gCAAgC;IACh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;IAED,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAED;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,MAAe;IACzC,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,MAAM,EAAE,+BAA+B,CAAC,CAAC;IACzD,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 { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n CreateInterfaceParams,\n CreateInterfaceResult,\n JsonRpcRequest,\n ComponentOrElement,\n InterfaceContext,\n ContentType,\n} from '@metamask/snaps-sdk';\nimport {\n ComponentOrElementStruct,\n InterfaceContextStruct,\n} from '@metamask/snaps-sdk';\nimport { type InferMatching } from '@metamask/snaps-utils';\nimport { StructError, create, object, optional } from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<CreateInterfaceMethodHooks> = {\n createInterface: true,\n};\n\nexport type CreateInterfaceMethodHooks = {\n /**\n * @param ui - The UI components.\n * @returns The unique identifier of the interface.\n */\n createInterface: (\n ui: ComponentOrElement,\n context?: InterfaceContext,\n contentType?: ContentType,\n ) => string;\n};\n\nexport const createInterfaceHandler: PermittedHandlerExport<\n CreateInterfaceMethodHooks,\n CreateInterfaceParameters,\n CreateInterfaceResult\n> = {\n methodNames: ['snap_createInterface'],\n implementation: getCreateInterfaceImplementation,\n hookNames,\n};\n\nconst CreateInterfaceParametersStruct = object({\n ui: ComponentOrElementStruct,\n context: optional(InterfaceContextStruct),\n});\n\nexport type CreateInterfaceParameters = InferMatching<\n typeof CreateInterfaceParametersStruct,\n CreateInterfaceParams\n>;\n\n/**\n * The `snap_createInterface` 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.createInterface - The function to create the interface.\n * @returns Nothing.\n */\nfunction getCreateInterfaceImplementation(\n req: JsonRpcRequest<CreateInterfaceParameters>,\n res: PendingJsonRpcResponse<CreateInterfaceResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { createInterface }: CreateInterfaceMethodHooks,\n): void {\n const { params } = req;\n\n try {\n const validatedParams = getValidatedParams(params);\n\n const { ui, context } = validatedParams;\n\n res.result = createInterface(ui, context);\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validate the createInterface 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 createInterface method parameter object.\n */\nfunction getValidatedParams(params: unknown): CreateInterfaceParameters {\n try {\n return create(params, CreateInterfaceParametersStruct);\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"]}
|
|
@@ -33,14 +33,14 @@ exports.sendWebSocketMessageHandler = {
|
|
|
33
33
|
* @param hooks.sendWebSocketMessage - The function to send a WebSocket message.
|
|
34
34
|
* @returns Nothing.
|
|
35
35
|
*/
|
|
36
|
-
function sendWebSocketMessageImplementation(req, res, _next, end, { hasPermission, sendWebSocketMessage }) {
|
|
36
|
+
async function sendWebSocketMessageImplementation(req, res, _next, end, { hasPermission, sendWebSocketMessage }) {
|
|
37
37
|
if (!hasPermission(endowments_1.SnapEndowments.NetworkAccess)) {
|
|
38
38
|
return end(rpc_errors_1.providerErrors.unauthorized());
|
|
39
39
|
}
|
|
40
40
|
const { params } = req;
|
|
41
41
|
try {
|
|
42
42
|
const { id, message } = getValidatedParams(params);
|
|
43
|
-
sendWebSocketMessage(id, message);
|
|
43
|
+
await sendWebSocketMessage(id, message);
|
|
44
44
|
res.result = null;
|
|
45
45
|
}
|
|
46
46
|
catch (error) {
|
|
@@ -1 +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,
|
|
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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,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[]) => Promise<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 */\nasync function sendWebSocketMessageImplementation(\n req: JsonRpcRequest<SendWebSocketMessageParameters>,\n res: PendingJsonRpcResponse<SendWebSocketMessageResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { hasPermission, sendWebSocketMessage }: SendWebSocketMessageMethodHooks,\n): Promise<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 await 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"]}
|
|
@@ -3,7 +3,7 @@ import type { SendWebSocketMessageParams, SendWebSocketMessageResult } from "@me
|
|
|
3
3
|
import type { InferMatching } from "@metamask/snaps-utils";
|
|
4
4
|
export type SendWebSocketMessageMethodHooks = {
|
|
5
5
|
hasPermission: (permissionName: string) => boolean;
|
|
6
|
-
sendWebSocketMessage: (id: string, data: string | number[]) => void
|
|
6
|
+
sendWebSocketMessage: (id: string, data: string | number[]) => Promise<void>;
|
|
7
7
|
};
|
|
8
8
|
declare const SendWebSocketMessageParametersStruct: import("@metamask/superstruct").Struct<{
|
|
9
9
|
id: string;
|
|
@@ -1 +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;
|
|
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,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9E,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"}
|
|
@@ -3,7 +3,7 @@ import type { SendWebSocketMessageParams, SendWebSocketMessageResult } from "@me
|
|
|
3
3
|
import type { InferMatching } from "@metamask/snaps-utils";
|
|
4
4
|
export type SendWebSocketMessageMethodHooks = {
|
|
5
5
|
hasPermission: (permissionName: string) => boolean;
|
|
6
|
-
sendWebSocketMessage: (id: string, data: string | number[]) => void
|
|
6
|
+
sendWebSocketMessage: (id: string, data: string | number[]) => Promise<void>;
|
|
7
7
|
};
|
|
8
8
|
declare const SendWebSocketMessageParametersStruct: import("@metamask/superstruct").Struct<{
|
|
9
9
|
id: string;
|
|
@@ -1 +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;
|
|
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,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9E,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"}
|
|
@@ -30,14 +30,14 @@ export const sendWebSocketMessageHandler = {
|
|
|
30
30
|
* @param hooks.sendWebSocketMessage - The function to send a WebSocket message.
|
|
31
31
|
* @returns Nothing.
|
|
32
32
|
*/
|
|
33
|
-
function sendWebSocketMessageImplementation(req, res, _next, end, { hasPermission, sendWebSocketMessage }) {
|
|
33
|
+
async function sendWebSocketMessageImplementation(req, res, _next, end, { hasPermission, sendWebSocketMessage }) {
|
|
34
34
|
if (!hasPermission(SnapEndowments.NetworkAccess)) {
|
|
35
35
|
return end(providerErrors.unauthorized());
|
|
36
36
|
}
|
|
37
37
|
const { params } = req;
|
|
38
38
|
try {
|
|
39
39
|
const { id, message } = getValidatedParams(params);
|
|
40
|
-
sendWebSocketMessage(id, message);
|
|
40
|
+
await sendWebSocketMessage(id, message);
|
|
41
41
|
res.result = null;
|
|
42
42
|
}
|
|
43
43
|
catch (error) {
|
|
@@ -1 +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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AAoB,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,MAAM,oBAAoB,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;QACxC,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[]) => Promise<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 */\nasync function sendWebSocketMessageImplementation(\n req: JsonRpcRequest<SendWebSocketMessageParameters>,\n res: PendingJsonRpcResponse<SendWebSocketMessageResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { hasPermission, sendWebSocketMessage }: SendWebSocketMessageMethodHooks,\n): Promise<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 await 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"]}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.set = exports.setStateHandler = void 0;
|
|
4
4
|
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
5
|
+
const snaps_utils_1 = require("@metamask/snaps-utils");
|
|
5
6
|
const superstruct_1 = require("@metamask/superstruct");
|
|
6
7
|
const utils_1 = require("@metamask/utils");
|
|
7
8
|
const manageState_1 = require("../restricted/manageState.cjs");
|
|
@@ -11,6 +12,7 @@ const hookNames = {
|
|
|
11
12
|
getSnapState: true,
|
|
12
13
|
getUnlockPromise: true,
|
|
13
14
|
updateSnapState: true,
|
|
15
|
+
getSnap: true,
|
|
14
16
|
};
|
|
15
17
|
/**
|
|
16
18
|
* `snap_setState` sets the state of the Snap.
|
|
@@ -39,9 +41,10 @@ const SetStateParametersStruct = (0, superstruct_1.object)({
|
|
|
39
41
|
* @param hooks.getSnapState - Get the state of the requesting Snap.
|
|
40
42
|
* @param hooks.getUnlockPromise - Wait for the extension to be unlocked.
|
|
41
43
|
* @param hooks.updateSnapState - Update the state of the requesting Snap.
|
|
44
|
+
* @param hooks.getSnap - The hook function to get Snap metadata.
|
|
42
45
|
* @returns Nothing.
|
|
43
46
|
*/
|
|
44
|
-
async function setStateImplementation(request, response, _next, end, { hasPermission, getSnapState, getUnlockPromise, updateSnapState, }) {
|
|
47
|
+
async function setStateImplementation(request, response, _next, end, { hasPermission, getSnapState, getUnlockPromise, updateSnapState, getSnap, }) {
|
|
45
48
|
const { params } = request;
|
|
46
49
|
if (!hasPermission(manageState_1.manageStateBuilder.targetName)) {
|
|
47
50
|
return end(rpc_errors_1.providerErrors.unauthorized());
|
|
@@ -56,11 +59,15 @@ async function setStateImplementation(request, response, _next, end, { hasPermis
|
|
|
56
59
|
await getUnlockPromise(true);
|
|
57
60
|
}
|
|
58
61
|
const newState = await getNewState(key, value, encrypted, getSnapState);
|
|
59
|
-
const
|
|
60
|
-
if (
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
const snap = getSnap(request.origin);
|
|
63
|
+
if (!snap?.preinstalled) {
|
|
64
|
+
// We know that the state is valid JSON as per previous validation.
|
|
65
|
+
const size = (0, snaps_utils_1.getJsonSizeUnsafe)(newState, true);
|
|
66
|
+
if (size > manageState_1.STORAGE_SIZE_LIMIT) {
|
|
67
|
+
throw rpc_errors_1.rpcErrors.invalidParams({
|
|
68
|
+
message: `Invalid params: The new state must not exceed ${manageState_1.STORAGE_SIZE_LIMIT / 1000000} MB in size.`,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
64
71
|
}
|
|
65
72
|
await updateSnapState(newState, encrypted);
|
|
66
73
|
response.result = null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setState.cjs","sourceRoot":"","sources":["../../src/permitted/setState.ts"],"names":[],"mappings":";;;AAEA,qDAAiE;AAIjE,uDAM+B;AAM/B,2CAMyB;AAEzB,+DAGmC;AAEnC,wCAA0D;AAE1D,MAAM,SAAS,GAAqC;IAClD,aAAa,EAAE,IAAI;IACnB,YAAY,EAAE,IAAI;IAClB,gBAAgB,EAAE,IAAI;IACtB,eAAe,EAAE,IAAI;CACtB,CAAC;AAEF;;GAEG;AACU,QAAA,eAAe,GAIxB;IACF,WAAW,EAAE,CAAC,eAAe,CAAC;IAC9B,cAAc,EAAE,sBAAsB;IACtC,SAAS;CACV,CAAC;AAsCF,MA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AAG,CAAC,CAAC;IAC5B,MAAM,cAAc,GAAG,MAAM,IAAI,EAAE,CAAC;IACpC,IAAI,aAAa,GAAyB,cAAc,CAAC;IAEzD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,MAAM,UAAU,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;QAC3B,IAAI,sBAAc,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC;YACxC,MAAM,sBAAS,CAAC,aAAa,CAC3B,oDAAoD,CACrD,CAAC;QACJ,CAAC;QAED,IAAI,CAAC,KAAK,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC1B,aAAa,CAAC,UAAU,CAAC,GAAG,KAAK,CAAC;YAClC,OAAO,cAAc,CAAC;QACxB,CAAC;QAED,IACE,CAAC,IAAA,mBAAW,EAAC,aAAa,EAAE,UAAU,CAAC;YACvC,aAAa,CAAC,UAAU,CAAC,KAAK,IAAI,EAClC,CAAC;YACD,aAAa,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC;QACjC,CAAC;aAAM,IAAI,CAAC,IAAA,gBAAQ,EAAC,aAAa,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC;YAChD,MAAM,sBAAS,CAAC,aAAa,CAC3B,oDAAoD,CACrD,CAAC;QACJ,CAAC;QAED,aAAa,GAAG,aAAa,CAAC,UAAU,CAAyB,CAAC;IACpE,CAAC;IAED,gCAAgC;IAChC,0BAA0B;IAC1B,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;AAC/D,CAAC;AAvCD,kBAuCC","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 { SetStateParams, SetStateResult } from '@metamask/snaps-sdk';\nimport type { JsonObject } from '@metamask/snaps-sdk/jsx';\nimport { type InferMatching } from '@metamask/snaps-utils';\nimport {\n boolean,\n create,\n object as objectStruct,\n optional,\n StructError,\n} from '@metamask/superstruct';\nimport type {\n PendingJsonRpcResponse,\n Json,\n JsonRpcRequest,\n} from '@metamask/utils';\nimport {\n getJsonSize,\n hasProperty,\n isObject,\n assert,\n JsonStruct,\n} from '@metamask/utils';\n\nimport {\n manageStateBuilder,\n STORAGE_SIZE_LIMIT,\n} from '../restricted/manageState';\nimport type { MethodHooksObject } from '../utils';\nimport { FORBIDDEN_KEYS, StateKeyStruct } from '../utils';\n\nconst hookNames: MethodHooksObject<SetStateHooks> = {\n hasPermission: true,\n getSnapState: true,\n getUnlockPromise: true,\n updateSnapState: true,\n};\n\n/**\n * `snap_setState` sets the state of the Snap.\n */\nexport const setStateHandler: PermittedHandlerExport<\n SetStateHooks,\n SetStateParameters,\n SetStateResult\n> = {\n methodNames: ['snap_setState'],\n implementation: setStateImplementation,\n hookNames,\n};\n\nexport type SetStateHooks = {\n /**\n * Check if the requesting origin has a given permission.\n *\n * @param permissionName - The name of the permission to check.\n * @returns Whether the origin has the permission.\n */\n hasPermission: (permissionName: string) => boolean;\n\n /**\n * Get the state of the requesting Snap.\n *\n * @param encrypted - Whether the state is encrypted.\n * @returns The current state of the Snap.\n */\n getSnapState: (encrypted: boolean) => Promise<Record<string, Json>>;\n\n /**\n * Wait for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n /**\n * Update the state of the requesting Snap.\n *\n * @param newState - The new state of the Snap.\n * @param encrypted - Whether the state should be encrypted.\n */\n updateSnapState: (\n newState: Record<string, Json>,\n encrypted: boolean,\n ) => Promise<void>;\n};\n\nconst SetStateParametersStruct = objectStruct({\n key: optional(StateKeyStruct),\n value: JsonStruct,\n encrypted: optional(boolean()),\n});\n\nexport type SetStateParameters = InferMatching<\n typeof SetStateParametersStruct,\n SetStateParams\n>;\n\n/**\n * The `snap_setState` method implementation.\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.hasPermission - Check whether a given origin has a given\n * permission.\n * @param hooks.getSnapState - Get the state of the requesting Snap.\n * @param hooks.getUnlockPromise - Wait for the extension to be unlocked.\n * @param hooks.updateSnapState - Update the state of the requesting Snap.\n * @returns Nothing.\n */\nasync function setStateImplementation(\n request: JsonRpcRequest<SetStateParameters>,\n response: PendingJsonRpcResponse<SetStateResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n {\n hasPermission,\n getSnapState,\n getUnlockPromise,\n updateSnapState,\n }: SetStateHooks,\n): Promise<void> {\n const { params } = request;\n\n if (!hasPermission(manageStateBuilder.targetName)) {\n return end(providerErrors.unauthorized());\n }\n\n try {\n const validatedParams = getValidatedParams(params);\n const { key, value, encrypted = true } = validatedParams;\n\n if (key === undefined && !isObject(value)) {\n return end(\n rpcErrors.invalidParams(\n 'Invalid params: Value must be an object if key is not provided.',\n ),\n );\n }\n\n if (encrypted) {\n await getUnlockPromise(true);\n }\n\n const newState = await getNewState(key, value, encrypted, getSnapState);\n\n const size = getJsonSize(newState);\n if (size > STORAGE_SIZE_LIMIT) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: The new state must not exceed ${\n STORAGE_SIZE_LIMIT / 1_000_000\n } MB in size.`,\n });\n }\n\n await updateSnapState(newState, encrypted);\n response.result = null;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validate the parameters of the `snap_setState` method.\n *\n * @param params - The parameters to validate.\n * @returns The validated parameters.\n */\nfunction getValidatedParams(params?: unknown) {\n try {\n return create(params, SetStateParametersStruct);\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 * Get the new state of the Snap.\n *\n * If the key is `undefined`, the value is expected to be an object. In this\n * case, the value is returned as the new state.\n *\n * If the key is not `undefined`, the value is set in the state at the key. If\n * the key does not exist, it is created (and any missing intermediate keys are\n * created as well).\n *\n * @param key - The key to set.\n * @param value - The value to set the key to.\n * @param encrypted - Whether the state is encrypted.\n * @param getSnapState - The `getSnapState` hook.\n * @returns The new state of the Snap.\n */\nasync function getNewState(\n key: string | undefined,\n value: Json,\n encrypted: boolean,\n getSnapState: SetStateHooks['getSnapState'],\n) {\n if (key === undefined) {\n assert(isObject(value));\n return value;\n }\n\n const state = await getSnapState(encrypted);\n return set(state, key, value);\n}\n\n/**\n * Set the value of a key in an object. The key may contain Lodash-style path\n * syntax, e.g., `a.b.c` (with the exception of array syntax). If the key does\n * not exist, it is created (and any missing intermediate keys are created as\n * well).\n *\n * This is a simplified version of Lodash's `set` function, but Lodash doesn't\n * seem to be maintained anymore, so we're using our own implementation.\n *\n * @param object - The object to get the key from.\n * @param key - The key to set.\n * @param value - The value to set the key to.\n * @returns The new object with the key set to the value.\n */\nexport function set(\n object: Record<string, Json> | null,\n key: string,\n value: Json,\n): JsonObject {\n const keys = key.split('.');\n const requiredObject = object ?? {};\n let currentObject: Record<string, Json> = requiredObject;\n\n for (let i = 0; i < keys.length; i++) {\n const currentKey = keys[i];\n if (FORBIDDEN_KEYS.includes(currentKey)) {\n throw rpcErrors.invalidParams(\n 'Invalid params: Key contains forbidden characters.',\n );\n }\n\n if (i === keys.length - 1) {\n currentObject[currentKey] = value;\n return requiredObject;\n }\n\n if (\n !hasProperty(currentObject, currentKey) ||\n currentObject[currentKey] === null\n ) {\n currentObject[currentKey] = {};\n } else if (!isObject(currentObject[currentKey])) {\n throw rpcErrors.invalidParams(\n 'Invalid params: Cannot overwrite non-object value.',\n );\n }\n\n currentObject = currentObject[currentKey] as Record<string, Json>;\n }\n\n // This should never be reached.\n /* istanbul ignore next */\n throw new Error('Unexpected error while setting the state.');\n}\n"]}
|
|
1
|
+
{"version":3,"file":"setState.cjs","sourceRoot":"","sources":["../../src/permitted/setState.ts"],"names":[],"mappings":";;;AAEA,qDAAiE;AAGjE,uDAI+B;AAC/B,uDAM+B;AAM/B,2CAA4E;AAE5E,+DAGmC;AAEnC,wCAA0D;AAE1D,MAAM,SAAS,GAAqC;IAClD,aAAa,EAAE,IAAI;IACnB,YAAY,EAAE,IAAI;IAClB,gBAAgB,EAAE,IAAI;IACtB,eAAe,EAAE,IAAI;IACrB,OAAO,EAAE,IAAI;CACd,CAAC;AAEF;;GAEG;AACU,QAAA,eAAe,GAIxB;IACF,WAAW,EAAE,CAAC,eAAe,CAAC;IAC9B,cAAc,EAAE,sBAAsB;IACtC,SAAS;CACV,CAAC;AA6CF,MAAM,wBAAwB,GAAG,IAAA,oBAAY,EAAC;IAC5C,GAAG,EAAE,IAAA,sBAAQ,EAAC,sBAAc,CAAC;IAC7B,KAAK,EAAE,kBAAU;IACjB,SAAS,EAAE,IAAA,sBAAQ,EAAC,IAAA,qBAAO,GAAE,CAAC;CAC/B,CAAC,CAAC;AAOH;;;;;;;;;;;;;;;;GAgBG;AACH,KAAK,UAAU,sBAAsB,CACnC,OAA2C,EAC3C,QAAgD,EAChD,KAAc,EACd,GAA6B,EAC7B,EACE,aAAa,EACb,YAAY,EACZ,gBAAgB,EAChB,eAAe,EACf,OAAO,GACO;IAEhB,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAE3B,IAAI,CAAC,aAAa,CAAC,gCAAkB,CAAC,UAAU,CAAC,EAAE,CAAC;QAClD,OAAO,GAAG,CAAC,2BAAc,CAAC,YAAY,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED,IAAI,CAAC;QACH,MAAM,eAAe,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;QACnD,MAAM,EAAE,GAAG,EAAE,KAAK,EAAE,SAAS,GAAG,IAAI,EAAE,GAAG,eAAe,CAAC;QAEzD,IAAI,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AW,EAAC,aAAa,EAAE,UAAU,CAAC;YACvC,aAAa,CAAC,UAAU,CAAC,KAAK,IAAI,EAClC,CAAC;YACD,aAAa,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC;QACjC,CAAC;aAAM,IAAI,CAAC,IAAA,gBAAQ,EAAC,aAAa,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC;YAChD,MAAM,sBAAS,CAAC,aAAa,CAC3B,oDAAoD,CACrD,CAAC;QACJ,CAAC;QAED,aAAa,GAAG,aAAa,CAAC,UAAU,CAAyB,CAAC;IACpE,CAAC;IAED,gCAAgC;IAChC,0BAA0B;IAC1B,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;AAC/D,CAAC;AAvCD,kBAuCC","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 { SetStateParams, SetStateResult } from '@metamask/snaps-sdk';\nimport type { JsonObject } from '@metamask/snaps-sdk/jsx';\nimport {\n getJsonSizeUnsafe,\n type InferMatching,\n type Snap,\n} from '@metamask/snaps-utils';\nimport {\n boolean,\n create,\n object as objectStruct,\n optional,\n StructError,\n} from '@metamask/superstruct';\nimport type {\n PendingJsonRpcResponse,\n Json,\n JsonRpcRequest,\n} from '@metamask/utils';\nimport { hasProperty, isObject, assert, JsonStruct } from '@metamask/utils';\n\nimport {\n manageStateBuilder,\n STORAGE_SIZE_LIMIT,\n} from '../restricted/manageState';\nimport type { MethodHooksObject } from '../utils';\nimport { FORBIDDEN_KEYS, StateKeyStruct } from '../utils';\n\nconst hookNames: MethodHooksObject<SetStateHooks> = {\n hasPermission: true,\n getSnapState: true,\n getUnlockPromise: true,\n updateSnapState: true,\n getSnap: true,\n};\n\n/**\n * `snap_setState` sets the state of the Snap.\n */\nexport const setStateHandler: PermittedHandlerExport<\n SetStateHooks,\n SetStateParameters,\n SetStateResult\n> = {\n methodNames: ['snap_setState'],\n implementation: setStateImplementation,\n hookNames,\n};\n\nexport type SetStateHooks = {\n /**\n * Check if the requesting origin has a given permission.\n *\n * @param permissionName - The name of the permission to check.\n * @returns Whether the origin has the permission.\n */\n hasPermission: (permissionName: string) => boolean;\n\n /**\n * Get the state of the requesting Snap.\n *\n * @param encrypted - Whether the state is encrypted.\n * @returns The current state of the Snap.\n */\n getSnapState: (encrypted: boolean) => Promise<Record<string, Json>>;\n\n /**\n * Wait for the extension to be unlocked.\n *\n * @returns A promise that resolves once the extension is unlocked.\n */\n getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;\n\n /**\n * Update the state of the requesting Snap.\n *\n * @param newState - The new state of the Snap.\n * @param encrypted - Whether the state should be encrypted.\n */\n updateSnapState: (\n newState: Record<string, Json>,\n encrypted: boolean,\n ) => Promise<void>;\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 SetStateParametersStruct = objectStruct({\n key: optional(StateKeyStruct),\n value: JsonStruct,\n encrypted: optional(boolean()),\n});\n\nexport type SetStateParameters = InferMatching<\n typeof SetStateParametersStruct,\n SetStateParams\n>;\n\n/**\n * The `snap_setState` method implementation.\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.hasPermission - Check whether a given origin has a given\n * permission.\n * @param hooks.getSnapState - Get the state of the requesting Snap.\n * @param hooks.getUnlockPromise - Wait for the extension to be unlocked.\n * @param hooks.updateSnapState - Update the state of the requesting Snap.\n * @param hooks.getSnap - The hook function to get Snap metadata.\n * @returns Nothing.\n */\nasync function setStateImplementation(\n request: JsonRpcRequest<SetStateParameters>,\n response: PendingJsonRpcResponse<SetStateResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n {\n hasPermission,\n getSnapState,\n getUnlockPromise,\n updateSnapState,\n getSnap,\n }: SetStateHooks,\n): Promise<void> {\n const { params } = request;\n\n if (!hasPermission(manageStateBuilder.targetName)) {\n return end(providerErrors.unauthorized());\n }\n\n try {\n const validatedParams = getValidatedParams(params);\n const { key, value, encrypted = true } = validatedParams;\n\n if (key === undefined && !isObject(value)) {\n return end(\n rpcErrors.invalidParams(\n 'Invalid params: Value must be an object if key is not provided.',\n ),\n );\n }\n\n if (encrypted) {\n await getUnlockPromise(true);\n }\n\n const newState = await getNewState(key, value, encrypted, getSnapState);\n\n const snap = getSnap(\n (request as JsonRpcRequest<SetStateParams> & { origin: string }).origin,\n );\n\n if (!snap?.preinstalled) {\n // We know that the state is valid JSON as per previous validation.\n const size = getJsonSizeUnsafe(newState, true);\n if (size > STORAGE_SIZE_LIMIT) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: The new state must not exceed ${\n STORAGE_SIZE_LIMIT / 1_000_000\n } MB in size.`,\n });\n }\n }\n\n await updateSnapState(newState, encrypted);\n response.result = null;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validate the parameters of the `snap_setState` method.\n *\n * @param params - The parameters to validate.\n * @returns The validated parameters.\n */\nfunction getValidatedParams(params?: unknown) {\n try {\n return create(params, SetStateParametersStruct);\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 * Get the new state of the Snap.\n *\n * If the key is `undefined`, the value is expected to be an object. In this\n * case, the value is returned as the new state.\n *\n * If the key is not `undefined`, the value is set in the state at the key. If\n * the key does not exist, it is created (and any missing intermediate keys are\n * created as well).\n *\n * @param key - The key to set.\n * @param value - The value to set the key to.\n * @param encrypted - Whether the state is encrypted.\n * @param getSnapState - The `getSnapState` hook.\n * @returns The new state of the Snap.\n */\nasync function getNewState(\n key: string | undefined,\n value: Json,\n encrypted: boolean,\n getSnapState: SetStateHooks['getSnapState'],\n) {\n if (key === undefined) {\n assert(isObject(value));\n return value;\n }\n\n const state = await getSnapState(encrypted);\n return set(state, key, value);\n}\n\n/**\n * Set the value of a key in an object. The key may contain Lodash-style path\n * syntax, e.g., `a.b.c` (with the exception of array syntax). If the key does\n * not exist, it is created (and any missing intermediate keys are created as\n * well).\n *\n * This is a simplified version of Lodash's `set` function, but Lodash doesn't\n * seem to be maintained anymore, so we're using our own implementation.\n *\n * @param object - The object to get the key from.\n * @param key - The key to set.\n * @param value - The value to set the key to.\n * @returns The new object with the key set to the value.\n */\nexport function set(\n object: Record<string, Json> | null,\n key: string,\n value: Json,\n): JsonObject {\n const keys = key.split('.');\n const requiredObject = object ?? {};\n let currentObject: Record<string, Json> = requiredObject;\n\n for (let i = 0; i < keys.length; i++) {\n const currentKey = keys[i];\n if (FORBIDDEN_KEYS.includes(currentKey)) {\n throw rpcErrors.invalidParams(\n 'Invalid params: Key contains forbidden characters.',\n );\n }\n\n if (i === keys.length - 1) {\n currentObject[currentKey] = value;\n return requiredObject;\n }\n\n if (\n !hasProperty(currentObject, currentKey) ||\n currentObject[currentKey] === null\n ) {\n currentObject[currentKey] = {};\n } else if (!isObject(currentObject[currentKey])) {\n throw rpcErrors.invalidParams(\n 'Invalid params: Cannot overwrite non-object value.',\n );\n }\n\n currentObject = currentObject[currentKey] as Record<string, Json>;\n }\n\n // This should never be reached.\n /* istanbul ignore next */\n throw new Error('Unexpected error while setting the state.');\n}\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { PermittedHandlerExport } from "@metamask/permission-controller";
|
|
2
2
|
import type { SetStateParams, SetStateResult } from "@metamask/snaps-sdk";
|
|
3
3
|
import type { JsonObject } from "@metamask/snaps-sdk/jsx";
|
|
4
|
-
import { type InferMatching } from "@metamask/snaps-utils";
|
|
4
|
+
import { type InferMatching, type Snap } from "@metamask/snaps-utils";
|
|
5
5
|
import type { Json } from "@metamask/utils";
|
|
6
6
|
/**
|
|
7
7
|
* `snap_setState` sets the state of the Snap.
|
|
@@ -35,6 +35,12 @@ export type SetStateHooks = {
|
|
|
35
35
|
* @param encrypted - Whether the state should be encrypted.
|
|
36
36
|
*/
|
|
37
37
|
updateSnapState: (newState: Record<string, Json>, encrypted: boolean) => Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* Get Snap metadata.
|
|
40
|
+
*
|
|
41
|
+
* @param snapId - The ID of a Snap.
|
|
42
|
+
*/
|
|
43
|
+
getSnap: (snapId: string) => Snap | undefined;
|
|
38
44
|
};
|
|
39
45
|
declare const SetStateParametersStruct: import("@metamask/superstruct").Struct<{
|
|
40
46
|
value: Json;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setState.d.cts","sourceRoot":"","sources":["../../src/permitted/setState.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,4BAA4B;AAC1E,OAAO,KAAK,EAAE,UAAU,EAAE,gCAAgC;AAC1D,OAAO,
|
|
1
|
+
{"version":3,"file":"setState.d.cts","sourceRoot":"","sources":["../../src/permitted/setState.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,4BAA4B;AAC1E,OAAO,KAAK,EAAE,UAAU,EAAE,gCAAgC;AAC1D,OAAO,EAEL,KAAK,aAAa,EAClB,KAAK,IAAI,EACV,8BAA8B;AAQ/B,OAAO,KAAK,EAEV,IAAI,EAEL,wBAAwB;AAkBzB;;GAEG;AACH,eAAO,MAAM,eAAe,EAAE,sBAAsB,CAClD,aAAa,EACb,kBAAkB,EAClB,cAAc,CAKf,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B;;;;;OAKG;IACH,aAAa,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,OAAO,CAAC;IAEnD;;;;;OAKG;IACH,YAAY,EAAE,CAAC,SAAS,EAAE,OAAO,KAAK,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;IAEpE;;;;OAIG;IACH,gBAAgB,EAAE,CAAC,uBAAuB,EAAE,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAEtE;;;;;OAKG;IACH,eAAe,EAAE,CACf,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,EAC9B,SAAS,EAAE,OAAO,KACf,OAAO,CAAC,IAAI,CAAC,CAAC;IAEnB;;;;OAIG;IACH,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,GAAG,SAAS,CAAC;CAC/C,CAAC;AAEF,QAAA,MAAM,wBAAwB;;;;;;;;EAI5B,CAAC;AAEH,MAAM,MAAM,kBAAkB,GAAG,aAAa,CAC5C,OAAO,wBAAwB,EAC/B,cAAc,CACf,CAAC;AAqIF;;;;;;;;;;;;;GAaG;AACH,wBAAgB,GAAG,CACjB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,IAAI,EACnC,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,IAAI,GACV,UAAU,CAmCZ"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { PermittedHandlerExport } from "@metamask/permission-controller";
|
|
2
2
|
import type { SetStateParams, SetStateResult } from "@metamask/snaps-sdk";
|
|
3
3
|
import type { JsonObject } from "@metamask/snaps-sdk/jsx";
|
|
4
|
-
import { type InferMatching } from "@metamask/snaps-utils";
|
|
4
|
+
import { type InferMatching, type Snap } from "@metamask/snaps-utils";
|
|
5
5
|
import type { Json } from "@metamask/utils";
|
|
6
6
|
/**
|
|
7
7
|
* `snap_setState` sets the state of the Snap.
|
|
@@ -35,6 +35,12 @@ export type SetStateHooks = {
|
|
|
35
35
|
* @param encrypted - Whether the state should be encrypted.
|
|
36
36
|
*/
|
|
37
37
|
updateSnapState: (newState: Record<string, Json>, encrypted: boolean) => Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* Get Snap metadata.
|
|
40
|
+
*
|
|
41
|
+
* @param snapId - The ID of a Snap.
|
|
42
|
+
*/
|
|
43
|
+
getSnap: (snapId: string) => Snap | undefined;
|
|
38
44
|
};
|
|
39
45
|
declare const SetStateParametersStruct: import("@metamask/superstruct").Struct<{
|
|
40
46
|
value: Json;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setState.d.mts","sourceRoot":"","sources":["../../src/permitted/setState.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,4BAA4B;AAC1E,OAAO,KAAK,EAAE,UAAU,EAAE,gCAAgC;AAC1D,OAAO,
|
|
1
|
+
{"version":3,"file":"setState.d.mts","sourceRoot":"","sources":["../../src/permitted/setState.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,4BAA4B;AAC1E,OAAO,KAAK,EAAE,UAAU,EAAE,gCAAgC;AAC1D,OAAO,EAEL,KAAK,aAAa,EAClB,KAAK,IAAI,EACV,8BAA8B;AAQ/B,OAAO,KAAK,EAEV,IAAI,EAEL,wBAAwB;AAkBzB;;GAEG;AACH,eAAO,MAAM,eAAe,EAAE,sBAAsB,CAClD,aAAa,EACb,kBAAkB,EAClB,cAAc,CAKf,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B;;;;;OAKG;IACH,aAAa,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,OAAO,CAAC;IAEnD;;;;;OAKG;IACH,YAAY,EAAE,CAAC,SAAS,EAAE,OAAO,KAAK,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;IAEpE;;;;OAIG;IACH,gBAAgB,EAAE,CAAC,uBAAuB,EAAE,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAEtE;;;;;OAKG;IACH,eAAe,EAAE,CACf,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,EAC9B,SAAS,EAAE,OAAO,KACf,OAAO,CAAC,IAAI,CAAC,CAAC;IAEnB;;;;OAIG;IACH,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,GAAG,SAAS,CAAC;CAC/C,CAAC;AAEF,QAAA,MAAM,wBAAwB;;;;;;;;EAI5B,CAAC;AAEH,MAAM,MAAM,kBAAkB,GAAG,aAAa,CAC5C,OAAO,wBAAwB,EAC/B,cAAc,CACf,CAAC;AAqIF;;;;;;;;;;;;;GAaG;AACH,wBAAgB,GAAG,CACjB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,IAAI,EACnC,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,IAAI,GACV,UAAU,CAmCZ"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { providerErrors, rpcErrors } from "@metamask/rpc-errors";
|
|
2
|
+
import { getJsonSizeUnsafe } from "@metamask/snaps-utils";
|
|
2
3
|
import { boolean, create, object as objectStruct, optional, StructError } from "@metamask/superstruct";
|
|
3
|
-
import {
|
|
4
|
+
import { hasProperty, isObject, assert, JsonStruct } from "@metamask/utils";
|
|
4
5
|
import { manageStateBuilder, STORAGE_SIZE_LIMIT } from "../restricted/manageState.mjs";
|
|
5
6
|
import { FORBIDDEN_KEYS, StateKeyStruct } from "../utils.mjs";
|
|
6
7
|
const hookNames = {
|
|
@@ -8,6 +9,7 @@ const hookNames = {
|
|
|
8
9
|
getSnapState: true,
|
|
9
10
|
getUnlockPromise: true,
|
|
10
11
|
updateSnapState: true,
|
|
12
|
+
getSnap: true,
|
|
11
13
|
};
|
|
12
14
|
/**
|
|
13
15
|
* `snap_setState` sets the state of the Snap.
|
|
@@ -36,9 +38,10 @@ const SetStateParametersStruct = objectStruct({
|
|
|
36
38
|
* @param hooks.getSnapState - Get the state of the requesting Snap.
|
|
37
39
|
* @param hooks.getUnlockPromise - Wait for the extension to be unlocked.
|
|
38
40
|
* @param hooks.updateSnapState - Update the state of the requesting Snap.
|
|
41
|
+
* @param hooks.getSnap - The hook function to get Snap metadata.
|
|
39
42
|
* @returns Nothing.
|
|
40
43
|
*/
|
|
41
|
-
async function setStateImplementation(request, response, _next, end, { hasPermission, getSnapState, getUnlockPromise, updateSnapState, }) {
|
|
44
|
+
async function setStateImplementation(request, response, _next, end, { hasPermission, getSnapState, getUnlockPromise, updateSnapState, getSnap, }) {
|
|
42
45
|
const { params } = request;
|
|
43
46
|
if (!hasPermission(manageStateBuilder.targetName)) {
|
|
44
47
|
return end(providerErrors.unauthorized());
|
|
@@ -53,11 +56,15 @@ async function setStateImplementation(request, response, _next, end, { hasPermis
|
|
|
53
56
|
await getUnlockPromise(true);
|
|
54
57
|
}
|
|
55
58
|
const newState = await getNewState(key, value, encrypted, getSnapState);
|
|
56
|
-
const
|
|
57
|
-
if (
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
const snap = getSnap(request.origin);
|
|
60
|
+
if (!snap?.preinstalled) {
|
|
61
|
+
// We know that the state is valid JSON as per previous validation.
|
|
62
|
+
const size = getJsonSizeUnsafe(newState, true);
|
|
63
|
+
if (size > STORAGE_SIZE_LIMIT) {
|
|
64
|
+
throw rpcErrors.invalidParams({
|
|
65
|
+
message: `Invalid params: The new state must not exceed ${STORAGE_SIZE_LIMIT / 1000000} MB in size.`,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
61
68
|
}
|
|
62
69
|
await updateSnapState(newState, encrypted);
|
|
63
70
|
response.result = null;
|