diff --git a/crates/torii/libp2p/Cargo.toml b/crates/torii/libp2p/Cargo.toml index 74e832ca95..7d27ad2581 100644 --- a/crates/torii/libp2p/Cargo.toml +++ b/crates/torii/libp2p/Cargo.toml @@ -10,10 +10,7 @@ version.workspace = true [dependencies] futures.workspace = true -libp2p = { git = "https://github.com/libp2p/rust-libp2p", features = [ "ed25519", "gossipsub", "identify", "macros", "mdns", "noise", "ping", "quic", "relay", "tcp", "tokio", "yamux" ] } -libp2p-webrtc = { git = "https://github.com/libp2p/rust-libp2p", features = [ "tokio" ] } rand = "0.8.5" -tokio.workspace = true serde.workspace = true serde_json.workspace = true thiserror.workspace = true @@ -21,3 +18,13 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] } tracing.workspace = true async-trait = "0.1.77" regex = "1.10.3" + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +tokio.workspace = true +libp2p = { git = "https://github.com/libp2p/rust-libp2p", features = [ "ed25519", "gossipsub", "identify", "macros", "noise", "ping", "quic", "relay", "tcp", "tokio", "yamux" ] } +libp2p-webrtc = { git = "https://github.com/libp2p/rust-libp2p", features = [ "tokio" ] } + +[target.'cfg(target_arch = "wasm32")'.dependencies] +libp2p = { git = "https://github.com/libp2p/rust-libp2p", features = [ "ed25519", "gossipsub", "identify", "macros", "ping", "tcp", "tokio", "wasm-bindgen" ] } +libp2p-webrtc-websys = { git = "https://github.com/libp2p/rust-libp2p" } +tracing-wasm = "0.2.1" diff --git a/crates/torii/libp2p/src/client/mod.rs b/crates/torii/libp2p/src/client/mod.rs index edc7c0d44c..93f9356765 100644 --- a/crates/torii/libp2p/src/client/mod.rs +++ b/crates/torii/libp2p/src/client/mod.rs @@ -4,7 +4,9 @@ use futures::channel::mpsc::UnboundedSender; use futures::StreamExt; use libp2p::gossipsub::{self, IdentTopic, MessageId}; use libp2p::swarm::{NetworkBehaviour, Swarm, SwarmEvent}; -use libp2p::{identify, identity, noise, ping, tcp, yamux, Multiaddr, PeerId}; +use libp2p::{identify, identity, ping, Multiaddr, PeerId}; +#[cfg(not(target_arch = "wasm32"))] +use libp2p::{noise, tcp, yamux}; use tracing::info; pub mod events; @@ -28,6 +30,7 @@ pub struct Libp2pClient { pub type Message = (PeerId, MessageId, ServerMessage); impl Libp2pClient { + #[cfg(not(target_arch = "wasm32"))] pub fn new(relay_addr: String) -> Result { let local_key = identity::Keypair::generate_ed25519(); let peer_id = PeerId::from(local_key.public()); @@ -64,6 +67,46 @@ impl Libp2pClient { Ok(Self { swarm, topics: HashMap::new() }) } + #[cfg(target_arch = "wasm32")] + pub fn new(relay_addr: String) -> Result { + let local_key = identity::Keypair::generate_ed25519(); + let peer_id = PeerId::from(local_key.public()); + + info!("Local peer id: {:?}", peer_id); + + let mut swarm = libp2p::SwarmBuilder::with_existing_identity(local_key) + .with_wasm_bindgen() + .with_other_transport(|key| { + libp2p_webrtc_websys::Transport::new(libp2p_webrtc_websys::Config::new(&key)) + }) + .expect("Failed to create WebRTC transport") + .with_behaviour(|key| { + let gossipsub_config: gossipsub::Config = gossipsub::ConfigBuilder::default() + .heartbeat_interval(std::time::Duration::from_secs(10)) + .build() + .expect("Gossipsup config is invalid"); + + Behaviour { + gossipsub: gossipsub::Behaviour::new( + gossipsub::MessageAuthenticity::Signed(key.clone()), + gossipsub_config, + ) + .expect("Gossipsub behaviour is invalid"), + identify: identify::Behaviour::new(identify::Config::new( + "/torii-client/0.0.1".to_string(), + key.public(), + )), + ping: ping::Behaviour::new(ping::Config::default()), + } + })? + .build(); + + info!("Dialing relay: {:?}", relay_addr); + swarm.dial(relay_addr.parse::()?)?; + + Ok(Self { swarm, topics: HashMap::new() }) + } + pub async fn run(&mut self, sender: &UnboundedSender) { loop { // Poll the swarm for new events. diff --git a/crates/torii/libp2p/src/errors.rs b/crates/torii/libp2p/src/errors.rs index 7091f522f0..4b2236c1c0 100644 --- a/crates/torii/libp2p/src/errors.rs +++ b/crates/torii/libp2p/src/errors.rs @@ -2,6 +2,7 @@ use std::convert::Infallible; use std::io; use libp2p::gossipsub::{PublishError, SubscriptionError}; +#[cfg(not(target_arch = "wasm32"))] use libp2p::noise; use thiserror::Error; @@ -10,6 +11,7 @@ pub enum Error { #[error(transparent)] MultiaddrParseError(#[from] libp2p::core::multiaddr::Error), + #[cfg(not(target_arch = "wasm32"))] #[error(transparent)] NoiseUpgradeError(#[from] noise::Error),