From 917baceda86a971ac4c27c22ddb18635e63169d7 Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Thu, 21 Aug 2025 13:40:35 +0800 Subject: [PATCH] ethclient, internal/ethapi: add support for EIP-695 (eth_chainId) #19694 #23778 (#1362) * ethclient, internal/ethapi: add support for EIP-695 (eth_chainId) #19694 * ethclient: fix typo #23778 --- ethclient/ethclient.go | 10 ++++++++++ internal/ethapi/api.go | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index 0d1b86d04d..19e15c77a2 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -63,6 +63,16 @@ func (ec *Client) Close() { // Blockchain Access +// ChainID retrieves the current chain ID for transaction replay protection. +func (ec *Client) ChainID(ctx context.Context) (*big.Int, error) { + var result hexutil.Big + err := ec.c.CallContext(ctx, &result, "eth_chainId") + if err != nil { + return nil, err + } + return (*big.Int)(&result), err +} + // BlockByHash returns the given full block. // // Note that loading full blocks requires two requests. Use HeaderByHash diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index acac735cbf..432d5a4306 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -306,6 +306,11 @@ func NewBlockChainAPI(b Backend, chainReader consensus.ChainReader) *BlockChainA } } +// ChainId returns the chainID value for transaction replay protection. +func (s *BlockChainAPI) ChainId() *hexutil.Big { + return (*hexutil.Big)(s.b.ChainConfig().ChainId) +} + // BlockNumber returns the block number of the chain head. func (s *BlockChainAPI) BlockNumber() hexutil.Uint64 { header, _ := s.b.HeaderByNumber(context.Background(), rpc.LatestBlockNumber) // latest header should always be available