Skip to content

Commit a3ac69f

Browse files
Merge pull request #251 from iqlusioninc/hkd32/bip39
hkd32: Merge `tiny-bip39`
2 parents 7304b2f + 31b0c93 commit a3ac69f

File tree

14 files changed

+2695
-328
lines changed

14 files changed

+2695
-328
lines changed

Cargo.lock

Lines changed: 2 additions & 200 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hkd32/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ homepage = "https://github.com/iqlusioninc/crates/"
1414
repository = "https://github.com/iqlusioninc/crates/tree/develop/hkd32"
1515
readme = "README.md"
1616
categories = ["cryptography", "no-std"]
17-
keywords = ["bip32", "derivation", "hd", "hmac", "key"]
17+
keywords = ["crypto", "bip32", "bip39", "derivation", "mnemonic"]
1818

1919
[badges]
2020
maintenance = { status = "passively-maintained" }
@@ -23,8 +23,9 @@ travis-ci = { repository = "iqlusioninc/crates", branch = "develop" }
2323
[dependencies]
2424
getrandom = { version = "0.1", optional = true }
2525
hmac = { version = "0.7", default-features = false }
26+
lazy_static = { version = "1", optional = true, default-features = false }
2627
sha2 = { version = "0.8", default-features = false }
27-
tiny-bip39 = { version = "0.6", default-features = false, optional = true }
28+
pbkdf2 = { version = "0.3", optional = true, default-features = false }
2829

2930
[dependencies.subtle-encoding]
3031
version = "0.4"
@@ -41,7 +42,7 @@ path = "../zeroize"
4142
default = ["alloc", "bech32", "getrandom"]
4243
alloc = ["zeroize/alloc"]
4344
bech32 = ["alloc", "subtle-encoding/bech32-preview"]
44-
mnemonic = ["alloc", "tiny-bip39"]
45+
mnemonic = ["alloc", "getrandom", "lazy_static", "pbkdf2"]
4546

4647
[package.metadata.docs.rs]
4748
all-features = true

hkd32/LICENSE-MIT

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017-2018 Stephen Oliver
4+
Copyright (c) 2019 iqlusion
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
this software and associated documentation files (the "Software"), to deal in
8+
the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

hkd32/README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Crate][crate-image]][crate-link]
55
[![Docs][docs-image]][docs-link]
66
[![Apache 2.0 Licensed][license-image]][license-link]
7-
![Rust 1.36+][rustc-image]
7+
![MSRV][rustc-image]
88
[![Build Status][build-image]][build-link]
99
[![Gitter Chat][gitter-image]][gitter-link]
1010

@@ -26,17 +26,14 @@ an initial 32-bytes of input key material.
2626

2727
Copyright © 2019 iqlusion
2828

29-
Licensed under the Apache License, Version 2.0 (the "License");
30-
you may not use this file except in compliance with the License.
31-
You may obtain a copy of the License at
29+
Includes code from the `bip39` crate. Copyright © 2017-2018 Stephen Oliver,
30+
with contributions by Maciej Hirsz.
3231

33-
https://www.apache.org/licenses/LICENSE-2.0
32+
**hkd32** is distributed under the terms of either the MIT license
33+
or the Apache License (Version 2.0), at your option.
3434

35-
Unless required by applicable law or agreed to in writing, software
36-
distributed under the License is distributed on an "AS IS" BASIS,
37-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38-
See the License for the specific language governing permissions and
39-
limitations under the License.
35+
See [LICENSE] (Apache License, Version 2.0) file in the `iqlusioninc/crates`
36+
toplevel directory of this repository or [LICENSE-MIT] for details.
4037

4138
## Contribution
4239

@@ -62,3 +59,5 @@ without any additional terms or conditions.
6259

6360
[bip32]: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
6461
[bip39]: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
62+
[LICENSE]: https://github.com/iqlusioninc/crates/blob/develop/LICENSE
63+
[LICENSE-MIT]: https://github.com/iqlusioninc/crates/blob/develop/hkd32/LICENSE-MIT

hkd32/src/key_material.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,6 @@ impl KeyMaterial {
127127
let b32 = Bech32::default().encode(hrp, self.as_bytes());
128128
Zeroizing::new(b32)
129129
}
130-
131-
/// Serialize this `KeyMaterial` as a BIP39 mnemonic phrase
132-
#[cfg(feature = "mnemonic")]
133-
pub fn to_mnemonic(&self, language: mnemonic::Language) -> mnemonic::Phrase {
134-
mnemonic::Phrase::from_key_material(self, language)
135-
}
136130
}
137131

