@metamask/snaps-rpc-methods 15.0.0 → 15.0.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 +17 -1
- package/dist/restricted/index.d.cts +1 -1
- package/dist/restricted/index.d.mts +1 -1
- package/dist/restricted/invokeSnap.cjs +2 -2
- package/dist/restricted/invokeSnap.cjs.map +1 -1
- package/dist/restricted/invokeSnap.d.cts +5 -5
- package/dist/restricted/invokeSnap.d.cts.map +1 -1
- package/dist/restricted/invokeSnap.d.mts +5 -5
- package/dist/restricted/invokeSnap.d.mts.map +1 -1
- package/dist/restricted/invokeSnap.mjs +2 -2
- package/dist/restricted/invokeSnap.mjs.map +1 -1
- package/dist/restricted/notify.cjs.map +1 -1
- package/dist/restricted/notify.d.cts +1 -1
- package/dist/restricted/notify.d.cts.map +1 -1
- package/dist/restricted/notify.d.mts +1 -1
- package/dist/restricted/notify.d.mts.map +1 -1
- package/dist/restricted/notify.mjs.map +1 -1
- package/package.json +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [15.0.2]
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- Bump `@metamask/json-rpc-engine` from `^10.2.3` to `^10.2.4` ([#3925](https://github.com/MetaMask/snaps/pull/3925))
|
|
15
|
+
- Bump `@metamask/messenger` from `^0.3.0` to `^1.1.0` ([#3930](https://github.com/MetaMask/snaps/pull/3930), [#3934](https://github.com/MetaMask/snaps/pull/3934))
|
|
16
|
+
- Bump `@metamask/permission-controller` from `^12.2.1` to `^12.3.0` ([#3930](https://github.com/MetaMask/snaps/pull/3930))
|
|
17
|
+
|
|
18
|
+
## [15.0.1]
|
|
19
|
+
|
|
20
|
+
### Changed
|
|
21
|
+
|
|
22
|
+
- Bump `@metamask/snaps-controllers` from `18.0.4` to `19.0.0` and handle breaking changes ([#3922](https://github.com/MetaMask/snaps/pull/3922))
|
|
23
|
+
|
|
10
24
|
## [15.0.0]
|
|
11
25
|
|
|
12
26
|
### Changed
|
|
@@ -582,7 +596,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
582
596
|
- The version of the package no longer needs to match the version of all other
|
|
583
597
|
MetaMask Snaps packages.
|
|
584
598
|
|
|
585
|
-
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@15.0.
|
|
599
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@15.0.2...HEAD
|
|
600
|
+
[15.0.2]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@15.0.1...@metamask/snaps-rpc-methods@15.0.2
|
|
601
|
+
[15.0.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@15.0.0...@metamask/snaps-rpc-methods@15.0.1
|
|
586
602
|
[15.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@14.3.0...@metamask/snaps-rpc-methods@15.0.0
|
|
587
603
|
[14.3.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@14.2.0...@metamask/snaps-rpc-methods@14.3.0
|
|
588
604
|
[14.2.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@14.1.1...@metamask/snaps-rpc-methods@14.2.0
|
|
@@ -90,7 +90,7 @@ export declare const restrictedMethodPermissionBuilders: {
|
|
|
90
90
|
allowedCaveats: readonly [string, ...string[]] | null;
|
|
91
91
|
validator: import("@metamask/permission-controller").PermissionValidatorConstraint;
|
|
92
92
|
sideEffect: {
|
|
93
|
-
onPermitted: import("@metamask/permission-controller").SideEffectHandler<import("./invokeSnap.cjs").
|
|
93
|
+
onPermitted: import("@metamask/permission-controller").SideEffectHandler<import("./invokeSnap.cjs").SnapControllerInstallSnapsAction | import("./invokeSnap.cjs").SnapControllerGetPermittedSnapsAction, never>;
|
|
94
94
|
};
|
|
95
95
|
}>;
|
|
96
96
|
readonly methodHooks: import("../utils.cjs").MethodHooksObject<InvokeSnapMethodHooks>;
|
|
@@ -90,7 +90,7 @@ export declare const restrictedMethodPermissionBuilders: {
|
|
|
90
90
|
allowedCaveats: readonly [string, ...string[]] | null;
|
|
91
91
|
validator: import("@metamask/permission-controller").PermissionValidatorConstraint;
|
|
92
92
|
sideEffect: {
|
|
93
|
-
onPermitted: import("@metamask/permission-controller").SideEffectHandler<import("./invokeSnap.mjs").
|
|
93
|
+
onPermitted: import("@metamask/permission-controller").SideEffectHandler<import("./invokeSnap.mjs").SnapControllerInstallSnapsAction | import("./invokeSnap.mjs").SnapControllerGetPermittedSnapsAction, never>;
|
|
94
94
|
};
|
|
95
95
|
}>;
|
|
96
96
|
readonly methodHooks: import("../utils.mjs").MethodHooksObject<InvokeSnapMethodHooks>;
|
|
@@ -16,14 +16,14 @@ exports.WALLET_SNAP_PERMISSION_KEY = 'wallet_snap';
|
|
|
16
16
|
const handleSnapInstall = async ({ requestData, messenger }) => {
|
|
17
17
|
const snaps = requestData.permissions[exports.WALLET_SNAP_PERMISSION_KEY].caveats?.[0]
|
|
18
18
|
.value;
|
|
19
|
-
const permittedSnaps = messenger.call(`SnapController:
|
|
19
|
+
const permittedSnaps = messenger.call(`SnapController:getPermittedSnaps`, requestData.metadata.origin);
|
|
20
20
|
const dedupedSnaps = Object.keys(snaps).reduce((filteredSnaps, snap) => {
|
|
21
21
|
if (!permittedSnaps[snap]) {
|
|
22
22
|
filteredSnaps[snap] = snaps[snap];
|
|
23
23
|
}
|
|
24
24
|
return filteredSnaps;
|
|
25
25
|
}, {});
|
|
26
|
-
return messenger.call(`SnapController:
|
|
26
|
+
return messenger.call(`SnapController:installSnaps`, requestData.metadata.origin, dedupedSnaps);
|
|
27
27
|
};
|
|
28
28
|
exports.handleSnapInstall = handleSnapInstall;
|
|
29
29
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"invokeSnap.cjs","sourceRoot":"","sources":["../../src/restricted/invokeSnap.ts"],"names":[],"mappings":";;;AAOA,2EAAiE;AACjE,qDAAiD;AAQjD,uDAAoE;AAKvD,QAAA,0BAA0B,GAAG,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"invokeSnap.cjs","sourceRoot":"","sources":["../../src/restricted/invokeSnap.ts"],"names":[],"mappings":";;;AAOA,2EAAiE;AACjE,qDAAiD;AAQjD,uDAAoE;AAKvD,QAAA,0BAA0B,GAAG,aAAa,CAAC;AA6CxD;;;;;;;GAOG;AACI,MAAM,iBAAiB,GAGX,KAAK,EAAE,EAAE,WAAW,EAAE,SAAS,EAAE,EAAE,EAAE;IACtD,MAAM,KAAK,GAAG,WAAW,CAAC,WAAW,CAAC,kCAA0B,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;SAC3E,KAA2B,CAAC;IAE/B,MAAM,cAAc,GAAG,SAAS,CAAC,IAAI,CACnC,kCAAkC,EAClC,WAAW,CAAC,QAAQ,CAAC,MAAM,CAC5B,CAAC;IAEF,MAAM,YAAY,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,CAC5C,CAAC,aAAa,EAAE,IAAI,EAAE,EAAE;QACtB,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,EAAE,CAAC;YAC1B,aAAa,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC;QACpC,CAAC;QACD,OAAO,aAAa,CAAC;IACvB,CAAC,EACD,EAAE,CACH,CAAC;IAEF,OAAO,SAAS,CAAC,IAAI,CACnB,6BAA6B,EAC7B,WAAW,CAAC,QAAQ,CAAC,MAAM,EAC3B,YAAY,CACb,CAAC;AACJ,CAAC,CAAC;AA3BW,QAAA,iBAAiB,qBA2B5B;AACF;;;;;;;;;;;GAWG;AACH,MAAM,oBAAoB,GAItB,CAAC,EAAE,WAAW,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AAC;IACd,CAAC,CAAC;AACJ,CAAC;AAnBD,kEAmBC","sourcesContent":["import type {\n PermissionSpecificationBuilder,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n PermissionValidatorConstraint,\n PermissionSideEffect,\n} from '@metamask/permission-controller';\nimport { PermissionType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n InvokeSnapParams,\n InvokeSnapResult,\n RequestSnapsParams,\n RequestSnapsResult,\n} from '@metamask/snaps-sdk';\nimport type { SnapRpcHookArgs } from '@metamask/snaps-utils';\nimport { HandlerType, SnapCaveatType } from '@metamask/snaps-utils';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\n\nexport const WALLET_SNAP_PERMISSION_KEY = 'wallet_snap';\n\n// Redeclare installSnaps action type to avoid circular dependencies\nexport type SnapControllerInstallSnapsAction = {\n type: `SnapController:installSnaps`;\n handler: (\n origin: string,\n requestedSnaps: RequestSnapsParams,\n ) => Promise<RequestSnapsResult>;\n};\n\nexport type SnapControllerGetPermittedSnapsAction = {\n type: `SnapController:getPermittedSnaps`;\n handler: (origin: string) => RequestSnapsResult;\n};\n\ntype AllowedActions =\n | SnapControllerInstallSnapsAction\n | SnapControllerGetPermittedSnapsAction;\n\nexport type InvokeSnapMethodHooks = {\n handleSnapRpcRequest: ({\n snapId,\n origin,\n handler,\n request,\n }: SnapRpcHookArgs & { snapId: string }) => Promise<unknown>;\n};\n\ntype InvokeSnapSpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: InvokeSnapMethodHooks;\n};\n\ntype InvokeSnapSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof WALLET_SNAP_PERMISSION_KEY;\n methodImplementation: ReturnType<typeof getInvokeSnapImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n sideEffect: {\n onPermitted: PermissionSideEffect<AllowedActions, never>['onPermitted'];\n };\n}>;\n\n/**\n * The side-effect method to handle the snap install.\n *\n * @param params - The side-effect params.\n * @param params.requestData - The request data associated to the requested permission.\n * @param params.messenger - The messenger to call an action.\n * @returns The result of the Snap installation.\n */\nexport const handleSnapInstall: PermissionSideEffect<\n AllowedActions,\n never\n>['onPermitted'] = async ({ requestData, messenger }) => {\n const snaps = requestData.permissions[WALLET_SNAP_PERMISSION_KEY].caveats?.[0]\n .value as RequestSnapsParams;\n\n const permittedSnaps = messenger.call(\n `SnapController:getPermittedSnaps`,\n requestData.metadata.origin,\n );\n\n const dedupedSnaps = Object.keys(snaps).reduce<RequestSnapsParams>(\n (filteredSnaps, snap) => {\n if (!permittedSnaps[snap]) {\n filteredSnaps[snap] = snaps[snap];\n }\n return filteredSnaps;\n },\n {},\n );\n\n return messenger.call(\n `SnapController:installSnaps`,\n requestData.metadata.origin,\n dedupedSnaps,\n );\n};\n/**\n * The specification builder for the `wallet_snap_*` permission.\n *\n * `wallet_snap_*` attempts to invoke an RPC method of the specified Snap.\n *\n * Requesting its corresponding permission will attempt to connect to the Snap,\n * and install it if it's not available yet.\n *\n * @param options - The specification builder options.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `wallet_snap_*` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n InvokeSnapSpecificationBuilderOptions,\n InvokeSnapSpecification\n> = ({ methodHooks }: InvokeSnapSpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName: WALLET_SNAP_PERMISSION_KEY,\n allowedCaveats: [SnapCaveatType.SnapIds],\n methodImplementation: getInvokeSnapImplementation(methodHooks),\n validator: ({ caveats }) => {\n if (caveats?.length !== 1 || caveats[0].type !== SnapCaveatType.SnapIds) {\n throw rpcErrors.invalidParams({\n message: `Expected a single \"${SnapCaveatType.SnapIds}\" caveat.`,\n });\n }\n },\n sideEffect: {\n onPermitted: handleSnapInstall,\n },\n };\n};\n\nconst methodHooks: MethodHooksObject<InvokeSnapMethodHooks> = {\n handleSnapRpcRequest: true,\n};\n\n/**\n * Calls the specified JSON-RPC API method of the specified Snap. The Snap\n * must be installed and the dapp must have permission to communicate with the\n * Snap, or the request is rejected. The dapp can install the Snap and request\n * permission to communicate with it using\n * [`wallet_requestSnaps`](http://docs.metamask.io/snaps/reference/snaps-api/wallet_requestsnaps).\n *\n * @example\n * ```ts\n * const result = await ethereum.request({\n * method: 'wallet_snap',\n * params: {\n * snapId: 'npm:my-snap',\n * request: {\n * method: 'hello',\n * params: { name: 'world' },\n * },\n * },\n * });\n * console.log(result); // \"Hello, world!\"\n * ```\n */\nexport const invokeSnapBuilder = Object.freeze({\n targetName: WALLET_SNAP_PERMISSION_KEY,\n specificationBuilder,\n methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `wallet_snap_*`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.handleSnapRpcRequest - A function that sends an RPC request to a snap's RPC handler or throws if that fails.\n * @returns The method implementation which returns the result of `handleSnapRpcRequest`.\n * @throws If the params are invalid.\n */\nexport function getInvokeSnapImplementation({\n handleSnapRpcRequest,\n}: InvokeSnapMethodHooks) {\n return async function invokeSnap(\n options: RestrictedMethodOptions<InvokeSnapParams>,\n ): Promise<InvokeSnapResult> {\n const { params = {}, context } = options;\n\n const { snapId, request } = params as InvokeSnapParams;\n\n const { origin } = context;\n\n return (await handleSnapRpcRequest({\n snapId,\n origin,\n request,\n handler: HandlerType.OnRpcRequest,\n })) as Json;\n };\n}\n"]}
|
|
@@ -5,15 +5,15 @@ import type { SnapRpcHookArgs } from "@metamask/snaps-utils";
|
|
|
5
5
|
import type { NonEmptyArray } from "@metamask/utils";
|
|
6
6
|
import type { MethodHooksObject } from "../utils.cjs";
|
|
7
7
|
export declare const WALLET_SNAP_PERMISSION_KEY = "wallet_snap";
|
|
8
|
-
export type
|
|
9
|
-
type: `SnapController:
|
|
8
|
+
export type SnapControllerInstallSnapsAction = {
|
|
9
|
+
type: `SnapController:installSnaps`;
|
|
10
10
|
handler: (origin: string, requestedSnaps: RequestSnapsParams) => Promise<RequestSnapsResult>;
|
|
11
11
|
};
|
|
12
|
-
export type
|
|
13
|
-
type: `SnapController:
|
|
12
|
+
export type SnapControllerGetPermittedSnapsAction = {
|
|
13
|
+
type: `SnapController:getPermittedSnaps`;
|
|
14
14
|
handler: (origin: string) => RequestSnapsResult;
|
|
15
15
|
};
|
|
16
|
-
type AllowedActions =
|
|
16
|
+
type AllowedActions = SnapControllerInstallSnapsAction | SnapControllerGetPermittedSnapsAction;
|
|
17
17
|
export type InvokeSnapMethodHooks = {
|
|
18
18
|
handleSnapRpcRequest: ({ snapId, origin, handler, request, }: SnapRpcHookArgs & {
|
|
19
19
|
snapId: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"invokeSnap.d.cts","sourceRoot":"","sources":["../../src/restricted/invokeSnap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,8BAA8B,EAC9B,uBAAuB,EAEvB,6BAA6B,EAC7B,oBAAoB,EACrB,wCAAwC;AACzC,OAAO,EAAE,cAAc,EAAE,wCAAwC;AAEjE,OAAO,KAAK,EACV,gBAAgB,EAChB,gBAAgB,EAChB,kBAAkB,EAClB,kBAAkB,EACnB,4BAA4B;AAC7B,OAAO,KAAK,EAAE,eAAe,EAAE,8BAA8B;AAE7D,OAAO,KAAK,EAAQ,aAAa,EAAE,wBAAwB;AAE3D,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAiB;AAElD,eAAO,MAAM,0BAA0B,gBAAgB,CAAC;AAGxD,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"invokeSnap.d.cts","sourceRoot":"","sources":["../../src/restricted/invokeSnap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,8BAA8B,EAC9B,uBAAuB,EAEvB,6BAA6B,EAC7B,oBAAoB,EACrB,wCAAwC;AACzC,OAAO,EAAE,c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gBAAgB;oBACnC,iCAAiC;8BACvB,WAAW,kCAAkC,CAAC;wBACpD,SAAS,cAAc,MAAM,CAAC,CAAC,GAAG,IAAI;mBAC3C,6BAA6B;oBAC5B;YACV,WAAW,EAAE,qBAAqB,cAAc,EAAE,KAAK,CAAC,CAAC,aAAa,CAAC,CAAC;SACzE;;;EAwGQ,CAAC;AAEZ;;;;;;;GAOG;AACH,wBAAgB,2BAA2B,CAAC,EAC1C,oBAAoB,GACrB,EAAE,qBAAqB,aAEX,wBAAwB,gBAAgB,CAAC,KACjD,QAAQ,gBAAgB,CAAC,CAc7B"}
|
|
@@ -5,15 +5,15 @@ import type { SnapRpcHookArgs } from "@metamask/snaps-utils";
|
|
|
5
5
|
import type { NonEmptyArray } from "@metamask/utils";
|
|
6
6
|
import type { MethodHooksObject } from "../utils.mjs";
|
|
7
7
|
export declare const WALLET_SNAP_PERMISSION_KEY = "wallet_snap";
|
|
8
|
-
export type
|
|
9
|
-
type: `SnapController:
|
|
8
|
+
export type SnapControllerInstallSnapsAction = {
|
|
9
|
+
type: `SnapController:installSnaps`;
|
|
10
10
|
handler: (origin: string, requestedSnaps: RequestSnapsParams) => Promise<RequestSnapsResult>;
|
|
11
11
|
};
|
|
12
|
-
export type
|
|
13
|
-
type: `SnapController:
|
|
12
|
+
export type SnapControllerGetPermittedSnapsAction = {
|
|
13
|
+
type: `SnapController:getPermittedSnaps`;
|
|
14
14
|
handler: (origin: string) => RequestSnapsResult;
|
|
15
15
|
};
|
|
16
|
-
type AllowedActions =
|
|
16
|
+
type AllowedActions = SnapControllerInstallSnapsAction | SnapControllerGetPermittedSnapsAction;
|
|
17
17
|
export type InvokeSnapMethodHooks = {
|
|
18
18
|
handleSnapRpcRequest: ({ snapId, origin, handler, request, }: SnapRpcHookArgs & {
|
|
19
19
|
snapId: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"invokeSnap.d.mts","sourceRoot":"","sources":["../../src/restricted/invokeSnap.ts"],"names":[],"m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C;CACjD,CAAC;AAEF,KAAK,cAAc,GACf,gCAAgC,GAChC,qCAAqC,CAAC;AAE1C,MAAM,MAAM,qBAAqB,GAAG;IAClC,oBAAoB,EAAE,CAAC,EACrB,MAAM,EACN,MAAM,EACN,OAAO,EACP,OAAO,GACR,EAAE,eAAe,GAAG;QAAE,MAAM,EAAE,MAAM,CAAA;KAAE,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;CAC9D,CAAC;AAEF,KAAK,qCAAqC,GAAG;IAC3C,cAAc,CAAC,EAAE,QAAQ,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,GAAG,IAAI,CAAC;IACxD,WAAW,EAAE,qBAAqB,CAAC;CACpC,CAAC;AAaF;;;;;;;GAOG;AACH,eAAO,MAAM,iBAAiB,EAAE,oBAAoB,CAClD,cAAc,EACd,KAAK,CACN,CAAC,aAAa,CAwBd,CAAC;AAwCF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,iBAAiB;;;wBA3GZ,eAAe,gBAAgB;oBACnC,iCAAiC;8BACvB,WAAW,kCAAkC,CAAC;wBACpD,SAAS,cAAc,MAAM,CAAC,CAAC,GAAG,IAAI;mBAC3C,6BAA6B;oBAC5B;YACV,WAAW,EAAE,qBAAqB,cAAc,EAAE,KAAK,CAAC,CAAC,aAAa,CAAC,CAAC;SACzE;;;EAwGQ,CAAC;AAEZ;;;;;;;GAOG;AACH,wBAAgB,2BAA2B,CAAC,EAC1C,oBAAoB,GACrB,EAAE,qBAAqB,aAEX,wBAAwB,gBAAgB,CAAC,KACjD,QAAQ,gBAAgB,CAAC,CAc7B"}
|
|
@@ -13,14 +13,14 @@ export const WALLET_SNAP_PERMISSION_KEY = 'wallet_snap';
|
|
|
13
13
|
export const handleSnapInstall = async ({ requestData, messenger }) => {
|
|
14
14
|
const snaps = requestData.permissions[WALLET_SNAP_PERMISSION_KEY].caveats?.[0]
|
|
15
15
|
.value;
|
|
16
|
-
const permittedSnaps = messenger.call(`SnapController:
|
|
16
|
+
const permittedSnaps = messenger.call(`SnapController:getPermittedSnaps`, requestData.metadata.origin);
|
|
17
17
|
const dedupedSnaps = Object.keys(snaps).reduce((filteredSnaps, snap) => {
|
|
18
18
|
if (!permittedSnaps[snap]) {
|
|
19
19
|
filteredSnaps[snap] = snaps[snap];
|
|
20
20
|
}
|
|
21
21
|
return filteredSnaps;
|
|
22
22
|
}, {});
|
|
23
|
-
return messenger.call(`SnapController:
|
|
23
|
+
return messenger.call(`SnapController:installSnaps`, requestData.metadata.origin, dedupedSnaps);
|
|
24
24
|
};
|
|
25
25
|
/**
|
|
26
26
|
* The specification builder for the `wallet_snap_*` permission.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"invokeSnap.mjs","sourceRoot":"","sources":["../../src/restricted/invokeSnap.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,cAAc,EAAE,wCAAwC;AACjE,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAQjD,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,8BAA8B;AAKpE,MAAM,CAAC,MAAM,0BAA0B,GAAG,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"invokeSnap.mjs","sourceRoot":"","sources":["../../src/restricted/invokeSnap.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,cAAc,EAAE,wCAAwC;AACjE,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAQjD,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,8BAA8B;AAKpE,MAAM,CAAC,MAAM,0BAA0B,GAAG,aAAa,CAAC;AA6CxD;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAGX,KAAK,EAAE,EAAE,WAAW,EAAE,SAAS,EAAE,EAAE,EAAE;IACtD,MAAM,KAAK,GAAG,WAAW,CAAC,WAAW,CAAC,0BAA0B,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;SAC3E,KAA2B,CAAC;IAE/B,MAAM,cAAc,GAAG,SAAS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;;;;;;;;;;;;;;GAqBG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,CAAC;IAC7C,UAAU,EAAE,0BAA0B;IACtC,oBAAoB;IACpB,WAAW;CACH,CAAC,CAAC;AAEZ;;;;;;;GAOG;AACH,MAAM,UAAU,2BAA2B,CAAC,EAC1C,oBAAoB,GACE;IACtB,OAAO,KAAK,UAAU,UAAU,CAC9B,OAAkD;QAElD,MAAM,EAAE,MAAM,GAAG,EAAE,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;QAEzC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,MAA0B,CAAC;QAEvD,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAE3B,OAAO,CAAC,MAAM,oBAAoB,CAAC;YACjC,MAAM;YACN,MAAM;YACN,OAAO;YACP,OAAO,EAAE,WAAW,CAAC,YAAY;SAClC,CAAC,CAAS,CAAC;IACd,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type {\n PermissionSpecificationBuilder,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n PermissionValidatorConstraint,\n PermissionSideEffect,\n} from '@metamask/permission-controller';\nimport { PermissionType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n InvokeSnapParams,\n InvokeSnapResult,\n RequestSnapsParams,\n RequestSnapsResult,\n} from '@metamask/snaps-sdk';\nimport type { SnapRpcHookArgs } from '@metamask/snaps-utils';\nimport { HandlerType, SnapCaveatType } from '@metamask/snaps-utils';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\n\nexport const WALLET_SNAP_PERMISSION_KEY = 'wallet_snap';\n\n// Redeclare installSnaps action type to avoid circular dependencies\nexport type SnapControllerInstallSnapsAction = {\n type: `SnapController:installSnaps`;\n handler: (\n origin: string,\n requestedSnaps: RequestSnapsParams,\n ) => Promise<RequestSnapsResult>;\n};\n\nexport type SnapControllerGetPermittedSnapsAction = {\n type: `SnapController:getPermittedSnaps`;\n handler: (origin: string) => RequestSnapsResult;\n};\n\ntype AllowedActions =\n | SnapControllerInstallSnapsAction\n | SnapControllerGetPermittedSnapsAction;\n\nexport type InvokeSnapMethodHooks = {\n handleSnapRpcRequest: ({\n snapId,\n origin,\n handler,\n request,\n }: SnapRpcHookArgs & { snapId: string }) => Promise<unknown>;\n};\n\ntype InvokeSnapSpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: InvokeSnapMethodHooks;\n};\n\ntype InvokeSnapSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof WALLET_SNAP_PERMISSION_KEY;\n methodImplementation: ReturnType<typeof getInvokeSnapImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n sideEffect: {\n onPermitted: PermissionSideEffect<AllowedActions, never>['onPermitted'];\n };\n}>;\n\n/**\n * The side-effect method to handle the snap install.\n *\n * @param params - The side-effect params.\n * @param params.requestData - The request data associated to the requested permission.\n * @param params.messenger - The messenger to call an action.\n * @returns The result of the Snap installation.\n */\nexport const handleSnapInstall: PermissionSideEffect<\n AllowedActions,\n never\n>['onPermitted'] = async ({ requestData, messenger }) => {\n const snaps = requestData.permissions[WALLET_SNAP_PERMISSION_KEY].caveats?.[0]\n .value as RequestSnapsParams;\n\n const permittedSnaps = messenger.call(\n `SnapController:getPermittedSnaps`,\n requestData.metadata.origin,\n );\n\n const dedupedSnaps = Object.keys(snaps).reduce<RequestSnapsParams>(\n (filteredSnaps, snap) => {\n if (!permittedSnaps[snap]) {\n filteredSnaps[snap] = snaps[snap];\n }\n return filteredSnaps;\n },\n {},\n );\n\n return messenger.call(\n `SnapController:installSnaps`,\n requestData.metadata.origin,\n dedupedSnaps,\n );\n};\n/**\n * The specification builder for the `wallet_snap_*` permission.\n *\n * `wallet_snap_*` attempts to invoke an RPC method of the specified Snap.\n *\n * Requesting its corresponding permission will attempt to connect to the Snap,\n * and install it if it's not available yet.\n *\n * @param options - The specification builder options.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `wallet_snap_*` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n InvokeSnapSpecificationBuilderOptions,\n InvokeSnapSpecification\n> = ({ methodHooks }: InvokeSnapSpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName: WALLET_SNAP_PERMISSION_KEY,\n allowedCaveats: [SnapCaveatType.SnapIds],\n methodImplementation: getInvokeSnapImplementation(methodHooks),\n validator: ({ caveats }) => {\n if (caveats?.length !== 1 || caveats[0].type !== SnapCaveatType.SnapIds) {\n throw rpcErrors.invalidParams({\n message: `Expected a single \"${SnapCaveatType.SnapIds}\" caveat.`,\n });\n }\n },\n sideEffect: {\n onPermitted: handleSnapInstall,\n },\n };\n};\n\nconst methodHooks: MethodHooksObject<InvokeSnapMethodHooks> = {\n handleSnapRpcRequest: true,\n};\n\n/**\n * Calls the specified JSON-RPC API method of the specified Snap. The Snap\n * must be installed and the dapp must have permission to communicate with the\n * Snap, or the request is rejected. The dapp can install the Snap and request\n * permission to communicate with it using\n * [`wallet_requestSnaps`](http://docs.metamask.io/snaps/reference/snaps-api/wallet_requestsnaps).\n *\n * @example\n * ```ts\n * const result = await ethereum.request({\n * method: 'wallet_snap',\n * params: {\n * snapId: 'npm:my-snap',\n * request: {\n * method: 'hello',\n * params: { name: 'world' },\n * },\n * },\n * });\n * console.log(result); // \"Hello, world!\"\n * ```\n */\nexport const invokeSnapBuilder = Object.freeze({\n targetName: WALLET_SNAP_PERMISSION_KEY,\n specificationBuilder,\n methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `wallet_snap_*`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.handleSnapRpcRequest - A function that sends an RPC request to a snap's RPC handler or throws if that fails.\n * @returns The method implementation which returns the result of `handleSnapRpcRequest`.\n * @throws If the params are invalid.\n */\nexport function getInvokeSnapImplementation({\n handleSnapRpcRequest,\n}: InvokeSnapMethodHooks) {\n return async function invokeSnap(\n options: RestrictedMethodOptions<InvokeSnapParams>,\n ): Promise<InvokeSnapResult> {\n const { params = {}, context } = options;\n\n const { snapId, request } = params as InvokeSnapParams;\n\n const { origin } = context;\n\n return (await handleSnapRpcRequest({\n snapId,\n origin,\n request,\n handler: HandlerType.OnRpcRequest,\n })) as Json;\n };\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"notify.cjs","sourceRoot":"","sources":["../../src/restricted/notify.ts"],"names":[],"mappings":";;;AAKA,2EAA8E;AAC9E,qDAAiD;AAOjD,mDAO6B;AAC7B,uDAI+B;AAE/B,uDAAiE;AAEjE,2CAAwD;AAIxD,MAAM,UAAU,GAAG,aAAa,CAAC;AAEjC,MAAM,wBAAwB,GAAG,IAAA,oBAAM,EAAC;IACtC,IAAI,EAAE,IAAA,qBAAS,EAAC,4BAAgB,CAAC,MAAM,CAAC;IACxC,OAAO,EAAE,IAAA,oBAAM,GAAE;CAClB,CAAC,CAAC;AAEH,MAAM,uBAAuB,GAAG,IAAA,oBAAM,EAAC;IACrC,IAAI,EAAE,IAAA,qBAAS,EAAC,4BAAgB,CAAC,KAAK,CAAC;IACvC,OAAO,EAAE,IAAA,oBAAM,GAAE;CAClB,CAAC,CAAC;AAEH,MAAM,kCAAkC,GAAG,IAAA,oBAAM,EAAC;IAChD,IAAI,EAAE,IAAA,qBAAS,EAAC,4BAAgB,CAAC,KAAK,CAAC;IACvC,OAAO,EAAE,IAAA,oBAAM,GAAE;IACjB,OAAO,EAAE,oCAAwB;IACjC,KAAK,EAAE,IAAA,oBAAM,GAAE;IACf,UAAU,EAAE,IAAA,sBAAQ,EAClB,IAAA,oBAAM,EAAC;QACL,IAAI,EAAE,IAAA,oBAAM,GAAE;QACd,IAAI,EAAE,IAAA,oBAAM,GAAE;KACf,CAAC,CACH;C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G,CAAC,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,CAAC;IAC5D,+DAA+D;IAC/D,IACE,IAAI,KAAK,4BAAgB,CAAC,MAAM;QAChC,CAAC,WAAW,IAAI,OAAO,CAAC,MAAM,IAAI,EAAE,CAAC,EACrC,CAAC;QACD,MAAM,sBAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EACL,yEAAyE;SAC5E,CAAC,CAAC;IACL,CAAC;IAED,IACE,IAAI,KAAK,4BAAgB,CAAC,KAAK;QAC/B,CAAC,WAAW,IAAI,OAAO,CAAC,MAAM,IAAI,GAAG,CAAC,EACtC,CAAC;QACD,MAAM,sBAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EACL,0EAA0E;SAC7E,CAAC,CAAC;IACL,CAAC;IAED,IAAI,CAAC;QACH,MAAM,eAAe,GAAG,IAAA,yBAAW,EACjC,MAAM,EACN,4BAA4B,EAC5B,MAAM,CACP,CAAC;QAEF,IAAA,+BAAiB,EAAC,eAAe,CAAC,OAAO,EAAE,gBAAgB,EAAE,OAAO,CAAC,CAAC;QAEtE,IAAI,IAAA,mBAAW,EAAC,eAAe,EAAE,YAAY,CAAC,EAAE,CAAC;YAC/C,IAAA,0BAAY,EAAC,eAAe,CAAC,UAAU,CAAC,IAAI,EAAE,gBAAgB,EAAE,OAAO,CAAC,CAAC;QAC3E,CAAC;QAED,OAAO,eAAe,CAAC;IACzB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,sBAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EAAE,mBAAmB,IAAA,2BAAe,EAAC,KAAK,CAAC,EAAE;SACrD,CAAC,CAAC;IACL,CAAC;AACH,CAAC;AAhED,gDAgEC","sourcesContent":["import type {\n PermissionSpecificationBuilder,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n NotifyParams,\n NotifyResult,\n InterfaceContext,\n ComponentOrElement,\n} from '@metamask/snaps-sdk';\nimport {\n enumValue,\n NotificationType,\n union,\n ContentType,\n getErrorMessage,\n ComponentOrElementStruct,\n} from '@metamask/snaps-sdk';\nimport {\n createUnion,\n validateLink,\n validateTextLinks,\n} from '@metamask/snaps-utils';\nimport type { InferMatching, Snap } from '@metamask/snaps-utils';\nimport { object, string, optional } from '@metamask/superstruct';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { hasProperty, isObject } from '@metamask/utils';\n\nimport { type MethodHooksObject } from '../utils';\n\nconst methodName = 'snap_notify';\n\nconst NativeNotificationStruct = object({\n type: enumValue(NotificationType.Native),\n message: string(),\n});\n\nconst InAppNotificationStruct = object({\n type: enumValue(NotificationType.InApp),\n message: string(),\n});\n\nconst InAppNotificationWithDetailsStruct = object({\n type: enumValue(NotificationType.InApp),\n message: string(),\n content: ComponentOrElementStruct,\n title: string(),\n footerLink: optional(\n object({\n href: string(),\n text: string(),\n }),\n ),\n});\n\nconst NotificationParametersStruct = union([\n InAppNotificationStruct,\n InAppNotificationWithDetailsStruct,\n NativeNotificationStruct,\n]);\n\nexport type NotificationArgs = InferMatching<\n typeof NotificationParametersStruct,\n NotifyParams\n>;\n\nexport type NotifyMethodHooks = {\n /**\n * @param snapId - The ID of the Snap that created the notification.\n * @param args - The notification arguments.\n */\n showNativeNotification: (\n snapId: string,\n args: NotificationArgs,\n ) => Promise<null>;\n\n /**\n * @param snapId - The ID of the Snap that created the notification.\n * @param args - The notification arguments.\n */\n showInAppNotification: (\n snapId: string,\n args: NotificationArgs,\n ) => Promise<null>;\n\n isOnPhishingList: (url: string) => boolean;\n\n maybeUpdatePhishingList: () => Promise<void>;\n\n createInterface: (\n origin: string,\n content: ComponentOrElement,\n context?: InterfaceContext,\n contentType?: ContentType,\n ) => Promise<string>;\n getSnap: (snapId: string) => Snap | undefined;\n};\n\ntype SpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: NotifyMethodHooks;\n};\n\ntype Specification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof methodName;\n methodImplementation: ReturnType<typeof getImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * The specification builder for the `snap_notify` permission.\n * `snap_notify` allows snaps to send multiple types of notifications to its users.\n *\n * @param options - The specification builder options.\n * @param options.allowedCaveats - The optional allowed caveats for the permission.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `snap_notify` permission.\n */\nexport const specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n SpecificationBuilderOptions,\n Specification\n> = ({ allowedCaveats = null, methodHooks }: SpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName: methodName,\n allowedCaveats,\n methodImplementation: getImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<NotifyMethodHooks> = {\n showNativeNotification: true,\n showInAppNotification: true,\n isOnPhishingList: true,\n maybeUpdatePhishingList: true,\n createInterface: true,\n getSnap: true,\n};\n\n/**\n * Display a\n * [notification](https://docs.metamask.io/snaps/features/notifications/) in\n * MetaMask or natively in the OS. Snaps can trigger a short (up to 80\n * characters) notification message for actionable or time sensitive\n * information. `inApp` notifications can also include an optional\n * [expanded view](https://docs.metamask.io/snaps/features/notifications/#expanded-view).\n * The expanded view has a title, content, and optional footer link shown when\n * a user clicks on the notification.\n *\n * @example Basic in app notification\n * ```json name=\"Manifest\"\n * {\n * \"initialPermissions\": {\n * \"snap_notify\": {}\n * }\n * }\n * ```\n * ```ts name=\"Usage\"\n * snap.request({\n * method: 'snap_notify',\n * params: {\n * type: 'inApp',\n * message: 'This is an in-app notification',\n * },\n * });\n * ```\n *\n * @example Expandable in app notification\n * ```json name=\"Manifest\"\n * {\n * \"initialPermissions\": {\n * \"snap_notify\": {}\n * }\n * }\n * ```\n * ```ts name=\"Usage\"\n * snap.request({\n * method: 'snap_notify',\n * params: {\n * type: 'inApp',\n * message: 'This is an in-app notification',\n * title: 'Notification Title',\n * content: (\n * <Box>\n * <Text>This is the expanded content of the notification.</Text>\n * </Box>\n * ),\n * footerLink: {\n * href: 'https://example.com',\n * text: 'Click here for more info',\n * },\n * },\n * });\n * ```\n *\n * @example Native notification\n * ```json name=\"Manifest\"\n * {\n * \"initialPermissions\": {\n * \"snap_notify\": {}\n * }\n * }\n * ```\n * ```ts name=\"Usage\"\n * snap.request({\n * method: 'snap_notify',\n * params: {\n * type: 'native',\n * message: 'This is a native notification',\n * },\n * });\n * ```\n */\nexport const notifyBuilder = Object.freeze({\n targetName: methodName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `snap_notify`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.showNativeNotification - A function that shows a native browser notification.\n * @param hooks.showInAppNotification - A function that shows a notification in the MetaMask UI.\n * @param hooks.isOnPhishingList - A function that checks for links against the phishing list.\n * @param hooks.maybeUpdatePhishingList - A function that updates the phishing list if needed.\n * @param hooks.createInterface - A function that creates the interface in SnapInterfaceController.\n * @param hooks.getSnap - A function that checks if a snap is installed.\n * @returns The method implementation which returns `null` on success.\n * @throws If the params are invalid.\n */\nexport function getImplementation({\n showNativeNotification,\n showInAppNotification,\n isOnPhishingList,\n maybeUpdatePhishingList,\n createInterface,\n getSnap,\n}: NotifyMethodHooks) {\n return async function implementation(\n args: RestrictedMethodOptions<NotifyParams>,\n ): Promise<NotifyResult> {\n const {\n params,\n context: { origin },\n } = args;\n\n await maybeUpdatePhishingList();\n\n const validatedParams = getValidatedParams(\n params,\n isOnPhishingList,\n getSnap,\n );\n\n if (hasProperty(validatedParams, 'content')) {\n const id = await createInterface(\n origin,\n validatedParams.content as ComponentOrElement,\n undefined,\n ContentType.Notification,\n );\n validatedParams.content = id;\n }\n\n switch (validatedParams.type) {\n case NotificationType.Native:\n return await showNativeNotification(origin, validatedParams);\n case NotificationType.InApp:\n return await showInAppNotification(origin, validatedParams);\n default:\n throw rpcErrors.invalidParams({\n message: 'Must specify a valid notification \"type\".',\n });\n }\n };\n}\n\n/**\n * Validates the notify 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 * @param isOnPhishingList - The function that checks for links against the phishing list.\n * @param getSnap - A function that checks if a snap is installed.\n * @returns The validated method parameter object.\n * @throws If the params are invalid.\n */\nexport function getValidatedParams(\n params: unknown,\n isOnPhishingList: NotifyMethodHooks['isOnPhishingList'],\n getSnap: NotifyMethodHooks['getSnap'],\n): NotifyParams {\n if (!isObject(params)) {\n throw rpcErrors.invalidParams({\n message: 'Expected params to be a single object.',\n });\n }\n\n const { type, message } = params;\n\n if (\n !type ||\n typeof type !== 'string' ||\n !Object.values(NotificationType).includes(type as NotificationType)\n ) {\n throw rpcErrors.invalidParams({\n message: 'Must specify a valid notification \"type\".',\n });\n }\n\n const isNotString = !message || typeof message !== 'string';\n // Set to the max message length on a Mac notification for now.\n if (\n type === NotificationType.Native &&\n (isNotString || message.length >= 50)\n ) {\n throw rpcErrors.invalidParams({\n message:\n 'Must specify a non-empty string \"message\" less than 50 characters long.',\n });\n }\n\n if (\n type === NotificationType.InApp &&\n (isNotString || message.length >= 500)\n ) {\n throw rpcErrors.invalidParams({\n message:\n 'Must specify a non-empty string \"message\" less than 500 characters long.',\n });\n }\n\n try {\n const validatedParams = createUnion(\n params,\n NotificationParametersStruct,\n 'type',\n );\n\n validateTextLinks(validatedParams.message, isOnPhishingList, getSnap);\n\n if (hasProperty(validatedParams, 'footerLink')) {\n validateLink(validatedParams.footerLink.href, isOnPhishingList, getSnap);\n }\n\n return validatedParams;\n } catch (error) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${getErrorMessage(error)}`,\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"notify.cjs","sourceRoot":"","sources":["../../src/restricted/notify.ts"],"names":[],"mappings":";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AC,aAAa,CAAC;YAC5B,OAAO,EACL,0EAA0E;SAC7E,CAAC,CAAC;IACL,CAAC;IAED,IAAI,CAAC;QACH,MAAM,eAAe,GAAG,IAAA,yBAAW,EACjC,MAAM,EACN,4BAA4B,EAC5B,MAAM,CACP,CAAC;QAEF,IAAA,+BAAiB,EAAC,eAAe,CAAC,OAAO,EAAE,gBAAgB,EAAE,OAAO,CAAC,CAAC;QAEtE,IAAI,IAAA,mBAAW,EAAC,eAAe,EAAE,YAAY,CAAC,EAAE,CAAC;YAC/C,IAAA,0BAAY,EAAC,eAAe,CAAC,UAAU,CAAC,IAAI,EAAE,gBAAgB,EAAE,OAAO,CAAC,CAAC;QAC3E,CAAC;QAED,OAAO,eAAe,CAAC;IACzB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,sBAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EAAE,mBAAmB,IAAA,2BAAe,EAAC,KAAK,CAAC,EAAE;SACrD,CAAC,CAAC;IACL,CAAC;AACH,CAAC;AAhED,gDAgEC","sourcesContent":["import type {\n PermissionSpecificationBuilder,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n NotifyParams,\n NotifyResult,\n InterfaceContext,\n ComponentOrElement,\n} from '@metamask/snaps-sdk';\nimport {\n enumValue,\n NotificationType,\n union,\n ContentType,\n getErrorMessage,\n ComponentOrElementStruct,\n} from '@metamask/snaps-sdk';\nimport {\n createUnion,\n validateLink,\n validateTextLinks,\n} from '@metamask/snaps-utils';\nimport type { InferMatching, Snap } from '@metamask/snaps-utils';\nimport { object, string, optional } from '@metamask/superstruct';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { hasProperty, isObject } from '@metamask/utils';\n\nimport { type MethodHooksObject } from '../utils';\n\nconst methodName = 'snap_notify';\n\nconst NativeNotificationStruct = object({\n type: enumValue(NotificationType.Native),\n message: string(),\n});\n\nconst InAppNotificationStruct = object({\n type: enumValue(NotificationType.InApp),\n message: string(),\n});\n\nconst InAppNotificationWithDetailsStruct = object({\n type: enumValue(NotificationType.InApp),\n message: string(),\n content: ComponentOrElementStruct,\n title: string(),\n footerLink: optional(\n object({\n href: string(),\n text: string(),\n }),\n ),\n});\n\nconst NotificationParametersStruct = union([\n InAppNotificationStruct,\n InAppNotificationWithDetailsStruct,\n NativeNotificationStruct,\n]);\n\nexport type NotificationArgs = InferMatching<\n typeof NotificationParametersStruct,\n NotifyParams\n>;\n\nexport type NotifyMethodHooks = {\n /**\n * @param snapId - The ID of the Snap that created the notification.\n * @param args - The notification arguments.\n */\n showNativeNotification: (\n snapId: string,\n args: NotificationArgs,\n ) => Promise<null>;\n\n /**\n * @param snapId - The ID of the Snap that created the notification.\n * @param args - The notification arguments.\n */\n showInAppNotification: (\n snapId: string,\n args: NotificationArgs,\n ) => Promise<null>;\n\n isOnPhishingList: (url: string) => boolean;\n\n maybeUpdatePhishingList: () => Promise<void>;\n\n createInterface: (\n origin: string,\n content: ComponentOrElement,\n context?: InterfaceContext,\n contentType?: ContentType,\n ) => Promise<string>;\n getSnap: (snapId: string) => Snap | null;\n};\n\ntype SpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: NotifyMethodHooks;\n};\n\ntype Specification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof methodName;\n methodImplementation: ReturnType<typeof getImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * The specification builder for the `snap_notify` permission.\n * `snap_notify` allows snaps to send multiple types of notifications to its users.\n *\n * @param options - The specification builder options.\n * @param options.allowedCaveats - The optional allowed caveats for the permission.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `snap_notify` permission.\n */\nexport const specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n SpecificationBuilderOptions,\n Specification\n> = ({ allowedCaveats = null, methodHooks }: SpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName: methodName,\n allowedCaveats,\n methodImplementation: getImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<NotifyMethodHooks> = {\n showNativeNotification: true,\n showInAppNotification: true,\n isOnPhishingList: true,\n maybeUpdatePhishingList: true,\n createInterface: true,\n getSnap: true,\n};\n\n/**\n * Display a\n * [notification](https://docs.metamask.io/snaps/features/notifications/) in\n * MetaMask or natively in the OS. Snaps can trigger a short (up to 80\n * characters) notification message for actionable or time sensitive\n * information. `inApp` notifications can also include an optional\n * [expanded view](https://docs.metamask.io/snaps/features/notifications/#expanded-view).\n * The expanded view has a title, content, and optional footer link shown when\n * a user clicks on the notification.\n *\n * @example Basic in app notification\n * ```json name=\"Manifest\"\n * {\n * \"initialPermissions\": {\n * \"snap_notify\": {}\n * }\n * }\n * ```\n * ```ts name=\"Usage\"\n * snap.request({\n * method: 'snap_notify',\n * params: {\n * type: 'inApp',\n * message: 'This is an in-app notification',\n * },\n * });\n * ```\n *\n * @example Expandable in app notification\n * ```json name=\"Manifest\"\n * {\n * \"initialPermissions\": {\n * \"snap_notify\": {}\n * }\n * }\n * ```\n * ```ts name=\"Usage\"\n * snap.request({\n * method: 'snap_notify',\n * params: {\n * type: 'inApp',\n * message: 'This is an in-app notification',\n * title: 'Notification Title',\n * content: (\n * <Box>\n * <Text>This is the expanded content of the notification.</Text>\n * </Box>\n * ),\n * footerLink: {\n * href: 'https://example.com',\n * text: 'Click here for more info',\n * },\n * },\n * });\n * ```\n *\n * @example Native notification\n * ```json name=\"Manifest\"\n * {\n * \"initialPermissions\": {\n * \"snap_notify\": {}\n * }\n * }\n * ```\n * ```ts name=\"Usage\"\n * snap.request({\n * method: 'snap_notify',\n * params: {\n * type: 'native',\n * message: 'This is a native notification',\n * },\n * });\n * ```\n */\nexport const notifyBuilder = Object.freeze({\n targetName: methodName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `snap_notify`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.showNativeNotification - A function that shows a native browser notification.\n * @param hooks.showInAppNotification - A function that shows a notification in the MetaMask UI.\n * @param hooks.isOnPhishingList - A function that checks for links against the phishing list.\n * @param hooks.maybeUpdatePhishingList - A function that updates the phishing list if needed.\n * @param hooks.createInterface - A function that creates the interface in SnapInterfaceController.\n * @param hooks.getSnap - A function that checks if a snap is installed.\n * @returns The method implementation which returns `null` on success.\n * @throws If the params are invalid.\n */\nexport function getImplementation({\n showNativeNotification,\n showInAppNotification,\n isOnPhishingList,\n maybeUpdatePhishingList,\n createInterface,\n getSnap,\n}: NotifyMethodHooks) {\n return async function implementation(\n args: RestrictedMethodOptions<NotifyParams>,\n ): Promise<NotifyResult> {\n const {\n params,\n context: { origin },\n } = args;\n\n await maybeUpdatePhishingList();\n\n const validatedParams = getValidatedParams(\n params,\n isOnPhishingList,\n getSnap,\n );\n\n if (hasProperty(validatedParams, 'content')) {\n const id = await createInterface(\n origin,\n validatedParams.content as ComponentOrElement,\n undefined,\n ContentType.Notification,\n );\n validatedParams.content = id;\n }\n\n switch (validatedParams.type) {\n case NotificationType.Native:\n return await showNativeNotification(origin, validatedParams);\n case NotificationType.InApp:\n return await showInAppNotification(origin, validatedParams);\n default:\n throw rpcErrors.invalidParams({\n message: 'Must specify a valid notification \"type\".',\n });\n }\n };\n}\n\n/**\n * Validates the notify 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 * @param isOnPhishingList - The function that checks for links against the phishing list.\n * @param getSnap - A function that checks if a snap is installed.\n * @returns The validated method parameter object.\n * @throws If the params are invalid.\n */\nexport function getValidatedParams(\n params: unknown,\n isOnPhishingList: NotifyMethodHooks['isOnPhishingList'],\n getSnap: NotifyMethodHooks['getSnap'],\n): NotifyParams {\n if (!isObject(params)) {\n throw rpcErrors.invalidParams({\n message: 'Expected params to be a single object.',\n });\n }\n\n const { type, message } = params;\n\n if (\n !type ||\n typeof type !== 'string' ||\n !Object.values(NotificationType).includes(type as NotificationType)\n ) {\n throw rpcErrors.invalidParams({\n message: 'Must specify a valid notification \"type\".',\n });\n }\n\n const isNotString = !message || typeof message !== 'string';\n // Set to the max message length on a Mac notification for now.\n if (\n type === NotificationType.Native &&\n (isNotString || message.length >= 50)\n ) {\n throw rpcErrors.invalidParams({\n message:\n 'Must specify a non-empty string \"message\" less than 50 characters long.',\n });\n }\n\n if (\n type === NotificationType.InApp &&\n (isNotString || message.length >= 500)\n ) {\n throw rpcErrors.invalidParams({\n message:\n 'Must specify a non-empty string \"message\" less than 500 characters long.',\n });\n }\n\n try {\n const validatedParams = createUnion(\n params,\n NotificationParametersStruct,\n 'type',\n );\n\n validateTextLinks(validatedParams.message, isOnPhishingList, getSnap);\n\n if (hasProperty(validatedParams, 'footerLink')) {\n validateLink(validatedParams.footerLink.href, isOnPhishingList, getSnap);\n }\n\n return validatedParams;\n } catch (error) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${getErrorMessage(error)}`,\n });\n }\n}\n"]}
|
|
@@ -328,7 +328,7 @@ export type NotifyMethodHooks = {
|
|
|
328
328
|
isOnPhishingList: (url: string) => boolean;
|
|
329
329
|
maybeUpdatePhishingList: () => Promise<void>;
|
|
330
330
|
createInterface: (origin: string, content: ComponentOrElement, context?: InterfaceContext, contentType?: ContentType) => Promise<string>;
|
|
331
|
-
getSnap: (snapId: string) => Snap |
|
|
331
|
+
getSnap: (snapId: string) => Snap | null;
|
|
332
332
|
};
|
|
333
333
|
type SpecificationBuilderOptions = {
|
|
334
334
|
allowedCaveats?: Readonly<NonEmptyArray<string>> | null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"notify.d.cts","sourceRoot":"","sources":["../../src/restricted/notify.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,8BAA8B,EA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,2BAA2B,EAC3B,aAAa,CASd,CAAC;AAWF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyEG;AACH,eAAO,MAAM,aAAa;;;wBAhHR,eAAe,gBAAgB;oBACnC,iBAAiB;8BACP,WAAW,wBAAwB,CAAC;wBAC1C,SAAS,cAAc,MAAM,CAAC,CAAC,GAAG,IAAI;;;EAiH7C,CAAC;AAEZ;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAAC,EAChC,sBAAsB,EACtB,qBAAqB,EACrB,gBAAgB,EAChB,uBAAuB,EACvB,eAAe,EACf,OAAO,GACR,EAAE,iBAAiB,UAEV,wBAAwB,YAAY,CAAC,KAC1C,QAAQ,YAAY,CAAC,CAmCzB;AAED;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,OAAO,EACf,gBAAgB,EAAE,iBAAiB,CAAC,kBAAkB,CAAC,EACvD,OAAO,EAAE,iBAAiB,CAAC,SAAS,CAAC,GACpC,YAAY,CA4Dd"}
|
|
@@ -328,7 +328,7 @@ export type NotifyMethodHooks = {
|
|
|
328
328
|
isOnPhishingList: (url: string) => boolean;
|
|
329
329
|
maybeUpdatePhishingList: () => Promise<void>;
|
|
330
330
|
createInterface: (origin: string, content: ComponentOrElement, context?: InterfaceContext, contentType?: ContentType) => Promise<string>;
|
|
331
|
-
getSnap: (snapId: string) => Snap |
|
|
331
|
+
getSnap: (snapId: string) => Snap | null;
|
|
332
332
|
};
|
|
333
333
|
type SpecificationBuilderOptions = {
|
|
334
334
|
allowedCaveats?: Readonly<NonEmptyArray<string>> | null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"notify.d.mts","sourceRoot":"","sources":["../../src/restricted/notify.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,8BAA8B,EAC9B,uBAAuB,EACvB,4BAA4B,EAC7B,wCAAwC;AACzC,O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AAO,CAAC,CAAC;QAC3E,CAAC;QAED,OAAO,eAAe,CAAC;IACzB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,SAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EAAE,mBAAmB,eAAe,CAAC,KAAK,CAAC,EAAE;SACrD,CAAC,CAAC;IACL,CAAC;AACH,CAAC","sourcesContent":["import type {\n PermissionSpecificationBuilder,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n NotifyParams,\n NotifyResult,\n InterfaceContext,\n ComponentOrElement,\n} from '@metamask/snaps-sdk';\nimport {\n enumValue,\n NotificationType,\n union,\n ContentType,\n getErrorMessage,\n ComponentOrElementStruct,\n} from '@metamask/snaps-sdk';\nimport {\n createUnion,\n validateLink,\n validateTextLinks,\n} from '@metamask/snaps-utils';\nimport type { InferMatching, Snap } from '@metamask/snaps-utils';\nimport { object, string, optional } from '@metamask/superstruct';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { hasProperty, isObject } from '@metamask/utils';\n\nimport { type MethodHooksObject } from '../utils';\n\nconst methodName = 'snap_notify';\n\nconst NativeNotificationStruct = object({\n type: enumValue(NotificationType.Native),\n message: string(),\n});\n\nconst InAppNotificationStruct = object({\n type: enumValue(NotificationType.InApp),\n message: string(),\n});\n\nconst InAppNotificationWithDetailsStruct = object({\n type: enumValue(NotificationType.InApp),\n message: string(),\n content: ComponentOrElementStruct,\n title: string(),\n footerLink: optional(\n object({\n href: string(),\n text: string(),\n }),\n ),\n});\n\nconst NotificationParametersStruct = union([\n InAppNotificationStruct,\n InAppNotificationWithDetailsStruct,\n NativeNotificationStruct,\n]);\n\nexport type NotificationArgs = InferMatching<\n typeof NotificationParametersStruct,\n NotifyParams\n>;\n\nexport type NotifyMethodHooks = {\n /**\n * @param snapId - The ID of the Snap that created the notification.\n * @param args - The notification arguments.\n */\n showNativeNotification: (\n snapId: string,\n args: NotificationArgs,\n ) => Promise<null>;\n\n /**\n * @param snapId - The ID of the Snap that created the notification.\n * @param args - The notification arguments.\n */\n showInAppNotification: (\n snapId: string,\n args: NotificationArgs,\n ) => Promise<null>;\n\n isOnPhishingList: (url: string) => boolean;\n\n maybeUpdatePhishingList: () => Promise<void>;\n\n createInterface: (\n origin: string,\n content: ComponentOrElement,\n context?: InterfaceContext,\n contentType?: ContentType,\n ) => Promise<string>;\n getSnap: (snapId: string) => Snap | undefined;\n};\n\ntype SpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: NotifyMethodHooks;\n};\n\ntype Specification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof methodName;\n methodImplementation: ReturnType<typeof getImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * The specification builder for the `snap_notify` permission.\n * `snap_notify` allows snaps to send multiple types of notifications to its users.\n *\n * @param options - The specification builder options.\n * @param options.allowedCaveats - The optional allowed caveats for the permission.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `snap_notify` permission.\n */\nexport const specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n SpecificationBuilderOptions,\n Specification\n> = ({ allowedCaveats = null, methodHooks }: SpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName: methodName,\n allowedCaveats,\n methodImplementation: getImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<NotifyMethodHooks> = {\n showNativeNotification: true,\n showInAppNotification: true,\n isOnPhishingList: true,\n maybeUpdatePhishingList: true,\n createInterface: true,\n getSnap: true,\n};\n\n/**\n * Display a\n * [notification](https://docs.metamask.io/snaps/features/notifications/) in\n * MetaMask or natively in the OS. Snaps can trigger a short (up to 80\n * characters) notification message for actionable or time sensitive\n * information. `inApp` notifications can also include an optional\n * [expanded view](https://docs.metamask.io/snaps/features/notifications/#expanded-view).\n * The expanded view has a title, content, and optional footer link shown when\n * a user clicks on the notification.\n *\n * @example Basic in app notification\n * ```json name=\"Manifest\"\n * {\n * \"initialPermissions\": {\n * \"snap_notify\": {}\n * }\n * }\n * ```\n * ```ts name=\"Usage\"\n * snap.request({\n * method: 'snap_notify',\n * params: {\n * type: 'inApp',\n * message: 'This is an in-app notification',\n * },\n * });\n * ```\n *\n * @example Expandable in app notification\n * ```json name=\"Manifest\"\n * {\n * \"initialPermissions\": {\n * \"snap_notify\": {}\n * }\n * }\n * ```\n * ```ts name=\"Usage\"\n * snap.request({\n * method: 'snap_notify',\n * params: {\n * type: 'inApp',\n * message: 'This is an in-app notification',\n * title: 'Notification Title',\n * content: (\n * <Box>\n * <Text>This is the expanded content of the notification.</Text>\n * </Box>\n * ),\n * footerLink: {\n * href: 'https://example.com',\n * text: 'Click here for more info',\n * },\n * },\n * });\n * ```\n *\n * @example Native notification\n * ```json name=\"Manifest\"\n * {\n * \"initialPermissions\": {\n * \"snap_notify\": {}\n * }\n * }\n * ```\n * ```ts name=\"Usage\"\n * snap.request({\n * method: 'snap_notify',\n * params: {\n * type: 'native',\n * message: 'This is a native notification',\n * },\n * });\n * ```\n */\nexport const notifyBuilder = Object.freeze({\n targetName: methodName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `snap_notify`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.showNativeNotification - A function that shows a native browser notification.\n * @param hooks.showInAppNotification - A function that shows a notification in the MetaMask UI.\n * @param hooks.isOnPhishingList - A function that checks for links against the phishing list.\n * @param hooks.maybeUpdatePhishingList - A function that updates the phishing list if needed.\n * @param hooks.createInterface - A function that creates the interface in SnapInterfaceController.\n * @param hooks.getSnap - A function that checks if a snap is installed.\n * @returns The method implementation which returns `null` on success.\n * @throws If the params are invalid.\n */\nexport function getImplementation({\n showNativeNotification,\n showInAppNotification,\n isOnPhishingList,\n maybeUpdatePhishingList,\n createInterface,\n getSnap,\n}: NotifyMethodHooks) {\n return async function implementation(\n args: RestrictedMethodOptions<NotifyParams>,\n ): Promise<NotifyResult> {\n const {\n params,\n context: { origin },\n } = args;\n\n await maybeUpdatePhishingList();\n\n const validatedParams = getValidatedParams(\n params,\n isOnPhishingList,\n getSnap,\n );\n\n if (hasProperty(validatedParams, 'content')) {\n const id = await createInterface(\n origin,\n validatedParams.content as ComponentOrElement,\n undefined,\n ContentType.Notification,\n );\n validatedParams.content = id;\n }\n\n switch (validatedParams.type) {\n case NotificationType.Native:\n return await showNativeNotification(origin, validatedParams);\n case NotificationType.InApp:\n return await showInAppNotification(origin, validatedParams);\n default:\n throw rpcErrors.invalidParams({\n message: 'Must specify a valid notification \"type\".',\n });\n }\n };\n}\n\n/**\n * Validates the notify 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 * @param isOnPhishingList - The function that checks for links against the phishing list.\n * @param getSnap - A function that checks if a snap is installed.\n * @returns The validated method parameter object.\n * @throws If the params are invalid.\n */\nexport function getValidatedParams(\n params: unknown,\n isOnPhishingList: NotifyMethodHooks['isOnPhishingList'],\n getSnap: NotifyMethodHooks['getSnap'],\n): NotifyParams {\n if (!isObject(params)) {\n throw rpcErrors.invalidParams({\n message: 'Expected params to be a single object.',\n });\n }\n\n const { type, message } = params;\n\n if (\n !type ||\n typeof type !== 'string' ||\n !Object.values(NotificationType).includes(type as NotificationType)\n ) {\n throw rpcErrors.invalidParams({\n message: 'Must specify a valid notification \"type\".',\n });\n }\n\n const isNotString = !message || typeof message !== 'string';\n // Set to the max message length on a Mac notification for now.\n if (\n type === NotificationType.Native &&\n (isNotString || message.length >= 50)\n ) {\n throw rpcErrors.invalidParams({\n message:\n 'Must specify a non-empty string \"message\" less than 50 characters long.',\n });\n }\n\n if (\n type === NotificationType.InApp &&\n (isNotString || message.length >= 500)\n ) {\n throw rpcErrors.invalidParams({\n message:\n 'Must specify a non-empty string \"message\" less than 500 characters long.',\n });\n }\n\n try {\n const validatedParams = createUnion(\n params,\n NotificationParametersStruct,\n 'type',\n );\n\n validateTextLinks(validatedParams.message, isOnPhishingList, getSnap);\n\n if (hasProperty(validatedParams, 'footerLink')) {\n validateLink(validatedParams.footerLink.href, isOnPhishingList, getSnap);\n }\n\n return validatedParams;\n } catch (error) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${getErrorMessage(error)}`,\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"notify.mjs","sourceRoot":"","sources":["../../src/restricted/notify.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,wCAAwC;AAC9E,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAOjD,OAAO,EACL,SAAS,EACT,gBAAgB,EAChB,KAAK,EACL,WAAW,EACX,eAAe,EACf,wBAAwB,EACzB,4BAA4B;AAC7B,OAAO,EACL,WAAW,EACX,YAAY,EACZ,iBAAiB,EAClB,8BAA8B;AAE/B,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,8BAA8B;AAEjE,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,wBAAwB;AAIxD,MAAM,UAAU,GAAG,aAAa,CAAC;AAEjC,MAAM,wBAAwB,GAAG,MAAM,CAAC;IACtC,IAAI,EAAE,SAAS,CAAC,gBAAgB,CAAC,MAAM,CAAC;IACxC,OAAO,EAAE,MAAM,EAAE;CAClB,CAAC,CAAC;AAEH,MAAM,uBAAuB,GAAG,MAAM,CAAC;IACrC,IAAI,EAAE,SAAS,CAAC,gBAAgB,CAAC,KAAK,CAAC;IACvC,OAAO,EAAE,MAAM,EAAE;CAClB,CAAC,CAAC;AAEH,MAAM,kCAAkC,GAAG,MAAM,CAAC;IAChD,IAAI,EAAE,SAAS,CAAC,gBAAgB,CAAC,KAAK,CAAC;IACvC,OAAO,EAAE,MAAM,EAAE;IACjB,OAAO,EAAE,w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AAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EAAE,2CAA2C;SACrD,CAAC,CAAC;IACL,CAAC;IAED,MAAM,WAAW,GAAG,CAAC,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,CAAC;IAC5D,+DAA+D;IAC/D,IACE,IAAI,KAAK,gBAAgB,CAAC,MAAM;QAChC,CAAC,WAAW,IAAI,OAAO,CAAC,MAAM,IAAI,EAAE,CAAC,EACrC,CAAC;QACD,MAAM,SAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EACL,yEAAyE;SAC5E,CAAC,CAAC;IACL,CAAC;IAED,IACE,IAAI,KAAK,gBAAgB,CAAC,KAAK;QAC/B,CAAC,WAAW,IAAI,OAAO,CAAC,MAAM,IAAI,GAAG,CAAC,EACtC,CAAC;QACD,MAAM,SAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EACL,0EAA0E;SAC7E,CAAC,CAAC;IACL,CAAC;IAED,IAAI,CAAC;QACH,MAAM,eAAe,GAAG,WAAW,CACjC,MAAM,EACN,4BAA4B,EAC5B,MAAM,CACP,CAAC;QAEF,iBAAiB,CAAC,eAAe,CAAC,OAAO,EAAE,gBAAgB,EAAE,OAAO,CAAC,CAAC;QAEtE,IAAI,WAAW,CAAC,eAAe,EAAE,YAAY,CAAC,EAAE,CAAC;YAC/C,YAAY,CAAC,eAAe,CAAC,UAAU,CAAC,IAAI,EAAE,gBAAgB,EAAE,OAAO,CAAC,CAAC;QAC3E,CAAC;QAED,OAAO,eAAe,CAAC;IACzB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,SAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EAAE,mBAAmB,eAAe,CAAC,KAAK,CAAC,EAAE;SACrD,CAAC,CAAC;IACL,CAAC;AACH,CAAC","sourcesContent":["import type {\n PermissionSpecificationBuilder,\n RestrictedMethodOptions,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n NotifyParams,\n NotifyResult,\n InterfaceContext,\n ComponentOrElement,\n} from '@metamask/snaps-sdk';\nimport {\n enumValue,\n NotificationType,\n union,\n ContentType,\n getErrorMessage,\n ComponentOrElementStruct,\n} from '@metamask/snaps-sdk';\nimport {\n createUnion,\n validateLink,\n validateTextLinks,\n} from '@metamask/snaps-utils';\nimport type { InferMatching, Snap } from '@metamask/snaps-utils';\nimport { object, string, optional } from '@metamask/superstruct';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { hasProperty, isObject } from '@metamask/utils';\n\nimport { type MethodHooksObject } from '../utils';\n\nconst methodName = 'snap_notify';\n\nconst NativeNotificationStruct = object({\n type: enumValue(NotificationType.Native),\n message: string(),\n});\n\nconst InAppNotificationStruct = object({\n type: enumValue(NotificationType.InApp),\n message: string(),\n});\n\nconst InAppNotificationWithDetailsStruct = object({\n type: enumValue(NotificationType.InApp),\n message: string(),\n content: ComponentOrElementStruct,\n title: string(),\n footerLink: optional(\n object({\n href: string(),\n text: string(),\n }),\n ),\n});\n\nconst NotificationParametersStruct = union([\n InAppNotificationStruct,\n InAppNotificationWithDetailsStruct,\n NativeNotificationStruct,\n]);\n\nexport type NotificationArgs = InferMatching<\n typeof NotificationParametersStruct,\n NotifyParams\n>;\n\nexport type NotifyMethodHooks = {\n /**\n * @param snapId - The ID of the Snap that created the notification.\n * @param args - The notification arguments.\n */\n showNativeNotification: (\n snapId: string,\n args: NotificationArgs,\n ) => Promise<null>;\n\n /**\n * @param snapId - The ID of the Snap that created the notification.\n * @param args - The notification arguments.\n */\n showInAppNotification: (\n snapId: string,\n args: NotificationArgs,\n ) => Promise<null>;\n\n isOnPhishingList: (url: string) => boolean;\n\n maybeUpdatePhishingList: () => Promise<void>;\n\n createInterface: (\n origin: string,\n content: ComponentOrElement,\n context?: InterfaceContext,\n contentType?: ContentType,\n ) => Promise<string>;\n getSnap: (snapId: string) => Snap | null;\n};\n\ntype SpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: NotifyMethodHooks;\n};\n\ntype Specification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof methodName;\n methodImplementation: ReturnType<typeof getImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * The specification builder for the `snap_notify` permission.\n * `snap_notify` allows snaps to send multiple types of notifications to its users.\n *\n * @param options - The specification builder options.\n * @param options.allowedCaveats - The optional allowed caveats for the permission.\n * @param options.methodHooks - The RPC method hooks needed by the method implementation.\n * @returns The specification for the `snap_notify` permission.\n */\nexport const specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n SpecificationBuilderOptions,\n Specification\n> = ({ allowedCaveats = null, methodHooks }: SpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName: methodName,\n allowedCaveats,\n methodImplementation: getImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<NotifyMethodHooks> = {\n showNativeNotification: true,\n showInAppNotification: true,\n isOnPhishingList: true,\n maybeUpdatePhishingList: true,\n createInterface: true,\n getSnap: true,\n};\n\n/**\n * Display a\n * [notification](https://docs.metamask.io/snaps/features/notifications/) in\n * MetaMask or natively in the OS. Snaps can trigger a short (up to 80\n * characters) notification message for actionable or time sensitive\n * information. `inApp` notifications can also include an optional\n * [expanded view](https://docs.metamask.io/snaps/features/notifications/#expanded-view).\n * The expanded view has a title, content, and optional footer link shown when\n * a user clicks on the notification.\n *\n * @example Basic in app notification\n * ```json name=\"Manifest\"\n * {\n * \"initialPermissions\": {\n * \"snap_notify\": {}\n * }\n * }\n * ```\n * ```ts name=\"Usage\"\n * snap.request({\n * method: 'snap_notify',\n * params: {\n * type: 'inApp',\n * message: 'This is an in-app notification',\n * },\n * });\n * ```\n *\n * @example Expandable in app notification\n * ```json name=\"Manifest\"\n * {\n * \"initialPermissions\": {\n * \"snap_notify\": {}\n * }\n * }\n * ```\n * ```ts name=\"Usage\"\n * snap.request({\n * method: 'snap_notify',\n * params: {\n * type: 'inApp',\n * message: 'This is an in-app notification',\n * title: 'Notification Title',\n * content: (\n * <Box>\n * <Text>This is the expanded content of the notification.</Text>\n * </Box>\n * ),\n * footerLink: {\n * href: 'https://example.com',\n * text: 'Click here for more info',\n * },\n * },\n * });\n * ```\n *\n * @example Native notification\n * ```json name=\"Manifest\"\n * {\n * \"initialPermissions\": {\n * \"snap_notify\": {}\n * }\n * }\n * ```\n * ```ts name=\"Usage\"\n * snap.request({\n * method: 'snap_notify',\n * params: {\n * type: 'native',\n * message: 'This is a native notification',\n * },\n * });\n * ```\n */\nexport const notifyBuilder = Object.freeze({\n targetName: methodName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n/**\n * Builds the method implementation for `snap_notify`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.showNativeNotification - A function that shows a native browser notification.\n * @param hooks.showInAppNotification - A function that shows a notification in the MetaMask UI.\n * @param hooks.isOnPhishingList - A function that checks for links against the phishing list.\n * @param hooks.maybeUpdatePhishingList - A function that updates the phishing list if needed.\n * @param hooks.createInterface - A function that creates the interface in SnapInterfaceController.\n * @param hooks.getSnap - A function that checks if a snap is installed.\n * @returns The method implementation which returns `null` on success.\n * @throws If the params are invalid.\n */\nexport function getImplementation({\n showNativeNotification,\n showInAppNotification,\n isOnPhishingList,\n maybeUpdatePhishingList,\n createInterface,\n getSnap,\n}: NotifyMethodHooks) {\n return async function implementation(\n args: RestrictedMethodOptions<NotifyParams>,\n ): Promise<NotifyResult> {\n const {\n params,\n context: { origin },\n } = args;\n\n await maybeUpdatePhishingList();\n\n const validatedParams = getValidatedParams(\n params,\n isOnPhishingList,\n getSnap,\n );\n\n if (hasProperty(validatedParams, 'content')) {\n const id = await createInterface(\n origin,\n validatedParams.content as ComponentOrElement,\n undefined,\n ContentType.Notification,\n );\n validatedParams.content = id;\n }\n\n switch (validatedParams.type) {\n case NotificationType.Native:\n return await showNativeNotification(origin, validatedParams);\n case NotificationType.InApp:\n return await showInAppNotification(origin, validatedParams);\n default:\n throw rpcErrors.invalidParams({\n message: 'Must specify a valid notification \"type\".',\n });\n }\n };\n}\n\n/**\n * Validates the notify 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 * @param isOnPhishingList - The function that checks for links against the phishing list.\n * @param getSnap - A function that checks if a snap is installed.\n * @returns The validated method parameter object.\n * @throws If the params are invalid.\n */\nexport function getValidatedParams(\n params: unknown,\n isOnPhishingList: NotifyMethodHooks['isOnPhishingList'],\n getSnap: NotifyMethodHooks['getSnap'],\n): NotifyParams {\n if (!isObject(params)) {\n throw rpcErrors.invalidParams({\n message: 'Expected params to be a single object.',\n });\n }\n\n const { type, message } = params;\n\n if (\n !type ||\n typeof type !== 'string' ||\n !Object.values(NotificationType).includes(type as NotificationType)\n ) {\n throw rpcErrors.invalidParams({\n message: 'Must specify a valid notification \"type\".',\n });\n }\n\n const isNotString = !message || typeof message !== 'string';\n // Set to the max message length on a Mac notification for now.\n if (\n type === NotificationType.Native &&\n (isNotString || message.length >= 50)\n ) {\n throw rpcErrors.invalidParams({\n message:\n 'Must specify a non-empty string \"message\" less than 50 characters long.',\n });\n }\n\n if (\n type === NotificationType.InApp &&\n (isNotString || message.length >= 500)\n ) {\n throw rpcErrors.invalidParams({\n message:\n 'Must specify a non-empty string \"message\" less than 500 characters long.',\n });\n }\n\n try {\n const validatedParams = createUnion(\n params,\n NotificationParametersStruct,\n 'type',\n );\n\n validateTextLinks(validatedParams.message, isOnPhishingList, getSnap);\n\n if (hasProperty(validatedParams, 'footerLink')) {\n validateLink(validatedParams.footerLink.href, isOnPhishingList, getSnap);\n }\n\n return validatedParams;\n } catch (error) {\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${getErrorMessage(error)}`,\n });\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/snaps-rpc-methods",
|
|
3
|
-
"version": "15.0.
|
|
3
|
+
"version": "15.0.2",
|
|
4
4
|
"description": "MetaMask Snaps JSON-RPC method implementations",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -58,20 +58,20 @@
|
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
60
|
"@metamask/key-tree": "^10.1.1",
|
|
61
|
-
"@metamask/permission-controller": "^12.
|
|
61
|
+
"@metamask/permission-controller": "^12.3.0",
|
|
62
62
|
"@metamask/rpc-errors": "^7.0.3",
|
|
63
63
|
"@metamask/snaps-sdk": "^11.0.0",
|
|
64
|
-
"@metamask/snaps-utils": "^12.1.
|
|
64
|
+
"@metamask/snaps-utils": "^12.1.3",
|
|
65
65
|
"@metamask/superstruct": "^3.2.1",
|
|
66
66
|
"@metamask/utils": "^11.10.0",
|
|
67
67
|
"@noble/hashes": "^1.7.1",
|
|
68
68
|
"async-mutex": "^0.5.0"
|
|
69
69
|
},
|
|
70
70
|
"devDependencies": {
|
|
71
|
-
"@lavamoat/allow-scripts": "^
|
|
71
|
+
"@lavamoat/allow-scripts": "^4.0.0",
|
|
72
72
|
"@metamask/auto-changelog": "^5.3.2",
|
|
73
|
-
"@metamask/json-rpc-engine": "^10.2.
|
|
74
|
-
"@metamask/messenger": "^
|
|
73
|
+
"@metamask/json-rpc-engine": "^10.2.4",
|
|
74
|
+
"@metamask/messenger": "^1.1.0",
|
|
75
75
|
"@swc/core": "1.11.31",
|
|
76
76
|
"@swc/jest": "^0.2.38",
|
|
77
77
|
"@ts-bridge/cli": "^0.6.1",
|