Skip to content

Commit ff4880a

Browse files
committed
hkd32: Run rustfmt on imported bip39 code
Applies rustfmt with no other changes, using the following version: rustfmt 1.2.2-stable (5274b49c 2019-04-24)
1 parent 17189ae commit ff4880a

File tree

12 files changed

+144
-92
lines changed

12 files changed

+144
-92
lines changed

hkd32/LICENSE-MIT

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
The MIT License (MIT)
2+
23
Copyright (c) 2017-2018 Stephen Oliver
4+
Copyright (c) 2019 iqlusion
35

46
Permission is hereby granted, free of charge, to any person obtaining a copy of
57
this software and associated documentation files (the "Software"), to deal in

hkd32/src/mnemonic/crypto.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
//!
77
88
extern crate rand;
9-
use self::rand::{ thread_rng, RngCore };
10-
use sha2::Digest;
9+
use self::rand::{thread_rng, RngCore};
1110
use hmac::Hmac;
11+
use sha2::Digest;
1212

1313
const PBKDF2_ROUNDS: usize = 2048;
1414
const PBKDF2_BYTES: usize = 64;

hkd32/src/mnemonic/error.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ use mnemonic_type::MnemonicType;
22

33
#[derive(Debug, Fail)]
44
pub enum ErrorKind {
5-
#[fail(display = "invalid checksum")]
6-
InvalidChecksum,
7-
#[fail(display = "invalid word in phrase")]
8-
InvalidWord,
9-
#[fail(display = "invalid keysize: {}", _0)]
10-
InvalidKeysize(usize),
11-
#[fail(display = "invalid number of words in phrase: {}", _0)]
12-
InvalidWordLength(usize),
13-
#[fail(display = "invalid entropy length {}bits for mnemonic type {:?}", _0, _1)]
14-
InvalidEntropyLength(usize, MnemonicType),
15-
}
5+
#[fail(display = "invalid checksum")]
6+
InvalidChecksum,
7+
#[fail(display = "invalid word in phrase")]
8+
InvalidWord,
9+
#[fail(display = "invalid keysize: {}", _0)]
10+
InvalidKeysize(usize),
11+
#[fail(display = "invalid number of words in phrase: {}", _0)]
12+
InvalidWordLength(usize),
13+
#[fail(
14+
display = "invalid entropy length {}bits for mnemonic type {:?}",
15+
_0, _1
16+
)]
17+
InvalidEntropyLength(usize, MnemonicType),
18+
}

hkd32/src/mnemonic/language.rs

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
use hashbrown::HashMap;
21
use error::ErrorKind;
32
use failure::Error;
4-
use util::{Bits11, Bits};
3+
use hashbrown::HashMap;
4+
use util::{Bits, Bits11};
55

66
pub struct WordMap {
7-
inner: HashMap<&'static str, Bits11>
8-
7+
inner: HashMap<&'static str, Bits11>,
98
}
109

1110
pub struct WordList {
12-
inner: Vec<&'static str>
11+
inner: Vec<&'static str>,
1312
}
1413

1514
impl WordMap {
1615
pub fn get_bits(&self, word: &str) -> Result<Bits11, Error> {
1716
match self.inner.get(word) {
1817
Some(n) => Ok(*n),
19-
None => Err(ErrorKind::InvalidWord)?
18+
None => Err(ErrorKind::InvalidWord)?,
2019
}
2120
}
2221
}
@@ -37,55 +36,62 @@ mod lazy {
3736

3837
debug_assert!(inner.len() == 2048, "Invalid wordlist length");
3938

40-
WordList {
41-
inner
42-
}
39+
WordList { inner }
4340
}
4441

4542
/// lazy generation of the word map
4643
fn gen_wordmap(wordlist: &WordList) -> WordMap {
47-
let inner = wordlist.inner
48-
.iter()
49-
.enumerate()
50-
.map(|(i, item)| (*item, Bits11::from(i as u16)))
51-
.collect();
52-
53-
WordMap {
54-
inner
55-
}
44+
let inner = wordlist
45+
.inner
46+
.iter()
47+
.enumerate()
48+
.map(|(i, item)| (*item, Bits11::from(i as u16)))
49+
.collect();
50+
51+
WordMap { inner }
5652
}
5753

