From 359c799cf0da875e41845b9ff949979f45611b65 Mon Sep 17 00:00:00 2001 From: georgi-l95 Date: Tue, 6 Dec 2022 10:37:41 +0200 Subject: [PATCH] Check `to` parameter in `eth_call` method (#744) Checks if to parameter is passed in eth_call method, before checking it's length. Signed-off-by: georgi-l95 --- packages/relay/src/lib/eth.ts | 7 +++---- packages/relay/tests/lib/eth.spec.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/packages/relay/src/lib/eth.ts b/packages/relay/src/lib/eth.ts index 7aaefa7123..69b2ab4515 100644 --- a/packages/relay/src/lib/eth.ts +++ b/packages/relay/src/lib/eth.ts @@ -894,10 +894,9 @@ export class EthImpl implements Eth { const requestIdPrefix = formatRequestIdMessage(requestId); this.logger.trace(`${requestIdPrefix} call(hash=${JSON.stringify(call)}, blockParam=${blockParam})`, call, blockParam); // The "to" address must always be 42 chars. - if (call.to.length != 42) { - throw new Error(requestIdPrefix+ - " Invalid Contract Address: '" + call.to + "'. Expected length of 42 chars but was" + call.to.length - ); + if (!call.to || call.to.length != 42) { + const callToExist = call.to && call.to.length ? ` Expected length of 42 chars but was ${call.to.length}.` : ''; + throw new Error(`${requestIdPrefix}Invalid Contract Address: '${call.to}'.${callToExist}`); } try { diff --git a/packages/relay/tests/lib/eth.spec.ts b/packages/relay/tests/lib/eth.spec.ts index 5de19a811e..d84f1325e6 100644 --- a/packages/relay/tests/lib/eth.spec.ts +++ b/packages/relay/tests/lib/eth.spec.ts @@ -132,6 +132,7 @@ describe('Eth calls using MirrorNode', async function () { const contractHash2 = '0x4a563af33c4871b51a8b108aa2fe1dd5280a30dfb7236170ae5e5e7957eb6393'; const contractHash3 = '0x4a563af33c4871b51a8b108aa2fe1dd5280a30dfb7236170ae5e5e7957eb6394'; const contractAddress2 = '0x000000000000000000000000000000000000055e'; + const wrongContractAddress = '0x00000000000000000000000000000000055e'; const contractTimestamp2 = '1653077542.701408897'; const contractTimestamp3 = '1653088542.123456789'; const contractId1 = '0.0.5001'; @@ -2154,6 +2155,31 @@ describe('Eth calls using MirrorNode', async function () { expect(result.message).to.equal('execution reverted: Set to revert'); expect(result.data).to.equal(defaultErrorMessage); }); + + it('eth_call with missing `to` field', async function() { + try { + await ethImpl.call({ + "from": contractAddress1, + "data": contractCallData, + "gas": maxGasLimitHex + }, 'latest'); + } catch (error: any) { + expect(error.message).to.equal(`Invalid Contract Address: '${undefined}'.`); + } + }); + + it('eth_call with wrong `to` field', async function() { + try { + await ethImpl.call({ + "from": contractAddress1, + "to": wrongContractAddress, + "data": contractCallData, + "gas": maxGasLimitHex + }, 'latest'); + } catch (error: any) { + expect(error.message).to.equal(`Invalid Contract Address: '${wrongContractAddress}'. Expected length of 42 chars but was ${wrongContractAddress.length}.`); + } + }); }); describe('eth_sendRawTransaction', async function() {