Skip to content

Commit

Permalink
feat: client compiling for wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Jan 22, 2024
1 parent e7536fc commit 8b8d498
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
13 changes: 10 additions & 3 deletions crates/torii/libp2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ 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
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"
45 changes: 44 additions & 1 deletion crates/torii/libp2p/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Self, Error> {
let local_key = identity::Keypair::generate_ed25519();
let peer_id = PeerId::from(local_key.public());
Expand Down Expand Up @@ -64,6 +67,46 @@ impl Libp2pClient {
Ok(Self { swarm, topics: HashMap::new() })
}

#[cfg(target_arch = "wasm32")]
pub fn new(relay_addr: String) -> Result<Self, Error> {
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::<Multiaddr>()?)?;

Ok(Self { swarm, topics: HashMap::new() })
}

pub async fn run(&mut self, sender: &UnboundedSender<Message>) {
loop {
// Poll the swarm for new events.
Expand Down
2 changes: 2 additions & 0 deletions crates/torii/libp2p/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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),

Expand Down

0 comments on commit 8b8d498

Please sign in to comment.