From 0e044db6c2a59097139060daeca49f5f27b24db4 Mon Sep 17 00:00:00 2001 From: Frederik Bolding Date: Thu, 2 Nov 2023 10:16:21 +0100 Subject: [PATCH] Add source parameter to OutboundRequest and OutboundResponse --- .../common/BaseSnapExecutor.test.browser.ts | 20 ++++++++++++++++ .../src/common/BaseSnapExecutor.ts | 24 +++++++++++++++---- .../src/common/endowments/network.test.ts | 14 +++++++++-- .../src/common/endowments/network.ts | 22 ++++++++++------- 4 files changed, 65 insertions(+), 15 deletions(-) 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 0748518f88..dbf2dc34b7 100644 --- a/packages/snaps-execution-environments/src/common/BaseSnapExecutor.test.browser.ts +++ b/packages/snaps-execution-environments/src/common/BaseSnapExecutor.test.browser.ts @@ -191,6 +191,7 @@ describe('BaseSnapExecutor', () => { expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundRequest', + params: { source: 'ethereum.request' }, }); const blockNumRequest = await executor.readRpc(); @@ -217,6 +218,7 @@ describe('BaseSnapExecutor', () => { expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundResponse', + params: { source: 'ethereum.request' }, }); expect(await executor.readCommand()).toStrictEqual({ @@ -255,6 +257,7 @@ describe('BaseSnapExecutor', () => { expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundRequest', + params: { source: 'snap.request' }, }); const walletRequest = await executor.readRpc(); @@ -281,6 +284,7 @@ describe('BaseSnapExecutor', () => { expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundResponse', + params: { source: 'snap.request' }, }); expect(await executor.readCommand()).toStrictEqual({ @@ -416,6 +420,7 @@ describe('BaseSnapExecutor', () => { expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundRequest', + params: { source: 'ethereum.request' }, }); const blockNumRequest = await executor.readRpc(); @@ -442,6 +447,7 @@ describe('BaseSnapExecutor', () => { expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundResponse', + params: { source: 'ethereum.request' }, }); expect(await executor.readCommand()).toStrictEqual({ @@ -482,6 +488,7 @@ describe('BaseSnapExecutor', () => { expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundRequest', + params: { source: 'snap.request' }, }); const getSnapsRequest = await executor.readRpc(); @@ -516,6 +523,7 @@ describe('BaseSnapExecutor', () => { expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundResponse', + params: { source: 'snap.request' }, }); expect(await executor.readCommand()).toStrictEqual({ @@ -898,6 +906,7 @@ describe('BaseSnapExecutor', () => { expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundRequest', + params: { source: 'snap.request' }, }); const request = await executor.readRpc(); @@ -930,6 +939,7 @@ describe('BaseSnapExecutor', () => { expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundResponse', + params: { source: 'snap.request' }, }); expect(await executor.readCommand()).toStrictEqual({ @@ -973,6 +983,7 @@ describe('BaseSnapExecutor', () => { expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundRequest', + params: { source: 'ethereum.request' }, }); const request = await executor.readRpc(); @@ -1007,6 +1018,7 @@ describe('BaseSnapExecutor', () => { expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundResponse', + params: { source: 'ethereum.request' }, }); expect(await executor.readCommand()).toStrictEqual({ @@ -1051,21 +1063,25 @@ describe('BaseSnapExecutor', () => { expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundRequest', + params: { source: 'fetch' }, }); expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundResponse', + params: { source: 'fetch' }, }); expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundRequest', + params: { source: 'fetch' }, }); expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundResponse', + params: { source: 'fetch' }, }); expect(await executor.readCommand()).toStrictEqual({ @@ -1624,6 +1640,7 @@ describe('BaseSnapExecutor', () => { expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundRequest', + params: { source: 'ethereum.request' }, }); const blockNumRequest = await executor.readRpc(); @@ -1731,6 +1748,7 @@ describe('BaseSnapExecutor', () => { expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundRequest', + params: { source: 'ethereum.request' }, }); const blockNumRequest = await executor.readRpc(); @@ -1823,6 +1841,7 @@ describe('BaseSnapExecutor', () => { expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundRequest', + params: { source: 'ethereum.request' }, }); const blockNumRequest = await executor.readRpc(); @@ -1852,6 +1871,7 @@ describe('BaseSnapExecutor', () => { expect(await executor.readCommand()).toStrictEqual({ jsonrpc: '2.0', method: 'OutboundResponse', + params: { source: 'ethereum.request' }, }); expect(await executor.readCommand()).toStrictEqual({ diff --git a/packages/snaps-execution-environments/src/common/BaseSnapExecutor.ts b/packages/snaps-execution-environments/src/common/BaseSnapExecutor.ts index 3460e6bfe0..e1c8e4829b 100644 --- a/packages/snaps-execution-environments/src/common/BaseSnapExecutor.ts +++ b/packages/snaps-execution-environments/src/common/BaseSnapExecutor.ts @@ -110,7 +110,9 @@ const EXECUTION_ENVIRONMENT_METHODS = { type Methods = typeof EXECUTION_ENVIRONMENT_METHODS; -export type NotifyFunction = (notification: Omit) => Promise; +export type NotifyFunction = ( + notification: Omit, +) => Promise; export class BaseSnapExecutor { private readonly snapData: Map; @@ -457,11 +459,17 @@ export class BaseSnapExecutor { assertSnapOutboundRequest(sanitizedArgs); return await withTeardown( (async () => { - await this.#notify({ method: 'OutboundRequest' }); + await this.#notify({ + method: 'OutboundRequest', + params: { source: 'snap.request' }, + }); try { return await originalRequest(sanitizedArgs); } finally { - await this.#notify({ method: 'OutboundResponse' }); + await this.#notify({ + method: 'OutboundResponse', + params: { source: 'snap.request' }, + }); } })(), this as any, @@ -503,11 +511,17 @@ export class BaseSnapExecutor { assertEthereumOutboundRequest(sanitizedArgs); return await withTeardown( (async () => { - await this.#notify({ method: 'OutboundRequest' }); + await this.#notify({ + method: 'OutboundRequest', + params: { source: 'ethereum.request' }, + }); try { return await originalRequest(sanitizedArgs); } finally { - await this.#notify({ method: 'OutboundResponse' }); + await this.#notify({ + method: 'OutboundResponse', + params: { source: 'ethereum.request' }, + }); } })(), this as any, diff --git a/packages/snaps-execution-environments/src/common/endowments/network.test.ts b/packages/snaps-execution-environments/src/common/endowments/network.test.ts index 1b19aa67a3..c9859dbcc1 100644 --- a/packages/snaps-execution-environments/src/common/endowments/network.test.ts +++ b/packages/snaps-execution-environments/src/common/endowments/network.test.ts @@ -33,11 +33,21 @@ describe('Network endowments', () => { const { fetch } = network.factory({ notify }); const result = await (await fetch('foo.com')).text(); expect(result).toStrictEqual(RESULT); - expect(notify).toHaveBeenCalledWith({ + expect(notify).toHaveBeenNthCalledWith(1, { method: 'OutboundRequest', + params: { source: 'fetch' }, }); - expect(notify).toHaveBeenCalledWith({ + expect(notify).toHaveBeenNthCalledWith(2, { method: 'OutboundResponse', + params: { source: 'fetch' }, + }); + expect(notify).toHaveBeenNthCalledWith(3, { + method: 'OutboundRequest', + params: { source: 'fetch' }, + }); + expect(notify).toHaveBeenNthCalledWith(4, { + method: 'OutboundResponse', + params: { source: 'fetch' }, }); expect(notify).toHaveBeenCalledTimes(4); }); diff --git a/packages/snaps-execution-environments/src/common/endowments/network.ts b/packages/snaps-execution-environments/src/common/endowments/network.ts index fb754ff817..2971b8e148 100644 --- a/packages/snaps-execution-environments/src/common/endowments/network.ts +++ b/packages/snaps-execution-environments/src/common/endowments/network.ts @@ -12,15 +12,15 @@ class ResponseWrapper implements Response { #ogResponse: Response; - #onStart: () => void; + #onStart: () => Promise; - #onFinish: () => void; + #onFinish: () => Promise; constructor( ogResponse: Response, teardownRef: { lastTeardown: number }, - onStart: () => void, - onFinish: () => void, + onStart: () => Promise, + onFinish: () => Promise, ) { this.#ogResponse = ogResponse; this.#teardownRef = teardownRef; @@ -175,7 +175,10 @@ const createNetwork = ({ notify }: EndowmentFactoryOptions = {}) => { const onStart = async () => { if (!started) { started = true; - await notify({ method: 'OutboundRequest' }); + await notify({ + method: 'OutboundRequest', + params: { source: 'fetch' }, + }); } }; @@ -183,14 +186,17 @@ const createNetwork = ({ notify }: EndowmentFactoryOptions = {}) => { const onFinish = async () => { if (!finished) { finished = true; - await notify({ method: 'OutboundResponse' }); + await notify({ + method: 'OutboundResponse', + params: { source: 'fetch' }, + }); } }; let res: Response; let openFetchConnection: { cancel: () => Promise } | undefined; try { - await notify({ method: 'OutboundRequest' }); + await notify({ method: 'OutboundRequest', params: { source: 'fetch' } }); const fetchPromise = fetch(input, { ...init, signal: abortController.signal, @@ -218,7 +224,7 @@ const createNetwork = ({ notify }: EndowmentFactoryOptions = {}) => { if (openFetchConnection !== undefined) { openConnections.delete(openFetchConnection); } - await notify({ method: 'OutboundResponse' }); + await notify({ method: 'OutboundResponse', params: { source: 'fetch' } }); } if (res.body !== null) {