58-
pub static WORDLIST_ENGLISH: Lazy<WordList> = sync_lazy!{ gen_wordlist(include_str!("langs/english.txt")) };
54+
pub static WORDLIST_ENGLISH: Lazy<WordList> =
55+
sync_lazy! { gen_wordlist(include_str!("langs/english.txt")) };
5956
#[cfg(feature = "chinese-simplified")]
60-
pub static WORDLIST_CHINESE_SIMPLIFIED: Lazy<WordList> = sync_lazy!{ gen_wordlist(include_str!("langs/chinese_simplified.txt")) };
57+
pub static WORDLIST_CHINESE_SIMPLIFIED: Lazy<WordList> =
58+
sync_lazy! { gen_wordlist(include_str!("langs/chinese_simplified.txt")) };
6159
#[cfg(feature = "chinese-traditional")]
62-
pub static WORDLIST_CHINESE_TRADITIONAL: Lazy<WordList> = sync_lazy!{ gen_wordlist(include_str!("langs/chinese_traditional.txt")) };
60+
pub static WORDLIST_CHINESE_TRADITIONAL: Lazy<WordList> =
61+
sync_lazy! { gen_wordlist(include_str!("langs/chinese_traditional.txt")) };
6362
#[cfg(feature = "french")]
64-
pub static WORDLIST_FRENCH: Lazy<WordList> = sync_lazy!{ gen_wordlist(include_str!("langs/french.txt")) };
63+
pub static WORDLIST_FRENCH: Lazy<WordList> =
64+
sync_lazy! { gen_wordlist(include_str!("langs/french.txt")) };
6565
#[cfg(feature = "italian")]
66-
pub static WORDLIST_ITALIAN: Lazy<WordList> = sync_lazy!{ gen_wordlist(include_str!("langs/italian.txt")) };
66+
pub static WORDLIST_ITALIAN: Lazy<WordList> =
67+
sync_lazy! { gen_wordlist(include_str!("langs/italian.txt")) };
6768
#[cfg(feature = "japanese")]
68-
pub static WORDLIST_JAPANESE: Lazy<WordList> = sync_lazy!{ gen_wordlist(include_str!("langs/japanese.txt")) };
69+
pub static WORDLIST_JAPANESE: Lazy<WordList> =
70+
sync_lazy! { gen_wordlist(include_str!("langs/japanese.txt")) };
6971
#[cfg(feature = "korean")]
70-
pub static WORDLIST_KOREAN: Lazy<WordList> = sync_lazy!{ gen_wordlist(include_str!("langs/korean.txt")) };
72+
pub static WORDLIST_KOREAN: Lazy<WordList> =
73+
sync_lazy! { gen_wordlist(include_str!("langs/korean.txt")) };
7174
#[cfg(feature = "spanish")]
72-
pub static WORDLIST_SPANISH: Lazy<WordList> = sync_lazy!{ gen_wordlist(include_str!("langs/spanish.txt")) };
75+
pub static WORDLIST_SPANISH: Lazy<WordList> =
76+
sync_lazy! { gen_wordlist(include_str!("langs/spanish.txt")) };
7377

