Skip to content

Commit

Permalink
Merge pull request #2862 from LukeSugiura/hotfix/eip1193-provider-result
Browse files Browse the repository at this point in the history
Fix JSON-RPC response validation
  • Loading branch information
nivida committed Jun 3, 2019
2 parents caa9410 + 7929596 commit 12592fe
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 24 deletions.
12 changes: 4 additions & 8 deletions packages/web3-providers/src/providers/Web3EthereumProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* @date 2018
*/

import JsonRpcResponseValidator from '../validators/JsonRpcResponseValidator';
import AbstractSocketProvider from '../../lib/providers/AbstractSocketProvider';

export default class Web3EthereumProvider extends AbstractSocketProvider {
Expand Down Expand Up @@ -129,14 +128,11 @@ export default class Web3EthereumProvider extends AbstractSocketProvider {
* @returns {Promise<Object>}
*/
async send(method, parameters) {
const response = await this.connection.send(method, parameters);
const validationResult = JsonRpcResponseValidator.validate(response);

if (validationResult instanceof Error) {
throw validationResult;
try {
return await this.connection.send(method, parameters);
} catch (error) {
throw new Error(`Node error: ${error.message}`);
}

return response;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Web3EthereumProvider from '../../../src/providers/Web3EthereumProvider';
import JsonRpcResponseValidator from '../../../src/validators/JsonRpcResponseValidator';
import AbstractMethod from '../../__mocks__/AbstractMethod';
import AbstractWeb3Module from '../../__mocks__/AbstractWeb3Module';
import AbstractSocketProvider from '../../../lib/providers/AbstractSocketProvider';
Expand Down Expand Up @@ -175,10 +174,6 @@ describe('Web3EthereumProviderTest', () => {
});

it('calls send and returns a resolved promise with the response', async () => {
JsonRpcResponseValidator.validate = jest.fn(() => {
return true;
});

socketMock.send = jest.fn((method, parameters) => {
expect(method).toEqual('method');

Expand All @@ -190,26 +185,18 @@ describe('Web3EthereumProviderTest', () => {
const response = await ethereumProvider.send('method', []);

expect(response).toEqual(true);

expect(JsonRpcResponseValidator.validate).toHaveBeenCalled();
});

it('calls send and returns a rejected promise because of a invalid response', async () => {
JsonRpcResponseValidator.validate = jest.fn(() => {
return new Error('invalid');
});

it('calls send and returns a rejected promise because of an error response', async () => {
socketMock.send = jest.fn((method, parameters) => {
expect(method).toEqual('method');

expect(parameters).toEqual([]);

return Promise.resolve(false);
return Promise.reject(new Error('invalid'));
});

await expect(ethereumProvider.send('method', [])).rejects.toThrow('invalid');

expect(JsonRpcResponseValidator.validate).toHaveBeenCalled();
await expect(ethereumProvider.send('method', [])).rejects.toThrow('Node error: invalid');
});

it('calls sendBatch and returns a resolved promise with the response', async () => {
Expand Down

0 comments on commit 12592fe

Please sign in to comment.