libp2p

package
v0.0.0-...-83876a5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 20, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package libp2p is the libp2p backend for mosey's transport.Transport. It speaks the "libp2p://..." (or bare "/ip4/..." multiaddr) scheme. Identity = a fresh Ed25519 key per Backend (Options.Identity to persist); listeners default to TCP+QUIC on all interfaces; NAT hole-punching + AutoRelay default on, with IPFS public bootstrap providing the relay swarm.

Authentication lives one level up (mosey's application-layer handshake on [api.ProtoAuth]) — this backend is plain libp2p with DefaultTransports, so QUIC is in play and DCUtR hole-punching actually works across NATs.

Index

Constants

View Source
const Scheme = "libp2p"

Scheme is the URI scheme this backend claims under a transport.Multi aggregator.

Variables

This section is empty.

Functions

func DefaultBootstrap

func DefaultBootstrap() ([]peer.AddrInfo, error)

DefaultBootstrap parses [ipfsBootstrapAddrs] into a peer.AddrInfo slice. Returns an error only when a hard-coded address fails to parse — that's a programming bug, not a runtime fault.

Types

type Backend

type Backend struct {
	// contains filtered or unexported fields
}

Backend implements transport.Transport over libp2p.

func New

func New(ctx context.Context, opts Options) (*Backend, error)

New constructs a libp2p transport.Transport backend. The returned backend immediately starts listening; call Close to release listener + outstanding streams.

When opts.Host is non-nil the backend uses the supplied host directly; its construction options (Identity, ListenAddrs, Bootstrap) must be zero and the caller retains ownership — Close becomes a no-op on the host so the caller can keep using it for other protocols.

func (*Backend) Close

func (b *Backend) Close() error

Close implements transport.Transport. When the host was caller-supplied (Options.Host), Close leaves it alone so the caller can keep using it for other protocols.

func (*Backend) Dial

func (b *Backend) Dial(ctx context.Context, endpoint, proto string) (transport.Stream, error)

Dial implements transport.Transport. Accepts two endpoint forms:

  • bare multiaddr starting with `/`: "/ip4/.../p2p/..."
  • prefixed: "libp2p:/ip4/.../p2p/..."

Both resolve via peer.AddrInfoFromP2pAddr. A full multiaddr carries its own dialable addrs; a bare "/p2p/<peer-id>" carries only the id, and Connect resolves current addrs via the DHT when the host was built with bootstrap peers (see New's routedhost.Wrap).

func (*Backend) Endpoints

func (b *Backend) Endpoints() []string

Endpoints implements transport.Transport. Returns each listen address, suffixed with /p2p/<peer-id>, prefixed with "libp2p:" so the multi-transport's scheme dispatch routes correctly.

func (*Backend) Handle

func (b *Backend) Handle(proto string, h transport.Handler)

Handle implements transport.Transport. Wraps the supplied transport.Handler in libp2p's stream-handler shape.

func (*Backend) Host

func (b *Backend) Host() host.Host

Host returns the underlying libp2p host. Escape hatch for integration tests that want to drive libp2p operations directly; production code should stay inside the transport.Transport surface.

func (*Backend) Schemes

func (b *Backend) Schemes() []string

Schemes implements transport.Transport.

func (*Backend) Unhandle

func (b *Backend) Unhandle(proto string)

Unhandle implements transport.Transport.

type Options

type Options struct {
	// Host, when non-nil, is used as-is and the backend will not
	// construct or close one of its own. Use this when the caller
	// already runs a libp2p host (so identity, listeners, and any
	// extra protocols stay on a single peer id) — for example a
	// daemon that wants /mosey/pty/ to share a connection with its
	// own RPC. Setting Host together with any of Identity,
	// ListenAddrs, or Bootstrap is a configuration error: those
	// fields describe construction of a host the backend doesn't
	// build.
	Host host.Host

	// Identity is the libp2p private key. Zero means "generate a
	// fresh Ed25519 key" — fine for ephemeral processes; persist
	// when you need a stable peer id across restarts (the cert
	// authenticator's identity will live in this key). Ignored when
	// Host is set.
	Identity crypto.PrivKey

	// ListenAddrs are the multiaddrs to bind. Zero means default
	// (TCP + QUIC on all interfaces, random ports). Pass an empty
	// (non-nil) slice for "no listener" — useful for client-only
	// configurations. Ignored when Host is set.
	ListenAddrs []multiaddr.Multiaddr

	// Bootstrap is the set of public peers to use for DHT
	// bootstrap, hole-punching, and AutoRelay discovery. Zero
	// means "use [DefaultBootstrap] (IPFS public)". Empty
	// (non-nil) slice means "no bootstrap". Ignored when Host is
	// set.
	Bootstrap []peer.AddrInfo
}

Options configures a Backend.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址