rift_torrent/lib.rs
1//! rift-torrent: SRT-based torrent peer discovery for zero-infrastructure P2P.
2//!
3//! This crate provides BitTorrent-compatible metadata parsing with Predictive
4//! Rendezvous (SRT) extensions for zero-infrastructure peer discovery.
5//!
6//! ## Key Features
7//!
8//! - **Zero-Infrastructure Discovery**: Use SRTs derived from infohash instead of
9//! trackers or DHT bootstrap nodes
10//! - **BitTorrent Compatible**: Parse and create standard .torrent files with
11//! optional SRT extensions
12//! - **Magnet URI Support**: Parse magnet URIs with `xs=riftd-srt://` parameter
13//! - **Deterministic Rendezvous**: Peers independently derive identical schedules
14//! from the infohash alone
15//!
16//! ## How It Works
17//!
18//! Instead of announcing to a tracker or bootstrapping DHT nodes, peers derive
19//! a Semantic Rendezvous Token (SRT) from the torrent infohash:
20//!
21//! ```text
22//! infohash → BLAKE3 → space_id (namespace isolation)
23//! infohash + t0 → BLAKE3 → seed (schedule derivation)
24//! ```
25//!
26//! Both peers compute identical rendezvous schedules and can discover each other
27//! without any central infrastructure.
28//!
29//! ## Example
30//!
31//! ```rust,no_run
32//! use rift_torrent::{MagnetUri, SwarmSrt, SwarmDiscovery, DiscoveryConfig, InfoHash};
33//! use rift_rndzv::PeerId;
34//!
35//! // Parse a magnet URI with SRT extension
36//! let magnet = MagnetUri::parse(
37//! "magnet:?xt=urn:btih:0123456789abcdef0123456789abcdef01234567&xs=riftd-srt://v1..."
38//! ).unwrap();
39//!
40//! // Or derive SRT from just an infohash
41//! let srt = SwarmSrt::from_infohash(*magnet.primary_hash());
42//!
43//! // Create a discovery instance
44//! let local_peer = PeerId([0u8; 32]); // Your peer ID
45//! let discovery = SwarmDiscovery::new(srt, local_peer, DiscoveryConfig::default());
46//!
47//! // Get the SRT URI for sharing
48//! let uri = discovery.srt_uri().unwrap();
49//! println!("Share this SRT: {}", uri);
50//! ```
51//!
52//! ## Magnet URI Extension
53//!
54//! Standard magnet URIs can include an SRT in the `xs` (exact source) parameter:
55//!
56//! ```text
57//! magnet:?xt=urn:btih:HASH&dn=NAME&xs=riftd-srt://v1?space=...&seed=...
58//! ```
59//!
60//! ## Fallback Strategy
61//!
62//! When SRT discovery fails or times out, the crate supports fallback to
63//! traditional methods:
64//!
65//! 1. **SRT Rendezvous** (primary) - Zero-infrastructure
66//! 2. **DHT Hints** (hybrid) - Use DHT for candidate addresses
67//! 3. **DHT Bootstrap** (fallback) - Standard Kademlia lookup
68//! 4. **Tracker Announce** (fallback) - HTTP/UDP trackers
69
70pub mod bencode;
71pub mod error;
72pub mod magnet;
73pub mod meta;
74pub mod peer;
75pub mod srt;
76
77// Re-exports for convenience
78pub use error::TorrentError;
79pub use magnet::MagnetUri;
80pub use meta::{FileInfo, InfoHash, PieceHash, SrtExtension, TorrentMeta};
81pub use peer::{DiscoveryConfig, DiscoveryMode, PeerExchange, PexPeer, SwarmDiscovery, SwarmPeer};
82pub use srt::{derive_seed, derive_space_id, SwarmSrt};
83
84// Re-export bencode utilities
85pub use bencode::{decode as decode_bencode, encode as encode_bencode, parse_torrent, BValue};
86
87// Re-export commonly used rift-rndzv types
88pub use rift_rndzv::{PeerId, RendezvousSpaceId, TimeModel};