Skip to content

Commit

Permalink
Merge pull request #280 from iqlusioninc/hkd32/bip39-cargo-feature
Browse files Browse the repository at this point in the history
hkd32: Split out `bip39` cargo feature
  • Loading branch information
tony-iqlusion authored Oct 13, 2019
2 parents 34607ba + 1b0c371 commit 2e28215
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 8 deletions.
3 changes: 2 additions & 1 deletion hkd32/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ path = "../zeroize"
default = ["alloc", "bech32", "getrandom"]
alloc = ["zeroize/alloc"]
bech32 = ["alloc", "subtle-encoding/bech32-preview"]
mnemonic = ["alloc", "getrandom", "lazy_static", "pbkdf2"]
mnemonic = ["alloc", "getrandom", "lazy_static"]
bip39 = ["mnemonic", "pbkdf2"]

[package.metadata.docs.rs]
all-features = true
2 changes: 1 addition & 1 deletion hkd32/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#![doc(html_root_url = "https://docs.rs/hkd32/0.2.0")]

#[cfg(feature = "alloc")]
#[cfg_attr(any(feature = "mnemonic", test), macro_use)]
#[cfg_attr(any(feature = "bip39", test), macro_use)]
extern crate alloc;

mod key_material;
Expand Down
5 changes: 4 additions & 1 deletion hkd32/src/mnemonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
mod bits;
mod language;
mod phrase;
#[cfg(feature = "bip39")]
mod seed;

pub use self::{language::Language, phrase::Phrase, seed::Seed};
#[cfg(feature = "bip39")]
pub use self::seed::Seed;
pub use self::{language::Language, phrase::Phrase};
14 changes: 11 additions & 3 deletions hkd32/src/mnemonic/phrase.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
//! BIP39 mnemonic phrases
#[cfg(feature = "bip39")]
use super::seed::{Seed, SEED_SIZE};
use super::{
bits::{BitWriter, IterExt},
language::Language,
seed::{Seed, SEED_SIZE},
};
use crate::{Error, KeyMaterial, Path, KEY_SIZE};
use alloc::string::String;
use core::convert::TryInto;
#[cfg(feature = "bip39")]
use hmac::Hmac;
use sha2::{Digest, Sha256, Sha512};
#[cfg(feature = "bip39")]
use sha2::Sha512;
use sha2::{Digest, Sha256};
use zeroize::{Zeroize, Zeroizing};

/// Number of PBKDF2 rounds to perform when deriving the seed
#[cfg(feature = "bip39")]
const PBKDF2_ROUNDS: usize = 2048;

/// Source entropy for a BIP39 mnemonic phrase
Expand Down Expand Up @@ -135,7 +140,10 @@ impl Phrase {
KeyMaterial::from(self).derive_subkey(path)
}

/// Convert this mnemonic phrase into the BIP39 seed value
/// Convert this mnemonic phrase into the BIP39 seed value.
///
/// This is only available when the `bip39` Cargo feature is enabled.
#[cfg(feature = "bip39")]
pub fn to_seed(&self, password: &str) -> Seed {
let salt = Zeroizing::new(format!("mnemonic{}", password));
let mut seed = [0u8; SEED_SIZE];
Expand Down
2 changes: 1 addition & 1 deletion hkd32/src/mnemonic/seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const BIP39_BASE_DERIVATION_KEY: [u8; 12] = [
/// Number of bytes of PBKDF2 output to extract
pub const SEED_SIZE: usize = 64;

/// BIP39 seeds
/// BIP39 seeds. Requires the `bip39` cargo feature is enabled.
pub struct Seed(pub(crate) [u8; SEED_SIZE]);

impl Seed {
Expand Down
2 changes: 1 addition & 1 deletion hkd32/tests/bip39_vectors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! BIP39 test vectors
#[cfg(feature = "mnemonic")]
#[cfg(feature = "bip39")]
mod mnemonic {
use core::convert::TryInto;
use hkd32::mnemonic;
Expand Down

0 comments on commit 2e28215

Please sign in to comment.