74-
pub static WORDMAP_ENGLISH: Lazy<WordMap> = sync_lazy!{ gen_wordmap(&WORDLIST_ENGLISH) };
78+
pub static WORDMAP_ENGLISH: Lazy<WordMap> = sync_lazy! { gen_wordmap(&WORDLIST_ENGLISH) };
7579
#[cfg(feature = "chinese-simplified")]
76-
pub static WORDMAP_CHINESE_SIMPLIFIED: Lazy<WordMap> = sync_lazy!{ gen_wordmap(&WORDLIST_CHINESE_SIMPLIFIED) };
80+
pub static WORDMAP_CHINESE_SIMPLIFIED: Lazy<WordMap> =
81+
sync_lazy! { gen_wordmap(&WORDLIST_CHINESE_SIMPLIFIED) };
7782
#[cfg(feature = "chinese-traditional")]
78-
pub static WORDMAP_CHINESE_TRADITIONAL: Lazy<WordMap> = sync_lazy!{ gen_wordmap(&WORDLIST_CHINESE_TRADITIONAL) };
83+
pub static WORDMAP_CHINESE_TRADITIONAL: Lazy<WordMap> =
84+
sync_lazy! { gen_wordmap(&WORDLIST_CHINESE_TRADITIONAL) };
7985
#[cfg(feature = "french")]
80-
pub static WORDMAP_FRENCH: Lazy<WordMap> = sync_lazy!{ gen_wordmap(&WORDLIST_FRENCH) };
86+
pub static WORDMAP_FRENCH: Lazy<WordMap> = sync_lazy! { gen_wordmap(&WORDLIST_FRENCH) };
8187
#[cfg(feature = "italian")]
82-
pub static WORDMAP_ITALIAN: Lazy<WordMap> = sync_lazy!{ gen_wordmap(&WORDLIST_ITALIAN) };
88+
pub static WORDMAP_ITALIAN: Lazy<WordMap> = sync_lazy! { gen_wordmap(&WORDLIST_ITALIAN) };
8389
#[cfg(feature = "japanese")]
84-
pub static WORDMAP_JAPANESE: Lazy<WordMap> = sync_lazy!{ gen_wordmap(&WORDLIST_JAPANESE) };
90+
pub static WORDMAP_JAPANESE: Lazy<WordMap> = sync_lazy! { gen_wordmap(&WORDLIST_JAPANESE) };
8591
#[cfg(feature = "korean")]
86-
pub static WORDMAP_KOREAN: Lazy<WordMap> = sync_lazy!{ gen_wordmap(&WORDLIST_KOREAN) };
92+
pub static WORDMAP_KOREAN: Lazy<WordMap> = sync_lazy! { gen_wordmap(&WORDLIST_KOREAN) };
8793
#[cfg(feature = "spanish")]
88-
pub static WORDMAP_SPANISH: Lazy<WordMap> = sync_lazy!{ gen_wordmap(&WORDLIST_SPANISH) };
94+
pub static WORDMAP_SPANISH: Lazy<WordMap> = sync_lazy! { gen_wordmap(&WORDLIST_SPANISH) };
8995

9096
}
9197

hkd32/src/mnemonic/lib.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,26 @@
2727
//! println!("{:X}", seed);
2828
//! ```
2929
//!
30-
#[macro_use] extern crate failure;
31-
#[macro_use] extern crate once_cell;
32-
extern crate pbkdf2;
30+
#[macro_use]
31+
extern crate failure;
32+
#[macro_use]
33+
extern crate once_cell;
3334
extern crate hashbrown;
34-
extern crate sha2;
3535
extern crate hmac;
36+
extern crate pbkdf2;
37+
extern crate sha2;
3638

37-
mod mnemonic;
3839
mod error;
39-
mod mnemonic_type;
4040
mod language;
41-
mod util;
41+
mod mnemonic;
42+
mod mnemonic_type;
4243
mod seed;
44+
mod util;
4345

4446
mod crypto;
4547

48+
pub use error::ErrorKind;
4649
pub use language::Language;
4750
pub use mnemonic::Mnemonic;
4851
pub use mnemonic_type::MnemonicType;
4952
pub use seed::Seed;
50-
pub use error::ErrorKind;

hkd32/src/mnemonic/mnemonic.rs

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use util::{checksum, IterExt, BitWriter};
21
use crypto::{gen_random_bytes, sha256_first_byte};
32
use error::ErrorKind;
43
use failure::Error;
5-
use mnemonic_type::MnemonicType;
64
use language::Language;
5+
use mnemonic_type::MnemonicType;
76
use std::fmt;
7+
use util::{checksum, BitWriter, IterExt};
88

