From 064bf57ba8310c044454f5fcde1a3dd96f4ccd6a Mon Sep 17 00:00:00 2001 From: Axel Chalon Date: Sat, 5 Jan 2019 21:45:30 +0100 Subject: [PATCH 1/2] Add chainId$ --- packages/api/src/rpc/eth/eth.js | 5 +++++ packages/light.js/src/index.ts | 1 + packages/light.js/src/rpc/eth.ts | 16 ++++++++++++++++ .../light.js/src/utils/testHelpers/mockApi.ts | 2 +- 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/api/src/rpc/eth/eth.js b/packages/api/src/rpc/eth/eth.js index 3263f32b..0d298109 100644 --- a/packages/api/src/rpc/eth/eth.js +++ b/packages/api/src/rpc/eth/eth.js @@ -39,6 +39,11 @@ class Eth { .send('eth_call', inOptions(options), inBlockNumber(blockNumber)); } + chainId () { + return this._provider + .send('eth_chainId'); + } + coinbase () { return this._provider .send('eth_coinbase') diff --git a/packages/light.js/src/index.ts b/packages/light.js/src/index.ts index 83893e2d..0d87fc52 100644 --- a/packages/light.js/src/index.ts +++ b/packages/light.js/src/index.ts @@ -17,6 +17,7 @@ export const { accounts$, accountsInfo$, balanceOf$, + chainId$, transactionCountOf$, blockNumber$, chainName$, diff --git a/packages/light.js/src/rpc/eth.ts b/packages/light.js/src/rpc/eth.ts index 7733d259..21b736c6 100644 --- a/packages/light.js/src/rpc/eth.ts +++ b/packages/light.js/src/rpc/eth.ts @@ -13,6 +13,22 @@ import frequency from '../frequency'; import { isNullOrLoading, RPC_LOADING } from '../utils/isLoading'; import { switchMapPromise } from '../utils/operators'; +/** + * Observable containing the EIP155 chain ID used for transaction signing. + * Calls `eth_chainId` + * + * @param options - Options to pass to {@link RpcObservableOptions}. + * @return - An Observable containing the chain ID. + */ +export function chainId$ (options?: RpcObservableOptions) { + return createRpc$({ + calls: ['eth_chainId'], + frequency: [frequency.onStartup$], + name: 'chainId$', + pipes: api => [switchMapPromise(() => api.eth.chainId())] + })(options)(); +} + /** * Observable which contains the array of all addresses managed by the light * client. diff --git a/packages/light.js/src/utils/testHelpers/mockApi.ts b/packages/light.js/src/utils/testHelpers/mockApi.ts index 341780c3..868b2ccd 100644 --- a/packages/light.js/src/utils/testHelpers/mockApi.ts +++ b/packages/light.js/src/utils/testHelpers/mockApi.ts @@ -22,7 +22,7 @@ export class MockProvider extends EventEmitter { // List of JSONRPCs we want to mock const listOfMockRps: { [index: string]: string[] } = { - eth: ['accounts', 'blockNumber', 'getBalance', 'getTransactionCount', 'newHeads', 'syncing'], + eth: ['accounts', 'blockNumber', 'chainId', 'getBalance', 'getTransactionCount', 'newHeads', 'syncing'], fake: ['method'], net: ['peerCount'], parity: ['accountsInfo', 'chain', 'postTransaction'] From eac5cb19358c607390e92221624eb33e55fd0328 Mon Sep 17 00:00:00 2001 From: Axel Chalon Date: Sun, 6 Jan 2019 22:39:54 +0100 Subject: [PATCH 2/2] api.eth.chainId: convert hex string to BigNumber --- packages/api/src/rpc/eth/eth.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/api/src/rpc/eth/eth.js b/packages/api/src/rpc/eth/eth.js index 0d298109..6aefd42d 100644 --- a/packages/api/src/rpc/eth/eth.js +++ b/packages/api/src/rpc/eth/eth.js @@ -41,7 +41,8 @@ class Eth { chainId () { return this._provider - .send('eth_chainId'); + .send('eth_chainId') + .then(outNumber); } coinbase () {