@metamask/snaps-rpc-methods 3.2.0 → 3.2.1
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
CHANGED
|
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [3.2.1]
|
|
10
|
+
### Fixed
|
|
11
|
+
- Fix `assertLinksAreSafe` import ([#1908](https://github.com/MetaMask/snaps/pull/1908))
|
|
12
|
+
|
|
9
13
|
## [3.2.0]
|
|
10
14
|
### Added
|
|
11
15
|
- Add support for links in custom UI and notifications ([#1814](https://github.com/MetaMask/snaps/pull/1814))
|
|
@@ -53,7 +57,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
53
57
|
- The version of the package no longer needs to match the version of all other
|
|
54
58
|
MetaMask Snaps packages.
|
|
55
59
|
|
|
56
|
-
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@3.2.
|
|
60
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@3.2.1...HEAD
|
|
61
|
+
[3.2.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@3.2.0...@metamask/snaps-rpc-methods@3.2.1
|
|
57
62
|
[3.2.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@3.1.0...@metamask/snaps-rpc-methods@3.2.0
|
|
58
63
|
[3.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@3.0.0...@metamask/snaps-rpc-methods@3.1.0
|
|
59
64
|
[3.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@2.0.0...@metamask/snaps-rpc-methods@3.0.0
|
|
@@ -27,7 +27,7 @@ _export(exports, {
|
|
|
27
27
|
});
|
|
28
28
|
const _permissioncontroller = require("@metamask/permission-controller");
|
|
29
29
|
const _rpcerrors = require("@metamask/rpc-errors");
|
|
30
|
-
const
|
|
30
|
+
const _snapsui = require("@metamask/snaps-ui");
|
|
31
31
|
const _utils = require("@metamask/utils");
|
|
32
32
|
const methodName = 'snap_notify';
|
|
33
33
|
var NotificationType;
|
|
@@ -62,7 +62,7 @@ function getImplementation({ showNativeNotification, showInAppNotification, isOn
|
|
|
62
62
|
const { params, context: { origin } } = args;
|
|
63
63
|
const validatedParams = getValidatedParams(params);
|
|
64
64
|
await maybeUpdatePhishingList();
|
|
65
|
-
(0,
|
|
65
|
+
(0, _snapsui.assertLinksAreSafe)(validatedParams.message, isOnPhishingList);
|
|
66
66
|
switch(validatedParams.type){
|
|
67
67
|
case NotificationType.Native:
|
|
68
68
|
return await showNativeNotification(origin, validatedParams);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/restricted/notify.ts"],"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
|
|
1
|
+
{"version":3,"sources":["../../../src/restricted/notify.ts"],"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 { assertLinksAreSafe } from '@metamask/snaps-ui';\nimport type { EnumToUnion } from '@metamask/snaps-utils';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { isObject } from '@metamask/utils';\n\nimport { type MethodHooksObject } from '../utils';\n\nconst methodName = 'snap_notify';\n\n// TODO: Move all the types to a shared place when implementing more\n// notifications.\nexport enum NotificationType {\n InApp = 'inApp',\n Native = 'native',\n}\n\nexport type NotificationArgs = {\n /**\n * Enum type to determine notification type.\n */\n type: EnumToUnion<NotificationType>;\n\n /**\n * A message to show on the notification.\n */\n message: string;\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\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};\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 * @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}: NotifyMethodHooks) {\n return async function implementation(\n args: RestrictedMethodOptions<NotificationArgs>,\n ): Promise<null> {\n const {\n params,\n context: { origin },\n } = args;\n\n const validatedParams = getValidatedParams(params);\n\n await maybeUpdatePhishingList();\n\n assertLinksAreSafe(validatedParams.message, isOnPhishingList);\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 * @returns The validated method parameter object.\n */\nexport function getValidatedParams(params: unknown): NotificationArgs {\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 // Set to the max message length on a Mac notification for now.\n if (!message || typeof message !== 'string' || message.length >= 50) {\n throw rpcErrors.invalidParams({\n message:\n 'Must specify a non-empty string \"message\" less than 50 characters long.',\n });\n }\n\n return params as NotificationArgs;\n}\n"],"names":["specificationBuilder","notifyBuilder","getImplementation","getValidatedParams","methodName","NotificationType","InApp","Native","allowedCaveats","methodHooks","permissionType","PermissionType","RestrictedMethod","targetName","methodImplementation","subjectTypes","SubjectType","Snap","showNativeNotification","showInAppNotification","isOnPhishingList","maybeUpdatePhishingList","Object","freeze","implementation","args","params","context","origin","validatedParams","assertLinksAreSafe","message","type","rpcErrors","invalidParams","isObject","values","includes","length"],"mappings":";;;;;;;;;;;;;;IAgFaA,oBAAoB;eAApBA;;IAqBAC,aAAa;eAAbA;;IAiBGC,iBAAiB;eAAjBA;;IAwCAC,kBAAkB;eAAlBA;;;sCAzJ4B;2BAClB;yBACS;uBAGV;AAIzB,MAAMC,aAAa;IAIZ;UAAKC,gBAAgB;IAAhBA,iBACVC,WAAQ;IADED,iBAEVE,YAAS;GAFCF,qBAAAA;AA8DL,MAAML,uBAIT,CAAC,EAAEQ,iBAAiB,IAAI,EAAEC,WAAW,EAA+B;IACtE,OAAO;QACLC,gBAAgBC,oCAAc,CAACC,gBAAgB;QAC/CC,YAAYT;QACZI;QACAM,sBAAsBZ,kBAAkBO;QACxCM,cAAc;YAACC,iCAAW,CAACC,IAAI;SAAC;IAClC;AACF;AAEA,MAAMR,cAAoD;IACxDS,wBAAwB;IACxBC,uBAAuB;IACvBC,kBAAkB;IAClBC,yBAAyB;AAC3B;AAEO,MAAMpB,gBAAgBqB,OAAOC,MAAM,CAAC;IACzCV,YAAYT;IACZJ;IACAS;AACF;AAaO,SAASP,kBAAkB,EAChCgB,sBAAsB,EACtBC,qBAAqB,EACrBC,gBAAgB,EAChBC,uBAAuB,EACL;IAClB,OAAO,eAAeG,eACpBC,IAA+C;QAE/C,MAAM,EACJC,MAAM,EACNC,SAAS,EAAEC,MAAM,EAAE,EACpB,GAAGH;QAEJ,MAAMI,kBAAkB1B,mBAAmBuB;QAE3C,MAAML;QAENS,IAAAA,2BAAkB,EAACD,gBAAgBE,OAAO,EAAEX;QAE5C,OAAQS,gBAAgBG,IAAI;YAC1B,KAAK3B,iBAAiBE,MAAM;gBAC1B,OAAO,MAAMW,uBAAuBU,QAAQC;YAC9C,KAAKxB,iBAAiBC,KAAK;gBACzB,OAAO,MAAMa,sBAAsBS,QAAQC;YAC7C;gBACE,MAAMI,oBAAS,CAACC,aAAa,CAAC;oBAC5BH,SAAS;gBACX;QACJ;IACF;AACF;AASO,SAAS5B,mBAAmBuB,MAAe;IAChD,IAAI,CAACS,IAAAA,eAAQ,EAACT,SAAS;QACrB,MAAMO,oBAAS,CAACC,aAAa,CAAC;YAC5BH,SAAS;QACX;IACF;IAEA,MAAM,EAAEC,IAAI,EAAED,OAAO,EAAE,GAAGL;IAE1B,IACE,CAACM,QACD,OAAOA,SAAS,YAChB,CAACV,OAAOc,MAAM,CAAC/B,kBAAkBgC,QAAQ,CAACL,OAC1C;QACA,MAAMC,oBAAS,CAACC,aAAa,CAAC;YAC5BH,SAAS;QACX;IACF;IAEA,+DAA+D;IAC/D,IAAI,CAACA,WAAW,OAAOA,YAAY,YAAYA,QAAQO,MAAM,IAAI,IAAI;QACnE,MAAML,oBAAS,CAACC,aAAa,CAAC;YAC5BH,SACE;QACJ;IACF;IAEA,OAAOL;AACT"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { PermissionType, SubjectType } from '@metamask/permission-controller';
|
|
2
2
|
import { rpcErrors } from '@metamask/rpc-errors';
|
|
3
|
-
import { assertLinksAreSafe } from '@metamask/snaps-
|
|
3
|
+
import { assertLinksAreSafe } from '@metamask/snaps-ui';
|
|
4
4
|
import { isObject } from '@metamask/utils';
|
|
5
5
|
const methodName = 'snap_notify';
|
|
6
6
|
export var NotificationType;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/restricted/notify.ts"],"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
|
|
1
|
+
{"version":3,"sources":["../../../src/restricted/notify.ts"],"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 { assertLinksAreSafe } from '@metamask/snaps-ui';\nimport type { EnumToUnion } from '@metamask/snaps-utils';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport { isObject } from '@metamask/utils';\n\nimport { type MethodHooksObject } from '../utils';\n\nconst methodName = 'snap_notify';\n\n// TODO: Move all the types to a shared place when implementing more\n// notifications.\nexport enum NotificationType {\n InApp = 'inApp',\n Native = 'native',\n}\n\nexport type NotificationArgs = {\n /**\n * Enum type to determine notification type.\n */\n type: EnumToUnion<NotificationType>;\n\n /**\n * A message to show on the notification.\n */\n message: string;\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\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};\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 * @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}: NotifyMethodHooks) {\n return async function implementation(\n args: RestrictedMethodOptions<NotificationArgs>,\n ): Promise<null> {\n const {\n params,\n context: { origin },\n } = args;\n\n const validatedParams = getValidatedParams(params);\n\n await maybeUpdatePhishingList();\n\n assertLinksAreSafe(validatedParams.message, isOnPhishingList);\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 * @returns The validated method parameter object.\n */\nexport function getValidatedParams(params: unknown): NotificationArgs {\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 // Set to the max message length on a Mac notification for now.\n if (!message || typeof message !== 'string' || message.length >= 50) {\n throw rpcErrors.invalidParams({\n message:\n 'Must specify a non-empty string \"message\" less than 50 characters long.',\n });\n }\n\n return params as NotificationArgs;\n}\n"],"names":["PermissionType","SubjectType","rpcErrors","assertLinksAreSafe","isObject","methodName","NotificationType","InApp","Native","specificationBuilder","allowedCaveats","methodHooks","permissionType","RestrictedMethod","targetName","methodImplementation","getImplementation","subjectTypes","Snap","showNativeNotification","showInAppNotification","isOnPhishingList","maybeUpdatePhishingList","notifyBuilder","Object","freeze","implementation","args","params","context","origin","validatedParams","getValidatedParams","message","type","invalidParams","values","includes","length"],"mappings":"AAKA,SAASA,cAAc,EAAEC,WAAW,QAAQ,kCAAkC;AAC9E,SAASC,SAAS,QAAQ,uBAAuB;AACjD,SAASC,kBAAkB,QAAQ,qBAAqB;AAGxD,SAASC,QAAQ,QAAQ,kBAAkB;AAI3C,MAAMC,aAAa;WAIZ;UAAKC,gBAAgB;IAAhBA,iBACVC,WAAQ;IADED,iBAEVE,YAAS;GAFCF,qBAAAA;AAqDZ;;;;;;;;CAQC,GACD,OAAO,MAAMG,uBAIT,CAAC,EAAEC,iBAAiB,IAAI,EAAEC,WAAW,EAA+B;IACtE,OAAO;QACLC,gBAAgBZ,eAAea,gBAAgB;QAC/CC,YAAYT;QACZK;QACAK,sBAAsBC,kBAAkBL;QACxCM,cAAc;YAAChB,YAAYiB,IAAI;SAAC;IAClC;AACF,EAAE;AAEF,MAAMP,cAAoD;IACxDQ,wBAAwB;IACxBC,uBAAuB;IACvBC,kBAAkB;IAClBC,yBAAyB;AAC3B;AAEA,OAAO,MAAMC,gBAAgBC,OAAOC,MAAM,CAAC;IACzCX,YAAYT;IACZI;IACAE;AACF,GAAY;AAEZ;;;;;;;;;;CAUC,GACD,OAAO,SAASK,kBAAkB,EAChCG,sBAAsB,EACtBC,qBAAqB,EACrBC,gBAAgB,EAChBC,uBAAuB,EACL;IAClB,OAAO,eAAeI,eACpBC,IAA+C;QAE/C,MAAM,EACJC,MAAM,EACNC,SAAS,EAAEC,MAAM,EAAE,EACpB,GAAGH;QAEJ,MAAMI,kBAAkBC,mBAAmBJ;QAE3C,MAAMN;QAENnB,mBAAmB4B,gBAAgBE,OAAO,EAAEZ;QAE5C,OAAQU,gBAAgBG,IAAI;YAC1B,KAAK5B,iBAAiBE,MAAM;gBAC1B,OAAO,MAAMW,uBAAuBW,QAAQC;YAC9C,KAAKzB,iBAAiBC,KAAK;gBACzB,OAAO,MAAMa,sBAAsBU,QAAQC;YAC7C;gBACE,MAAM7B,UAAUiC,aAAa,CAAC;oBAC5BF,SAAS;gBACX;QACJ;IACF;AACF;AAEA;;;;;;CAMC,GACD,OAAO,SAASD,mBAAmBJ,MAAe;IAChD,IAAI,CAACxB,SAASwB,SAAS;QACrB,MAAM1B,UAAUiC,aAAa,CAAC;YAC5BF,SAAS;QACX;IACF;IAEA,MAAM,EAAEC,IAAI,EAAED,OAAO,EAAE,GAAGL;IAE1B,IACE,CAACM,QACD,OAAOA,SAAS,YAChB,CAACV,OAAOY,MAAM,CAAC9B,kBAAkB+B,QAAQ,CAACH,OAC1C;QACA,MAAMhC,UAAUiC,aAAa,CAAC;YAC5BF,SAAS;QACX;IACF;IAEA,+DAA+D;IAC/D,IAAI,CAACA,WAAW,OAAOA,YAAY,YAAYA,QAAQK,MAAM,IAAI,IAAI;QACnE,MAAMpC,UAAUiC,aAAa,CAAC;YAC5BF,SACE;QACJ;IACF;IAEA,OAAOL;AACT"}
|