From 15d740cc5bdca86c87c3c9fd79bbce5f785b105e Mon Sep 17 00:00:00 2001 From: Tomasz Cichowicz Date: Tue, 21 May 2019 10:09:43 +0200 Subject: [PATCH] Add eth_chainId Fixes trufflesuite#339 --- README.md | 1 + lib/subproviders/geth_api_double.js | 5 +++++ test/requests.js | 15 +++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/README.md b/README.md index 5b9338904d..62543f8f51 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ The RPC methods currently implemented are: * [eth_accounts](https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_accounts) * [eth_blockNumber](https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_blockNumber) * [eth_call](https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_call) +* `eth_chainId` * [eth_coinbase](https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_coinbase) * [eth_estimateGas](https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_estimateGas) * [eth_gasPrice](https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_gasPrice) diff --git a/lib/subproviders/geth_api_double.js b/lib/subproviders/geth_api_double.js index 8ecc635ab4..d23b923250 100644 --- a/lib/subproviders/geth_api_double.js +++ b/lib/subproviders/geth_api_double.js @@ -145,6 +145,11 @@ GethApiDouble.prototype.eth_blockNumber = function(callback) { }); }; +GethApiDouble.prototype.eth_chainId = function(callback) { + // chainId of 1337 is the default for private networks as per EIP-155 + callback(null, to.hex(1337)); +}; + GethApiDouble.prototype.eth_coinbase = function(callback) { callback(null, this.state.coinbase); }; diff --git a/test/requests.js b/test/requests.js index 5d74c05aa1..5dcd2fa010 100644 --- a/test/requests.js +++ b/test/requests.js @@ -89,6 +89,21 @@ const tests = function(web3) { }); }); + describe("eth_chainId", function() { + it("should return a default chain id of a private network", async function() { + const send = pify(web3._provider.send.bind(web3._provider)); + + const result = await send({ + id: new Date().getTime(), + jsonrpc: "2.0", + method: "eth_chainId", + params: [] + }); + + assert.strictEqual(result.result, "0x539"); // 0x539 === 1337 + }); + }); + describe("eth_coinbase", function() { it("should return correct address", async function() { const coinbase = await web3.eth.getCoinbase();