Dart Libp2p

Dart License

A comprehensive Dart implementation of the libp2p networking stack, providing a modular and extensible foundation for building peer-to-peer applications.

๐Ÿš€ Features

  • Modular Architecture: Pluggable transports, security protocols, and stream multiplexers
  • Multiple Transports: TCP and custom UDX (UDP-based) transport support
  • Security: Noise protocol for encrypted and authenticated connections
  • Stream Multiplexing: Yamux for efficient multi-stream communication
  • Peer Discovery: mDNS and routing-based peer discovery mechanisms
  • Protocol Support: Built-in support for Ping, Identify, and other core libp2p protocols
  • Resource Management: Built-in protection against resource exhaustion
  • Event System: Comprehensive event bus for monitoring network activity
  • NAT Traversal: Hole punching and relay support for NAT traversal

๐Ÿ“ฆ Installation

Add dart_libp2p to your pubspec.yaml:

dependencies:
  dart_libp2p: ^0.5.2

Then run:

dart pub get

๐Ÿƒโ€โ™‚๏ธ Quick Start

Here's a simple example of creating two libp2p nodes and connecting them:

import 'package:dart_libp2p/dart_libp2p.dart';
import 'package:dart_libp2p/config/config.dart' as p2p_config;
import 'package:dart_libp2p/core/crypto/ed25519.dart' as crypto_ed25519;
import 'package:dart_libp2p/core/multiaddr.dart';
import 'package:dart_libp2p/p2p/security/noise/noise_protocol.dart';
import 'package:dart_libp2p/p2p/transport/udx_transport.dart';
import 'package:dart_libp2p/p2p/transport/connection_manager.dart' as p2p_conn_manager;
import 'package:dart_udx/dart_udx.dart';

Future<Host> createHost({String? listen}) async {
  final keyPair = await crypto_ed25519.generateEd25519KeyPair();
  final udx = UDX();
  final connMgr = p2p_conn_manager.ConnectionManager();

  final options = <p2p_config.Option>[
    p2p_config.Libp2p.identity(keyPair),
    p2p_config.Libp2p.connManager(connMgr),
    p2p_config.Libp2p.transport(UDXTransport(connManager: connMgr, udxInstance: udx)),
    p2p_config.Libp2p.security(await NoiseSecurity.create(keyPair)),
    if (listen != null) p2p_config.Libp2p.listenAddrs([MultiAddr(listen)]),
  ];

  final host = await p2p_config.Libp2p.new_(options);
  await host.start();
  return host;
}

void main() async {
  final host1 = await createHost(listen: '/ip4/0.0.0.0/udp/0/udx');
  final host2 = await createHost(listen: '/ip4/0.0.0.0/udp/0/udx');

  print('Host 1: ${host1.id}');
  print('Host 2: ${host2.id}');

  await host1.connect(AddrInfo(host2.id, host2.addrs));
  print('Connected successfully!');

  await host1.close();
  await host2.close();
}

๐Ÿ—๏ธ Architecture

Dart Libp2p follows a layered architecture where each component provides services to the layer above it:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚           Application               โ”‚
โ”‚        (Your Custom Protocols)      โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚              Host                   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚           Network/Swarm             โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚            Upgrader                 โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  Multiplexer (Yamux) โ”‚ Security     โ”‚
โ”‚                      โ”‚ (Noise)      โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚           Transport                 โ”‚
โ”‚        (TCP, UDX)                   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Core Components

  • Host: Central entry point that ties all components together
  • Network/Swarm: Manages connections, peers, and upgrade lifecycle
  • Upgrader: Handles security and multiplexing negotiation
  • Transport: Establishes raw connections (TCP, UDX)
  • Security: Encrypts and authenticates connections (Noise)
  • Multiplexer: Enables multiple streams over single connections (Yamux)

๐Ÿš› Transports

TCP Transport

  • Protocols: /ip4/tcp, /ip6/tcp
  • Use Case: Reliable, widely available transport for most applications
  • Best For: Data centers, servers with public IPs

UDX Transport

  • Protocols: /ip4/udp/udx, /ip6/udp/udx
  • Use Case: Custom UDP-based transport with built-in reliability
  • Best For: NAT traversal, hole punching, peer-to-peer connections

Note: This implementation does not support QUIC. Instead, we've opted for a custom dart-udx implementation that provides similar benefits for peer-to-peer networking.

๐Ÿ” Security

Dart Libp2p uses the Noise protocol for securing connections:

  • Encryption: All communication is encrypted
  • Authentication: Remote peer identity is verified
  • Perfect Forward Secrecy: Session keys are ephemeral
  • Handshake: Efficient key exchange and authentication

