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

Update the crate to edition 2018 #5

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions .travis.yml
Expand Up @@ -7,7 +7,7 @@ matrix:
- rust: beta
- rust: nightly
env: BENCHES=true
- rust: 1.22.0
- rust: 1.36.0

before_install:
- sudo apt-get -qq update
Expand All @@ -19,5 +19,5 @@ script:
- cargo build --verbose --features rand,all-languages
- cargo test --verbose --features rand,all-languages
# benchmarks
- if ${BENCHES}; then cargo bench --verbose --features rand; fi
- if ${BENCHES}; then cargo bench --verbose --features rand,japanese; fi
- if [[ $TRAVIS_RUST_VERSION == "nightly" && ${BENCHES} ]]; then cargo bench --verbose --features rand; fi
- if [[ $TRAVIS_RUST_VERSION == "nightly" && ${BENCHES} ]]; then cargo bench --verbose --features rand,japanese; fi
9 changes: 5 additions & 4 deletions Cargo.toml
Expand Up @@ -2,6 +2,7 @@
name = "bip39"
version = "1.0.0-rc1"
authors = ["Steven Roose <steven@stevenroose.org>"]
edition = "2018"
license = "CC0-1.0"
homepage = "https://github.com/rust-bitcoin/rust-bip39/"
repository = "https://github.com/rust-bitcoin/rust-bip39/"
Expand Down Expand Up @@ -37,9 +38,9 @@ all-languages = [
]

[dependencies]
bitcoin_hashes = "0.7.6"
unicode-normalization = "=0.1.9"
rand = { version = "0.6.0", optional = true }
bitcoin_hashes = "0.9.3"
unicode-normalization = "0.1.9"
rand = { version = "0.7.0", optional = true }

[dev-dependencies]
rand = { version = "0.6.0", optional = false }
rand = "0.7.0"
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -5,5 +5,4 @@ A Rust implementation of BIP-39 mnemonic codes.

## MSRV

This crate supports Rust v1.22 and up.

This crate supports Rust v1.36 and up.
13 changes: 7 additions & 6 deletions benches/bench.rs
Expand Up @@ -32,8 +32,8 @@ const LANG: Language = Language::Spanish;

#[bench]
fn validate(b: &mut Bencher) {
let entropy = "7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f".as_bytes();
let mnemonic = Mnemonic::from_entropy_in(LANG, &entropy).unwrap();
let entropy = b"7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f";
let mnemonic = Mnemonic::from_entropy_in(LANG, &entropy[..]).unwrap();
assert_eq!(mnemonic.word_count(), 24);
let phrase = mnemonic.as_str();

Expand All @@ -44,13 +44,14 @@ fn validate(b: &mut Bencher) {

#[bench]
fn from_entropy(b: &mut Bencher) {
let entropy = "7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f".as_bytes();
let entropy = b"7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f";

b.iter(|| {
let _ = Mnemonic::from_entropy_in(LANG, &entropy).unwrap();
let _ = Mnemonic::from_entropy_in(LANG, &entropy[..]).unwrap();
});
}

#[cfg(feature = "rand")]
#[bench]
fn new_mnemonic(b: &mut Bencher) {
b.iter(|| {
Expand All @@ -60,8 +61,8 @@ fn new_mnemonic(b: &mut Bencher) {

#[bench]
fn to_seed(b: &mut Bencher) {
let entropy = "7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f".as_bytes();
let m = Mnemonic::from_entropy_in(LANG, &entropy).unwrap();
let entropy = b"7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f";
let m = Mnemonic::from_entropy_in(LANG, &entropy[..]).unwrap();

b.iter(|| {
let _ = m.to_seed("");
Expand Down
2 changes: 1 addition & 1 deletion src/language/mod.rs
Expand Up @@ -198,7 +198,7 @@ mod tests {
let mut digest = sha256::Hash::engine();
for (_idx, word) in lang.word_list().iter().enumerate() {
assert!(::unicode_normalization::is_nfkd(&word));
write!(&mut digest, "{}\n", word).unwrap();
writeln!(&mut digest, "{}", word).unwrap();
}
assert_eq!(&sha256::Hash::from_engine(digest).to_string(), sum,
"word list for language {} failed checksum check", lang,
Expand Down
32 changes: 7 additions & 25 deletions src/lib.rs
Expand Up @@ -26,11 +26,6 @@
#![deny(unused_imports)]
#![deny(missing_docs)]

extern crate bitcoin_hashes;
extern crate unicode_normalization;
#[cfg(feature = "rand")]
extern crate rand;

use std::{error, fmt, str};
use std::borrow::Cow;

Expand All @@ -40,15 +35,15 @@ use unicode_normalization::UnicodeNormalization;
mod language;
mod pbkdf2;

pub use language::Language;
pub use self::language::Language;

/// The ideagrapic space that should be used for Japanese lists.
#[cfg(feature = "japanese")]
#[allow(unused)]
const IDEOGRAPHIC_SPACE: char = ' ';

/// A BIP39 error.
#[derive(Clone, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Error {
/// Mnemonic has a word count that is not a multiple of 6.
BadWordCount(usize),
Expand Down Expand Up @@ -80,21 +75,8 @@ impl fmt::Display for Error {
}
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}

impl error::Error for Error {
fn cause(&self) -> Option<&error::Error> {
None
}

fn description(&self) -> &str {
"description() is deprecated; use Display"
}
}
impl error::Error for Error {}

/// A mnemonic code.
///
Expand Down Expand Up @@ -232,7 +214,7 @@ impl Mnemonic {
pub fn language_of(s: &str) -> Result<Language, Error> {
// First we try wordlists that have guaranteed unique words.
let first_word = s.split_whitespace().next().unwrap();
if first_word.len() == 0 {
if first_word.is_empty() {
return Err(Error::BadWordCount(0));
}
for language in Language::all().iter().filter(|l| l.unique_words()) {
Expand Down Expand Up @@ -566,19 +548,19 @@ mod tests {
fn test_invalid_entropy() {
//between 128 and 256 bits, but not divisible by 32
assert_eq!(
Mnemonic::from_entropy(&vec![b'x'; 17]),
Mnemonic::from_entropy(&[b'x'; 17]),
Err(Error::BadEntropyBitCount(136))
);

//less than 128 bits
assert_eq!(
Mnemonic::from_entropy(&vec![b'x'; 4]),
Mnemonic::from_entropy(&[b'x'; 4]),
Err(Error::BadEntropyBitCount(32))
);

//greater than 256 bits
assert_eq!(
Mnemonic::from_entropy(&vec![b'x'; 36]),
Mnemonic::from_entropy(&[b'x'; 36]),
Err(Error::BadEntropyBitCount(288))
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/pbkdf2.rs
Expand Up @@ -7,8 +7,8 @@ fn u32_to_array_be(val: u32) -> [u8; 4] {
debug_assert_eq!(::std::mem::size_of::<u32>(), 4); // size_of isn't a constfn in 1.22

let mut res = [0; 4];
for i in 0..4 {
res[i] = ((val >> (4 - i - 1)*8) & 0xff) as u8;
for (i, item) in res.iter_mut().enumerate() {
*item = ((val >> ((4 - i - 1)*8)) & 0xff) as u8;
}
res
}
Expand Down