dart_libp2p 0.5.3 copy "dart_libp2p: ^0.5.3" to clipboard
dart_libp2p: ^0.5.3 copied to clipboard

A comprehensive Dart implementation of the libp2p networking stack with modular architecture, multiple transports (TCP/UDX), Noise security, and peer-to-peer capabilities.

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
4
likes
120
points
224
downloads

Publisher

unverified uploader

Weekly Downloads

A comprehensive Dart implementation of the libp2p networking stack with modular architecture, multiple transports (TCP/UDX), Noise security, and peer-to-peer capabilities.

Repository (GitHub)
View/report issues

Documentation

Documentation
API reference

License

MIT (license)

Dependencies

args, async, base32, base_x, bs58, buffer, collection, convert, crypto, cryptography, dart_multihash, dart_udx, dcid, fixnum, logging, meta, multicast_dns, pointycastle, protobuf, stack_trace, synchronized, uuid, x25519

More

Packages that depend on dart_libp2p