138132
impl From<[u8; KEY_SIZE]> for KeyMaterial {

hkd32/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#![doc(html_root_url = "https://docs.rs/hkd32/0.1.2")]
4242

4343
#[cfg(feature = "alloc")]
44-
#[cfg_attr(test, macro_use)]
44+
#[cfg_attr(any(feature = "mnemonic", test), macro_use)]
4545
extern crate alloc;
4646

4747
mod key_material;

hkd32/src/mnemonic.rs

Lines changed: 9 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,13 @@
1-
//! Support for BIP39 mnemonics.
1+
//! Support for BIP39 mnemonic phrases.
22
//!
33
//! These enable deriving `hkd32::KeyMaterial` from a 24-word BIP39 phrase.
4+
//!
5+
//! Adapted from the `bip39` crate.
6+
//! Copyright © 2017-2018 Stephen Oliver with contributions by Maciej Hirsz.
47
5-
use crate::{Error, KeyMaterial};
6-
use alloc::string::String;
7-
use zeroize::Zeroize;
8-
9-
/// Number of words required for an HKD32-compatible mnemonic phrase
10-
pub const WORD_COUNT: usize = 24;
11-
12-
/// Supported languages.
13-
///
14-
/// Presently only English is specified by the BIP39 standard
15-
#[derive(Copy, Clone, Debug)]
16-
pub enum Language {
17-
/// English is presently the only supported language
18-
English,
19-
}
20-
21-
impl Default for Language {
22-
fn default() -> Language {
23-
Language::English
24-
}
25-
}
26-
27-
impl From<Language> for bip39::Language {
28-
fn from(language: Language) -> bip39::Language {
29-
match language {
30-
Language::English => bip39::Language::English,
31-
}
32-
}
33-
}
34-
35-
/// 24-word BIP39 Mnemonic phrase
36-
pub struct Phrase {
37-
/// String value containing the phrase
38-
string: String,
39-
40-
/// Associated language for this phrase
41-
language: Language,
42-
}
43-
44-
impl Phrase {
45-
/// Create a random BIP39 mnemonic phrase.
46-
pub fn random(language: Language) -> Self {
47-
Self::from_key_material(&KeyMaterial::random(), language)
48-
}
49-
50-
/// Create a new BIP39 mnemonic phrase from the given string.
51-
///
52-
/// Must be a valid 24-word BIP39 mnemonic.
53-
pub fn new<S>(phrase: S, language: Language) -> Result<Phrase, Error>
54-
where
55-
S: AsRef<str>,
56-
{
57-
let phrase = phrase.as_ref();
58-
59-
if bip39::Mnemonic::validate(phrase, language.into()).is_err() {
60-
return Err(Error);
61-
}
62-
63-
if phrase.split(' ').count() != WORD_COUNT {
64-
return Err(Error);
65-
}
66-
67-
Ok(Phrase {
68-
string: phrase.into(),
69-
language,
70-
})
71-
}
72-
73-
/// Create a new BIP39 mnemonic phrase from the given `KeyMaterial`
74-
pub(crate) fn from_key_material(key_material: &KeyMaterial, language: Language) -> Self {
75-
let mnemonic =
76-
bip39::Mnemonic::from_entropy(key_material.as_bytes(), language.into()).unwrap();
77-
78-
Phrase {
79-
string: mnemonic.into_phrase(),
80-
language,
81-
}
82-
}
83-
84-
/// Borrow this mnemonic phrase as a string.
85-
pub fn as_str(&self) -> &str {
86-
self.string.as_ref()
87-
}
88-
89-
/// Language this phrase's wordlist is for
90-
pub fn language(&self) -> Language {
91-
self.language
92-
}
93-
}
94-
95-
impl Drop for Phrase {
96-
fn drop(&mut self) {
97-
self.string.zeroize();
98-
}
99-
}
8+
mod bits;
9+
mod language;
10+
mod phrase;
11+
mod seed;
10012

101-
impl From<Phrase> for KeyMaterial {
102-
fn from(phrase: Phrase) -> KeyMaterial {
103-
let mnemonic =
104-
bip39::Mnemonic::from_phrase(&phrase.string, phrase.language.into()).unwrap();
105-
Self::from_bytes(mnemonic.entropy()).unwrap()
106-
}
107-
}
13+
pub use self::{language::Language, phrase::Phrase, seed::Seed};

0 commit comments

Comments
 (0)