Skip to content

Commit

Permalink
feat(webrtc-websys): hide libp2p_noise from the public API
Browse files Browse the repository at this point in the history
Currently, `libp2p-webrtc-websys` exposes the `libp2p_noise` dependency in its public API. It should really be a private dependency of the crate. By wrapping it in a new-type, we can achieve this.

Pull-Request: #4969.
  • Loading branch information
thomaseizinger authored Dec 1, 2023
1 parent 6d21e6e commit c1925e5
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 7 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions misc/webrtc-utils/src/noise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ use libp2p_noise as noise;

use crate::fingerprint::Fingerprint;

pub use noise::Error;

pub async fn inbound<T>(
id_keys: identity::Keypair,
stream: T,
client_fingerprint: Fingerprint,
server_fingerprint: Fingerprint,
) -> Result<PeerId, libp2p_noise::Error>
) -> Result<PeerId, Error>
where
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
Expand All @@ -54,7 +56,7 @@ pub async fn outbound<T>(
stream: T,
server_fingerprint: Fingerprint,
client_fingerprint: Fingerprint,
) -> Result<PeerId, libp2p_noise::Error>
) -> Result<PeerId, Error>
where
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
Expand Down
2 changes: 2 additions & 0 deletions transports/webrtc-websys/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Bump version in order to publish a new version dependent on latest `libp2p-core`.
See [PR 4959](https://github.com/libp2p/rust-libp2p/pull/4959).
- Remove `libp2p_noise` from the public API.
See [PR 4969](https://github.com/libp2p/rust-libp2p/pull/4969).

## 0.2.0-alpha

Expand Down
1 change: 0 additions & 1 deletion transports/webrtc-websys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ hex = "0.4.3"
js-sys = { version = "0.3" }
libp2p-core = { workspace = true }
libp2p-identity = { workspace = true }
libp2p-noise = { workspace = true }
libp2p-webrtc-utils = { workspace = true }
send_wrapper = { version = "0.6.0", features = ["futures"] }
serde = { version = "1.0", features = ["derive"] }
Expand Down
9 changes: 7 additions & 2 deletions transports/webrtc-websys/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ pub enum Error {
Connection(String),

#[error("Authentication error")]
Authentication(#[from] libp2p_noise::Error),
Authentication(#[from] AuthenticationError),
}

/// New-type wrapper to hide `libp2p_noise` from the public API.
#[derive(thiserror::Error, Debug)]
#[error(transparent)]
pub struct AuthenticationError(pub(crate) libp2p_webrtc_utils::noise::Error);

impl Error {
pub(crate) fn from_js_value(value: JsValue) -> Self {
let s = if value.is_instance_of::<js_sys::Error>() {
Expand All @@ -38,7 +43,7 @@ impl Error {
}
}

impl std::convert::From<wasm_bindgen::JsValue> for Error {
impl From<JsValue> for Error {
fn from(value: JsValue) -> Self {
Error::from_js_value(value)
}
Expand Down
5 changes: 4 additions & 1 deletion transports/webrtc-websys/src/upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::Error;
use crate::connection::RtcPeerConnection;
use crate::error::AuthenticationError;
use crate::sdp;
use crate::Connection;
use libp2p_identity::{Keypair, PeerId};
Expand Down Expand Up @@ -48,7 +49,9 @@ async fn outbound_inner(
tracing::trace!(?local_fingerprint);
tracing::trace!(?remote_fingerprint);

let peer_id = noise::outbound(id_keys, channel, remote_fingerprint, local_fingerprint).await?;
let peer_id = noise::outbound(id_keys, channel, remote_fingerprint, local_fingerprint)
.await
.map_err(AuthenticationError)?;

tracing::debug!(peer=%peer_id, "Remote peer identified");

Expand Down

0 comments on commit c1925e5

Please sign in to comment.