Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make rand an optional dependency #42

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ italian = []
japanese = []
korean = []
spanish = []
default-langs = ["chinese-simplified", "chinese-traditional", "french", "italian", "japanese", "korean", "spanish"]

default = ["chinese-simplified", "chinese-traditional", "french", "italian", "japanese", "korean", "spanish"]
default = ["default-langs", "rand"]

[dependencies]
anyhow = "1.0.57"
Expand All @@ -37,7 +38,7 @@ rustc-hash = "1.1.0"
sha2 = "0.10.2"
hmac = "0.12.1"
pbkdf2 = { version = "0.11.0", default-features = false }
rand = "0.8.5"
rand = { version = "0.8.5", optional = true }
once_cell = "1.12.0"
unicode-normalization = "0.1.19"
zeroize = { version = "1.5.5", features = ["zeroize_derive"] }
Expand Down
5 changes: 3 additions & 2 deletions src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
//! [Seed]: ../seed/struct.Seed.html
//!

extern crate rand;
use self::rand::{thread_rng, RngCore};
use hmac::Hmac;
#[cfg(feature = "rand")]
use rand::{thread_rng, RngCore};
use sha2::Digest;

const PBKDF2_ROUNDS: u32 = 2048;
Expand All @@ -21,6 +21,7 @@ pub(crate) fn sha256_first_byte(input: &[u8]) -> u8 {

/// Random byte generator, used to create new mnemonics
///
#[cfg(feature = "rand")]
pub(crate) fn gen_random_bytes(byte_length: usize) -> Vec<u8> {
let mut rng = thread_rng();
let mut bytes = vec![0u8; byte_length];
Expand Down
39 changes: 23 additions & 16 deletions src/mnemonic.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use std::fmt;
use anyhow::Error;
use std::mem;
use unicode_normalization::UnicodeNormalization;
use zeroize::Zeroizing;
use crate::crypto::{gen_random_bytes, sha256_first_byte};
#[cfg(feature = "rand")]
use crate::crypto::gen_random_bytes;
use crate::crypto::sha256_first_byte;
use crate::error::ErrorKind;
use crate::language::Language;
use crate::mnemonic_type::MnemonicType;
use crate::util::{checksum, BitWriter, IterExt};
use anyhow::Error;
use std::fmt;
use std::mem;
use unicode_normalization::UnicodeNormalization;
use zeroize::Zeroizing;

/// The primary type in this crate, most tasks require creating or using one.
///
Expand Down Expand Up @@ -61,6 +63,7 @@ impl Mnemonic {
///
/// [Mnemonic]: ./mnemonic/struct.Mnemonic.html
/// [Mnemonic::phrase()]: ./mnemonic/struct.Mnemonic.html#method.phrase
#[cfg(feature = "rand")]
pub fn new(mtype: MnemonicType, lang: Language) -> Mnemonic {
let entropy = gen_random_bytes(mtype.entropy_bits() / 8);

Expand Down Expand Up @@ -107,12 +110,14 @@ impl Mnemonic {
//
// Given the entropy is of correct size, this ought to give us the correct word
// count.
let phrase = Zeroizing::new(entropy
.iter()
.chain(Some(&checksum_byte))
.bits()
.map(|bits| wordlist.get_word(bits))
.join(" "));
let phrase = Zeroizing::new(
entropy
.iter()
.chain(Some(&checksum_byte))
.bits()
.map(|bits| wordlist.get_word(bits))
.join(" "),
);

Mnemonic {
phrase,
Expand All @@ -139,10 +144,12 @@ impl Mnemonic {
///
/// [Mnemonic]: ../mnemonic/struct.Mnemonic.html
pub fn from_phrase(phrase: &str, lang: Language) -> Result<Mnemonic, Error> {
let phrase = Zeroizing::new(phrase
.split_whitespace()
.map(|w| w.nfkd())
.join::<String>(" "));
let phrase = Zeroizing::new(
phrase
.split_whitespace()
.map(|w| w.nfkd())
.join::<String>(" "),
);

// this also validates the checksum and phrase length before returning the entropy so we
// can store it. We don't use the validate function here to avoid having a public API that
Expand Down
Loading