diff --git a/packages/api/src/rpc/eth/eth.js b/packages/api/src/rpc/eth/eth.js index 3263f32b..6aefd42d 100644 --- a/packages/api/src/rpc/eth/eth.js +++ b/packages/api/src/rpc/eth/eth.js @@ -39,6 +39,12 @@ class Eth { .send('eth_call', inOptions(options), inBlockNumber(blockNumber)); } + chainId () { + return this._provider + .send('eth_chainId') + .then(outNumber); + } + 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']