From ea7596d144fbb0ad2c52a418cdf2853ec68d22df Mon Sep 17 00:00:00 2001 From: Frederik Bolding Date: Tue, 30 Apr 2024 17:06:58 +0200 Subject: [PATCH] fix: make onUserInput export optional --- .../common/BaseSnapExecutor.test.browser.ts | 41 +++++++++++++++++++ packages/snaps-utils/src/handlers.ts | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/packages/snaps-execution-environments/src/common/BaseSnapExecutor.test.browser.ts b/packages/snaps-execution-environments/src/common/BaseSnapExecutor.test.browser.ts index 6b3bff43e4..fe6d4a9b10 100644 --- a/packages/snaps-execution-environments/src/common/BaseSnapExecutor.test.browser.ts +++ b/packages/snaps-execution-environments/src/common/BaseSnapExecutor.test.browser.ts @@ -1567,6 +1567,47 @@ describe('BaseSnapExecutor', () => { }); }); + it('returns null if no onUserInput export is found', async () => { + const CODE = ` + module.exports.onRpcRequest = () => {} + `; + + const executor = new TestSnapExecutor(); + await executor.executeSnap(1, MOCK_SNAP_ID, CODE, []); + + expect(await executor.readCommand()).toStrictEqual({ + jsonrpc: '2.0', + id: 1, + result: 'OK', + }); + + const params = { + id: 'foo', + event: { + type: UserInputEventType.ButtonClickEvent, + name: 'bar', + }, + }; + + await executor.writeCommand({ + jsonrpc: '2.0', + id: 2, + method: 'snapRpc', + params: [ + MOCK_SNAP_ID, + HandlerType.OnUserInput, + MOCK_ORIGIN, + { jsonrpc: '2.0', method: 'foo', params }, + ], + }); + + expect(await executor.readCommand()).toStrictEqual({ + id: 2, + jsonrpc: '2.0', + result: null, + }); + }); + describe('lifecycle hooks', () => { const LIFECYCLE_HOOKS = [HandlerType.OnInstall, HandlerType.OnUpdate]; diff --git a/packages/snaps-utils/src/handlers.ts b/packages/snaps-utils/src/handlers.ts index 6c8262b5de..0cde673514 100644 --- a/packages/snaps-utils/src/handlers.ts +++ b/packages/snaps-utils/src/handlers.ts @@ -98,7 +98,7 @@ export const SNAP_EXPORTS = { }, [HandlerType.OnUserInput]: { type: HandlerType.OnUserInput, - required: true, + required: false, validator: (snapExport: unknown): snapExport is OnUserInputHandler => { return typeof snapExport === 'function'; },