The Swift implementation of the libp2p networking stack
libp2p is a networking stack and library modularized out of The IPFS Project, and bundled separately for other tools to use.
libp2p is the product of a long, and arduous quest of understanding -- a deep dive into the internet's network stack, and plentiful peer-to-peer protocols from the past. Building large-scale peer-to-peer systems has been complex and difficult in the last 15 years, and libp2p is a way to fix that. It is a "network stack" -- a protocol suite -- that cleanly separates concerns, and enables sophisticated applications to only use the protocols they absolutely need, without giving up interoperability and upgradeability. libp2p grew out of IPFS, but it is built so that lots of people can use it, for lots of different projects.
To learn more, check out the following resources:
- the libp2p documentation
- the libp2p community discussion forum
- the libp2p specification
- the go-libp2p implementation
- the js-libp2p implementation
- the rust-libp2p implementation
βΌοΈ This is a work in progressβΌοΈ βΌοΈ Please don't use swift-libp2p in anything other than experimental projects until it reaches 1.0βΌοΈ βΌοΈ Help it get there sooner by contributingβΌοΈ
Include the following dependency in your Package.swift file
let package = Package(
...
dependencies: [
...
.package(name: "LibP2P", url: "https://github.com/swift-libp2p/swift-libp2p.git", .upToNextMinor(from: "0.3.0"))
],
...
.target(
...
dependencies: [
...
.product(name: "LibP2P", package: "swift-libp2p"),
]),
...
)import LibP2P
import LibP2PNoise
import LibP2PYAMUX
/// Configure your Libp2p networking stack...
let lib = try await Application.make(.detect(), peerID: .ephemeral(.Ed25519))
// Configure the libp2p instance with the modules of your choosing
lib.security.use(.noise)
lib.muxers.use(.yamux)
// Start a TCP server listening on localhost:10000
lib.servers.use(.tcp(host: "127.0.0.1", port: 10_000))
/// Register your routes handlers...
/// - Note: Uses the same syntax as swift-vapor
try lib.routes()
/// Start libp2p
try await lib.startup()
/// Do some networking stuff... π‘
/// At some later point, when you're done with libp2p...
try await lib.asyncShutdown()- Check out the libp2p-app-template repo for a bare-bones executable app ready to be customized
- Check out the Configure an Echo Server tutorial in the documentation for more info
- List of packages currently in existence for swift libp2p:
- Legend: π’ = kinda works, π‘ = doesn't really work yet, π΄ = not started yet, but on the radar
| Name | Status | Description | Build (macOS & Linux) |
|---|---|---|---|
| Libp2p | |||
swift-libp2p |
π’ | swift-libp2p entry point | |
swift-libp2p-core |
π’ | Core interfaces, types, and abstractions | |
| Protocol Negotiation | |||
swift-libp2p-mss |
π’ | MultistreamSelect transport upgrader (embedded) | |
| Transport | |||
swift-libp2p-tcp |
π’ | TCP transport (embedded) | N/A |
swift-libp2p-udp |
π‘ | UDP transport (embedded) | N/A |
swift-libp2p-quic |
π΄ | TODO: QUIC transport | N/A |
swift-libp2p-websocket |
π’ | WebSocket transport | |
swift-libp2p-http |
π΄ | TODO: HTTP1 transport | N/A |
swift-libp2p-http2 |
π΄ | TODO: HTTP2 transport | N/A |
swift-libp2p-webrtc |
π΄ | TODO: WebRTC & WebRTC Direct | N/A |
swift-libp2p-webtransport |
π΄ | TODO: WebTransport | N/A |
| Encrypted Channels | |||
swift-libp2p-plaintext |
π’ | Plaintext channel | |
swift-libp2p-noise |
π’ | Noise crypto channel | |
swift-libp2p-tls |
π΄ | TODO: TLS 1.3+ crypto channel | N/A |
| Stream Muxers | |||
swift-libp2p-mplex |
π’ | MPLEX stream multiplexer | |
swift-libp2p-yamux |
π’ | YAMUX stream multiplexer | |
| Private Network | |||
swift-libp2p-pnet |
π΄ | TODO: Private Networks (psk) | N/A |
| NAT Traversal | |||
swift-libp2p-autonat |
π΄ | TODO: AutoNAT v1 & v2 | N/A |
| Peerstore | |||
swift-libp2p-peerstore |
π‘ | Reference implementation of peer metadata storage component (embedded) | N/A |
| Connection Manager | |||
swift-libp2p-connection-manager |
π‘ | Reference implementation of connection manager (embedded) | N/A |
| Routing | |||
swift-libp2p-kad-dht |
π‘ | Kademlia Distributed Hash Table | |
swift-libp2p-circuit |
π΄ | TODO: Circuit Relay v1 & v2 | N/A |
swift-libp2p-dcutr |
π΄ | TODO: DCUtR (hole punching) | N/A |
| Pubsub | |||
swift-libp2p-pubsub |
π‘ | Core PubSub Protocols & FloodSub and GossipSub Routers | |
| RPC | |||
swift-libp2p-rpc |
π΄ | TODO: A simple RPC library for libp2p | N/A |
| Utilities/miscellaneous | |||
swift-libp2p-dnsaddr |
π’ | A DNSAddr Resolver | |
swift-libp2p-mdns |
π‘ | MulticastDNS for LAN discovery | |
swift-libp2p-identify |
π’ | IPFS Identify Protocols (embedded) | |
swift-libp2p-rendezvous |
π΄ | TODO: Rendezvous (protocol poster board) | N/A |
| Integrations | |||
swift-libp2p-queues-redis-driver |
π’ | A Queues driver powered by Redis | |
swift-libp2p-fluent |
π’ | Fluent, a Database Abstraction Layer | |
| Testing and examples | |||
swift-libp2p-testing |
π΄ | TODO: A collection of testing utilities for libp2p | N/A |
| Name | Description | Build (macOS & Linux) |
|---|---|---|
| Cryptography | ||
swift-libp2p-crypto |
Crypto abstractions for Keys, Hashes and Ciphers | |
| Multiformats | ||
swift-multibase |
Self Identifying Base Encodings | |
swift-multicodec |
Multiformat Codecs | |
swift-multihash |
Self Identifying Hashes | |
swift-multiaddr |
Self Identifying Addresses | |
swift-peer-id |
Peer IDs | |
| Utilities | ||
swift-bases |
Base encodings & decodings | |
swift-varint |
Protocol Buffer Variable Integers | |
swift-cid |
Content Identifiers | |
| External | ||
swift-nio |
Network application framework | N/A |
/// TODOContributions are welcomed! This code is very much a proof of concept. I can guarantee you there's a better / safer way to accomplish the same results. Any suggestions, improvements, or even just critiques, are welcome!
Let's make this code better together! π€
MIT Β© 2022 Breth Inc.
