Skip to main content

NetworkId

Type-safe Ethereum network identifiers for peer-to-peer network discovery.

Overview

Branded number type representing Ethereum network IDs. Network IDs are used for peer-to-peer network identification and discovery, distinct from Chain IDs which are used for transaction replay protection.
NetworkId vs ChainId: These are different concepts!
  • NetworkId: Used for P2P network discovery (devp2p protocol)
  • ChainId: Used for transaction replay protection (EIP-155)
For most public networks they happen to be the same value, but they serve different purposes.

Quick Start

// Get network id via JSON-RPC
// {"method":"net_version","params":[]}
// Chain id via JSON-RPC
// {"method":"eth_chainId","params":[]}

Network Constants

ConstantValueNetwork
MAINNET1Ethereum Mainnet
SEPOLIA11155111Sepolia Testnet
HOLESKY17000Holesky Testnet
GOERLI5Goerli Testnet (deprecated)

API Reference

Constructors

// Use plain integers for network id (P2P) and chain id (EIP-155)
const mainnet_id: u64 = 1;
const sepolia_id: u64 = 11155111;

Methods

// Compare as integers
const a: u64 = 1;
const b: u64 = 11155111;
const equal = a == b; // false

NetworkId vs ChainId

Understanding the difference:
AspectNetworkIdChainId
PurposeP2P network discoveryTransaction replay protection
Protocoldevp2pEIP-155
RPC Methodnet_versioneth_chainId
Used InPeer handshakeTransaction signing
For most public networks, NetworkId and ChainId have the same value:
// Example values
const networkId_mainnet: u64 = 1;      // net_version
const chainId_mainnet: u64 = 1;        // eth_chainId
const networkId_sepolia: u64 = 11155111;
const chainId_sepolia: u64 = 11155111;
However, private networks or some L2s may use different values for each.

Use Cases

Network Detection

// Map integer ids to names as needed.

Multi-Network Support

// Choose RPC URL by network id.

Node Connection Validation

// Validate by comparing net_version result to expected id.
// {"method":"net_version","params":[]}
  • Chain - Chain configuration with ChainId
  • PeerId - Peer identifiers for P2P
  • NodeInfo - Node information structure

References