๐Ÿ“š Documentation

For detailed documentation, visit the docs directory:

๐Ÿงช Examples

Check out the examples directory for working examples:

  • Ping App: Basic ping/pong communication between nodes
  • Chat Application: Simple peer-to-peer chat implementation

๐Ÿงช Testing

Run the test suite:

dart test

๐Ÿค Contributing

We welcome contributions! Please see our contributing guidelines and code of conduct.

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • libp2p - The original protocol specification
  • dart-udx - Custom UDP transport implementation
  • The libp2p community for inspiration and guidance
  • libp2p.io - Official libp2p documentation
  • Dart - Dart programming language
  • pub.dev - Package on pub.dev

Libraries

config/config
config/defaults
config/multistream_config
config/stream_muxer
core/alias
Package core provides convenient access to foundational, central libp2p primitives via type aliases.
core/certified_addr_book
CertifiedAddrBook manages signed peer records and "self-certified" addresses contained within them. Use this interface with an AddrBook.
core/connmgr/conn_gater
core/connmgr/conn_manager
core/connmgr/decay
core/crypto/ecdsa
core/crypto/ed25519
core/crypto/keys
core/crypto/pb/crypto.pb
core/crypto/pb/crypto.pbenum
core/crypto/pb/crypto.pbjson
core/crypto/pb/crypto.pbserver
core/crypto/rsa
core/discovery
core/event/addrs
Address-related events for libp2p.
core/event/bus
Package event provides a type-based event subscription system.
core/event/dht
core/event/identify
Identify-related events for libp2p.
core/event/nattype
core/event/protocol
Protocol-related events for libp2p.
core/event/reachability
core/exceptions
core/host/helpers
Helper functions for working with Host objects.
core/host/host
Package host provides the core Host interface for libp2p.
core/interfaces
core/multiaddr
core/network/common
core/network/conn
core/network/context
core/network/errors
core/network/mux
core/network/nattype
core/network/network
core/network/notifiee
core/network/rcmgr
core/network/stream
core/network/transport_conn
core/peer/addr_info
core/peer/pb/peer_record.pb
core/peer/pb/peer_record.pbenum
core/peer/pb/peer_record.pbjson
core/peer/pb/peer_record.pbserver
core/peer/peer_id
core/peer/peer_serde
core/peer/record
core/peerstore
Package peerstore provides types and interfaces for local storage of address information, metadata, and public key material about libp2p peers.
core/protocol/autonatv1/autonatv1
core/protocol/autonatv2/autonatv2
core/protocol/protocol
Package protocol provides core interfaces for protocol routing and negotiation in libp2p.
core/protocol/switch
Package protocol provides core interfaces for protocol routing and negotiation in libp2p.
core/record/envelope
core/record/pb/envelope.pb
core/record/pb/envelope.pbenum
core/record/pb/envelope.pbjson
core/record/pb/envelope.pbserver
core/record/record_registry
core/routing/options
core/routing/query
core/routing/routing
dart_libp2p
p2p/crypto/key_generator
p2p/crypto/pb/crypto.pb
p2p/crypto/pb/crypto.pbenum
p2p/crypto/pb/crypto.pbjson
p2p/crypto/pb/crypto.pbserver
p2p/crypto/pb/pb
p2p/discovery/backoff/backoff
p2p/discovery/backoff/backoff_cache
p2p/discovery/backoff/backoff_connector
p2p/discovery/backoff/lru_cache
p2p/discovery/discovery
p2p/discovery/mdns
p2p/discovery/mdns/mdns
p2p/discovery/mdns/service_registry
p2p/discovery/peer_info
p2p/discovery/routing/routing
p2p/host/autonat/autonat
p2p/host/autonat/client
p2p/host/autonat/dial_policy
p2p/host/autonat/metrics
p2p/host/autonat/options
p2p/host/autonat/pb/autonat.pb
p2p/host/autonat/pb/autonat.pbenum
p2p/host/autonat/pb/autonat.pbjson
p2p/host/autonat/pb/autonat.pbserver
p2p/host/autonat/pb/autonat/v1/autonat.pb
p2p/host/autonat/pb/autonat/v1/autonat.pbenum
p2p/host/autonat/pb/autonat/v1/autonat.pbjson
p2p/host/autonat/pb/autonat/v1/autonat.pbserver
p2p/host/autonat/service
p2p/host/autorelay/autorelay
p2p/host/autorelay/autorelay_address_utils
p2p/host/autorelay/autorelay_config
p2p/host/autorelay/autorelay_metrics
p2p/host/autorelay/relay_finder
p2p/host/basic/basic_host
p2p/host/basic/index
Basic host implementation for libp2p.
p2p/host/basic/internal/backoff/backoff
Package backoff provides an exponential backoff implementation.
p2p/host/basic/natmgr
NAT manager implementation for the basic host.
p2p/host/eventbus/basic
Implementation of the EventBus interface.
p2p/host/eventbus/eventbus
Public API for the EventBus implementation.
p2p/host/eventbus/metrics
p2p/host/eventbus/opts
Options for the EventBus implementation.
p2p/host/host
Host implementations for libp2p.
p2p/host/peerstore/peerstore
Package peerstore provides utility functions for working with peerstores.
p2p/host/peerstore/pstoremem
Export the memory-based peerstore implementation.
p2p/host/peerstore/pstoremem/addr_book
AddrBook implementation for the memory-based peerstore.
p2p/host/peerstore/pstoremem/key_book
KeyBook implementation for the memory-based peerstore.
p2p/host/peerstore/pstoremem/metadata
Metadata implementation for the memory-based peerstore.
p2p/host/peerstore/pstoremem/metrics
Metrics implementation for the memory-based peerstore.
p2p/host/peerstore/pstoremem/peerstore
Peerstore implementation for the memory-based peerstore.
p2p/host/peerstore/pstoremem/proto_book
ProtoBook implementation for the memory-based peerstore.
p2p/host/pstoremanager/pstoremanager
Package pstoremanager provides a manager for the peerstore that removes peers that have disconnected and haven't reconnected within a grace period.
p2p/host/relaysvc/relay_manager
p2p/host/resource_manager/limit
p2p/host/resource_manager/limiter
p2p/host/resource_manager/resource_manager_impl
p2p/host/resource_manager/scope_impl
p2p/host/resource_manager/scopes/connection_scope_impl
p2p/host/resource_manager/scopes/peer_scope_impl
p2p/host/resource_manager/scopes/protocol_scope_impl
p2p/host/resource_manager/scopes/service_scope_impl
p2p/host/resource_manager/scopes/stream_scope_impl
p2p/host/resource_manager/scopes/system_scope_impl
p2p/host/resource_manager/scopes/transient_scope_impl
p2p/host/routed/routed_host
p2p/multiaddr/codec
p2p/multiaddr/protocol
p2p/multiaddr/validator
p2p/nat/nat_behavior
p2p/nat/nat_behavior_discovery
p2p/nat/nat_behavior_tracker
p2p/nat/nat_status_reporter
p2p/nat/nat_traversal_strategy
p2p/nat/nat_type
p2p/nat/network_interface_monitor
p2p/nat/storage_broker
p2p/nat/stun/stun_client
p2p/nat/stun/stun_client_pool
p2p/nat/stun/stun_message
p2p/network/conn_gater
p2p/network/connmgr/connmgr
p2p/network/connmgr/null_conn_mgr
p2p/network/swarm/connection_health
p2p/network/swarm/swarm
p2p/network/swarm/swarm_conn
p2p/network/swarm/swarm_dial
p2p/network/swarm/swarm_stream
p2p/peerstore
Export the peerstore interfaces.
p2p/protocol/autonatv2
p2p/protocol/autonatv2/autonatv2
p2p/protocol/autonatv2/client
p2p/protocol/autonatv2/options
p2p/protocol/autonatv2/pb/autonatv2.pb
p2p/protocol/autonatv2/pb/autonatv2.pbenum
p2p/protocol/autonatv2/pb/autonatv2.pbjson
p2p/protocol/autonatv2/pb/autonatv2.pbserver
p2p/protocol/autonatv2/server
p2p/protocol/circuitv2/client/circuit_connection
p2p/protocol/circuitv2/client/client
p2p/protocol/circuitv2/client/conn
p2p/protocol/circuitv2/client/reservation
p2p/protocol/circuitv2/pb/circuit.pb
p2p/protocol/circuitv2/pb/circuit.pbenum
p2p/protocol/circuitv2/pb/circuit.pbjson
p2p/protocol/circuitv2/pb/circuit.pbserver
p2p/protocol/circuitv2/pb/voucher.pb
p2p/protocol/circuitv2/pb/voucher.pbenum
p2p/protocol/circuitv2/pb/voucher.pbjson
p2p/protocol/circuitv2/pb/voucher.pbserver
p2p/protocol/circuitv2/proto
p2p/protocol/circuitv2/relay/options
p2p/protocol/circuitv2/relay/relay
p2p/protocol/circuitv2/relay/resources
p2p/protocol/circuitv2/util/io
p2p/protocol/circuitv2/util/pbconv
p2p/protocol/circuitv2/voucher
p2p/protocol/holepunch
Package holepunch provides the holepunch service for libp2p.
p2p/protocol/holepunch/filter
Address filter for the holepunch protocol.
p2p/protocol/holepunch/holepunch
Package holepunch provides the holepunch service for libp2p.
p2p/protocol/holepunch/holepunch_service
Package holepunch provides the holepunch service for libp2p.
p2p/protocol/holepunch/holepuncher
The holepuncher implementation for the holepunch protocol.
p2p/protocol/holepunch/metrics
Metrics for the holepunch protocol.
p2p/protocol/holepunch/pb/holepunch.pb
p2p/protocol/holepunch/pb/holepunch.pbenum
p2p/protocol/holepunch/pb/holepunch.pbjson
p2p/protocol/holepunch/pb/holepunch.pbserver
p2p/protocol/holepunch/service
Implementation of the holepunch service.
p2p/protocol/holepunch/tracer
Tracer implementation for the holepunch protocol.
p2p/protocol/holepunch/util
Utility functions for the holepunch protocol.
p2p/protocol/http/http_protocol
p2p/protocol/identify/id_service
Package identify provides the identify service for libp2p.
p2p/protocol/identify/identify
Identify service for libp2p.
p2p/protocol/identify/metrics
Metrics for the identify service.
p2p/protocol/identify/nat_emitter
NAT emitter for libp2p.
p2p/protocol/identify/observed_addr_manager
Observed address manager for libp2p.
p2p/protocol/identify/options
Options for the identify service.
p2p/protocol/identify/pb/identify.pb
p2p/protocol/identify/pb/identify.pbenum
p2p/protocol/identify/pb/identify.pbjson
p2p/protocol/identify/pb/identify.pbserver
p2p/protocol/identify/user_agent
User agent for the identify service.
p2p/protocol/multistream/client
Package multistream implements client functionality for the multistream-select protocol. The protocol is defined at https://github.com/multiformats/multistream-select
p2p/protocol/multistream/lazy_client
Package multistream implements lazy client functionality for the multistream-select protocol. The protocol is defined at https://github.com/multiformats/multistream-select
p2p/protocol/multistream/multistream
Package multistream implements a simple stream router for the multistream-select protocol. The protocol is defined at https://github.com/multiformats/multistream-select
p2p/protocol/obp/obp_frame
p2p/protocol/obp/obp_protocol_handler
p2p/protocol/ping/ping
p2p/protocol/stomp
STOMP (Simple Text Oriented Messaging Protocol) implementation for libp2p.
p2p/protocol/stomp/example/stomp_example
Example demonstrating the STOMP protocol implementation for dart-libp2p
p2p/protocol/stomp/stomp_client
p2p/protocol/stomp/stomp_constants
p2p/protocol/stomp/stomp_exceptions
p2p/protocol/stomp/stomp_frame
p2p/protocol/stomp/stomp_server
p2p/protocol/stomp/stomp_service
p2p/protocol/stomp/stomp_subscription
p2p/protocol/stomp/stomp_transaction
p2p/security/noise/handshake_state
p2p/security/noise/message_framing
p2p/security/noise/noise_message
p2p/security/noise/noise_protocol
p2p/security/noise/noise_state
p2p/security/noise/xx_pattern
p2p/security/secured_connection
p2p/security/security_protocol
p2p/transport/basic_upgrader
p2p/transport/connection_manager
p2p/transport/connection_state
p2p/transport/listener
p2p/transport/multiplexing/multiplexer
p2p/transport/multiplexing/yamux/frame
p2p/transport/multiplexing/yamux/session
p2p/transport/multiplexing/yamux/stream
p2p/transport/multiplexing/yamux/yamux_exceptions
Yamux-specific exception handling and classification
p2p/transport/network_service
p2p/transport/p2p_stream_adapter
p2p/transport/tcp_connection
p2p/transport/tcp_listener
p2p/transport/tcp_transport
p2p/transport/transport
p2p/transport/transport_config
p2p/transport/udx_exceptions
p2p/transport/udx_stream_adapter
p2p/transport/udx_transport
p2p/transport/upgrader
pb/noise/payload.pb
pb/noise/payload.pbenum
pb/noise/payload.pbjson
pb/noise/payload.pbserver
pb/proto/noise/payload.pb
pb/proto/noise/payload.pbenum
pb/proto/noise/payload.pbjson
pb/proto/noise/payload.pbserver
utils/protobuf_utils
utils/varint