Make rand a feature-gated dep

This commit is contained in:
Ivan Boldyrev 2023-08-10 13:23:25 +04:00
parent 35ea650928
commit 9a006f1f35
5 changed files with 15 additions and 4 deletions

1
Cargo.lock generated
View File

@ -726,6 +726,7 @@ dependencies = [
"curve25519-dalek", "curve25519-dalek",
"ed25519", "ed25519",
"rand 0.7.3", "rand 0.7.3",
"rand_core 0.5.1",
"serde", "serde",
"serde_bytes", "serde_bytes",
"sha2 0.9.9", "sha2 0.9.9",

View File

@ -10,8 +10,7 @@ repository = "https://github.com/fluencelabs/trust-graph"
[dependencies] [dependencies]
serde = { version = "1.0.118", features = ["derive"] } serde = { version = "1.0.118", features = ["derive"] }
bs58 = "0.5.0" bs58 = "0.5.0"
ed25519-dalek = { version = "1.0.1", features = ["serde", "std"] } ed25519-dalek = { version = "1.0.1", features = ["serde", "std"], default-features = false }
rand = "0.8.5"
thiserror = "1.0.23" thiserror = "1.0.23"
lazy_static = "1.4" lazy_static = "1.4"
sha2 = "0.10.6" sha2 = "0.10.6"
@ -20,9 +19,15 @@ serde_bytes = "0.11"
eyre = "0.6.5" eyre = "0.6.5"
libp2p-identity = { workspace = true, default-features = false, features = ["peerid", "ed25519"] } libp2p-identity = { workspace = true, default-features = false, features = ["peerid", "ed25519"] }
multihash = { version = "0.18.0", features = ["identity"] } multihash = { version = "0.18.0", features = ["identity"] }
# rand fails to compile for restricted environments like NEAR contracts
rand = { version = "0.8.5", optional = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] [target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ring = { version = "0.16.9", features = ["alloc", "std"], default-features = false } ring = { version = "0.16.9", features = ["alloc", "std"], default-features = false }
[features]
default = ["rand"]
rand = ["dep:rand", "ed25519-dalek/rand_core"]
[dev-dependencies] [dev-dependencies]
quickcheck = "1.0.3" quickcheck = "1.0.3"

View File

@ -22,6 +22,7 @@
use crate::error::{DecodingError, SigningError, VerificationError}; use crate::error::{DecodingError, SigningError, VerificationError};
use core::fmt; use core::fmt;
use ed25519_dalek::{self as ed25519, Signer as _, Verifier as _}; use ed25519_dalek::{self as ed25519, Signer as _, Verifier as _};
#[cfg(feature = "rand")]
use rand::RngCore; use rand::RngCore;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::convert::TryFrom; use std::convert::TryFrom;
@ -32,6 +33,7 @@ pub struct Keypair(ed25519::Keypair);
impl Keypair { impl Keypair {
/// Generate a new Ed25519 keypair. /// Generate a new Ed25519 keypair.
#[cfg(feature = "rand")]
pub fn generate() -> Self { pub fn generate() -> Self {
Keypair::from(SecretKey::generate()) Keypair::from(SecretKey::generate())
} }
@ -174,6 +176,7 @@ impl fmt::Debug for SecretKey {
impl SecretKey { impl SecretKey {
/// Generate a new Ed25519 secret key. /// Generate a new Ed25519 secret key.
#[cfg(feature = "rand")]
pub fn generate() -> Self { pub fn generate() -> Self {
let mut bytes = [0u8; 32]; let mut bytes = [0u8; 32];
rand::thread_rng().fill_bytes(&mut bytes); rand::thread_rng().fill_bytes(&mut bytes);
@ -198,7 +201,7 @@ impl SecretKey {
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct Signature(pub Vec<u8>); pub struct Signature(pub Vec<u8>);
#[cfg(test)] #[cfg(all(test, feature = "rand"))]
mod tests { mod tests {
use super::*; use super::*;
use crate::KeyPair; use crate::KeyPair;

View File

@ -95,6 +95,7 @@ pub enum KeyPair {
} }
impl KeyPair { impl KeyPair {
#[cfg(feature = "rand")]
pub fn generate(format: KeyFormat) -> KeyPair { pub fn generate(format: KeyFormat) -> KeyPair {
match format { match format {
KeyFormat::Ed25519 => KeyPair::generate_ed25519(), KeyFormat::Ed25519 => KeyPair::generate_ed25519(),
@ -102,6 +103,7 @@ impl KeyPair {
} }
/// Generate a new Ed25519 keypair. /// Generate a new Ed25519 keypair.
#[cfg(feature = "rand")]
pub fn generate_ed25519() -> KeyPair { pub fn generate_ed25519() -> KeyPair {
KeyPair::Ed25519(ed25519::Keypair::generate()) KeyPair::Ed25519(ed25519::Keypair::generate())
} }

View File

@ -149,7 +149,7 @@ fn as_public_key(peer_id: &PeerId) -> Option<libp2p_identity::PublicKey> {
} }
} }
#[cfg(test)] #[cfg(all(test, feature = "rand"))]
mod tests { mod tests {
use super::*; use super::*;
use crate::KeyPair; use crate::KeyPair;