@metamask/snaps-rpc-methods 11.8.0 → 11.9.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 +14 -1
- package/dist/endowments/cronjob.cjs +8 -0
- package/dist/endowments/cronjob.cjs.map +1 -1
- package/dist/endowments/cronjob.d.cts.map +1 -1
- package/dist/endowments/cronjob.d.mts.map +1 -1
- package/dist/endowments/cronjob.mjs +9 -1
- package/dist/endowments/cronjob.mjs.map +1 -1
- package/dist/permitted/cancelBackgroundEvent.cjs +69 -0
- package/dist/permitted/cancelBackgroundEvent.cjs.map +1 -0
- package/dist/permitted/cancelBackgroundEvent.d.cts +16 -0
- package/dist/permitted/cancelBackgroundEvent.d.cts.map +1 -0
- package/dist/permitted/cancelBackgroundEvent.d.mts +16 -0
- package/dist/permitted/cancelBackgroundEvent.d.mts.map +1 -0
- package/dist/permitted/cancelBackgroundEvent.mjs +66 -0
- package/dist/permitted/cancelBackgroundEvent.mjs.map +1 -0
- package/dist/permitted/clearState.cjs +73 -0
- package/dist/permitted/clearState.cjs.map +1 -0
- package/dist/permitted/clearState.d.cts +28 -0
- package/dist/permitted/clearState.d.cts.map +1 -0
- package/dist/permitted/clearState.d.mts +28 -0
- package/dist/permitted/clearState.d.mts.map +1 -0
- package/dist/permitted/clearState.mjs +70 -0
- package/dist/permitted/clearState.mjs.map +1 -0
- package/dist/permitted/getBackgroundEvents.cjs +42 -0
- package/dist/permitted/getBackgroundEvents.cjs.map +1 -0
- package/dist/permitted/getBackgroundEvents.d.cts +8 -0
- package/dist/permitted/getBackgroundEvents.d.cts.map +1 -0
- package/dist/permitted/getBackgroundEvents.d.mts +8 -0
- package/dist/permitted/getBackgroundEvents.d.mts.map +1 -0
- package/dist/permitted/getBackgroundEvents.mjs +39 -0
- package/dist/permitted/getBackgroundEvents.mjs.map +1 -0
- package/dist/permitted/getState.cjs +118 -0
- package/dist/permitted/getState.cjs.map +1 -0
- package/dist/permitted/getState.d.cts +53 -0
- package/dist/permitted/getState.d.cts.map +1 -0
- package/dist/permitted/getState.d.mts +53 -0
- package/dist/permitted/getState.d.mts.map +1 -0
- package/dist/permitted/getState.mjs +114 -0
- package/dist/permitted/getState.mjs.map +1 -0
- package/dist/permitted/handlers.cjs +12 -0
- package/dist/permitted/handlers.cjs.map +1 -1
- package/dist/permitted/handlers.d.cts +8 -2
- package/dist/permitted/handlers.d.cts.map +1 -1
- package/dist/permitted/handlers.d.mts +8 -2
- package/dist/permitted/handlers.d.mts.map +1 -1
- package/dist/permitted/handlers.mjs +12 -0
- package/dist/permitted/handlers.mjs.map +1 -1
- package/dist/permitted/index.cjs.map +1 -1
- package/dist/permitted/index.d.cts +7 -1
- package/dist/permitted/index.d.cts.map +1 -1
- package/dist/permitted/index.d.mts +7 -1
- package/dist/permitted/index.d.mts.map +1 -1
- package/dist/permitted/index.mjs.map +1 -1
- package/dist/permitted/scheduleBackgroundEvent.cjs +122 -0
- package/dist/permitted/scheduleBackgroundEvent.cjs.map +1 -0
- package/dist/permitted/scheduleBackgroundEvent.d.cts +33 -0
- package/dist/permitted/scheduleBackgroundEvent.d.cts.map +1 -0
- package/dist/permitted/scheduleBackgroundEvent.d.mts +33 -0
- package/dist/permitted/scheduleBackgroundEvent.d.mts.map +1 -0
- package/dist/permitted/scheduleBackgroundEvent.mjs +119 -0
- package/dist/permitted/scheduleBackgroundEvent.mjs.map +1 -0
- package/dist/permitted/setState.cjs +154 -0
- package/dist/permitted/setState.cjs.map +1 -0
- package/dist/permitted/setState.d.cts +65 -0
- package/dist/permitted/setState.d.cts.map +1 -0
- package/dist/permitted/setState.d.mts +65 -0
- package/dist/permitted/setState.d.mts.map +1 -0
- package/dist/permitted/setState.mjs +150 -0
- package/dist/permitted/setState.mjs.map +1 -0
- package/dist/utils.cjs +22 -1
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +9 -0
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +9 -0
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +20 -0
- package/dist/utils.mjs.map +1 -1
- package/package.json +11 -9
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [11.9.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Add support for non-recurring cronjobs via `snap_scheduleBackgroundEvent` ([#2941](https://github.com/MetaMask/snaps/pull/2941), [#2975](https://github.com/MetaMask/snaps/pull/2975))
|
|
15
|
+
- Add `snap_getState`, `snap_setState`, `snap_clearState` RPC methods ([#2916](https://github.com/MetaMask/snaps/pull/2916))
|
|
16
|
+
- Add `hideBalances` to `snap_getPreferences` ([#2978](https://github.com/MetaMask/snaps/pull/2978))
|
|
17
|
+
|
|
18
|
+
### Changed
|
|
19
|
+
|
|
20
|
+
- Bump MetaMask dependencies ([#2946](https://github.com/MetaMask/snaps/pull/2946), [#2999](https://github.com/MetaMask/snaps/pull/2999), [#2989](https://github.com/MetaMask/snaps/pull/2989))
|
|
21
|
+
|
|
10
22
|
## [11.8.0]
|
|
11
23
|
|
|
12
24
|
### Added
|
|
@@ -349,7 +361,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
349
361
|
- The version of the package no longer needs to match the version of all other
|
|
350
362
|
MetaMask Snaps packages.
|
|
351
363
|
|
|
352
|
-
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@11.
|
|
364
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@11.9.0...HEAD
|
|
365
|
+
[11.9.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@11.8.0...@metamask/snaps-rpc-methods@11.9.0
|
|
353
366
|
[11.8.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@11.7.0...@metamask/snaps-rpc-methods@11.8.0
|
|
354
367
|
[11.7.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@11.6.0...@metamask/snaps-rpc-methods@11.7.0
|
|
355
368
|
[11.6.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@11.5.1...@metamask/snaps-rpc-methods@11.6.0
|
|
@@ -5,6 +5,7 @@ const permission_controller_1 = require("@metamask/permission-controller");
|
|
|
5
5
|
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
6
6
|
const snaps_utils_1 = require("@metamask/snaps-utils");
|
|
7
7
|
const utils_1 = require("@metamask/utils");
|
|
8
|
+
const caveats_1 = require("./caveats/index.cjs");
|
|
8
9
|
const enum_1 = require("./enum.cjs");
|
|
9
10
|
const permissionName = enum_1.SnapEndowments.Cronjob;
|
|
10
11
|
/**
|
|
@@ -20,6 +21,10 @@ const specificationBuilder = (_builderOptions) => {
|
|
|
20
21
|
allowedCaveats: [snaps_utils_1.SnapCaveatType.SnapCronjob],
|
|
21
22
|
endowmentGetter: (_getterOptions) => null,
|
|
22
23
|
subjectTypes: [permission_controller_1.SubjectType.Snap],
|
|
24
|
+
validator: (0, caveats_1.createGenericPermissionValidator)([
|
|
25
|
+
{ type: snaps_utils_1.SnapCaveatType.SnapCronjob, optional: true },
|
|
26
|
+
{ type: snaps_utils_1.SnapCaveatType.MaxRequestTime, optional: true },
|
|
27
|
+
]),
|
|
23
28
|
};
|
|
24
29
|
};
|
|
25
30
|
exports.cronjobEndowmentBuilder = Object.freeze({
|
|
@@ -35,6 +40,9 @@ exports.cronjobEndowmentBuilder = Object.freeze({
|
|
|
35
40
|
* @returns The caveat specification.
|
|
36
41
|
*/
|
|
37
42
|
function getCronjobCaveatMapper(value) {
|
|
43
|
+
if (!value || !(0, utils_1.isObject)(value) || Object.keys(value).length === 0) {
|
|
44
|
+
return { caveats: null };
|
|
45
|
+
}
|
|
38
46
|
return {
|
|
39
47
|
caveats: [
|
|
40
48
|
{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cronjob.cjs","sourceRoot":"","sources":["../../src/endowments/cronjob.ts"],"names":[],"mappings":";;;AAQA,2EAA8E;AAC9E,qDAAiD;AAEjD,uDAG+B;AAE/B,
|
|
1
|
+
{"version":3,"file":"cronjob.cjs","sourceRoot":"","sources":["../../src/endowments/cronjob.ts"],"names":[],"mappings":";;;AAQA,2EAA8E;AAC9E,qDAAiD;AAEjD,uDAG+B;AAE/B,2CAA+E;AAE/E,iDAA6D;AAC7D,qCAAwC;AAExC,MAAM,cAAc,GAAG,qBAAc,CAAC,OAAO,CAAC;AAS9C;;;;;GAKG;AACH,MAAM,oBAAoB,GAItB,CAAC,eAAqB,EAAE,EAAE;IAC5B,OAAO;QACL,cAAc,EAAE,sCAAc,CAAC,SAAS;QACxC,UAAU,EAAE,cAAc;QAC1B,cAAc,EAAE,CAAC,4BAAc,CAAC,WAAW,CAAC;QAC5C,eAAe,EAAE,CAAC,cAAsC,EAAE,EAAE,CAAC,IAAI;QACjE,YAAY,EAAE,CAAC,mCAAW,CAAC,IAAI,CAAC;QAChC,SAAS,EAAE,IAAA,0CAAgC,EAAC;YAC1C,EAAE,IAAI,EAAE,4BAAc,CAAC,WAAW,EAAE,QAAQ,EAAE,IAAI,EAAE;YACpD,EAAE,IAAI,EAAE,4BAAc,CAAC,cAAc,EAAE,QAAQ,EAAE,IAAI,EAAE;SACxD,CAAC;KACH,CAAC;AACJ,CAAC,CAAC;AAEW,QAAA,uBAAuB,GAAG,MAAM,CAAC,MAAM,CAAC;IACnD,UAAU,EAAE,cAAc;IAC1B,oBAAoB;CACZ,CAAC,CAAC;AAEZ;;;;;;;GAOG;AACH,SAAgB,sBAAsB,CACpC,KAAW;IAEX,IAAI,CAAC,KAAK,IAAI,CAAC,IAAA,gBAAQ,EAAC,KAAK,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,KAAK,CAAC,EAA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IAAA,yCAA2B,EAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;QAC7C,MAAM,sBAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EAAE,+CAA+C;SACzD,CAAC,CAAC;IACL,CAAC;AACH,CAAC;AApBD,sDAoBC;AAED;;GAEG;AACU,QAAA,2BAA2B,GAGpC;IACF,CAAC,4BAAc,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QAC1C,IAAI,EAAE,4BAAc,CAAC,WAAW;QAChC,SAAS,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,qBAAqB,CAAC,MAAM,CAAC;KACrD,CAAC;CACH,CAAC","sourcesContent":["import type {\n PermissionSpecificationBuilder,\n EndowmentGetterParams,\n ValidPermissionSpecification,\n PermissionConstraint,\n Caveat,\n CaveatSpecificationConstraint,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { CronjobSpecification } from '@metamask/snaps-utils';\nimport {\n SnapCaveatType,\n isCronjobSpecificationArray,\n} from '@metamask/snaps-utils';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\nimport { assert, hasProperty, isObject, isPlainObject } from '@metamask/utils';\n\nimport { createGenericPermissionValidator } from './caveats';\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.Cronjob;\n\ntype CronjobEndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetName: typeof permissionName;\n endowmentGetter: (_options?: any) => null;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * `endowment:cronjob` returns nothing; it is intended to be used as a flag to determine whether the snap wants to run cronjobs.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the cronjob endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n any,\n CronjobEndowmentSpecification\n> = (_builderOptions?: any) => {\n return {\n permissionType: PermissionType.Endowment,\n targetName: permissionName,\n allowedCaveats: [SnapCaveatType.SnapCronjob],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => null,\n subjectTypes: [SubjectType.Snap],\n validator: createGenericPermissionValidator([\n { type: SnapCaveatType.SnapCronjob, optional: true },\n { type: SnapCaveatType.MaxRequestTime, optional: true },\n ]),\n };\n};\n\nexport const cronjobEndowmentBuilder = Object.freeze({\n targetName: permissionName,\n specificationBuilder,\n} as const);\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function getCronjobCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n if (!value || !isObject(value) || Object.keys(value).length === 0) {\n return { caveats: null };\n }\n\n return {\n caveats: [\n {\n type: SnapCaveatType.SnapCronjob,\n value,\n },\n ],\n };\n}\n\n/**\n * Getter function to get the cronjobs from a permission.\n *\n * This does basic validation of the caveat, but does not validate the type or\n * value of the namespaces object itself, as this is handled by the\n * `PermissionsController` when the permission is requested.\n *\n * @param permission - The permission to get the keyring namespaces from.\n * @returns The cronjobs, or `null` if the permission does not have a\n * cronjob caveat.\n */\nexport function getCronjobCaveatJobs(\n permission?: PermissionConstraint,\n): CronjobSpecification[] | null {\n if (!permission?.caveats) {\n return null;\n }\n\n assert(permission.caveats.length === 1);\n assert(permission.caveats[0].type === SnapCaveatType.SnapCronjob);\n\n const caveat = permission.caveats[0] as Caveat<string, { jobs: Json[] }>;\n\n return (caveat.value?.jobs as CronjobSpecification[]) ?? null;\n}\n\n/**\n * Validate the cronjob specification values associated with a caveat.\n * This validates that the value is a non-empty array with valid\n * cronjob expression and request object.\n *\n * @param caveat - The caveat to validate.\n * @throws If the value is invalid.\n */\nexport function validateCronjobCaveat(caveat: Caveat<string, any>) {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n throw rpcErrors.invalidParams({\n message: 'Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n\n if (!hasProperty(value, 'jobs') || !isPlainObject(value)) {\n throw rpcErrors.invalidParams({\n message: 'Expected a plain object.',\n });\n }\n\n if (!isCronjobSpecificationArray(value.jobs)) {\n throw rpcErrors.invalidParams({\n message: 'Expected a valid cronjob specification array.',\n });\n }\n}\n\n/**\n * Caveat specification for the Cronjob.\n */\nexport const cronjobCaveatSpecifications: Record<\n SnapCaveatType.SnapCronjob,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.SnapCronjob]: Object.freeze({\n type: SnapCaveatType.SnapCronjob,\n validator: (caveat) => validateCronjobCaveat(caveat),\n }),\n};\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cronjob.d.cts","sourceRoot":"","sources":["../../src/endowments/cronjob.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,8BAA8B,EAG9B,oBAAoB,EACpB,MAAM,EACN,6BAA6B,EAC9B,wCAAwC;AACzC,OAAO,EAAE,cAAc,EAAe,wCAAwC;AAE9E,OAAO,KAAK,EAAE,oBAAoB,EAAE,8BAA8B;AAClE,OAAO,EACL,cAAc,EAEf,8BAA8B;AAC/B,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,wBAAwB;
|
|
1
|
+
{"version":3,"file":"cronjob.d.cts","sourceRoot":"","sources":["../../src/endowments/cronjob.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,8BAA8B,EAG9B,oBAAoB,EACpB,MAAM,EACN,6BAA6B,EAC9B,wCAAwC;AACzC,OAAO,EAAE,cAAc,EAAe,wCAAwC;AAE9E,OAAO,KAAK,EAAE,oBAAoB,EAAE,8BAA8B;AAClE,OAAO,EACL,cAAc,EAEf,8BAA8B;AAC/B,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,wBAAwB;AAI3D,OAAO,EAAE,cAAc,EAAE,mBAAe;AAExC,QAAA,MAAM,cAAc,yBAAyB,CAAC;AAiC9C,eAAO,MAAM,uBAAuB;;;wBA9BlB,eAAe,SAAS;oBAC5B,qBAAqB;qCACJ,GAAG,KAAK,IAAI;wBACzB,SAAS,cAAc,MAAM,CAAC,CAAC,GAAG,IAAI;;EA8B7C,CAAC;AAEZ;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACpC,KAAK,EAAE,IAAI,GACV,IAAI,CAAC,oBAAoB,EAAE,SAAS,CAAC,CAavC;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,oBAAoB,CAClC,UAAU,CAAC,EAAE,oBAAoB,GAChC,oBAAoB,EAAE,GAAG,IAAI,CAW/B;AAED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QAoBhE;AAED;;GAEG;AACH,eAAO,MAAM,2BAA2B,EAAE,MAAM,CAC9C,cAAc,CAAC,WAAW,EAC1B,6BAA6B,CAM9B,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cronjob.d.mts","sourceRoot":"","sources":["../../src/endowments/cronjob.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,8BAA8B,EAG9B,oBAAoB,EACpB,MAAM,EACN,6BAA6B,EAC9B,wCAAwC;AACzC,OAAO,EAAE,cAAc,EAAe,wCAAwC;AAE9E,OAAO,KAAK,EAAE,oBAAoB,EAAE,8BAA8B;AAClE,OAAO,EACL,cAAc,EAEf,8BAA8B;AAC/B,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,wBAAwB;
|
|
1
|
+
{"version":3,"file":"cronjob.d.mts","sourceRoot":"","sources":["../../src/endowments/cronjob.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,8BAA8B,EAG9B,oBAAoB,EACpB,MAAM,EACN,6BAA6B,EAC9B,wCAAwC;AACzC,OAAO,EAAE,cAAc,EAAe,wCAAwC;AAE9E,OAAO,KAAK,EAAE,oBAAoB,EAAE,8BAA8B;AAClE,OAAO,EACL,cAAc,EAEf,8BAA8B;AAC/B,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,wBAAwB;AAI3D,OAAO,EAAE,cAAc,EAAE,mBAAe;AAExC,QAAA,MAAM,cAAc,yBAAyB,CAAC;AAiC9C,eAAO,MAAM,uBAAuB;;;wBA9BlB,eAAe,SAAS;oBAC5B,qBAAqB;qCACJ,GAAG,KAAK,IAAI;wBACzB,SAAS,cAAc,MAAM,CAAC,CAAC,GAAG,IAAI;;EA8B7C,CAAC;AAEZ;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACpC,KAAK,EAAE,IAAI,GACV,IAAI,CAAC,oBAAoB,EAAE,SAAS,CAAC,CAavC;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,oBAAoB,CAClC,UAAU,CAAC,EAAE,oBAAoB,GAChC,oBAAoB,EAAE,GAAG,IAAI,CAW/B;AAED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QAoBhE;AAED;;GAEG;AACH,eAAO,MAAM,2BAA2B,EAAE,MAAM,CAC9C,cAAc,CAAC,WAAW,EAC1B,6BAA6B,CAM9B,CAAC"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { PermissionType, SubjectType } from "@metamask/permission-controller";
|
|
2
2
|
import { rpcErrors } from "@metamask/rpc-errors";
|
|
3
3
|
import { SnapCaveatType, isCronjobSpecificationArray } from "@metamask/snaps-utils";
|
|
4
|
-
import { assert, hasProperty, isPlainObject } from "@metamask/utils";
|
|
4
|
+
import { assert, hasProperty, isObject, isPlainObject } from "@metamask/utils";
|
|
5
|
+
import { createGenericPermissionValidator } from "./caveats/index.mjs";
|
|
5
6
|
import { SnapEndowments } from "./enum.mjs";
|
|
6
7
|
const permissionName = SnapEndowments.Cronjob;
|
|
7
8
|
/**
|
|
@@ -17,6 +18,10 @@ const specificationBuilder = (_builderOptions) => {
|
|
|
17
18
|
allowedCaveats: [SnapCaveatType.SnapCronjob],
|
|
18
19
|
endowmentGetter: (_getterOptions) => null,
|
|
19
20
|
subjectTypes: [SubjectType.Snap],
|
|
21
|
+
validator: createGenericPermissionValidator([
|
|
22
|
+
{ type: SnapCaveatType.SnapCronjob, optional: true },
|
|
23
|
+
{ type: SnapCaveatType.MaxRequestTime, optional: true },
|
|
24
|
+
]),
|
|
20
25
|
};
|
|
21
26
|
};
|
|
22
27
|
export const cronjobEndowmentBuilder = Object.freeze({
|
|
@@ -32,6 +37,9 @@ export const cronjobEndowmentBuilder = Object.freeze({
|
|
|
32
37
|
* @returns The caveat specification.
|
|
33
38
|
*/
|
|
34
39
|
export function getCronjobCaveatMapper(value) {
|
|
40
|
+
if (!value || !isObject(value) || Object.keys(value).length === 0) {
|
|
41
|
+
return { caveats: null };
|
|
42
|
+
}
|
|
35
43
|
return {
|
|
36
44
|
caveats: [
|
|
37
45
|
{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cronjob.mjs","sourceRoot":"","sources":["../../src/endowments/cronjob.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,wCAAwC;AAC9E,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAEjD,OAAO,EACL,cAAc,EACd,2BAA2B,EAC5B,8BAA8B;AAE/B,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,aAAa,EAAE,wBAAwB;
|
|
1
|
+
{"version":3,"file":"cronjob.mjs","sourceRoot":"","sources":["../../src/endowments/cronjob.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,wCAAwC;AAC9E,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAEjD,OAAO,EACL,cAAc,EACd,2BAA2B,EAC5B,8BAA8B;AAE/B,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,aAAa,EAAE,wBAAwB;AAE/E,OAAO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AC,qBAAqB,CAAC,MAAM,CAAC;KACrD,CAAC;CACH,CAAC","sourcesContent":["import type {\n PermissionSpecificationBuilder,\n EndowmentGetterParams,\n ValidPermissionSpecification,\n PermissionConstraint,\n Caveat,\n CaveatSpecificationConstraint,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { CronjobSpecification } from '@metamask/snaps-utils';\nimport {\n SnapCaveatType,\n isCronjobSpecificationArray,\n} from '@metamask/snaps-utils';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\nimport { assert, hasProperty, isObject, isPlainObject } from '@metamask/utils';\n\nimport { createGenericPermissionValidator } from './caveats';\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.Cronjob;\n\ntype CronjobEndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetName: typeof permissionName;\n endowmentGetter: (_options?: any) => null;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * `endowment:cronjob` returns nothing; it is intended to be used as a flag to determine whether the snap wants to run cronjobs.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the cronjob endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n any,\n CronjobEndowmentSpecification\n> = (_builderOptions?: any) => {\n return {\n permissionType: PermissionType.Endowment,\n targetName: permissionName,\n allowedCaveats: [SnapCaveatType.SnapCronjob],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => null,\n subjectTypes: [SubjectType.Snap],\n validator: createGenericPermissionValidator([\n { type: SnapCaveatType.SnapCronjob, optional: true },\n { type: SnapCaveatType.MaxRequestTime, optional: true },\n ]),\n };\n};\n\nexport const cronjobEndowmentBuilder = Object.freeze({\n targetName: permissionName,\n specificationBuilder,\n} as const);\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function getCronjobCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n if (!value || !isObject(value) || Object.keys(value).length === 0) {\n return { caveats: null };\n }\n\n return {\n caveats: [\n {\n type: SnapCaveatType.SnapCronjob,\n value,\n },\n ],\n };\n}\n\n/**\n * Getter function to get the cronjobs from a permission.\n *\n * This does basic validation of the caveat, but does not validate the type or\n * value of the namespaces object itself, as this is handled by the\n * `PermissionsController` when the permission is requested.\n *\n * @param permission - The permission to get the keyring namespaces from.\n * @returns The cronjobs, or `null` if the permission does not have a\n * cronjob caveat.\n */\nexport function getCronjobCaveatJobs(\n permission?: PermissionConstraint,\n): CronjobSpecification[] | null {\n if (!permission?.caveats) {\n return null;\n }\n\n assert(permission.caveats.length === 1);\n assert(permission.caveats[0].type === SnapCaveatType.SnapCronjob);\n\n const caveat = permission.caveats[0] as Caveat<string, { jobs: Json[] }>;\n\n return (caveat.value?.jobs as CronjobSpecification[]) ?? null;\n}\n\n/**\n * Validate the cronjob specification values associated with a caveat.\n * This validates that the value is a non-empty array with valid\n * cronjob expression and request object.\n *\n * @param caveat - The caveat to validate.\n * @throws If the value is invalid.\n */\nexport function validateCronjobCaveat(caveat: Caveat<string, any>) {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n throw rpcErrors.invalidParams({\n message: 'Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n\n if (!hasProperty(value, 'jobs') || !isPlainObject(value)) {\n throw rpcErrors.invalidParams({\n message: 'Expected a plain object.',\n });\n }\n\n if (!isCronjobSpecificationArray(value.jobs)) {\n throw rpcErrors.invalidParams({\n message: 'Expected a valid cronjob specification array.',\n });\n }\n}\n\n/**\n * Caveat specification for the Cronjob.\n */\nexport const cronjobCaveatSpecifications: Record<\n SnapCaveatType.SnapCronjob,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.SnapCronjob]: Object.freeze({\n type: SnapCaveatType.SnapCronjob,\n validator: (caveat) => validateCronjobCaveat(caveat),\n }),\n};\n"]}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cancelBackgroundEventHandler = void 0;
|
|
4
|
+
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
5
|
+
const superstruct_1 = require("@metamask/superstruct");
|
|
6
|
+
const endowments_1 = require("../endowments/index.cjs");
|
|
7
|
+
const methodName = 'snap_cancelBackgroundEvent';
|
|
8
|
+
const hookNames = {
|
|
9
|
+
cancelBackgroundEvent: true,
|
|
10
|
+
hasPermission: true,
|
|
11
|
+
};
|
|
12
|
+
exports.cancelBackgroundEventHandler = {
|
|
13
|
+
methodNames: [methodName],
|
|
14
|
+
implementation: getCancelBackgroundEventImplementation,
|
|
15
|
+
hookNames,
|
|
16
|
+
};
|
|
17
|
+
const CancelBackgroundEventsParametersStruct = (0, superstruct_1.object)({
|
|
18
|
+
id: (0, superstruct_1.string)(),
|
|
19
|
+
});
|
|
20
|
+
/**
|
|
21
|
+
* The `snap_cancelBackgroundEvent` method implementation.
|
|
22
|
+
*
|
|
23
|
+
* @param req - The JSON-RPC request object.
|
|
24
|
+
* @param res - The JSON-RPC response object.
|
|
25
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
26
|
+
* function.
|
|
27
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
28
|
+
* @param hooks - The RPC method hooks.
|
|
29
|
+
* @param hooks.cancelBackgroundEvent - The function to cancel a background event.
|
|
30
|
+
* @param hooks.hasPermission - The function to check if a snap has the `endowment:cronjob` permission.
|
|
31
|
+
* @returns Nothing.
|
|
32
|
+
*/
|
|
33
|
+
async function getCancelBackgroundEventImplementation(req, res, _next, end, { cancelBackgroundEvent, hasPermission }) {
|
|
34
|
+
const { params } = req;
|
|
35
|
+
if (!hasPermission(endowments_1.SnapEndowments.Cronjob)) {
|
|
36
|
+
return end(rpc_errors_1.providerErrors.unauthorized());
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
const validatedParams = getValidatedParams(params);
|
|
40
|
+
const { id } = validatedParams;
|
|
41
|
+
cancelBackgroundEvent(id);
|
|
42
|
+
res.result = null;
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
return end(error);
|
|
46
|
+
}
|
|
47
|
+
return end();
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Validate the cancelBackgroundEvent method `params` and returns them cast to the correct type. Throws if validation fails.
|
|
51
|
+
*
|
|
52
|
+
* @param params - The unvalidated params object from the method request.
|
|
53
|
+
* @returns The validated resolveInterface method parameter object.
|
|
54
|
+
*/
|
|
55
|
+
function getValidatedParams(params) {
|
|
56
|
+
try {
|
|
57
|
+
return (0, superstruct_1.create)(params, CancelBackgroundEventsParametersStruct);
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
if (error instanceof superstruct_1.StructError) {
|
|
61
|
+
throw rpc_errors_1.rpcErrors.invalidParams({
|
|
62
|
+
message: `Invalid params: ${error.message}.`,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/* istanbul ignore next */
|
|
66
|
+
throw rpc_errors_1.rpcErrors.internal();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=cancelBackgroundEvent.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cancelBackgroundEvent.cjs","sourceRoot":"","sources":["../../src/permitted/cancelBackgroundEvent.ts"],"names":[],"mappings":";;;AAEA,qDAAiE;AAOjE,uDAA4E;AAG5E,wDAA+C;AAG/C,MAAM,UAAU,GAAG,4BAA4B,CAAC;AAEhD,MAAM,SAAS,GAAwD;IACrE,qBAAqB,EAAE,IAAI;IAC3B,aAAa,EAAE,IAAI;CACpB,CAAC;AAOW,QAAA,4BAA4B,GAIrC;IACF,WAAW,EAAE,CAAC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7B,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 CancelBackgroundEventParams,\n CancelBackgroundEventResult,\n} from '@metamask/snaps-sdk';\nimport { type InferMatching } from '@metamask/snaps-utils';\nimport { StructError, create, object, string } from '@metamask/superstruct';\nimport { type PendingJsonRpcResponse } from '@metamask/utils';\n\nimport { SnapEndowments } from '../endowments';\nimport type { MethodHooksObject } from '../utils';\n\nconst methodName = 'snap_cancelBackgroundEvent';\n\nconst hookNames: MethodHooksObject<CancelBackgroundEventMethodHooks> = {\n cancelBackgroundEvent: true,\n hasPermission: true,\n};\n\nexport type CancelBackgroundEventMethodHooks = {\n cancelBackgroundEvent: (id: string) => void;\n hasPermission: (permissionName: string) => boolean;\n};\n\nexport const cancelBackgroundEventHandler: PermittedHandlerExport<\n CancelBackgroundEventMethodHooks,\n CancelBackgroundEventParameters,\n CancelBackgroundEventResult\n> = {\n methodNames: [methodName],\n implementation: getCancelBackgroundEventImplementation,\n hookNames,\n};\n\nconst CancelBackgroundEventsParametersStruct = object({\n id: string(),\n});\n\nexport type CancelBackgroundEventParameters = InferMatching<\n typeof CancelBackgroundEventsParametersStruct,\n CancelBackgroundEventParams\n>;\n\n/**\n * The `snap_cancelBackgroundEvent` 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.cancelBackgroundEvent - The function to cancel a background event.\n * @param hooks.hasPermission - The function to check if a snap has the `endowment:cronjob` permission.\n * @returns Nothing.\n */\nasync function getCancelBackgroundEventImplementation(\n req: JsonRpcRequest<CancelBackgroundEventParameters>,\n res: PendingJsonRpcResponse<CancelBackgroundEventResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { cancelBackgroundEvent, hasPermission }: CancelBackgroundEventMethodHooks,\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\n const { id } = validatedParams;\n\n cancelBackgroundEvent(id);\n res.result = null;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validate the cancelBackgroundEvent method `params` and returns them cast to the correct 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(params: unknown): CancelBackgroundEventParameters {\n try {\n return create(params, CancelBackgroundEventsParametersStruct);\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,16 @@
|
|
|
1
|
+
import type { PermittedHandlerExport } from "@metamask/permission-controller";
|
|
2
|
+
import type { CancelBackgroundEventParams, CancelBackgroundEventResult } from "@metamask/snaps-sdk";
|
|
3
|
+
import { type InferMatching } from "@metamask/snaps-utils";
|
|
4
|
+
export type CancelBackgroundEventMethodHooks = {
|
|
5
|
+
cancelBackgroundEvent: (id: string) => void;
|
|
6
|
+
hasPermission: (permissionName: string) => boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare const cancelBackgroundEventHandler: PermittedHandlerExport<CancelBackgroundEventMethodHooks, CancelBackgroundEventParameters, CancelBackgroundEventResult>;
|
|
9
|
+
declare const CancelBackgroundEventsParametersStruct: import("@metamask/superstruct").Struct<{
|
|
10
|
+
id: string;
|
|
11
|
+
}, {
|
|
12
|
+
id: import("@metamask/superstruct").Struct<string, null>;
|
|
13
|
+
}>;
|
|
14
|
+
export type CancelBackgroundEventParameters = InferMatching<typeof CancelBackgroundEventsParametersStruct, CancelBackgroundEventParams>;
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=cancelBackgroundEvent.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cancelBackgroundEvent.d.cts","sourceRoot":"","sources":["../../src/permitted/cancelBackgroundEvent.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,KAAK,EAEV,2BAA2B,EAC3B,2BAA2B,EAC5B,4BAA4B;AAC7B,OAAO,EAAE,KAAK,aAAa,EAAE,8BAA8B;AAc3D,MAAM,MAAM,gCAAgC,GAAG;IAC7C,qBAAqB,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5C,aAAa,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,OAAO,CAAC;CACpD,CAAC;AAEF,eAAO,MAAM,4BAA4B,EAAE,sBAAsB,CAC/D,gCAAgC,EAChC,+BAA+B,EAC/B,2BAA2B,CAK5B,CAAC;AAEF,QAAA,MAAM,sCAAsC;;;;EAE1C,CAAC;AAEH,MAAM,MAAM,+BAA+B,GAAG,aAAa,CACzD,OAAO,sCAAsC,EAC7C,2BAA2B,CAC5B,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { PermittedHandlerExport } from "@metamask/permission-controller";
|
|
2
|
+
import type { CancelBackgroundEventParams, CancelBackgroundEventResult } from "@metamask/snaps-sdk";
|
|
3
|
+
import { type InferMatching } from "@metamask/snaps-utils";
|
|
4
|
+
export type CancelBackgroundEventMethodHooks = {
|
|
5
|
+
cancelBackgroundEvent: (id: string) => void;
|
|
6
|
+
hasPermission: (permissionName: string) => boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare const cancelBackgroundEventHandler: PermittedHandlerExport<CancelBackgroundEventMethodHooks, CancelBackgroundEventParameters, CancelBackgroundEventResult>;
|
|
9
|
+
declare const CancelBackgroundEventsParametersStruct: import("@metamask/superstruct").Struct<{
|
|
10
|
+
id: string;
|
|
11
|
+
}, {
|
|
12
|
+
id: import("@metamask/superstruct").Struct<string, null>;
|
|
13
|
+
}>;
|
|
14
|
+
export type CancelBackgroundEventParameters = InferMatching<typeof CancelBackgroundEventsParametersStruct, CancelBackgroundEventParams>;
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=cancelBackgroundEvent.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cancelBackgroundEvent.d.mts","sourceRoot":"","sources":["../../src/permitted/cancelBackgroundEvent.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,KAAK,EAEV,2BAA2B,EAC3B,2BAA2B,EAC5B,4BAA4B;AAC7B,OAAO,EAAE,KAAK,aAAa,EAAE,8BAA8B;AAc3D,MAAM,MAAM,gCAAgC,GAAG;IAC7C,qBAAqB,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5C,aAAa,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,OAAO,CAAC;CACpD,CAAC;AAEF,eAAO,MAAM,4BAA4B,EAAE,sBAAsB,CAC/D,gCAAgC,EAChC,+BAA+B,EAC/B,2BAA2B,CAK5B,CAAC;AAEF,QAAA,MAAM,sCAAsC;;;;EAE1C,CAAC;AAEH,MAAM,MAAM,+BAA+B,GAAG,aAAa,CACzD,OAAO,sCAAsC,EAC7C,2BAA2B,CAC5B,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { providerErrors, rpcErrors } from "@metamask/rpc-errors";
|
|
2
|
+
import { StructError, create, object, string } from "@metamask/superstruct";
|
|
3
|
+
import { SnapEndowments } from "../endowments/index.mjs";
|
|
4
|
+
const methodName = 'snap_cancelBackgroundEvent';
|
|
5
|
+
const hookNames = {
|
|
6
|
+
cancelBackgroundEvent: true,
|
|
7
|
+
hasPermission: true,
|
|
8
|
+
};
|
|
9
|
+
export const cancelBackgroundEventHandler = {
|
|
10
|
+
methodNames: [methodName],
|
|
11
|
+
implementation: getCancelBackgroundEventImplementation,
|
|
12
|
+
hookNames,
|
|
13
|
+
};
|
|
14
|
+
const CancelBackgroundEventsParametersStruct = object({
|
|
15
|
+
id: string(),
|
|
16
|
+
});
|
|
17
|
+
/**
|
|
18
|
+
* The `snap_cancelBackgroundEvent` method implementation.
|
|
19
|
+
*
|
|
20
|
+
* @param req - The JSON-RPC request object.
|
|
21
|
+
* @param res - The JSON-RPC response object.
|
|
22
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
23
|
+
* function.
|
|
24
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
25
|
+
* @param hooks - The RPC method hooks.
|
|
26
|
+
* @param hooks.cancelBackgroundEvent - The function to cancel a background event.
|
|
27
|
+
* @param hooks.hasPermission - The function to check if a snap has the `endowment:cronjob` permission.
|
|
28
|
+
* @returns Nothing.
|
|
29
|
+
*/
|
|
30
|
+
async function getCancelBackgroundEventImplementation(req, res, _next, end, { cancelBackgroundEvent, hasPermission }) {
|
|
31
|
+
const { params } = req;
|
|
32
|
+
if (!hasPermission(SnapEndowments.Cronjob)) {
|
|
33
|
+
return end(providerErrors.unauthorized());
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
const validatedParams = getValidatedParams(params);
|
|
37
|
+
const { id } = validatedParams;
|
|
38
|
+
cancelBackgroundEvent(id);
|
|
39
|
+
res.result = null;
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
return end(error);
|
|
43
|
+
}
|
|
44
|
+
return end();
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Validate the cancelBackgroundEvent method `params` and returns them cast to the correct type. Throws if validation fails.
|
|
48
|
+
*
|
|
49
|
+
* @param params - The unvalidated params object from the method request.
|
|
50
|
+
* @returns The validated resolveInterface method parameter object.
|
|
51
|
+
*/
|
|
52
|
+
function getValidatedParams(params) {
|
|
53
|
+
try {
|
|
54
|
+
return create(params, CancelBackgroundEventsParametersStruct);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
if (error instanceof StructError) {
|
|
58
|
+
throw rpcErrors.invalidParams({
|
|
59
|
+
message: `Invalid params: ${error.message}.`,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/* istanbul ignore next */
|
|
63
|
+
throw rpcErrors.internal();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=cancelBackgroundEvent.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cancelBackgroundEvent.mjs","sourceRoot":"","sources":["../../src/permitted/cancelBackgroundEvent.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,6BAA6B;AAOjE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,8BAA8B;AAG5E,OAAO,EAAE,cAAc,EAAE,gCAAsB;AAG/C,MAAM,UAAU,GAAG,4BAA4B,CAAC;AAEhD,MAAM,SAAS,GAAwD;IACrE,qBAAqB,EAAE,IAAI;IAC3B,aAAa,EAAE,IAAI;CACpB,CAAC;AAOF,MAAM,CAAC,MAAM,4BAA4B,GAIrC;IACF,WAAW,EAAE,CAAC,UAAU,CAAC;IACzB,cAAc,EAAE,sCAAsC;IACtD,SAAS;CACV,CAAC;AAEF,MAAM,sCAAsC,GAAG,MAAM,CAAC;IACpD,EAAE,EAAE,MAAM,EAAE;CACb,CAAC,CAAC;AAOH;;;;;;;;;;;;GAYG;AACH,KAAK,UAAU,sCAAsC,CACnD,GAAoD,EACpD,GAAwD,EACxD,KAAc,EACd,GAA6B,EAC7B,EAAE,qBAAqB,EAAE,aAAa,EAAoC;IAE1E,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IAEvB,IAAI,CAAC,aAAa,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE,CAAC;QAC3C,OAAO,GAAG,CAAC,cAAc,CAAC,YAAY,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED,IAAI,CAAC;QACH,MAAM,eAAe,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;QAEnD,MAAM,EAAE,EAAE,EAAE,GAAG,eAAe,CAAC;QAE/B,qBAAqB,CAAC,EAAE,CAAC,CAAC;QAC1B,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;;;;;GAKG;AACH,SAAS,kBAAkB,CAAC,MAAe;IACzC,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,MAAM,EAAE,sCAAsC,CAAC,CAAC;IAChE,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 CancelBackgroundEventParams,\n CancelBackgroundEventResult,\n} from '@metamask/snaps-sdk';\nimport { type InferMatching } from '@metamask/snaps-utils';\nimport { StructError, create, object, string } from '@metamask/superstruct';\nimport { type PendingJsonRpcResponse } from '@metamask/utils';\n\nimport { SnapEndowments } from '../endowments';\nimport type { MethodHooksObject } from '../utils';\n\nconst methodName = 'snap_cancelBackgroundEvent';\n\nconst hookNames: MethodHooksObject<CancelBackgroundEventMethodHooks> = {\n cancelBackgroundEvent: true,\n hasPermission: true,\n};\n\nexport type CancelBackgroundEventMethodHooks = {\n cancelBackgroundEvent: (id: string) => void;\n hasPermission: (permissionName: string) => boolean;\n};\n\nexport const cancelBackgroundEventHandler: PermittedHandlerExport<\n CancelBackgroundEventMethodHooks,\n CancelBackgroundEventParameters,\n CancelBackgroundEventResult\n> = {\n methodNames: [methodName],\n implementation: getCancelBackgroundEventImplementation,\n hookNames,\n};\n\nconst CancelBackgroundEventsParametersStruct = object({\n id: string(),\n});\n\nexport type CancelBackgroundEventParameters = InferMatching<\n typeof CancelBackgroundEventsParametersStruct,\n CancelBackgroundEventParams\n>;\n\n/**\n * The `snap_cancelBackgroundEvent` 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.cancelBackgroundEvent - The function to cancel a background event.\n * @param hooks.hasPermission - The function to check if a snap has the `endowment:cronjob` permission.\n * @returns Nothing.\n */\nasync function getCancelBackgroundEventImplementation(\n req: JsonRpcRequest<CancelBackgroundEventParameters>,\n res: PendingJsonRpcResponse<CancelBackgroundEventResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { cancelBackgroundEvent, hasPermission }: CancelBackgroundEventMethodHooks,\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\n const { id } = validatedParams;\n\n cancelBackgroundEvent(id);\n res.result = null;\n } catch (error) {\n return end(error);\n }\n\n return end();\n}\n\n/**\n * Validate the cancelBackgroundEvent method `params` and returns them cast to the correct 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(params: unknown): CancelBackgroundEventParameters {\n try {\n return create(params, CancelBackgroundEventsParametersStruct);\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,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.clearStateHandler = void 0;
|
|
4
|
+
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
5
|
+
const superstruct_1 = require("@metamask/superstruct");
|
|
6
|
+
const manageState_1 = require("../restricted/manageState.cjs");
|
|
7
|
+
const hookNames = {
|
|
8
|
+
clearSnapState: true,
|
|
9
|
+
hasPermission: true,
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* `snap_clearState` clears the state of the Snap.
|
|
13
|
+
*/
|
|
14
|
+
exports.clearStateHandler = {
|
|
15
|
+
methodNames: ['snap_clearState'],
|
|
16
|
+
implementation: clearStateImplementation,
|
|
17
|
+
hookNames,
|
|
18
|
+
};
|
|
19
|
+
const ClearStateParametersStruct = (0, superstruct_1.object)({
|
|
20
|
+
encrypted: (0, superstruct_1.optional)((0, superstruct_1.boolean)()),
|
|
21
|
+
});
|
|
22
|
+
/**
|
|
23
|
+
* The `snap_clearState` method implementation.
|
|
24
|
+
*
|
|
25
|
+
* @param request - The JSON-RPC request object.
|
|
26
|
+
* @param response - The JSON-RPC response object.
|
|
27
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
28
|
+
* function.
|
|
29
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
30
|
+
* @param hooks - The RPC method hooks.
|
|
31
|
+
* @param hooks.clearSnapState - A function that clears the state of the
|
|
32
|
+
* requesting Snap.
|
|
33
|
+
* @param hooks.hasPermission - Check whether a given origin has a given
|
|
34
|
+
* permission.
|
|
35
|
+
* @returns Nothing.
|
|
36
|
+
*/
|
|
37
|
+
async function clearStateImplementation(request, response, _next, end, { clearSnapState, hasPermission }) {
|
|
38
|
+
const { params } = request;
|
|
39
|
+
if (!hasPermission(manageState_1.manageStateBuilder.targetName)) {
|
|
40
|
+
return end(rpc_errors_1.providerErrors.unauthorized());
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
const validatedParams = getValidatedParams(params);
|
|
44
|
+
const { encrypted = true } = validatedParams;
|
|
45
|
+
clearSnapState(encrypted);
|
|
46
|
+
response.result = null;
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
return end(error);
|
|
50
|
+
}
|
|
51
|
+
return end();
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Validate the parameters of the `snap_clearState` method.
|
|
55
|
+
*
|
|
56
|
+
* @param params - The parameters to validate.
|
|
57
|
+
* @returns The validated parameters.
|
|
58
|
+
*/
|
|
59
|
+
function getValidatedParams(params) {
|
|
60
|
+
try {
|
|
61
|
+
return (0, superstruct_1.create)(params, ClearStateParametersStruct);
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
if (error instanceof superstruct_1.StructError) {
|
|
65
|
+
throw rpc_errors_1.rpcErrors.invalidParams({
|
|
66
|
+
message: `Invalid params: ${error.message}.`,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/* istanbul ignore next */
|
|
70
|
+
throw rpc_errors_1.rpcErrors.internal();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=clearState.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clearState.cjs","sourceRoot":"","sources":["../../src/permitted/clearState.ts"],"names":[],"mappings":";;;AAEA,qDAAiE;AAGjE,uDAM+B;AAG/B,+DAA+D;AAG/D,MAAM,SAAS,GAAuC;IACpD,cAAc,EAAE,IAAI;IACpB,aAAa,EAAE,IAAI;CACpB,CAAC;AAEF;;GAEG;AACU,QAAA,iBAAiB,GAI1B;IACF,WAAW,EAAE,CAAC,iBAAiB,CAAC;IAChC,cA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AQ,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 { ClearStateParams, ClearStateResult } from '@metamask/snaps-sdk';\nimport { type InferMatching } from '@metamask/snaps-utils';\nimport {\n boolean,\n create,\n object,\n optional,\n StructError,\n} from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse, JsonRpcRequest } from '@metamask/utils';\n\nimport { manageStateBuilder } from '../restricted/manageState';\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<ClearStateHooks> = {\n clearSnapState: true,\n hasPermission: true,\n};\n\n/**\n * `snap_clearState` clears the state of the Snap.\n */\nexport const clearStateHandler: PermittedHandlerExport<\n ClearStateHooks,\n ClearStateParameters,\n ClearStateResult\n> = {\n methodNames: ['snap_clearState'],\n implementation: clearStateImplementation,\n hookNames,\n};\n\nexport type ClearStateHooks = {\n /**\n * A function that clears the state of the requesting Snap.\n */\n clearSnapState: (encrypted: boolean) => void;\n\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\nconst ClearStateParametersStruct = object({\n encrypted: optional(boolean()),\n});\n\nexport type ClearStateParameters = InferMatching<\n typeof ClearStateParametersStruct,\n ClearStateParams\n>;\n\n/**\n * The `snap_clearState` 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.clearSnapState - A function that clears the state of the\n * requesting Snap.\n * @param hooks.hasPermission - Check whether a given origin has a given\n * permission.\n * @returns Nothing.\n */\nasync function clearStateImplementation(\n request: JsonRpcRequest<ClearStateParameters>,\n response: PendingJsonRpcResponse<ClearStateResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { clearSnapState, hasPermission }: ClearStateHooks,\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 { encrypted = true } = validatedParams;\n\n clearSnapState(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_clearState` 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, ClearStateParametersStruct);\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"]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { PermittedHandlerExport } from "@metamask/permission-controller";
|
|
2
|
+
import type { ClearStateParams, ClearStateResult } from "@metamask/snaps-sdk";
|
|
3
|
+
import { type InferMatching } from "@metamask/snaps-utils";
|
|
4
|
+
/**
|
|
5
|
+
* `snap_clearState` clears the state of the Snap.
|
|
6
|
+
*/
|
|
7
|
+
export declare const clearStateHandler: PermittedHandlerExport<ClearStateHooks, ClearStateParameters, ClearStateResult>;
|
|
8
|
+
export type ClearStateHooks = {
|
|
9
|
+
/**
|
|
10
|
+
* A function that clears the state of the requesting Snap.
|
|
11
|
+
*/
|
|
12
|
+
clearSnapState: (encrypted: boolean) => void;
|
|
13
|
+
/**
|
|
14
|
+
* Check if the requesting origin has a given permission.
|
|
15
|
+
*
|
|
16
|
+
* @param permissionName - The name of the permission to check.
|
|
17
|
+
* @returns Whether the origin has the permission.
|
|
18
|
+
*/
|
|
19
|
+
hasPermission: (permissionName: string) => boolean;
|
|
20
|
+
};
|
|
21
|
+
declare const ClearStateParametersStruct: import("@metamask/superstruct").Struct<{
|
|
22
|
+
encrypted?: boolean | undefined;
|
|
23
|
+
}, {
|
|
24
|
+
encrypted: import("@metamask/superstruct").Struct<boolean | undefined, null>;
|
|
25
|
+
}>;
|
|
26
|
+
export type ClearStateParameters = InferMatching<typeof ClearStateParametersStruct, ClearStateParams>;
|
|
27
|
+
export {};
|
|
28
|
+
//# sourceMappingURL=clearState.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clearState.d.cts","sourceRoot":"","sources":["../../src/permitted/clearState.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,KAAK,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,4BAA4B;AAC9E,OAAO,EAAE,KAAK,aAAa,EAAE,8BAA8B;AAkB3D;;GAEG;AACH,eAAO,MAAM,iBAAiB,EAAE,sBAAsB,CACpD,eAAe,EACf,oBAAoB,EACpB,gBAAgB,CAKjB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B;;OAEG;IACH,cAAc,EAAE,CAAC,SAAS,EAAE,OAAO,KAAK,IAAI,CAAC;IAE7C;;;;;OAKG;IACH,aAAa,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,OAAO,CAAC;CACpD,CAAC;AAEF,QAAA,MAAM,0BAA0B;;;;EAE9B,CAAC;AAEH,MAAM,MAAM,oBAAoB,GAAG,aAAa,CAC9C,OAAO,0BAA0B,EACjC,gBAAgB,CACjB,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { PermittedHandlerExport } from "@metamask/permission-controller";
|
|
2
|
+
import type { ClearStateParams, ClearStateResult } from "@metamask/snaps-sdk";
|
|
3
|
+
import { type InferMatching } from "@metamask/snaps-utils";
|
|
4
|
+
/**
|
|
5
|
+
* `snap_clearState` clears the state of the Snap.
|
|
6
|
+
*/
|
|
7
|
+
export declare const clearStateHandler: PermittedHandlerExport<ClearStateHooks, ClearStateParameters, ClearStateResult>;
|
|
8
|
+
export type ClearStateHooks = {
|
|
9
|
+
/**
|
|
10
|
+
* A function that clears the state of the requesting Snap.
|
|
11
|
+
*/
|
|
12
|
+
clearSnapState: (encrypted: boolean) => void;
|
|
13
|
+
/**
|
|
14
|
+
* Check if the requesting origin has a given permission.
|
|
15
|
+
*
|
|
16
|
+
* @param permissionName - The name of the permission to check.
|
|
17
|
+
* @returns Whether the origin has the permission.
|
|
18
|
+
*/
|
|
19
|
+
hasPermission: (permissionName: string) => boolean;
|
|
20
|
+
};
|
|
21
|
+
declare const ClearStateParametersStruct: import("@metamask/superstruct").Struct<{
|
|
22
|
+
encrypted?: boolean | undefined;
|
|
23
|
+
}, {
|
|
24
|
+
encrypted: import("@metamask/superstruct").Struct<boolean | undefined, null>;
|
|
25
|
+
}>;
|
|
26
|
+
export type ClearStateParameters = InferMatching<typeof ClearStateParametersStruct, ClearStateParams>;
|
|
27
|
+
export {};
|
|
28
|
+
//# sourceMappingURL=clearState.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clearState.d.mts","sourceRoot":"","sources":["../../src/permitted/clearState.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAwC;AAE9E,OAAO,KAAK,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,4BAA4B;AAC9E,OAAO,EAAE,KAAK,aAAa,EAAE,8BAA8B;AAkB3D;;GAEG;AACH,eAAO,MAAM,iBAAiB,EAAE,sBAAsB,CACpD,eAAe,EACf,oBAAoB,EACpB,gBAAgB,CAKjB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B;;OAEG;IACH,cAAc,EAAE,CAAC,SAAS,EAAE,OAAO,KAAK,IAAI,CAAC;IAE7C;;;;;OAKG;IACH,aAAa,EAAE,CAAC,cAAc,EAAE,MAAM,KAAK,OAAO,CAAC;CACpD,CAAC;AAEF,QAAA,MAAM,0BAA0B;;;;EAE9B,CAAC;AAEH,MAAM,MAAM,oBAAoB,GAAG,aAAa,CAC9C,OAAO,0BAA0B,EACjC,gBAAgB,CACjB,CAAC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { providerErrors, rpcErrors } from "@metamask/rpc-errors";
|
|
2
|
+
import { boolean, create, object, optional, StructError } from "@metamask/superstruct";
|
|
3
|
+
import { manageStateBuilder } from "../restricted/manageState.mjs";
|
|
4
|
+
const hookNames = {
|
|
5
|
+
clearSnapState: true,
|
|
6
|
+
hasPermission: true,
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* `snap_clearState` clears the state of the Snap.
|
|
10
|
+
*/
|
|
11
|
+
export const clearStateHandler = {
|
|
12
|
+
methodNames: ['snap_clearState'],
|
|
13
|
+
implementation: clearStateImplementation,
|
|
14
|
+
hookNames,
|
|
15
|
+
};
|
|
16
|
+
const ClearStateParametersStruct = object({
|
|
17
|
+
encrypted: optional(boolean()),
|
|
18
|
+
});
|
|
19
|
+
/**
|
|
20
|
+
* The `snap_clearState` method implementation.
|
|
21
|
+
*
|
|
22
|
+
* @param request - The JSON-RPC request object.
|
|
23
|
+
* @param response - The JSON-RPC response object.
|
|
24
|
+
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
|
|
25
|
+
* function.
|
|
26
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
27
|
+
* @param hooks - The RPC method hooks.
|
|
28
|
+
* @param hooks.clearSnapState - A function that clears the state of the
|
|
29
|
+
* requesting Snap.
|
|
30
|
+
* @param hooks.hasPermission - Check whether a given origin has a given
|
|
31
|
+
* permission.
|
|
32
|
+
* @returns Nothing.
|
|
33
|
+
*/
|
|
34
|
+
async function clearStateImplementation(request, response, _next, end, { clearSnapState, hasPermission }) {
|
|
35
|
+
const { params } = request;
|
|
36
|
+
if (!hasPermission(manageStateBuilder.targetName)) {
|
|
37
|
+
return end(providerErrors.unauthorized());
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
const validatedParams = getValidatedParams(params);
|
|
41
|
+
const { encrypted = true } = validatedParams;
|
|
42
|
+
clearSnapState(encrypted);
|
|
43
|
+
response.result = null;
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
return end(error);
|
|
47
|
+
}
|
|
48
|
+
return end();
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Validate the parameters of the `snap_clearState` method.
|
|
52
|
+
*
|
|
53
|
+
* @param params - The parameters to validate.
|
|
54
|
+
* @returns The validated parameters.
|
|
55
|
+
*/
|
|
56
|
+
function getValidatedParams(params) {
|
|
57
|
+
try {
|
|
58
|
+
return create(params, ClearStateParametersStruct);
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
if (error instanceof StructError) {
|
|
62
|
+
throw rpcErrors.invalidParams({
|
|
63
|
+
message: `Invalid params: ${error.message}.`,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/* istanbul ignore next */
|
|
67
|
+
throw rpcErrors.internal();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=clearState.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clearState.mjs","sourceRoot":"","sources":["../../src/permitted/clearState.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,6BAA6B;AAGjE,OAAO,EACL,OAAO,EACP,MAAM,EACN,MAAM,EACN,QAAQ,EACR,WAAW,EACZ,8BAA8B;AAG/B,OAAO,EAAE,kBAAkB,EAAE,sCAAkC;AAG/D,MAAM,SAAS,GAAuC;IACpD,cAAc,EAAE,IAAI;IACpB,aAAa,EAAE,IAAI;CACpB,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAI1B;IACF,WAAW,EAAE,CAAC,iBAAiB,CAAC;IAChC,cAAc,EAAE,wBAAwB;IACxC,SAAS;CACV,CAAC;AAiBF,MAAM,0BAA0B,GAAG,MAAM,CAAC;IACxC,SAAS,EAAE,QAAQ,CAAC,OAAO,EAAE,CAAC;CAC/B,CAAC,CAAC;AAOH;;;;;;;;;;;;;;GAcG;AACH,KAAK,UAAU,wBAAwB,CACrC,OAA6C,EAC7C,QAAkD,EAClD,KAAc,EACd,GAA6B,EAC7B,EAAE,cAAc,EAAE,aAAa,EAAmB;IAElD,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAE3B,IAAI,CAAC,aAAa,CAAC,kBAAkB,CAAC,UAAU,CAAC,EAAE,CAAC;QAClD,OAAO,GAAG,CAAC,cAAc,CAAC,YAAY,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED,IAAI,CAAC;QACH,MAAM,eAAe,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;QACnD,MAAM,EAAE,SAAS,GAAG,IAAI,EAAE,GAAG,eAAe,CAAC;QAE7C,cAAc,CAAC,SAAS,CAAC,CAAC;QAC1B,QAAQ,CAAC,MAAM,GAAG,IAAI,CAAC;IACzB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,OAAO,GAAG,EAAE,CAAC;AACf,CAAC;AAED;;;;;GAKG;AACH,SAAS,kBAAkB,CAAC,MAAgB;IAC1C,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,MAAM,EAAE,0BAA0B,CAAC,CAAC;IACpD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,WAAW,EAAE,CAAC;YACjC,MAAM,SAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,mBAAmB,KAAK,CAAC,OAAO,GAAG;aAC7C,CAAC,CAAC;QACL,CAAC;QAED,0BAA0B;QAC1B,MAAM,SAAS,CAAC,QAAQ,EAAE,CAAC;IAC7B,CAAC;AACH,CAAC","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 { ClearStateParams, ClearStateResult } from '@metamask/snaps-sdk';\nimport { type InferMatching } from '@metamask/snaps-utils';\nimport {\n boolean,\n create,\n object,\n optional,\n StructError,\n} from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse, JsonRpcRequest } from '@metamask/utils';\n\nimport { manageStateBuilder } from '../restricted/manageState';\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<ClearStateHooks> = {\n clearSnapState: true,\n hasPermission: true,\n};\n\n/**\n * `snap_clearState` clears the state of the Snap.\n */\nexport const clearStateHandler: PermittedHandlerExport<\n ClearStateHooks,\n ClearStateParameters,\n ClearStateResult\n> = {\n methodNames: ['snap_clearState'],\n implementation: clearStateImplementation,\n hookNames,\n};\n\nexport type ClearStateHooks = {\n /**\n * A function that clears the state of the requesting Snap.\n */\n clearSnapState: (encrypted: boolean) => void;\n\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\nconst ClearStateParametersStruct = object({\n encrypted: optional(boolean()),\n});\n\nexport type ClearStateParameters = InferMatching<\n typeof ClearStateParametersStruct,\n ClearStateParams\n>;\n\n/**\n * The `snap_clearState` 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.clearSnapState - A function that clears the state of the\n * requesting Snap.\n * @param hooks.hasPermission - Check whether a given origin has a given\n * permission.\n * @returns Nothing.\n */\nasync function clearStateImplementation(\n request: JsonRpcRequest<ClearStateParameters>,\n response: PendingJsonRpcResponse<ClearStateResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { clearSnapState, hasPermission }: ClearStateHooks,\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 { encrypted = true } = validatedParams;\n\n clearSnapState(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_clearState` 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, ClearStateParametersStruct);\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"]}
|