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