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 db5a26a43c..1bbd354b96 100644 --- a/packages/snaps-execution-environments/src/common/BaseSnapExecutor.test.browser.ts +++ b/packages/snaps-execution-environments/src/common/BaseSnapExecutor.test.browser.ts @@ -1113,6 +1113,69 @@ describe('BaseSnapExecutor', () => { }); }); + it('exposes a Blob global works with instanceof', async () => { + const CODE = ` + module.exports.onRpcRequest = () => fetch('https://metamask.io').then(res => res.blob()).then(blob => blob instanceof Blob); + `; + + const fetchSpy = spy(globalThis, 'fetch'); + + fetchSpy.mockImplementation(async () => { + return new Response('foo'); + }); + + const executor = new TestSnapExecutor(); + await executor.executeSnap(1, MOCK_SNAP_ID, CODE, ['fetch', 'Blob']); + + expect(await executor.readCommand()).toStrictEqual({ + jsonrpc: '2.0', + id: 1, + result: 'OK', + }); + + await executor.writeCommand({ + jsonrpc: '2.0', + id: 2, + method: 'snapRpc', + params: { + snapId: MOCK_SNAP_ID, + handler: HandlerType.OnRpcRequest, + origin: MOCK_ORIGIN, + request: { jsonrpc: '2.0', method: '' }, + }, + }); + + 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({ + id: 2, + jsonrpc: '2.0', + result: true, + }); + }); + it('notifies execution service of out of band errors via unhandledrejection', async () => { const CODE = ` module.exports.onRpcRequest = async () => 'foo'; diff --git a/packages/snaps-execution-environments/src/common/endowments/commonEndowmentFactory.ts b/packages/snaps-execution-environments/src/common/endowments/commonEndowmentFactory.ts index b5dcb560e7..70468d5663 100644 --- a/packages/snaps-execution-environments/src/common/endowments/commonEndowmentFactory.ts +++ b/packages/snaps-execution-environments/src/common/endowments/commonEndowmentFactory.ts @@ -82,6 +82,7 @@ const commonEndowments: CommonEndowmentSpecification[] = [ { endowment: BigInt, name: 'BigInt' }, { endowment: BigInt64Array, name: 'BigInt64Array' }, { endowment: BigUint64Array, name: 'BigUint64Array' }, + { endowment: Blob, name: 'Blob' }, { endowment: btoa, name: 'btoa', bind: true }, { endowment: DataView, name: 'DataView' }, { endowment: Float32Array, name: 'Float32Array' }, diff --git a/packages/snaps-execution-environments/src/common/endowments/endowments.test.browser.ts b/packages/snaps-execution-environments/src/common/endowments/endowments.test.browser.ts index 1e768d79ff..907ba80f89 100644 --- a/packages/snaps-execution-environments/src/common/endowments/endowments.test.browser.ts +++ b/packages/snaps-execution-environments/src/common/endowments/endowments.test.browser.ts @@ -166,6 +166,10 @@ describe('endowments', () => { endowments: { DateAttenuated }, factory: () => new DateAttenuated(), }, + Blob: { + endowments: { Blob }, + factory: () => new Blob([new ArrayBuffer(64)]), + }, // Objects. consoleAttenuated: { @@ -348,6 +352,10 @@ describe('endowments', () => { factory: expect.any(Function), names: ['BigUint64Array'], }, + { + factory: expect.any(Function), + names: ['Blob'], + }, { factory: expect.any(Function), names: ['btoa'], diff --git a/packages/snaps-simulation/src/methods/specifications.test.ts b/packages/snaps-simulation/src/methods/specifications.test.ts index e5a7ca11f2..ead64029ef 100644 --- a/packages/snaps-simulation/src/methods/specifications.test.ts +++ b/packages/snaps-simulation/src/methods/specifications.test.ts @@ -415,6 +415,7 @@ describe('getEndowments', () => { "ArrayBuffer", "AbortController", "AbortSignal", + "Blob", "fetch", "Request", "Headers", diff --git a/packages/snaps-utils/src/default-endowments.ts b/packages/snaps-utils/src/default-endowments.ts index 7cf263bc02..87275688eb 100644 --- a/packages/snaps-utils/src/default-endowments.ts +++ b/packages/snaps-utils/src/default-endowments.ts @@ -38,4 +38,5 @@ export const DEFAULT_ENDOWMENTS: readonly string[] = Object.freeze([ // https://github.com/MetaMask/snaps-monorepo/discussions/678 'AbortController', 'AbortSignal', + 'Blob', ]);