Skip to content

Commit 107f556

Browse files
hackmodfjl
authored andcommitted
internal/ethapi: add eth_chainId method (#17617)
This implements EIP-695.
1 parent 44eb695 commit 107f556

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

eth/api.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ func (api *PublicEthereumAPI) Hashrate() hexutil.Uint64 {
6767
return hexutil.Uint64(api.e.Miner().HashRate())
6868
}
6969

70+
// ChainId is the EIP-155 replay-protection chain id for the current ethereum chain config.
71+
func (api *PublicEthereumAPI) ChainId() hexutil.Uint64 {
72+
chainID := new(big.Int)
73+
if config := api.e.chainConfig; config.IsEIP155(api.e.blockchain.CurrentBlock().Number()) {
74+
chainID = config.ChainID
75+
}
76+
return (hexutil.Uint64)(chainID.Uint64())
77+
}
78+
7079
// PublicMinerAPI provides an API to control the miner.
7180
// It offers only methods that operate on data that pose no security risk when it is publicly accessible.
7281
type PublicMinerAPI struct {

internal/web3ext/web3ext.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,11 @@ const Eth_JS = `
433433
web3._extend({
434434
property: 'eth',
435435
methods: [
436+
new web3._extend.Method({
437+
name: 'chainId',
438+
call: 'eth_chainId',
439+
params: 0
440+
}),
436441
new web3._extend.Method({
437442
name: 'sign',
438443
call: 'eth_sign',

0 commit comments

Comments
 (0)