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",
"ed25519",
"rand 0.7.3",
"rand_core 0.5.1",
"serde",
"serde_bytes",
"sha2 0.9.9",

View File

@ -10,8 +10,7 @@ repository = "https://github.com/fluencelabs/trust-graph"
[dependencies]
serde = { version = "1.0.118", features = ["derive"] }
bs58 = "0.5.0"
ed25519-dalek = { version = "1.0.1", features = ["serde", "std"] }
rand = "0.8.5"
ed25519-dalek = { version = "1.0.1", features = ["serde", "std"], default-features = false }
thiserror = "1.0.23"
lazy_static = "1.4"
sha2 = "0.10.6"
@ -20,9 +19,15 @@ serde_bytes = "0.11"
eyre = "0.6.5"
libp2p-identity = { workspace = true, default-features = false, features = ["peerid", "ed25519"] }
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]
ring = { version = "0.16.9", features = ["alloc", "std"], default-features = false }
[features]
default = ["rand"]
rand = ["dep:rand", "ed25519-dalek/rand_core"]
[dev-dependencies]
quickcheck = "1.0.3"

View File

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

View File

@ -95,6 +95,7 @@ pub enum KeyPair {
}
impl KeyPair {
#[cfg(feature = "rand")]
pub fn generate(format: KeyFormat) -> KeyPair {
match format {
KeyFormat::Ed25519 => KeyPair::generate_ed25519(),
@ -102,6 +103,7 @@ impl KeyPair {
}
/// Generate a new Ed25519 keypair.
#[cfg(feature = "rand")]
pub fn generate_ed25519() -> KeyPair {
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 {
use super::*;
use crate::KeyPair;