@metamask/snaps-rpc-methods 3.1.0 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md 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.0]
10
+ ### Added
11
+ - Add support for links in custom UI and notifications ([#1814](https://github.com/MetaMask/snaps/pull/1814))
12
+
9
13
  ## [3.1.0]
10
14
  ### Changed
11
15
  - Rename package to `@metamask/snaps-rpc-methods` ([#1864](https://github.com/MetaMask/snaps/pull/1864))
@@ -49,7 +53,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
49
53
  - The version of the package no longer needs to match the version of all other
50
54
  MetaMask Snaps packages.
51
55
 
52
- [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@3.1.0...HEAD
56
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@3.2.0...HEAD
57
+ [3.2.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@3.1.0...@metamask/snaps-rpc-methods@3.2.0
53
58
  [3.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@3.0.0...@metamask/snaps-rpc-methods@3.1.0
54
59
  [3.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@2.0.0...@metamask/snaps-rpc-methods@3.0.0
55
60
  [2.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-rpc-methods@0.38.3-flask.1...@metamask/snaps-rpc-methods@2.0.0
@@ -57,7 +57,9 @@ const PlaceholderStruct = (0, _superstruct.optional)((0, _superstruct.size)((0,
57
57
  };
58
58
  };
59
59
  const methodHooks = {
60
- showDialog: true
60
+ showDialog: true,
61
+ isOnPhishingList: true,
62
+ maybeUpdatePhishingList: true
61
63
  };
62
64
  const dialogBuilder = Object.freeze({
63
65
  targetName: methodName,
@@ -96,12 +98,14 @@ const structs = {
96
98
  [DialogType.Confirmation]: ConfirmationParametersStruct,
97
99
  [DialogType.Prompt]: PromptParametersStruct
98
100
  };
99
- function getDialogImplementation({ showDialog }) {
101
+ function getDialogImplementation({ showDialog, isOnPhishingList, maybeUpdatePhishingList }) {
100
102
  return async function dialogImplementation(args) {
101
103
  const { params, context: { origin } } = args;
102
104
  const validatedType = getValidatedType(params);
103
105
  const validatedParams = getValidatedParams(params, structs[validatedType]);
104
106
  const { content } = validatedParams;
107
+ await maybeUpdatePhishingList();
108
+ (0, _snapsui.assertUILinksAreSafe)(content, isOnPhishingList);
105
109
  const placeholder = validatedParams.type === DialogType.Prompt ? validatedParams.placeholder : undefined;
106
110
  return showDialog(origin, validatedType, content, placeholder);
107
111
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/restricted/dialog.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 type { Component } from '@metamask/snaps-ui';\nimport { ComponentStruct } from '@metamask/snaps-ui';\nimport type { EnumToUnion } from '@metamask/snaps-utils';\nimport { enumValue } from '@metamask/snaps-utils';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport type { Infer, Struct } from 'superstruct';\nimport {\n create,\n enums,\n object,\n optional,\n size,\n string,\n StructError,\n type,\n union,\n} from 'superstruct';\n\nimport type { MethodHooksObject } from '../utils';\n\nconst methodName = 'snap_dialog';\n\nexport enum DialogType {\n Alert = 'alert',\n Confirmation = 'confirmation',\n Prompt = 'prompt',\n}\n\nconst PlaceholderStruct = optional(size(string(), 1, 40));\n\nexport type Placeholder = Infer<typeof PlaceholderStruct>;\n\ntype ShowDialog = (\n snapId: string,\n type: EnumToUnion<DialogType>,\n content: Component,\n placeholder?: Placeholder,\n) => Promise<null | boolean | string>;\n\nexport type DialogMethodHooks = {\n /**\n * @param snapId - The ID of the Snap that created the alert.\n * @param type - The dialog type.\n * @param content - The dialog custom UI.\n * @param placeholder - The placeholder for the Prompt dialog input.\n */\n showDialog: ShowDialog;\n};\n\ntype DialogSpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: DialogMethodHooks;\n};\n\ntype DialogSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof methodName;\n methodImplementation: ReturnType<typeof getDialogImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * The specification builder for the `snap_dialog` permission. `snap_dialog`\n * lets the Snap display one of the following dialogs to the user:\n * - An alert, for displaying information.\n * - A confirmation, for accepting or rejecting some action.\n * - A prompt, for inputting some information.\n *\n * @param options - The specification builder options.\n * @param options.allowedCaveats - The optional allowed caveats for the\n * permission.\n * @param options.methodHooks - The RPC method hooks needed by the method\n * implementation.\n * @returns The specification for the `snap_dialog` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n DialogSpecificationBuilderOptions,\n DialogSpecification\n> = ({\n allowedCaveats = null,\n methodHooks,\n}: DialogSpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName: methodName,\n allowedCaveats,\n methodImplementation: getDialogImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<DialogMethodHooks> = {\n showDialog: true,\n};\n\nexport const dialogBuilder = Object.freeze({\n targetName: methodName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n// Note: We use `type` here instead of `object` because `type` does not validate\n// the keys of the object, which is what we want.\nconst BaseParamsStruct = type({\n type: enums([DialogType.Alert, DialogType.Confirmation, DialogType.Prompt]),\n});\n\nconst AlertParametersStruct = object({\n type: enumValue(DialogType.Alert),\n content: ComponentStruct,\n});\n\nconst ConfirmationParametersStruct = object({\n type: enumValue(DialogType.Confirmation),\n content: ComponentStruct,\n});\n\nconst PromptParametersStruct = object({\n type: enumValue(DialogType.Prompt),\n content: ComponentStruct,\n placeholder: PlaceholderStruct,\n});\n\nconst DialogParametersStruct = union([\n AlertParametersStruct,\n ConfirmationParametersStruct,\n PromptParametersStruct,\n]);\n\nexport type DialogParameters = Infer<typeof DialogParametersStruct>;\n\nconst structs = {\n [DialogType.Alert]: AlertParametersStruct,\n [DialogType.Confirmation]: ConfirmationParametersStruct,\n [DialogType.Prompt]: PromptParametersStruct,\n};\n\n/**\n * Builds the method implementation for `snap_dialog`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.showDialog - A function that shows the specified dialog in the\n * MetaMask UI and returns the appropriate value for the dialog type.\n * @returns The method implementation which return value depends on the dialog\n * type, valid return types are: string, boolean, null.\n */\nexport function getDialogImplementation({ showDialog }: DialogMethodHooks) {\n return async function dialogImplementation(\n args: RestrictedMethodOptions<DialogParameters>,\n ): Promise<boolean | null | string> {\n const {\n params,\n context: { origin },\n } = args;\n\n const validatedType = getValidatedType(params);\n const validatedParams = getValidatedParams(params, structs[validatedType]);\n\n const { content } = validatedParams;\n\n const placeholder =\n validatedParams.type === DialogType.Prompt\n ? validatedParams.placeholder\n : undefined;\n\n return showDialog(origin, validatedType, content, placeholder);\n };\n}\n\n/**\n * Get the validated type of the dialog parameters. Throws an error if the type\n * is invalid.\n *\n * @param params - The parameters to validate.\n * @returns The validated type of the dialog parameters.\n */\nfunction getValidatedType(params: unknown): DialogType {\n try {\n return create(params, BaseParamsStruct).type;\n } catch (error) {\n throw rpcErrors.invalidParams({\n message: `The \"type\" property must be one of: ${Object.values(\n DialogType,\n ).join(', ')}.`,\n });\n }\n}\n\n/**\n * Validates the confirm 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 struct - The struct to validate the params against.\n * @returns The validated confirm method parameter object.\n */\nfunction getValidatedParams(\n params: unknown,\n struct: Struct<any>,\n): DialogParameters {\n try {\n return create(params, struct);\n } catch (error) {\n if (error instanceof StructError) {\n const { key, type: errorType } = error;\n\n if (key === 'placeholder' && errorType === 'never') {\n throw rpcErrors.invalidParams({\n message:\n 'Invalid params: Alerts or confirmations may not specify a \"placeholder\" field.',\n });\n }\n\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n"],"names":["dialogBuilder","getDialogImplementation","methodName","DialogType","Alert","Confirmation","Prompt","PlaceholderStruct","optional","size","string","specificationBuilder","allowedCaveats","methodHooks","permissionType","PermissionType","RestrictedMethod","targetName","methodImplementation","subjectTypes","SubjectType","Snap","showDialog","Object","freeze","BaseParamsStruct","type","enums","AlertParametersStruct","object","enumValue","content","ComponentStruct","ConfirmationParametersStruct","PromptParametersStruct","placeholder","DialogParametersStruct","union","structs","dialogImplementation","args","params","context","origin","validatedType","getValidatedType","validatedParams","getValidatedParams","undefined","create","error","rpcErrors","invalidParams","message","values","join","struct","StructError","key","errorType","internal"],"mappings":"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"}
1
+ {"version":3,"sources":["../../../src/restricted/dialog.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 type { Component } from '@metamask/snaps-ui';\nimport { ComponentStruct, assertUILinksAreSafe } from '@metamask/snaps-ui';\nimport type { EnumToUnion } from '@metamask/snaps-utils';\nimport { enumValue } from '@metamask/snaps-utils';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport type { Infer, Struct } from 'superstruct';\nimport {\n create,\n enums,\n object,\n optional,\n size,\n string,\n StructError,\n type,\n union,\n} from 'superstruct';\n\nimport { type MethodHooksObject } from '../utils';\n\nconst methodName = 'snap_dialog';\n\nexport enum DialogType {\n Alert = 'alert',\n Confirmation = 'confirmation',\n Prompt = 'prompt',\n}\n\nconst PlaceholderStruct = optional(size(string(), 1, 40));\n\nexport type Placeholder = Infer<typeof PlaceholderStruct>;\n\ntype ShowDialog = (\n snapId: string,\n type: EnumToUnion<DialogType>,\n content: Component,\n placeholder?: Placeholder,\n) => Promise<null | boolean | string>;\n\ntype MaybeUpdatePhisingList = () => Promise<void>;\ntype IsOnPhishingList = (url: string) => boolean;\n\nexport type DialogMethodHooks = {\n /**\n * @param snapId - The ID of the Snap that created the alert.\n * @param type - The dialog type.\n * @param content - The dialog custom UI.\n * @param placeholder - The placeholder for the Prompt dialog input.\n */\n showDialog: ShowDialog;\n\n maybeUpdatePhishingList: MaybeUpdatePhisingList;\n\n /**\n * @param url - The URL to check against the phishing list.\n */\n isOnPhishingList: IsOnPhishingList;\n};\n\ntype DialogSpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: DialogMethodHooks;\n};\n\ntype DialogSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof methodName;\n methodImplementation: ReturnType<typeof getDialogImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * The specification builder for the `snap_dialog` permission. `snap_dialog`\n * lets the Snap display one of the following dialogs to the user:\n * - An alert, for displaying information.\n * - A confirmation, for accepting or rejecting some action.\n * - A prompt, for inputting some information.\n *\n * @param options - The specification builder options.\n * @param options.allowedCaveats - The optional allowed caveats for the\n * permission.\n * @param options.methodHooks - The RPC method hooks needed by the method\n * implementation.\n * @returns The specification for the `snap_dialog` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n DialogSpecificationBuilderOptions,\n DialogSpecification\n> = ({\n allowedCaveats = null,\n methodHooks,\n}: DialogSpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName: methodName,\n allowedCaveats,\n methodImplementation: getDialogImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<DialogMethodHooks> = {\n showDialog: true,\n isOnPhishingList: true,\n maybeUpdatePhishingList: true,\n};\n\nexport const dialogBuilder = Object.freeze({\n targetName: methodName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n// Note: We use `type` here instead of `object` because `type` does not validate\n// the keys of the object, which is what we want.\nconst BaseParamsStruct = type({\n type: enums([DialogType.Alert, DialogType.Confirmation, DialogType.Prompt]),\n});\n\nconst AlertParametersStruct = object({\n type: enumValue(DialogType.Alert),\n content: ComponentStruct,\n});\n\nconst ConfirmationParametersStruct = object({\n type: enumValue(DialogType.Confirmation),\n content: ComponentStruct,\n});\n\nconst PromptParametersStruct = object({\n type: enumValue(DialogType.Prompt),\n content: ComponentStruct,\n placeholder: PlaceholderStruct,\n});\n\nconst DialogParametersStruct = union([\n AlertParametersStruct,\n ConfirmationParametersStruct,\n PromptParametersStruct,\n]);\n\nexport type DialogParameters = Infer<typeof DialogParametersStruct>;\n\nconst structs = {\n [DialogType.Alert]: AlertParametersStruct,\n [DialogType.Confirmation]: ConfirmationParametersStruct,\n [DialogType.Prompt]: PromptParametersStruct,\n};\n\n/**\n * Builds the method implementation for `snap_dialog`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.showDialog - A function that shows the specified dialog in the\n * MetaMask UI and returns the appropriate value for the dialog type.\n * @param hooks.isOnPhishingList - A function that checks a link against the\n * phishing list and return true if it's in, otherwise false.\n * @param hooks.maybeUpdatePhishingList - A function that updates the phishing list if needed.\n * @returns The method implementation which return value depends on the dialog\n * type, valid return types are: string, boolean, null.\n */\nexport function getDialogImplementation({\n showDialog,\n isOnPhishingList,\n maybeUpdatePhishingList,\n}: DialogMethodHooks) {\n return async function dialogImplementation(\n args: RestrictedMethodOptions<DialogParameters>,\n ): Promise<boolean | null | string> {\n const {\n params,\n context: { origin },\n } = args;\n\n const validatedType = getValidatedType(params);\n const validatedParams = getValidatedParams(params, structs[validatedType]);\n\n const { content } = validatedParams;\n\n await maybeUpdatePhishingList();\n\n assertUILinksAreSafe(content, isOnPhishingList);\n\n const placeholder =\n validatedParams.type === DialogType.Prompt\n ? validatedParams.placeholder\n : undefined;\n\n return showDialog(origin, validatedType, content, placeholder);\n };\n}\n\n/**\n * Get the validated type of the dialog parameters. Throws an error if the type\n * is invalid.\n *\n * @param params - The parameters to validate.\n * @returns The validated type of the dialog parameters.\n */\nfunction getValidatedType(params: unknown): DialogType {\n try {\n return create(params, BaseParamsStruct).type;\n } catch (error) {\n throw rpcErrors.invalidParams({\n message: `The \"type\" property must be one of: ${Object.values(\n DialogType,\n ).join(', ')}.`,\n });\n }\n}\n\n/**\n * Validates the confirm 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 struct - The struct to validate the params against.\n * @returns The validated confirm method parameter object.\n */\nfunction getValidatedParams(\n params: unknown,\n struct: Struct<any>,\n): DialogParameters {\n try {\n return create(params, struct);\n } catch (error) {\n if (error instanceof StructError) {\n const { key, type: errorType } = error;\n\n if (key === 'placeholder' && errorType === 'never') {\n throw rpcErrors.invalidParams({\n message:\n 'Invalid params: Alerts or confirmations may not specify a \"placeholder\" field.',\n });\n }\n\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n"],"names":["dialogBuilder","getDialogImplementation","methodName","DialogType","Alert","Confirmation","Prompt","PlaceholderStruct","optional","size","string","specificationBuilder","allowedCaveats","methodHooks","permissionType","PermissionType","RestrictedMethod","targetName","methodImplementation","subjectTypes","SubjectType","Snap","showDialog","isOnPhishingList","maybeUpdatePhishingList","Object","freeze","BaseParamsStruct","type","enums","AlertParametersStruct","object","enumValue","content","ComponentStruct","ConfirmationParametersStruct","PromptParametersStruct","placeholder","DialogParametersStruct","union","structs","dialogImplementation","args","params","context","origin","validatedType","getValidatedType","validatedParams","getValidatedParams","assertUILinksAreSafe","undefined","create","error","rpcErrors","invalidParams","message","values","join","struct","StructError","key","errorType","internal"],"mappings":"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"}
@@ -27,6 +27,7 @@ _export(exports, {
27
27
  });
28
28
  const _permissioncontroller = require("@metamask/permission-controller");
29
29
  const _rpcerrors = require("@metamask/rpc-errors");
30
+ const _snapsutils = require("@metamask/snaps-utils");
30
31
  const _utils = require("@metamask/utils");
31
32
  const methodName = 'snap_notify';
32
33
  var NotificationType;
@@ -47,17 +48,21 @@ const specificationBuilder = ({ allowedCaveats = null, methodHooks })=>{
47
48
  };
48
49
  const methodHooks = {
49
50
  showNativeNotification: true,
50
- showInAppNotification: true
51
+ showInAppNotification: true,
52
+ isOnPhishingList: true,
53
+ maybeUpdatePhishingList: true
51
54
  };
52
55
  const notifyBuilder = Object.freeze({
53
56
  targetName: methodName,
54
57
  specificationBuilder,
55
58
  methodHooks
56
59
  });
57
- function getImplementation({ showNativeNotification, showInAppNotification }) {
60
+ function getImplementation({ showNativeNotification, showInAppNotification, isOnPhishingList, maybeUpdatePhishingList }) {
58
61
  return async function implementation(args) {
59
62
  const { params, context: { origin } } = args;
60
63
  const validatedParams = getValidatedParams(params);
64
+ await maybeUpdatePhishingList();
65
+ (0, _snapsutils.assertLinksAreSafe)(validatedParams.message, isOnPhishingList);
61
66
  switch(validatedParams.type){
62
67
  case NotificationType.Native:
63
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 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\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};\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 * @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}: 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 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","Object","freeze","implementation","args","params","context","origin","validatedParams","type","rpcErrors","invalidParams","message","isObject","values","includes","length"],"mappings":";;;;;;;;;;;;;;IA2EaA,oBAAoB;eAApBA;;IAmBAC,aAAa;eAAbA;;IAeGC,iBAAiB;eAAjBA;;IAkCAC,kBAAkB;eAAlBA;;;sCA1I4B;2BAClB;uBAGD;AAIzB,MAAMC,aAAa;IAIZ;UAAKC,gBAAgB;IAAhBA,iBACVC,WAAQ;IADED,iBAEVE,YAAS;GAFCF,qBAAAA;AA0DL,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;AACzB;AAEO,MAAMlB,gBAAgBmB,OAAOC,MAAM,CAAC;IACzCR,YAAYT;IACZJ;IACAS;AACF;AAWO,SAASP,kBAAkB,EAChCgB,sBAAsB,EACtBC,qBAAqB,EACH;IAClB,OAAO,eAAeG,eACpBC,IAA+C;QAE/C,MAAM,EACJC,MAAM,EACNC,SAAS,EAAEC,MAAM,EAAE,EACpB,GAAGH;QAEJ,MAAMI,kBAAkBxB,mBAAmBqB;QAE3C,OAAQG,gBAAgBC,IAAI;YAC1B,KAAKvB,iBAAiBE,MAAM;gBAC1B,OAAO,MAAMW,uBAAuBQ,QAAQC;YAC9C,KAAKtB,iBAAiBC,KAAK;gBACzB,OAAO,MAAMa,sBAAsBO,QAAQC;YAC7C;gBACE,MAAME,oBAAS,CAACC,aAAa,CAAC;oBAC5BC,SAAS;gBACX;QACJ;IACF;AACF;AASO,SAAS5B,mBAAmBqB,MAAe;IAChD,IAAI,CAACQ,IAAAA,eAAQ,EAACR,SAAS;QACrB,MAAMK,oBAAS,CAACC,aAAa,CAAC;YAC5BC,SAAS;QACX;IACF;IAEA,MAAM,EAAEH,IAAI,EAAEG,OAAO,EAAE,GAAGP;IAE1B,IACE,CAACI,QACD,OAAOA,SAAS,YAChB,CAACR,OAAOa,MAAM,CAAC5B,kBAAkB6B,QAAQ,CAACN,OAC1C;QACA,MAAMC,oBAAS,CAACC,aAAa,CAAC;YAC5BC,SAAS;QACX;IACF;IAEA,+DAA+D;IAC/D,IAAI,CAACA,WAAW,OAAOA,YAAY,YAAYA,QAAQI,MAAM,IAAI,IAAI;QACnE,MAAMN,oBAAS,CAACC,aAAa,CAAC;YAC5BC,SACE;QACJ;IACF;IAEA,OAAOP;AACT"}
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 type { EnumToUnion } from '@metamask/snaps-utils';\nimport { assertLinksAreSafe } 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;4BAES;uBAEV;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,8BAAkB,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 { ComponentStruct } from '@metamask/snaps-ui';
3
+ import { ComponentStruct, assertUILinksAreSafe } from '@metamask/snaps-ui';
4
4
  import { enumValue } from '@metamask/snaps-utils';
5
5
  import { create, enums, object, optional, size, string, StructError, type, union } from 'superstruct';
6
6
  const methodName = 'snap_dialog';
@@ -36,7 +36,9 @@ const PlaceholderStruct = optional(size(string(), 1, 40));
36
36
  };
37
37
  };
38
38
  const methodHooks = {
39
- showDialog: true
39
+ showDialog: true,
40
+ isOnPhishingList: true,
41
+ maybeUpdatePhishingList: true
40
42
  };
41
43
  export const dialogBuilder = Object.freeze({
42
44
  targetName: methodName,
@@ -81,14 +83,19 @@ const structs = {
81
83
  * @param hooks - The RPC method hooks.
82
84
  * @param hooks.showDialog - A function that shows the specified dialog in the
83
85
  * MetaMask UI and returns the appropriate value for the dialog type.
86
+ * @param hooks.isOnPhishingList - A function that checks a link against the
87
+ * phishing list and return true if it's in, otherwise false.
88
+ * @param hooks.maybeUpdatePhishingList - A function that updates the phishing list if needed.
84
89
  * @returns The method implementation which return value depends on the dialog
85
90
  * type, valid return types are: string, boolean, null.
86
- */ export function getDialogImplementation({ showDialog }) {
91
+ */ export function getDialogImplementation({ showDialog, isOnPhishingList, maybeUpdatePhishingList }) {
87
92
  return async function dialogImplementation(args) {
88
93
  const { params, context: { origin } } = args;
89
94
  const validatedType = getValidatedType(params);
90
95
  const validatedParams = getValidatedParams(params, structs[validatedType]);
91
96
  const { content } = validatedParams;
97
+ await maybeUpdatePhishingList();
98
+ assertUILinksAreSafe(content, isOnPhishingList);
92
99
  const placeholder = validatedParams.type === DialogType.Prompt ? validatedParams.placeholder : undefined;
93
100
  return showDialog(origin, validatedType, content, placeholder);
94
101
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/restricted/dialog.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 type { Component } from '@metamask/snaps-ui';\nimport { ComponentStruct } from '@metamask/snaps-ui';\nimport type { EnumToUnion } from '@metamask/snaps-utils';\nimport { enumValue } from '@metamask/snaps-utils';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport type { Infer, Struct } from 'superstruct';\nimport {\n create,\n enums,\n object,\n optional,\n size,\n string,\n StructError,\n type,\n union,\n} from 'superstruct';\n\nimport type { MethodHooksObject } from '../utils';\n\nconst methodName = 'snap_dialog';\n\nexport enum DialogType {\n Alert = 'alert',\n Confirmation = 'confirmation',\n Prompt = 'prompt',\n}\n\nconst PlaceholderStruct = optional(size(string(), 1, 40));\n\nexport type Placeholder = Infer<typeof PlaceholderStruct>;\n\ntype ShowDialog = (\n snapId: string,\n type: EnumToUnion<DialogType>,\n content: Component,\n placeholder?: Placeholder,\n) => Promise<null | boolean | string>;\n\nexport type DialogMethodHooks = {\n /**\n * @param snapId - The ID of the Snap that created the alert.\n * @param type - The dialog type.\n * @param content - The dialog custom UI.\n * @param placeholder - The placeholder for the Prompt dialog input.\n */\n showDialog: ShowDialog;\n};\n\ntype DialogSpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: DialogMethodHooks;\n};\n\ntype DialogSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof methodName;\n methodImplementation: ReturnType<typeof getDialogImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * The specification builder for the `snap_dialog` permission. `snap_dialog`\n * lets the Snap display one of the following dialogs to the user:\n * - An alert, for displaying information.\n * - A confirmation, for accepting or rejecting some action.\n * - A prompt, for inputting some information.\n *\n * @param options - The specification builder options.\n * @param options.allowedCaveats - The optional allowed caveats for the\n * permission.\n * @param options.methodHooks - The RPC method hooks needed by the method\n * implementation.\n * @returns The specification for the `snap_dialog` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n DialogSpecificationBuilderOptions,\n DialogSpecification\n> = ({\n allowedCaveats = null,\n methodHooks,\n}: DialogSpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName: methodName,\n allowedCaveats,\n methodImplementation: getDialogImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<DialogMethodHooks> = {\n showDialog: true,\n};\n\nexport const dialogBuilder = Object.freeze({\n targetName: methodName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n// Note: We use `type` here instead of `object` because `type` does not validate\n// the keys of the object, which is what we want.\nconst BaseParamsStruct = type({\n type: enums([DialogType.Alert, DialogType.Confirmation, DialogType.Prompt]),\n});\n\nconst AlertParametersStruct = object({\n type: enumValue(DialogType.Alert),\n content: ComponentStruct,\n});\n\nconst ConfirmationParametersStruct = object({\n type: enumValue(DialogType.Confirmation),\n content: ComponentStruct,\n});\n\nconst PromptParametersStruct = object({\n type: enumValue(DialogType.Prompt),\n content: ComponentStruct,\n placeholder: PlaceholderStruct,\n});\n\nconst DialogParametersStruct = union([\n AlertParametersStruct,\n ConfirmationParametersStruct,\n PromptParametersStruct,\n]);\n\nexport type DialogParameters = Infer<typeof DialogParametersStruct>;\n\nconst structs = {\n [DialogType.Alert]: AlertParametersStruct,\n [DialogType.Confirmation]: ConfirmationParametersStruct,\n [DialogType.Prompt]: PromptParametersStruct,\n};\n\n/**\n * Builds the method implementation for `snap_dialog`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.showDialog - A function that shows the specified dialog in the\n * MetaMask UI and returns the appropriate value for the dialog type.\n * @returns The method implementation which return value depends on the dialog\n * type, valid return types are: string, boolean, null.\n */\nexport function getDialogImplementation({ showDialog }: DialogMethodHooks) {\n return async function dialogImplementation(\n args: RestrictedMethodOptions<DialogParameters>,\n ): Promise<boolean | null | string> {\n const {\n params,\n context: { origin },\n } = args;\n\n const validatedType = getValidatedType(params);\n const validatedParams = getValidatedParams(params, structs[validatedType]);\n\n const { content } = validatedParams;\n\n const placeholder =\n validatedParams.type === DialogType.Prompt\n ? validatedParams.placeholder\n : undefined;\n\n return showDialog(origin, validatedType, content, placeholder);\n };\n}\n\n/**\n * Get the validated type of the dialog parameters. Throws an error if the type\n * is invalid.\n *\n * @param params - The parameters to validate.\n * @returns The validated type of the dialog parameters.\n */\nfunction getValidatedType(params: unknown): DialogType {\n try {\n return create(params, BaseParamsStruct).type;\n } catch (error) {\n throw rpcErrors.invalidParams({\n message: `The \"type\" property must be one of: ${Object.values(\n DialogType,\n ).join(', ')}.`,\n });\n }\n}\n\n/**\n * Validates the confirm 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 struct - The struct to validate the params against.\n * @returns The validated confirm method parameter object.\n */\nfunction getValidatedParams(\n params: unknown,\n struct: Struct<any>,\n): DialogParameters {\n try {\n return create(params, struct);\n } catch (error) {\n if (error instanceof StructError) {\n const { key, type: errorType } = error;\n\n if (key === 'placeholder' && errorType === 'never') {\n throw rpcErrors.invalidParams({\n message:\n 'Invalid params: Alerts or confirmations may not specify a \"placeholder\" field.',\n });\n }\n\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n"],"names":["PermissionType","SubjectType","rpcErrors","ComponentStruct","enumValue","create","enums","object","optional","size","string","StructError","type","union","methodName","DialogType","Alert","Confirmation","Prompt","PlaceholderStruct","specificationBuilder","allowedCaveats","methodHooks","permissionType","RestrictedMethod","targetName","methodImplementation","getDialogImplementation","subjectTypes","Snap","showDialog","dialogBuilder","Object","freeze","BaseParamsStruct","AlertParametersStruct","content","ConfirmationParametersStruct","PromptParametersStruct","placeholder","DialogParametersStruct","structs","dialogImplementation","args","params","context","origin","validatedType","getValidatedType","validatedParams","getValidatedParams","undefined","error","invalidParams","message","values","join","struct","key","errorType","internal"],"mappings":"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"}
1
+ {"version":3,"sources":["../../../src/restricted/dialog.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 type { Component } from '@metamask/snaps-ui';\nimport { ComponentStruct, assertUILinksAreSafe } from '@metamask/snaps-ui';\nimport type { EnumToUnion } from '@metamask/snaps-utils';\nimport { enumValue } from '@metamask/snaps-utils';\nimport type { NonEmptyArray } from '@metamask/utils';\nimport type { Infer, Struct } from 'superstruct';\nimport {\n create,\n enums,\n object,\n optional,\n size,\n string,\n StructError,\n type,\n union,\n} from 'superstruct';\n\nimport { type MethodHooksObject } from '../utils';\n\nconst methodName = 'snap_dialog';\n\nexport enum DialogType {\n Alert = 'alert',\n Confirmation = 'confirmation',\n Prompt = 'prompt',\n}\n\nconst PlaceholderStruct = optional(size(string(), 1, 40));\n\nexport type Placeholder = Infer<typeof PlaceholderStruct>;\n\ntype ShowDialog = (\n snapId: string,\n type: EnumToUnion<DialogType>,\n content: Component,\n placeholder?: Placeholder,\n) => Promise<null | boolean | string>;\n\ntype MaybeUpdatePhisingList = () => Promise<void>;\ntype IsOnPhishingList = (url: string) => boolean;\n\nexport type DialogMethodHooks = {\n /**\n * @param snapId - The ID of the Snap that created the alert.\n * @param type - The dialog type.\n * @param content - The dialog custom UI.\n * @param placeholder - The placeholder for the Prompt dialog input.\n */\n showDialog: ShowDialog;\n\n maybeUpdatePhishingList: MaybeUpdatePhisingList;\n\n /**\n * @param url - The URL to check against the phishing list.\n */\n isOnPhishingList: IsOnPhishingList;\n};\n\ntype DialogSpecificationBuilderOptions = {\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n methodHooks: DialogMethodHooks;\n};\n\ntype DialogSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.RestrictedMethod;\n targetName: typeof methodName;\n methodImplementation: ReturnType<typeof getDialogImplementation>;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * The specification builder for the `snap_dialog` permission. `snap_dialog`\n * lets the Snap display one of the following dialogs to the user:\n * - An alert, for displaying information.\n * - A confirmation, for accepting or rejecting some action.\n * - A prompt, for inputting some information.\n *\n * @param options - The specification builder options.\n * @param options.allowedCaveats - The optional allowed caveats for the\n * permission.\n * @param options.methodHooks - The RPC method hooks needed by the method\n * implementation.\n * @returns The specification for the `snap_dialog` permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.RestrictedMethod,\n DialogSpecificationBuilderOptions,\n DialogSpecification\n> = ({\n allowedCaveats = null,\n methodHooks,\n}: DialogSpecificationBuilderOptions) => {\n return {\n permissionType: PermissionType.RestrictedMethod,\n targetName: methodName,\n allowedCaveats,\n methodImplementation: getDialogImplementation(methodHooks),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nconst methodHooks: MethodHooksObject<DialogMethodHooks> = {\n showDialog: true,\n isOnPhishingList: true,\n maybeUpdatePhishingList: true,\n};\n\nexport const dialogBuilder = Object.freeze({\n targetName: methodName,\n specificationBuilder,\n methodHooks,\n} as const);\n\n// Note: We use `type` here instead of `object` because `type` does not validate\n// the keys of the object, which is what we want.\nconst BaseParamsStruct = type({\n type: enums([DialogType.Alert, DialogType.Confirmation, DialogType.Prompt]),\n});\n\nconst AlertParametersStruct = object({\n type: enumValue(DialogType.Alert),\n content: ComponentStruct,\n});\n\nconst ConfirmationParametersStruct = object({\n type: enumValue(DialogType.Confirmation),\n content: ComponentStruct,\n});\n\nconst PromptParametersStruct = object({\n type: enumValue(DialogType.Prompt),\n content: ComponentStruct,\n placeholder: PlaceholderStruct,\n});\n\nconst DialogParametersStruct = union([\n AlertParametersStruct,\n ConfirmationParametersStruct,\n PromptParametersStruct,\n]);\n\nexport type DialogParameters = Infer<typeof DialogParametersStruct>;\n\nconst structs = {\n [DialogType.Alert]: AlertParametersStruct,\n [DialogType.Confirmation]: ConfirmationParametersStruct,\n [DialogType.Prompt]: PromptParametersStruct,\n};\n\n/**\n * Builds the method implementation for `snap_dialog`.\n *\n * @param hooks - The RPC method hooks.\n * @param hooks.showDialog - A function that shows the specified dialog in the\n * MetaMask UI and returns the appropriate value for the dialog type.\n * @param hooks.isOnPhishingList - A function that checks a link against the\n * phishing list and return true if it's in, otherwise false.\n * @param hooks.maybeUpdatePhishingList - A function that updates the phishing list if needed.\n * @returns The method implementation which return value depends on the dialog\n * type, valid return types are: string, boolean, null.\n */\nexport function getDialogImplementation({\n showDialog,\n isOnPhishingList,\n maybeUpdatePhishingList,\n}: DialogMethodHooks) {\n return async function dialogImplementation(\n args: RestrictedMethodOptions<DialogParameters>,\n ): Promise<boolean | null | string> {\n const {\n params,\n context: { origin },\n } = args;\n\n const validatedType = getValidatedType(params);\n const validatedParams = getValidatedParams(params, structs[validatedType]);\n\n const { content } = validatedParams;\n\n await maybeUpdatePhishingList();\n\n assertUILinksAreSafe(content, isOnPhishingList);\n\n const placeholder =\n validatedParams.type === DialogType.Prompt\n ? validatedParams.placeholder\n : undefined;\n\n return showDialog(origin, validatedType, content, placeholder);\n };\n}\n\n/**\n * Get the validated type of the dialog parameters. Throws an error if the type\n * is invalid.\n *\n * @param params - The parameters to validate.\n * @returns The validated type of the dialog parameters.\n */\nfunction getValidatedType(params: unknown): DialogType {\n try {\n return create(params, BaseParamsStruct).type;\n } catch (error) {\n throw rpcErrors.invalidParams({\n message: `The \"type\" property must be one of: ${Object.values(\n DialogType,\n ).join(', ')}.`,\n });\n }\n}\n\n/**\n * Validates the confirm 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 struct - The struct to validate the params against.\n * @returns The validated confirm method parameter object.\n */\nfunction getValidatedParams(\n params: unknown,\n struct: Struct<any>,\n): DialogParameters {\n try {\n return create(params, struct);\n } catch (error) {\n if (error instanceof StructError) {\n const { key, type: errorType } = error;\n\n if (key === 'placeholder' && errorType === 'never') {\n throw rpcErrors.invalidParams({\n message:\n 'Invalid params: Alerts or confirmations may not specify a \"placeholder\" field.',\n });\n }\n\n throw rpcErrors.invalidParams({\n message: `Invalid params: ${error.message}.`,\n });\n }\n\n /* istanbul ignore next */\n throw rpcErrors.internal();\n }\n}\n"],"names":["PermissionType","SubjectType","rpcErrors","ComponentStruct","assertUILinksAreSafe","enumValue","create","enums","object","optional","size","string","StructError","type","union","methodName","DialogType","Alert","Confirmation","Prompt","PlaceholderStruct","specificationBuilder","allowedCaveats","methodHooks","permissionType","RestrictedMethod","targetName","methodImplementation","getDialogImplementation","subjectTypes","Snap","showDialog","isOnPhishingList","maybeUpdatePhishingList","dialogBuilder","Object","freeze","BaseParamsStruct","AlertParametersStruct","content","ConfirmationParametersStruct","PromptParametersStruct","placeholder","DialogParametersStruct","structs","dialogImplementation","args","params","context","origin","validatedType","getValidatedType","validatedParams","getValidatedParams","undefined","error","invalidParams","message","values","join","struct","key","errorType","internal"],"mappings":"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"}
@@ -1,5 +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-utils';
3
4
  import { isObject } from '@metamask/utils';
4
5
  const methodName = 'snap_notify';
5
6
  export var NotificationType;
@@ -28,7 +29,9 @@ export var NotificationType;
28
29
  };
29
30
  const methodHooks = {
30
31
  showNativeNotification: true,
31
- showInAppNotification: true
32
+ showInAppNotification: true,
33
+ isOnPhishingList: true,
34
+ maybeUpdatePhishingList: true
32
35
  };
33
36
  export const notifyBuilder = Object.freeze({
34
37
  targetName: methodName,
@@ -41,12 +44,16 @@ export const notifyBuilder = Object.freeze({
41
44
  * @param hooks - The RPC method hooks.
42
45
  * @param hooks.showNativeNotification - A function that shows a native browser notification.
43
46
  * @param hooks.showInAppNotification - A function that shows a notification in the MetaMask UI.
47
+ * @param hooks.isOnPhishingList - A function that checks for links against the phishing list.
48
+ * @param hooks.maybeUpdatePhishingList - A function that updates the phishing list if needed.
44
49
  * @returns The method implementation which returns `null` on success.
45
50
  * @throws If the params are invalid.
46
- */ export function getImplementation({ showNativeNotification, showInAppNotification }) {
51
+ */ export function getImplementation({ showNativeNotification, showInAppNotification, isOnPhishingList, maybeUpdatePhishingList }) {
47
52
  return async function implementation(args) {
48
53
  const { params, context: { origin } } = args;
49
54
  const validatedParams = getValidatedParams(params);
55
+ await maybeUpdatePhishingList();
56
+ assertLinksAreSafe(validatedParams.message, isOnPhishingList);
50
57
  switch(validatedParams.type){
51
58
  case NotificationType.Native:
52
59
  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 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\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};\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 * @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}: 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 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","isObject","methodName","NotificationType","InApp","Native","specificationBuilder","allowedCaveats","methodHooks","permissionType","RestrictedMethod","targetName","methodImplementation","getImplementation","subjectTypes","Snap","showNativeNotification","showInAppNotification","notifyBuilder","Object","freeze","implementation","args","params","context","origin","validatedParams","getValidatedParams","type","invalidParams","message","values","includes","length"],"mappings":"AAKA,SAASA,cAAc,EAAEC,WAAW,QAAQ,kCAAkC;AAC9E,SAASC,SAAS,QAAQ,uBAAuB;AAGjD,SAASC,QAAQ,QAAQ,kBAAkB;AAI3C,MAAMC,aAAa;WAIZ;UAAKC,gBAAgB;IAAhBA,iBACVC,WAAQ;IADED,iBAEVE,YAAS;GAFCF,qBAAAA;AAiDZ;;;;;;;;CAQC,GACD,OAAO,MAAMG,uBAIT,CAAC,EAAEC,iBAAiB,IAAI,EAAEC,WAAW,EAA+B;IACtE,OAAO;QACLC,gBAAgBX,eAAeY,gBAAgB;QAC/CC,YAAYT;QACZK;QACAK,sBAAsBC,kBAAkBL;QACxCM,cAAc;YAACf,YAAYgB,IAAI;SAAC;IAClC;AACF,EAAE;AAEF,MAAMP,cAAoD;IACxDQ,wBAAwB;IACxBC,uBAAuB;AACzB;AAEA,OAAO,MAAMC,gBAAgBC,OAAOC,MAAM,CAAC;IACzCT,YAAYT;IACZI;IACAE;AACF,GAAY;AAEZ;;;;;;;;CAQC,GACD,OAAO,SAASK,kBAAkB,EAChCG,sBAAsB,EACtBC,qBAAqB,EACH;IAClB,OAAO,eAAeI,eACpBC,IAA+C;QAE/C,MAAM,EACJC,MAAM,EACNC,SAAS,EAAEC,MAAM,EAAE,EACpB,GAAGH;QAEJ,MAAMI,kBAAkBC,mBAAmBJ;QAE3C,OAAQG,gBAAgBE,IAAI;YAC1B,KAAKzB,iBAAiBE,MAAM;gBAC1B,OAAO,MAAMW,uBAAuBS,QAAQC;YAC9C,KAAKvB,iBAAiBC,KAAK;gBACzB,OAAO,MAAMa,sBAAsBQ,QAAQC;YAC7C;gBACE,MAAM1B,UAAU6B,aAAa,CAAC;oBAC5BC,SAAS;gBACX;QACJ;IACF;AACF;AAEA;;;;;;CAMC,GACD,OAAO,SAASH,mBAAmBJ,MAAe;IAChD,IAAI,CAACtB,SAASsB,SAAS;QACrB,MAAMvB,UAAU6B,aAAa,CAAC;YAC5BC,SAAS;QACX;IACF;IAEA,MAAM,EAAEF,IAAI,EAAEE,OAAO,EAAE,GAAGP;IAE1B,IACE,CAACK,QACD,OAAOA,SAAS,YAChB,CAACT,OAAOY,MAAM,CAAC5B,kBAAkB6B,QAAQ,CAACJ,OAC1C;QACA,MAAM5B,UAAU6B,aAAa,CAAC;YAC5BC,SAAS;QACX;IACF;IAEA,+DAA+D;IAC/D,IAAI,CAACA,WAAW,OAAOA,YAAY,YAAYA,QAAQG,MAAM,IAAI,IAAI;QACnE,MAAMjC,UAAU6B,aAAa,CAAC;YAC5BC,SACE;QACJ;IACF;IAEA,OAAOP;AACT"}
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 type { EnumToUnion } from '@metamask/snaps-utils';\nimport { assertLinksAreSafe } 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;AAEjD,SAASC,kBAAkB,QAAQ,wBAAwB;AAE3D,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"}
@@ -4,7 +4,7 @@ import type { Component } from '@metamask/snaps-ui';
4
4
  import type { EnumToUnion } from '@metamask/snaps-utils';
5
5
  import type { NonEmptyArray } from '@metamask/utils';
6
6
  import type { Infer, Struct } from 'superstruct';
7
- import type { MethodHooksObject } from '../utils';
7
+ import { type MethodHooksObject } from '../utils';
8
8
  declare const methodName = "snap_dialog";
9
9
  export declare enum DialogType {
10
10
  Alert = "alert",
@@ -14,6 +14,8 @@ export declare enum DialogType {
14
14
  declare const PlaceholderStruct: Struct<string | undefined, null>;
15
15
  export declare type Placeholder = Infer<typeof PlaceholderStruct>;
16
16
  declare type ShowDialog = (snapId: string, type: EnumToUnion<DialogType>, content: Component, placeholder?: Placeholder) => Promise<null | boolean | string>;
17
+ declare type MaybeUpdatePhisingList = () => Promise<void>;
18
+ declare type IsOnPhishingList = (url: string) => boolean;
17
19
  export declare type DialogMethodHooks = {
18
20
  /**
19
21
  * @param snapId - The ID of the Snap that created the alert.
@@ -22,6 +24,11 @@ export declare type DialogMethodHooks = {
22
24
  * @param placeholder - The placeholder for the Prompt dialog input.
23
25
  */
24
26
  showDialog: ShowDialog;
27
+ maybeUpdatePhishingList: MaybeUpdatePhisingList;
28
+ /**
29
+ * @param url - The URL to check against the phishing list.
30
+ */
31
+ isOnPhishingList: IsOnPhishingList;
25
32
  };
26
33
  declare type DialogSpecificationBuilderOptions = {
27
34
  allowedCaveats?: Readonly<NonEmptyArray<string>> | null;
@@ -106,8 +113,11 @@ export declare type DialogParameters = Infer<typeof DialogParametersStruct>;
106
113
  * @param hooks - The RPC method hooks.
107
114
  * @param hooks.showDialog - A function that shows the specified dialog in the
108
115
  * MetaMask UI and returns the appropriate value for the dialog type.
116
+ * @param hooks.isOnPhishingList - A function that checks a link against the
117
+ * phishing list and return true if it's in, otherwise false.
118
+ * @param hooks.maybeUpdatePhishingList - A function that updates the phishing list if needed.
109
119
  * @returns The method implementation which return value depends on the dialog
110
120
  * type, valid return types are: string, boolean, null.
111
121
  */
112
- export declare function getDialogImplementation({ showDialog }: DialogMethodHooks): (args: RestrictedMethodOptions<DialogParameters>) => Promise<boolean | null | string>;
122
+ export declare function getDialogImplementation({ showDialog, isOnPhishingList, maybeUpdatePhishingList, }: DialogMethodHooks): (args: RestrictedMethodOptions<DialogParameters>) => Promise<boolean | null | string>;
113
123
  export {};
@@ -2,7 +2,7 @@ import type { PermissionSpecificationBuilder, RestrictedMethodOptions, ValidPerm
2
2
  import { PermissionType } from '@metamask/permission-controller';
3
3
  import type { EnumToUnion } from '@metamask/snaps-utils';
4
4
  import type { NonEmptyArray } from '@metamask/utils';
5
- import type { MethodHooksObject } from '../utils';
5
+ import { type MethodHooksObject } from '../utils';
6
6
  declare const methodName = "snap_notify";
7
7
  export declare enum NotificationType {
8
8
  InApp = "inApp",
@@ -29,6 +29,8 @@ export declare type NotifyMethodHooks = {
29
29
  * @param args - The notification arguments.
30
30
  */
31
31
  showInAppNotification: (snapId: string, args: NotificationArgs) => Promise<null>;
32
+ isOnPhishingList: (url: string) => boolean;
33
+ maybeUpdatePhishingList: () => Promise<void>;
32
34
  };
33
35
  declare type SpecificationBuilderOptions = {
34
36
  allowedCaveats?: Readonly<NonEmptyArray<string>> | null;
@@ -66,10 +68,12 @@ export declare const notifyBuilder: Readonly<{
66
68
  * @param hooks - The RPC method hooks.
67
69
  * @param hooks.showNativeNotification - A function that shows a native browser notification.
68
70
  * @param hooks.showInAppNotification - A function that shows a notification in the MetaMask UI.
71
+ * @param hooks.isOnPhishingList - A function that checks for links against the phishing list.
72
+ * @param hooks.maybeUpdatePhishingList - A function that updates the phishing list if needed.
69
73
  * @returns The method implementation which returns `null` on success.
70
74
  * @throws If the params are invalid.
71
75
  */
72
- export declare function getImplementation({ showNativeNotification, showInAppNotification, }: NotifyMethodHooks): (args: RestrictedMethodOptions<NotificationArgs>) => Promise<null>;
76
+ export declare function getImplementation({ showNativeNotification, showInAppNotification, isOnPhishingList, maybeUpdatePhishingList, }: NotifyMethodHooks): (args: RestrictedMethodOptions<NotificationArgs>) => Promise<null>;
73
77
  /**
74
78
  * Validates the notify method `params` and returns them cast to the correct
75
79
  * type. Throws if validation fails.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/snaps-rpc-methods",
3
- "version": "3.1.0",
3
+ "version": "3.2.0",
4
4
  "description": "MetaMask Snaps JSON-RPC method implementations.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -39,8 +39,8 @@
39
39
  "@metamask/key-tree": "^9.0.0",
40
40
  "@metamask/permission-controller": "^5.0.0",
41
41
  "@metamask/rpc-errors": "^6.1.0",
42
- "@metamask/snaps-ui": "^3.0.1",
43
- "@metamask/snaps-utils": "^3.1.0",
42
+ "@metamask/snaps-ui": "^3.1.0",
43
+ "@metamask/snaps-utils": "^3.2.0",
44
44
  "@metamask/utils": "^8.1.0",
45
45
  "@noble/hashes": "^1.3.1",
46
46
  "superstruct": "^1.0.3"