dart_libp2p 0.5.3
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 #
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:
- Getting Started: Step-by-step setup guide
- Architecture: Detailed architecture overview
- Configuration: Configuration options
- Host: Host component documentation
- Transports: Transport layer details
- Security: Security protocol information
- Multiplexing: Stream multiplexing
- Protocols: Built-in protocol documentation
- Peerstore: Peer information management
- Event Bus: Event system documentation
- Resource Manager: Resource protection
- Cookbook: Practical examples and recipes
๐งช 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