Libp2p.Multiaddr (libp2p_elixir v0.9.0)

Minimal Multiaddr implementation (binary codec + a small parser).

Ethereum consensus clients mostly need /ip4|ip6/.../tcp/... (and often see additional components like /p2p/..., /udp/.../quic-v1, /tls, /ws in peer advertisements). We implement enough of the multiaddr table to:

  • encode/decode common addresses seen in the wild
  • extract a :inet tuple + port for TCP dialing/listening

Summary

Functions

Parse a multiaddr string like /ip4/1.2.3.4/tcp/9000(/p2p/<peerid>).

Best-effort conversion to a multiaddr string. Unknown/unsupported protos are not represented.

Extract a TCP socket address from a multiaddr (first ip4/ip6 + tcp). Returns {:ok, {ip, port}} or {:error, reason}.

Types

proto()

@type proto() ::
  {:ip4, :inet.ip4_address()}
  | {:ip6, :inet.ip6_address()}
  | {:tcp, :inet.port_number()}
  | {:udp, :inet.port_number()}
  | {:dns, binary()}
  | {:dns4, binary()}
  | {:dns6, binary()}
  | {:dnsaddr, binary()}
  | {:p2p, binary()}
  | {:quic, nil}
  | {:quic_v1, nil}
  | {:webtransport, nil}
  | {:tls, nil}
  | {:ws, nil}
  | {:wss, nil}
  | {:certhash, binary()}

t()

@type t() :: %Libp2p.Multiaddr{bytes: binary(), protos: [proto()]}

Functions

from_bytes(bin)

@spec from_bytes(binary()) :: t()

from_string(str)

@spec from_string(binary()) :: t()

Parse a multiaddr string like /ip4/1.2.3.4/tcp/9000(/p2p/<peerid>).

from_tcp_socketaddr(ip, port)

@spec from_tcp_socketaddr(:inet.ip_address(), :inet.port_number()) :: t()

new(protos)

@spec new([proto()]) :: t()

to_bytes(multiaddr)

@spec to_bytes(t()) :: binary()

to_string(multiaddr)

@spec to_string(t()) :: binary()

Best-effort conversion to a multiaddr string. Unknown/unsupported protos are not represented.

to_tcp_socketaddr(multiaddr)

@spec to_tcp_socketaddr(t()) ::
  {:ok, {:inet.ip_address(), :inet.port_number()}} | {:error, term()}

Extract a TCP socket address from a multiaddr (first ip4/ip6 + tcp). Returns {:ok, {ip, port}} or {:error, reason}.