Verify the chain behind an Ethereum RPC URL

A working HTTP response can come from the wrong network. Check the signing chain ID and a known block before pointing an indexer at a replacement URL.

Compare the signing domain

Send the request below to the candidate endpoint. A mainnet execution client returns 0x1, the hexadecimal representation of chain ID 1. Parse the response as a hexadecimal integer when comparing it to a decimal configuration value.

{"jsonrpc":"2.0","id":1,"method":"eth_chainId","params":[]}

Use eth_chainId for this check. net_version returns the network ID, which can differ from the chain ID. Keep the expected value in deployment configuration so the candidate server cannot supply both the observation and the reference. eips.ethereum.org Chain and network IDs in EIP-695

Challenge the endpoint with a block it should know

A private fork can reuse a public chain ID and share its genesis. From an independent trusted endpoint, record the number and hash of a recent finalized block. Send the same eth_getBlockByNumber request by explicit height to the candidate. As an illustration of the request shape, height 20,000,000 is encoded below. Substitute the height of your checkpoint.

{"jsonrpc":"2.0","id":2,"method":"eth_getBlockByNumber","params":["0x1312d00",false]}

If the candidate returns null, check whether it has reached that height. A different hash at the recorded height calls for investigating the endpoint configuration. Matching both checks establishes agreement through the chosen checkpoint. A fork taken after that block will pass, so refresh the checkpoint as part of subsequent cutovers.