Skip to content

Commit

Permalink
Stop assuming wrapped responses from MetaMask provider (#854)
Browse files Browse the repository at this point in the history
* Stop assuming wrapped responses from MetaMask provider

* Simplify

* Re add tests
  • Loading branch information
FrederikBolding committed Oct 24, 2022
1 parent 4356412 commit d1ff57c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 58 deletions.
68 changes: 19 additions & 49 deletions packages/provider/src/MultiChainProvider.test.ts
Expand Up @@ -33,12 +33,8 @@ async function getProvider(
>;

request.mockImplementation(async () => ({
jsonrpc: '2.0',
id: 1,
result: {
namespaces: {
eip155: getSessionNamespace(),
},
namespaces: {
eip155: getSessionNamespace(),
},
}));

Expand Down Expand Up @@ -69,12 +65,8 @@ describe('MultiChainProvider', () => {
>;

request.mockImplementation(async () => ({
jsonrpc: '2.0',
id: 1,
result: {
namespaces: {
eip155: getSessionNamespace(),
},
namespaces: {
eip155: getSessionNamespace(),
},
}));

Expand All @@ -93,12 +85,8 @@ describe('MultiChainProvider', () => {
>;

request.mockImplementation(async () => ({
jsonrpc: '2.0',
id: 1,
result: {
namespaces: {
eip155: getSessionNamespace(),
},
namespaces: {
eip155: getSessionNamespace(),
},
}));

Expand Down Expand Up @@ -152,12 +140,8 @@ describe('MultiChainProvider', () => {
>;

request.mockImplementation(async () => ({
jsonrpc: '2.0',
id: 1,
result: {
namespaces: {
eip155: 'foo',
},
namespaces: {
eip155: 'foo',
},
}));

Expand All @@ -177,26 +161,21 @@ describe('MultiChainProvider', () => {
expect(listener).not.toHaveBeenCalled();
});

it('throws on JSON-RPC error response', async () => {
it('throws on errors', async () => {
const request = ethereum.request as jest.MockedFunction<
typeof ethereum.request
>;

request.mockImplementation(async () => ({
jsonrpc: '2.0',
id: 1,
error: {
code: -1,
message: 'foo',
},
}));
request.mockImplementation(async () => {
throw new Error('foo');
});

const provider = new MultiChainProvider();

const { approval } = await provider.connect({ requiredNamespaces: {} });
expect(provider.isConnected).toBe(false);

await expect(approval()).rejects.toThrow('JSON-RPC request failed: foo');
await expect(approval()).rejects.toThrow('foo');
});
});

Expand Down Expand Up @@ -244,11 +223,7 @@ describe('MultiChainProvider', () => {
typeof ethereum.request
>;

request.mockImplementation(async () => ({
jsonrpc: '2.0',
id: 1,
result: 'foo',
}));
request.mockImplementation(async () => 'foo');

const result = await provider.request({
chainId: 'eip155:1',
Expand Down Expand Up @@ -326,21 +301,16 @@ describe('MultiChainProvider', () => {
expect(firstId).not.toBe(secondId);
});

it('throws on JSON-RPC errors', async () => {
it('throws on errors', async () => {
const provider = await getProvider();

const request = ethereum.request as jest.MockedFunction<
typeof ethereum.request
>;

request.mockImplementation(async () => ({
jsonrpc: '2.0',
id: 1,
error: {
code: -1,
message: 'foo',
},
}));
request.mockImplementation(async () => {
throw new Error('foo');
});

await expect(
provider.request({
Expand All @@ -349,7 +319,7 @@ describe('MultiChainProvider', () => {
method: 'eth_accounts',
},
}),
).rejects.toThrow('JSON-RPC request failed: foo');
).rejects.toThrow('foo');
});
});

Expand Down
12 changes: 3 additions & 9 deletions packages/provider/src/MultiChainProvider.ts
Expand Up @@ -2,7 +2,6 @@ import SafeEventEmitter from '@metamask/safe-event-emitter';
import { nanoid } from 'nanoid';
import {
assertIsConnectArguments,
assertIsJsonRpcSuccess,
assertIsMetaMaskNotification,
assertIsMultiChainRequest,
assertIsSession,
Expand Down Expand Up @@ -92,17 +91,15 @@ export class MultiChainProvider extends SafeEventEmitter implements Provider {
);

this.#isConnected = false;
const response = await this.#rpcRequest({
const session = await this.#rpcRequest({
method: 'metamask_handshake',
params: { requiredNamespaces },
});

assertIsJsonRpcSuccess(response);
assertIsSession(response.result);
assertIsSession(session);

this.#isConnected = true;

const session = response.result;
this.emit('session_update', { params: session });
return session;
},
Expand All @@ -129,16 +126,13 @@ export class MultiChainProvider extends SafeEventEmitter implements Provider {

assertIsMultiChainRequest(args);

const response = await this.#rpcRequest({
return this.#rpcRequest({
method: 'caip_request',
params: {
chainId: args.chainId,
request: { method: args.request.method, params: args.request.params },
},
});

assertIsJsonRpcSuccess(response);
return response.result;
}

/**
Expand Down

0 comments on commit d1ff57c

Please sign in to comment.