Skip to content

Commit

Permalink
Some fixes to #[cfg] regarding Wasi (#1633)
Browse files Browse the repository at this point in the history
Co-authored-by: Roman Borschel <[email protected]>
  • Loading branch information
tomaka and romanb authored Jun 30, 2020
1 parent 826f513 commit 79ff1e4
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pin-project = "0.4.17"
smallvec = "1.0"
wasm-timer = "0.2.4"

[target.'cfg(not(any(target_os = "emscripten", target_os = "unknown")))'.dependencies]
[target.'cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))'.dependencies]
libp2p-deflate = { version = "0.19.2", path = "protocols/deflate", optional = true }
libp2p-dns = { version = "0.19.0", path = "transports/dns", optional = true }
libp2p-mdns = { version = "0.19.2", path = "protocols/mdns", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ unsigned-varint = "0.4"
void = "1"
zeroize = "1"

[target.'cfg(not(any(target_os = "emscripten", target_os = "unknown")))'.dependencies]
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ring = { version = "0.16.9", features = ["alloc", "std"], default-features = false }

[dev-dependencies]
Expand Down
20 changes: 10 additions & 10 deletions core/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//! A node's network identity keys.
pub mod ed25519;
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
pub mod rsa;
#[cfg(feature = "secp256k1")]
pub mod secp256k1;
Expand Down Expand Up @@ -52,7 +52,7 @@ use crate::{PeerId, keys_proto};
pub enum Keypair {
/// An Ed25519 keypair.
Ed25519(ed25519::Keypair),
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
/// An RSA keypair.
Rsa(rsa::Keypair),
/// A Secp256k1 keypair.
Expand All @@ -76,7 +76,7 @@ impl Keypair {
/// format (i.e. unencrypted) as defined in [RFC5208].
///
/// [RFC5208]: https://tools.ietf.org/html/rfc5208#section-5
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
pub fn rsa_from_pkcs8(pkcs8_der: &mut [u8]) -> Result<Keypair, DecodingError> {
rsa::Keypair::from_pkcs8(pkcs8_der).map(Keypair::Rsa)
}
Expand All @@ -97,7 +97,7 @@ impl Keypair {
use Keypair::*;
match self {
Ed25519(ref pair) => Ok(pair.sign(msg)),
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
Rsa(ref pair) => pair.sign(msg),
#[cfg(feature = "secp256k1")]
Secp256k1(ref pair) => pair.secret().sign(msg)
Expand All @@ -109,7 +109,7 @@ impl Keypair {
use Keypair::*;
match self {
Ed25519(pair) => PublicKey::Ed25519(pair.public()),
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
Rsa(pair) => PublicKey::Rsa(pair.public()),
#[cfg(feature = "secp256k1")]
Secp256k1(pair) => PublicKey::Secp256k1(pair.public().clone()),
Expand All @@ -122,7 +122,7 @@ impl Keypair {
pub enum PublicKey {
/// A public Ed25519 key.
Ed25519(ed25519::PublicKey),
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
/// A public RSA key.
Rsa(rsa::PublicKey),
#[cfg(feature = "secp256k1")]
Expand All @@ -139,7 +139,7 @@ impl PublicKey {
use PublicKey::*;
match self {
Ed25519(pk) => pk.verify(msg, sig),
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
Rsa(pk) => pk.verify(msg, sig),
#[cfg(feature = "secp256k1")]
Secp256k1(pk) => pk.verify(msg, sig)
Expand All @@ -157,7 +157,7 @@ impl PublicKey {
r#type: keys_proto::KeyType::Ed25519 as i32,
data: key.encode().to_vec()
},
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
PublicKey::Rsa(key) =>
keys_proto::PublicKey {
r#type: keys_proto::KeyType::Rsa as i32,
Expand Down Expand Up @@ -192,11 +192,11 @@ impl PublicKey {
keys_proto::KeyType::Ed25519 => {
ed25519::PublicKey::decode(&pubkey.data).map(PublicKey::Ed25519)
},
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
keys_proto::KeyType::Rsa => {
rsa::PublicKey::decode_x509(&pubkey.data).map(PublicKey::Rsa)
}
#[cfg(any(target_os = "emscripten", target_os = "unknown"))]
#[cfg(target_arch = "wasm32")]
keys_proto::KeyType::Rsa => {
log::debug!("support for RSA was disabled at compile-time");
Err(DecodingError::new("Unsupported"))
Expand Down
4 changes: 2 additions & 2 deletions protocols/noise/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ static_assertions = "1"
x25519-dalek = "0.6.0"
zeroize = "1"

[target.'cfg(not(target_os = "unknown"))'.dependencies]
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
snow = { version = "0.7.0", features = ["ring-resolver"], default-features = false }

[target.'cfg(target_os = "unknown")'.dependencies]
[target.'cfg(target_arch = "wasm32")'.dependencies]
snow = { version = "0.7.0", features = ["default-resolver"], default-features = false }

[dev-dependencies]
Expand Down
8 changes: 4 additions & 4 deletions protocols/noise/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,22 +229,22 @@ impl snow::resolvers::CryptoResolver for Resolver {
}

fn resolve_hash(&self, choice: &snow::params::HashChoice) -> Option<Box<dyn snow::types::Hash>> {
#[cfg(target_os = "unknown")]
#[cfg(target_arch = "wasm32")]
{
snow::resolvers::DefaultResolver.resolve_hash(choice)
}
#[cfg(not(target_os = "unknown"))]
#[cfg(not(target_arch = "wasm32"))]
{
snow::resolvers::RingResolver.resolve_hash(choice)
}
}

fn resolve_cipher(&self, choice: &snow::params::CipherChoice) -> Option<Box<dyn snow::types::Cipher>> {
#[cfg(target_os = "unknown")]
#[cfg(target_arch = "wasm32")]
{
snow::resolvers::DefaultResolver.resolve_cipher(choice)
}
#[cfg(not(target_os = "unknown"))]
#[cfg(not(target_arch = "wasm32"))]
{
snow::resolvers::RingResolver.resolve_cipher(choice)
}
Expand Down
4 changes: 2 additions & 2 deletions protocols/secio/src/algo_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
//! helps you with.
use crate::error::SecioError;
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
use ring::digest;
use std::cmp::Ordering;
use crate::stream_cipher::Cipher;
Expand Down Expand Up @@ -204,7 +204,7 @@ pub fn select_digest(r: Ordering, ours: &str, theirs: &str) -> Result<Digest, Se
Err(SecioError::NoSupportIntersection)
}

#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
impl Into<&'static digest::Algorithm> for Digest {
#[inline]
fn into(self) -> &'static digest::Algorithm {
Expand Down
4 changes: 2 additions & 2 deletions protocols/secio/src/exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ use futures::prelude::*;
use crate::SecioError;

#[path = "exchange/impl_ring.rs"]
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
mod platform;
#[path = "exchange/impl_webcrypto.rs"]
#[cfg(any(target_os = "emscripten", target_os = "unknown"))]
#[cfg(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown"))]
mod platform;

/// Possible key agreement algorithms.
Expand Down
2 changes: 1 addition & 1 deletion protocols/secio/src/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ mod tests {
use futures::{prelude::*, channel::oneshot};

#[test]
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
fn handshake_with_self_succeeds_rsa() {
let key1 = {
let mut private = include_bytes!("../tests/test-rsa-private-key.pk8").to_vec();
Expand Down
24 changes: 12 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
//! Example ([`secio`] + [`yamux`] Protocol Upgrade):
//!
//! ```rust
//! # #[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp-async-std", feature = "secio", feature = "yamux"))] {
//! # #[cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), feature = "tcp-async-std", feature = "secio", feature = "yamux"))] {
//! use libp2p::{Transport, core::upgrade, tcp::TcpConfig, secio::SecioConfig, identity::Keypair, yamux};
//! let tcp = TcpConfig::new();
//! let secio = SecioConfig::new(Keypair::generate_ed25519());
Expand Down Expand Up @@ -166,12 +166,12 @@ pub use multihash;
pub use libp2p_core as core;
#[cfg(feature = "deflate")]
#[cfg_attr(docsrs, doc(cfg(feature = "deflate")))]
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
#[doc(inline)]
pub use libp2p_deflate as deflate;
#[cfg(feature = "dns")]
#[cfg_attr(docsrs, doc(cfg(feature = "dns")))]
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
#[doc(inline)]
pub use libp2p_dns as dns;
#[cfg(feature = "identify")]
Expand All @@ -196,7 +196,7 @@ pub use libp2p_gossipsub as gossipsub;
pub use libp2p_mplex as mplex;
#[cfg(feature = "mdns")]
#[cfg_attr(docsrs, doc(cfg(feature = "mdns")))]
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
#[doc(inline)]
pub use libp2p_mdns as mdns;
#[cfg(feature = "noise")]
Expand All @@ -219,7 +219,7 @@ pub use libp2p_secio as secio;
pub use libp2p_swarm as swarm;
#[cfg(any(feature = "tcp-async-std", feature = "tcp-tokio"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "tcp-async-std", feature = "tcp-tokio"))))]
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
#[doc(inline)]
pub use libp2p_tcp as tcp;
#[cfg(feature = "uds")]
Expand All @@ -232,7 +232,7 @@ pub use libp2p_uds as uds;
pub use libp2p_wasm_ext as wasm_ext;
#[cfg(feature = "websocket")]
#[cfg_attr(docsrs, doc(cfg(feature = "websocket")))]
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
#[doc(inline)]
pub use libp2p_websocket as websocket;
#[cfg(feature = "yamux")]
Expand Down Expand Up @@ -266,8 +266,8 @@ pub use self::transport_ext::TransportExt;
///
/// > **Note**: This `Transport` is not suitable for production usage, as its implementation
/// > reserves the right to support additional protocols or remove deprecated protocols.
#[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), any(feature = "tcp-async-std", feature = "tcp-tokio"), feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))]
#[cfg_attr(docsrs, doc(cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), any(feature = "tcp-async-std", feature = "tcp-tokio"), feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))))]
#[cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), any(feature = "tcp-async-std", feature = "tcp-tokio"), feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))]
#[cfg_attr(docsrs, doc(cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), any(feature = "tcp-async-std", feature = "tcp-tokio"), feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))))]
pub fn build_development_transport(keypair: identity::Keypair)
-> std::io::Result<impl Transport<Output = (PeerId, impl core::muxing::StreamMuxer<OutboundSubstream = impl Send, Substream = impl Send, Error = impl Into<std::io::Error>> + Send + Sync), Error = impl std::error::Error + Send, Listener = impl Send, Dial = impl Send, ListenerUpgrade = impl Send> + Clone>
{
Expand All @@ -280,8 +280,8 @@ pub fn build_development_transport(keypair: identity::Keypair)
/// and mplex or yamux as the multiplexing layer.
///
/// > **Note**: If you ever need to express the type of this `Transport`.
#[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), any(feature = "tcp-async-std", feature = "tcp-tokio"), feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))]
#[cfg_attr(docsrs, doc(cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), any(feature = "tcp-async-std", feature = "tcp-tokio"), feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))))]
#[cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), any(feature = "tcp-async-std", feature = "tcp-tokio"), feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))]
#[cfg_attr(docsrs, doc(cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), any(feature = "tcp-async-std", feature = "tcp-tokio"), feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))))]
pub fn build_tcp_ws_secio_mplex_yamux(keypair: identity::Keypair)
-> std::io::Result<impl Transport<Output = (PeerId, impl core::muxing::StreamMuxer<OutboundSubstream = impl Send, Substream = impl Send, Error = impl Into<std::io::Error>> + Send + Sync), Error = impl std::error::Error + Send, Listener = impl Send, Dial = impl Send, ListenerUpgrade = impl Send> + Clone>
{
Expand Down Expand Up @@ -309,8 +309,8 @@ pub fn build_tcp_ws_secio_mplex_yamux(keypair: identity::Keypair)
/// and mplex or yamux as the multiplexing layer.
///
/// > **Note**: If you ever need to express the type of this `Transport`.
#[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), any(feature = "tcp-async-std", feature = "tcp-tokio"), feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux", feature = "pnet"))]
#[cfg_attr(docsrs, doc(cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), any(feature = "tcp-async-std", feature = "tcp-tokio"), feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux", feature = "pnet"))))]
#[cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), any(feature = "tcp-async-std", feature = "tcp-tokio"), feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux", feature = "pnet"))]
#[cfg_attr(docsrs, doc(cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), any(feature = "tcp-async-std", feature = "tcp-tokio"), feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux", feature = "pnet"))))]
pub fn build_tcp_ws_pnet_secio_mplex_yamux(keypair: identity::Keypair, psk: PreSharedKey)
-> std::io::Result<impl Transport<Output = (PeerId, impl core::muxing::StreamMuxer<OutboundSubstream = impl Send, Substream = impl Send, Error = impl Into<std::io::Error>> + Send + Sync), Error = impl std::error::Error + Send, Listener = impl Send, Dial = impl Send, ListenerUpgrade = impl Send> + Clone>
{
Expand Down
4 changes: 2 additions & 2 deletions transports/uds/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ repository = "https://github.com/libp2p/rust-libp2p"
keywords = ["peer-to-peer", "libp2p", "networking"]
categories = ["network-programming", "asynchronous"]

[target.'cfg(all(unix, not(any(target_os = "emscripten", target_os = "unknown"))))'.dependencies]
[target.'cfg(all(unix, not(target_os = "emscripten")))'.dependencies]
async-std = { version = "1.6.2", optional = true }
libp2p-core = { version = "0.19.2", path = "../../core" }
log = "0.4.1"
futures = "0.3.1"
tokio = { version = "0.2", default-features = false, features = ["uds"], optional = true }

[target.'cfg(all(unix, not(any(target_os = "emscripten", target_os = "unknown"))))'.dev-dependencies]
[target.'cfg(all(unix, not(target_os = "emscripten")))'.dev-dependencies]
tempfile = "3.0"

[features]
Expand Down
4 changes: 2 additions & 2 deletions transports/uds/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
//! The `UdsConfig` structs implements the `Transport` trait of the `core` library. See the
//! documentation of `core` and of libp2p in general to learn how to use the `Transport` trait.
#![cfg(all(unix, not(any(target_os = "emscripten", target_os = "unknown"))))]
#![cfg_attr(docsrs, doc(cfg(all(unix, not(any(target_os = "emscripten", target_os = "unknown"))))))]
#![cfg(all(unix, not(target_os = "emscripten")))]
#![cfg_attr(docsrs, doc(cfg(all(unix, not(target_os = "emscripten")))))]

use futures::{prelude::*, future::{BoxFuture, Ready}};
use futures::stream::BoxStream;
Expand Down

0 comments on commit 79ff1e4

Please sign in to comment.