99
/// The primary type in this crate, most tasks require creating or using one.
1010
///
@@ -86,7 +86,7 @@ impl Mnemonic {
8686

8787
fn from_entropy_unchecked<E>(entropy: E, lang: Language) -> Mnemonic
8888
where
89-
E: Into<Vec<u8>>
89+
E: Into<Vec<u8>>,
9090
{
9191
let entropy = entropy.into();
9292
let wordlist = lang.wordlist();
@@ -102,16 +102,17 @@ impl Mnemonic {
102102
//
103103
// Given the entropy is of correct size, this ought to give us the correct word
104104
// count.
105-
let phrase = entropy.iter()
106-
.chain(Some(&checksum_byte))
107-
.bits()
108-
.map(|bits| wordlist.get_word(bits))
109-
.join(" ");
105+
let phrase = entropy
106+
.iter()
107+
.chain(Some(&checksum_byte))
108+
.bits()
109+
.map(|bits| wordlist.get_word(bits))
110+
.join(" ");
110111

111112
Mnemonic {
112113
phrase,
113114
lang,
114-
entropy
115+
entropy,
115116
}
116117
}
117118

@@ -189,7 +190,10 @@ impl Mnemonic {
189190

190191
let mtype = MnemonicType::for_word_count(bits.len() / 11)?;
191192

192-
debug_assert!(bits.len() == mtype.total_bits(), "Insufficient amount of bits to validate");
193+
debug_assert!(
194+
bits.len() == mtype.total_bits(),
195+
"Insufficient amount of bits to validate"
196+
);
193197

194198
let mut entropy = bits.into_bytes();
195199
let entropy_bytes = mtype.entropy_bits() / 8;
@@ -319,7 +323,10 @@ mod test {
319323

320324
#[test]
321325
fn mnemonic_from_entropy() {
322-
let entropy = &[0x33, 0xE4, 0x6B, 0xB1, 0x3A, 0x74, 0x6E, 0xA4, 0x1C, 0xDD, 0xE4, 0x5C, 0x90, 0x84, 0x6A, 0x79];
326+
let entropy = &[
327+
0x33, 0xE4, 0x6B, 0xB1, 0x3A, 0x74, 0x6E, 0xA4, 0x1C, 0xDD, 0xE4, 0x5C, 0x90, 0x84,
328+
0x6A, 0x79,
329+
];
323330
let phrase = "crop cash unable insane eight faith inflict route frame loud box vibrant";
324331

325332
let mnemonic = Mnemonic::from_entropy(entropy, Language::English).unwrap();
@@ -329,7 +336,10 @@ mod test {
329336

330337
#[test]
331338
fn mnemonic_from_phrase() {
332-
let entropy = &[0x33, 0xE4, 0x6B, 0xB1, 0x3A, 0x74, 0x6E, 0xA4, 0x1C, 0xDD, 0xE4, 0x5C, 0x90, 0x84, 0x6A, 0x79];
339+
let entropy = &[
340+
0x33, 0xE4, 0x6B, 0xB1, 0x3A, 0x74, 0x6E, 0xA4, 0x1C, 0xDD, 0xE4, 0x5C, 0x90, 0x84,
341+
0x6A, 0x79,
342+
];
333343
let phrase = "crop cash unable insane eight faith inflict route frame loud box vibrant";
334344

335345
let mnemonic = Mnemonic::from_phrase(phrase, Language::English).unwrap();
@@ -346,13 +356,28 @@ mod test {
346356

347357
#[test]
348358
fn mnemonic_hex_format() {
349-
let entropy = &[0x03, 0xE4, 0x6B, 0xB1, 0x3A, 0x74, 0x6E, 0xA4, 0x1C, 0xDD, 0xE4, 0x5C, 0x90, 0x84, 0x6A, 0x79];
359+
let entropy = &[
360+
0x03, 0xE4, 0x6B, 0xB1, 0x3A, 0x74, 0x6E, 0xA4, 0x1C, 0xDD, 0xE4, 0x5C, 0x90, 0x84,
361+
0x6A, 0x79,
362+
];
350363

351364
let mnemonic = Mnemonic::from_entropy(entropy, Language::English).unwrap();
352365

353-
assert_eq!(format!("{:x}", mnemonic), "03e46bb13a746ea41cdde45c90846a79");
354-
assert_eq!(format!("{:X}", mnemonic), "03E46BB13A746EA41CDDE45C90846A79");
355-
assert_eq!(format!("{:#x}", mnemonic), "0x03e46bb13a746ea41cdde45c90846a79");
356-
assert_eq!(format!("{:#X}", mnemonic), "0x03E46BB13A746EA41CDDE45C90846A79");
366+
assert_eq!(
367+
format!("{:x}", mnemonic),
368+
"03e46bb13a746ea41cdde45c90846a79"
369+
);
370+
assert_eq!(
371+
format!("{:X}", mnemonic),
372+
"03E46BB13A746EA41CDDE45C90846A79"
373+
);
374+
assert_eq!(
375+
format!("{:#x}", mnemonic),
376+
"0x03e46bb13a746ea41cdde45c90846a79"
377+
);
378+
assert_eq!(
379+
format!("{:#X}", mnemonic),
380+
"0x03E46BB13A746EA41CDDE45C90846A79"
381+
);
357382
}
358383
}

hkd32/src/mnemonic/mnemonic_type.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl MnemonicType {
5858
18 => MnemonicType::Words18,
5959
21 => MnemonicType::Words21,
6060
24 => MnemonicType::Words24,
61-
_ => Err(ErrorKind::InvalidWordLength(size))?
61+
_ => Err(ErrorKind::InvalidWordLength(size))?,
6262
};
6363

6464
Ok(mnemonic_type)
@@ -82,7 +82,7 @@ impl MnemonicType {
8282
192 => MnemonicType::Words18,
8383
224 => MnemonicType::Words21,
8484
256 => MnemonicType::Words24,
85-
_ => Err(ErrorKind::InvalidKeysize(size))?
85+
_ => Err(ErrorKind::InvalidKeysize(size))?,
8686
};
8787

8888
Ok(mnemonic_type)
@@ -190,7 +190,12 @@ impl Default for MnemonicType {
190190

191191
impl fmt::Display for MnemonicType {
192192
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
193-
write!(f, "{} words ({}bits)", self.word_count(), self.entropy_bits())
193+
write!(
194+
f,
195+
"{} words ({}bits)",
196+
self.word_count(),
197+
self.entropy_bits()
198+
)
194199
}
195200
}
196201

hkd32/src/mnemonic/seed.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ impl Seed {
2929
let salt = format!("mnemonic{}", password);
3030
let bytes = pbkdf2(mnemonic.phrase().as_bytes(), &salt);
3131

32-
Self {
33-
bytes,
34-
}
32+
Self { bytes }
3533
}
3634

3735
/// Get the seed value as a byte slice
@@ -87,7 +85,10 @@ mod test {
8785

8886
#[test]
8987
fn seed_hex_format() {
90-
let entropy = &[0x33, 0xE4, 0x6B, 0xB1, 0x3A, 0x74, 0x6E, 0xA4, 0x1C, 0xDD, 0xE4, 0x5C, 0x90, 0x84, 0x6A, 0x79];
88+
let entropy = &[
89+
0x33, 0xE4, 0x6B, 0xB1, 0x3A, 0x74, 0x6E, 0xA4, 0x1C, 0xDD, 0xE4, 0x5C, 0x90, 0x84,
90+
0x6A, 0x79,
91+
];
9192

9293
let mnemonic = Mnemonic::from_entropy(entropy, Language::English).unwrap();
9394
let seed = Seed::new(&mnemonic, "password");

0 commit comments

Comments
 (0)