Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ describe('endowments', () => {
endowments: { DateAttenuated },
factory: () => new DateAttenuated(),
},
Blob: {
endowments: { Blob },
factory: () => new Blob([new ArrayBuffer(64)]),
},

// Objects.
consoleAttenuated: {
Expand Down Expand Up @@ -348,6 +352,10 @@ describe('endowments', () => {
factory: expect.any(Function),
names: ['BigUint64Array'],
},
{
factory: expect.any(Function),
names: ['Blob'],
},
{
factory: expect.any(Function),
names: ['btoa'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ describe('getEndowments', () => {
"ArrayBuffer",
"AbortController",
"AbortSignal",
"Blob",
"fetch",
"Request",
"Headers",
Expand Down
1 change: 1 addition & 0 deletions packages/snaps-utils/src/default-endowments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ export const DEFAULT_ENDOWMENTS: readonly string[] = Object.freeze([
// https://github.com/MetaMask/snaps-monorepo/discussions/678
'AbortController',
'AbortSignal',
'Blob',
]);
Loading