From 9e6a96f9f341c5c800649d8b3fa0ba467f454b65 Mon Sep 17 00:00:00 2001 From: Guillaume Roux Date: Wed, 23 Nov 2022 15:09:41 +0100 Subject: [PATCH 1/3] add placeholder parameter for Prompt dialog --- packages/rpc-methods/jest.config.js | 6 +-- .../rpc-methods/src/restricted/dialog.test.ts | 52 +++++++++++++++++++ packages/rpc-methods/src/restricted/dialog.ts | 20 +++++-- 3 files changed, 71 insertions(+), 7 deletions(-) diff --git a/packages/rpc-methods/jest.config.js b/packages/rpc-methods/jest.config.js index a571703b0a..7420b911df 100644 --- a/packages/rpc-methods/jest.config.js +++ b/packages/rpc-methods/jest.config.js @@ -6,10 +6,10 @@ module.exports = deepmerge(baseConfig, { coveragePathIgnorePatterns: ['./src/index.ts'], coverageThreshold: { global: { - branches: 86.84, + branches: 87.09, functions: 87.27, - lines: 83.18, - statements: 83.18, + lines: 83.27, + statements: 83.27, }, }, testTimeout: 2500, diff --git a/packages/rpc-methods/src/restricted/dialog.test.ts b/packages/rpc-methods/src/restricted/dialog.test.ts index a18129c94e..b7f75b1c36 100644 --- a/packages/rpc-methods/src/restricted/dialog.test.ts +++ b/packages/rpc-methods/src/restricted/dialog.test.ts @@ -108,6 +108,7 @@ describe('implementation', () => { fields: { title: 'Foo', description: 'Bar', + placeholder: 'Baz', }, }, }); @@ -116,6 +117,7 @@ describe('implementation', () => { expect(hooks.showDialog).toHaveBeenCalledWith('foo', DialogType.Prompt, { title: 'Foo', description: 'Bar', + placeholder: 'Baz', }); }); }); @@ -307,6 +309,31 @@ describe('implementation', () => { }, ); + it.each([true, 2, [], {}, new (class {})()])( + 'rejects invalid placeholder contents', + async (value) => { + const hooks = getMockDialogHooks(); + const implementation = getDialogImplementation(hooks); + + await expect( + implementation({ + context: { origin: 'foo' }, + method: 'snap_dialog', + params: { + type: DialogType.Prompt, + fields: { + title: 'Foo', + description: 'Bar', + placeholder: value, + } as any, + }, + }), + ).rejects.toThrow( + /Invalid params: At path: fields\.placeholder -- Expected a string, but received: .*\./u, + ); + }, + ); + it('rejects too long text area contents', async () => { const hooks = getMockDialogHooks(); const implementation = getDialogImplementation(hooks); @@ -349,5 +376,30 @@ describe('implementation', () => { 'Invalid params: Prompts may not specify a "textAreaContent" field.', ); }); + + it.each([DialogType.Alert, DialogType.Confirmation])( + 'rejects placehoder field for alerts and confirmations', + async (type) => { + const hooks = getMockDialogHooks(); + const implementation = getDialogImplementation(hooks); + await expect( + implementation({ + context: { origin: 'foo' }, + method: 'snap_dialog', + params: { + type, + fields: { + title: 'Foo', + description: 'Bar', + textAreaContent: 'Baz', + placeholder: 'Foobar', + } as any, + }, + }), + ).rejects.toThrow( + 'Invalid params: Alerts or Confirmations may not specify a "placeholder" field.', + ); + }, + ); }); }); diff --git a/packages/rpc-methods/src/restricted/dialog.ts b/packages/rpc-methods/src/restricted/dialog.ts index b38dbd3a2c..d28c1484fb 100644 --- a/packages/rpc-methods/src/restricted/dialog.ts +++ b/packages/rpc-methods/src/restricted/dialog.ts @@ -34,10 +34,15 @@ const BaseFieldsStruct = object({ title: size(string(), 1, 40), description: optional(size(string(), 1, 140)), textAreaContent: optional(size(string(), 1, 1800)), + placeholder: optional(size(string(), 1, 40)), }); const PromptFieldsStruct = omit(BaseFieldsStruct, ['textAreaContent']); +const AlertFieldsStruct = omit(BaseFieldsStruct, ['placeholder']); + +const ConfirmationFieldsStruct = omit(BaseFieldsStruct, ['placeholder']); + /** * @property title - The alert title, no greater than 40 characters long. * @property description - A description, displayed with the title, no greater @@ -45,7 +50,7 @@ const PromptFieldsStruct = omit(BaseFieldsStruct, ['textAreaContent']); * @property textAreaContent - Free-from text content, no greater than 1800 * characters long. */ -export type AlertFields = Infer; +export type AlertFields = Infer; /** * @property title - A question describing what the user is confirming, no @@ -55,7 +60,7 @@ export type AlertFields = Infer; * @property textAreaContent - Free-from text content, no greater than 1800 * characters long. */ -export type ConfirmationFields = Infer; +export type ConfirmationFields = Infer; /** * @property title - The prompt title, no greater than 40 characters long. @@ -139,12 +144,12 @@ const BaseParamsStruct = type({ const AlertParametersStruct = object({ type: literal(DialogType.Alert), - fields: BaseFieldsStruct, + fields: AlertFieldsStruct, }); const ConfirmationParametersStruct = object({ type: literal(DialogType.Confirmation), - fields: BaseFieldsStruct, + fields: ConfirmationFieldsStruct, }); const PromptParametersStruct = object({ @@ -235,6 +240,13 @@ function getValidatedParams( }); } + if (key === 'placeholder' && errorType === 'never') { + throw ethErrors.rpc.invalidParams({ + message: + 'Invalid params: Alerts or Confirmations may not specify a "placeholder" field.', + }); + } + throw ethErrors.rpc.invalidParams({ message: `Invalid params: ${error.message}.`, }); From 508040a87674a04f103b909469b5e32c2758b58a Mon Sep 17 00:00:00 2001 From: Guillaume Roux Date: Wed, 23 Nov 2022 15:27:58 +0100 Subject: [PATCH 2/3] fix typo in tests --- packages/rpc-methods/src/restricted/dialog.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rpc-methods/src/restricted/dialog.test.ts b/packages/rpc-methods/src/restricted/dialog.test.ts index b7f75b1c36..efbab6a40a 100644 --- a/packages/rpc-methods/src/restricted/dialog.test.ts +++ b/packages/rpc-methods/src/restricted/dialog.test.ts @@ -378,7 +378,7 @@ describe('implementation', () => { }); it.each([DialogType.Alert, DialogType.Confirmation])( - 'rejects placehoder field for alerts and confirmations', + 'rejects placeholder field for alerts and confirmations', async (type) => { const hooks = getMockDialogHooks(); const implementation = getDialogImplementation(hooks); From c8120c3c0012b65f81cf7ceeed8a59df48beac73 Mon Sep 17 00:00:00 2001 From: Guillaume Roux Date: Wed, 23 Nov 2022 15:31:42 +0100 Subject: [PATCH 3/3] fix typo in dialog errors --- packages/rpc-methods/src/restricted/dialog.test.ts | 2 +- packages/rpc-methods/src/restricted/dialog.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/rpc-methods/src/restricted/dialog.test.ts b/packages/rpc-methods/src/restricted/dialog.test.ts index efbab6a40a..d12d218777 100644 --- a/packages/rpc-methods/src/restricted/dialog.test.ts +++ b/packages/rpc-methods/src/restricted/dialog.test.ts @@ -397,7 +397,7 @@ describe('implementation', () => { }, }), ).rejects.toThrow( - 'Invalid params: Alerts or Confirmations may not specify a "placeholder" field.', + 'Invalid params: Alerts or confirmations may not specify a "placeholder" field.', ); }, ); diff --git a/packages/rpc-methods/src/restricted/dialog.ts b/packages/rpc-methods/src/restricted/dialog.ts index d28c1484fb..c8963d1622 100644 --- a/packages/rpc-methods/src/restricted/dialog.ts +++ b/packages/rpc-methods/src/restricted/dialog.ts @@ -243,7 +243,7 @@ function getValidatedParams( if (key === 'placeholder' && errorType === 'never') { throw ethErrors.rpc.invalidParams({ message: - 'Invalid params: Alerts or Confirmations may not specify a "placeholder" field.', + 'Invalid params: Alerts or confirmations may not specify a "placeholder" field.', }); }