Skip to content

Commit

Permalink
Check to parameter in eth_call method (#744)
Browse files Browse the repository at this point in the history
Checks if to parameter is passed in eth_call method, before checking it's length.

Signed-off-by: georgi-l95 <[email protected]>
  • Loading branch information
georgi-l95 authored and lukelee-sl committed Dec 6, 2022
1 parent 8686666 commit 359c799
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
7 changes: 3 additions & 4 deletions packages/relay/src/lib/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions packages/relay/tests/lib/eth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 359c799

Please sign in to comment.