diff --git a/crates/torii/libp2p/Cargo.toml b/crates/torii/libp2p/Cargo.toml new file mode 100644 index 0000000000..aaccdacdd0 --- /dev/null +++ b/crates/torii/libp2p/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "torii-libp2p" +edition.workspace = true +license.workspace = true +license-file.workspace = true +repository.workspace = true +version.workspace = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +libp2p = { path = "0.53.2", features = [ "tokio", "gossipsub", "mdns", "noise", "macros", "tcp", "yamux", "quic"] } diff --git a/crates/torii/libp2p/src/lib.rs b/crates/torii/libp2p/src/lib.rs new file mode 100644 index 0000000000..bfe15ae421 --- /dev/null +++ b/crates/torii/libp2p/src/lib.rs @@ -0,0 +1 @@ +pub mod server; \ No newline at end of file diff --git a/crates/torii/libp2p/src/server.rs b/crates/torii/libp2p/src/server.rs new file mode 100644 index 0000000000..a00a98ed5e --- /dev/null +++ b/crates/torii/libp2p/src/server.rs @@ -0,0 +1,45 @@ +use libp2p::{gossipsub, mdns, noise, swarm::NetworkBehaviour, swarm::SwarmEvent, tcp, yamux}; + +use std::collections::HashMap; + +#[derive(NetworkBehaviour)] +pub struct Behaviour { + kademlia: Kademlia, + rooms: HashMap>, +} + +impl NetworkBehaviourEventProcess for Behaviour { + fn inject_event(&mut self, event: KademliaEvent) { + match event { + KademliaEvent::UnroutedQuery { .. } => {} + KademliaEvent::RoutableQuery { .. } => {} + KademliaEvent::RoutingUpdated { .. } => {} + KademliaEvent::RoutingUpdateFailed { .. } => {} + KademliaEvent::QueryResult { .. } => {} + } + } +} + +pub fn new() -> Swarm { + let local_key = identity::Keypair::generate_ed25519(); + let local_peer_id = PeerId::from(local_key.public()); + + let transport = DnsConfig::new( + TcpConfig::new() + .upgrade(libp2p::core::upgrade::Version::V1) + .authenticate(NoiseConfig::xx(Keypair::new().into_authentic(&local_key)).unwrap()) + .multiplex(libp2p::yamux::Config::default()) + .boxed(), + ); + + let store = MemoryStore::new(local_peer_id.clone()); + let kademlia = Kademlia::with_config(local_peer_id.clone(), store, KademliaConfig::default()); + + let behaviour = Behaviour { kademlia, rooms: HashMap::new() }; + + SwarmBuilder::new(transport, behaviour, local_peer_id) + .executor(Box::new(|fut| { + async_std::task::spawn(fut); + })) + .build() +}