aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2020-11-28 10:48:42 +0100
committerNicholas Rodrigues Lordello <[email protected]>2020-11-28 10:48:42 +0100
commitbd7a6880f5dce74fd154f593a5a5f7034affeee4 (patch)
tree09fc799b239632a5ebe854d62cb2c7c3574d55ac
parentcd7beb199e654825cb1b3ab6ac85b805ffdb7871 (diff)
started implementing mnemonic word lists
-rw-r--r--Cargo.lock374
-rw-r--r--Cargo.toml11
-rw-r--r--src/cmd.rs1
-rw-r--r--src/cmd/new.rs11
-rw-r--r--src/main.rs19
-rw-r--r--src/mnemonic.rs6
-rw-r--r--src/mnemonic/language.rs60
-rw-r--r--src/mnemonic/wordlist.rs67
8 files changed, 545 insertions, 4 deletions
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..b85ddf3
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,374 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "ansi_term"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
+dependencies = [
+ "winapi",
+]
+
+[[package]]
+name = "anyhow"
+version = "1.0.34"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bf8dcb5b4bbaa28653b647d8c77bd4ed40183b48882e130c1f1ffb73de069fd7"
+
+[[package]]
+name = "atty"
+version = "0.2.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
+dependencies = [
+ "hermit-abi",
+ "libc",
+ "winapi",
+]
+
+[[package]]
+name = "bitflags"
+version = "1.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
+
+[[package]]
+name = "block-buffer"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4"
+dependencies = [
+ "block-padding",
+ "generic-array",
+]
+
+[[package]]
+name = "block-padding"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae"
+
+[[package]]
+name = "cc"
+version = "1.0.65"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "95752358c8f7552394baf48cd82695b345628ad3f170d607de3ca03b8dacca15"
+
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "clap"
+version = "2.33.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
+dependencies = [
+ "ansi_term",
+ "atty",
+ "bitflags",
+ "strsim",
+ "textwrap",
+ "unicode-width",
+ "vec_map",
+]
+
+[[package]]
+name = "cpuid-bool"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634"
+
+[[package]]
+name = "crypto-mac"
+version = "0.10.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4857fd85a0c34b3c3297875b747c1e02e06b6a0ea32dd892d8192b9ce0813ea6"
+dependencies = [
+ "generic-array",
+ "subtle",
+]
+
+[[package]]
+name = "digest"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066"
+dependencies = [
+ "generic-array",
+]
+
+[[package]]
+name = "generic-array"
+version = "0.14.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817"
+dependencies = [
+ "typenum",
+ "version_check",
+]
+
+[[package]]
+name = "hdwallet"
+version = "0.1.0"
+dependencies = [
+ "anyhow",
+ "hmac",
+ "secp256k1",
+ "sha2",
+ "sha3",
+ "structopt",
+]
+
+[[package]]
+name = "heck"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205"
+dependencies = [
+ "unicode-segmentation",
+]
+
+[[package]]
+name = "hermit-abi"
+version = "0.1.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5aca5565f760fb5b220e499d72710ed156fdb74e631659e99377d9ebfbd13ae8"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "hmac"
+version = "0.10.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c1441c6b1e930e2817404b5046f1f989899143a12bf92de603b69f4e0aee1e15"
+dependencies = [
+ "crypto-mac",
+ "digest",
+]
+
+[[package]]
+name = "keccak"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "67c21572b4949434e4fc1e1978b99c5f77064153c59d998bf13ecd96fb5ecba7"
+
+[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
+[[package]]
+name = "libc"
+version = "0.2.80"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614"
+
+[[package]]
+name = "opaque-debug"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
+
+[[package]]
+name = "proc-macro-error"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
+dependencies = [
+ "proc-macro-error-attr",
+ "proc-macro2",
+ "quote",
+ "syn",
+ "version_check",
+]
+
+[[package]]
+name = "proc-macro-error-attr"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "version_check",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.24"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
+dependencies = [
+ "unicode-xid",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "secp256k1"
+version = "0.19.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c6179428c22c73ac0fbb7b5579a56353ce78ba29759b3b8575183336ea74cdfb"
+dependencies = [
+ "secp256k1-sys",
+]
+
+[[package]]
+name = "secp256k1-sys"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "11553d210db090930f4432bea123b31f70bbf693ace14504ea2a35e796c28dd2"
+dependencies = [
+ "cc",
+]
+
+[[package]]
+name = "sha2"
+version = "0.9.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6e7aab86fe2149bad8c507606bdb3f4ef5e7b2380eb92350f56122cca72a42a8"
+dependencies = [
+ "block-buffer",
+ "cfg-if",
+ "cpuid-bool",
+ "digest",
+ "opaque-debug",
+]
+
+[[package]]
+name = "sha3"
+version = "0.9.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f81199417d4e5de3f04b1e871023acea7389672c4135918f05aa9cbf2f2fa809"
+dependencies = [
+ "block-buffer",
+ "digest",
+ "keccak",
+ "opaque-debug",
+]
+
+[[package]]
+name = "strsim"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
+
+[[package]]
+name = "structopt"
+version = "0.3.20"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "126d630294ec449fae0b16f964e35bf3c74f940da9dca17ee9b905f7b3112eb8"
+dependencies = [
+ "clap",
+ "lazy_static",
+ "structopt-derive",
+]
+
+[[package]]
+name = "structopt-derive"
+version = "0.4.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "65e51c492f9e23a220534971ff5afc14037289de430e3c83f9daf6a1b6ae91e8"
+dependencies = [
+ "heck",
+ "proc-macro-error",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "subtle"
+version = "2.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "343f3f510c2915908f155e94f17220b19ccfacf2a64a2a5d8004f2c3e311e7fd"
+
+[[package]]
+name = "syn"
+version = "1.0.52"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6c1e438504729046a5cfae47f97c30d6d083c7d91d94603efdae3477fc070d4c"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-xid",
+]
+
+[[package]]
+name = "textwrap"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
+dependencies = [
+ "unicode-width",
+]
+
+[[package]]
+name = "typenum"
+version = "1.12.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33"
+
+[[package]]
+name = "unicode-segmentation"
+version = "1.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796"
+
+[[package]]
+name = "unicode-width"
+version = "0.1.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
+
+[[package]]
+name = "unicode-xid"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
+
+[[package]]
+name = "vec_map"
+version = "0.8.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
+
+[[package]]
+name = "version_check"
+version = "0.9.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
+
+[[package]]
+name = "winapi"
+version = "0.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
+dependencies = [
+ "winapi-i686-pc-windows-gnu",
+ "winapi-x86_64-pc-windows-gnu",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
diff --git a/Cargo.toml b/Cargo.toml
index 946af78..a4e4d7b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,9 +1,14 @@
[package]
name = "hdwallet"
version = "0.1.0"
-authors = ["Nicholas Rodrigues Lordello <[email protected]>"]
+authors = ["Nicholas Rodrigues Lordello <[email protected]>"]
edition = "2018"
-
-# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+license = "GPL-3.0-or-later"
[dependencies]
+anyhow = "1"
+hmac = "0.10"
+secp256k1 = { version = "0.19", features = ["recovery"] }
+sha2 = "0.9"
+sha3 = "0.9"
+structopt = "0.3"
diff --git a/src/cmd.rs b/src/cmd.rs
new file mode 100644
index 0000000..9d52a2b
--- /dev/null
+++ b/src/cmd.rs
@@ -0,0 +1 @@
+pub mod new;
diff --git a/src/cmd/new.rs b/src/cmd/new.rs
new file mode 100644
index 0000000..5a2c6e4
--- /dev/null
+++ b/src/cmd/new.rs
@@ -0,0 +1,11 @@
+//! Module implementing the `new` command for generating a mnemonic for a new
+//! hierarchical deterministic wallet.
+
+use crate::mnemonic::Language;
+use structopt::StructOpt;
+
+#[derive(Debug, StructOpt)]
+#[structopt(about = "generate a new HD wallet mnemonic")]
+pub struct Options {
+ language: Language,
+}
diff --git a/src/main.rs b/src/main.rs
index e7a11a9..74b1bc5 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,20 @@
+mod cmd;
+mod mnemonic;
+
+use crate::cmd::*;
+use structopt::StructOpt;
+
+#[derive(Debug, StructOpt)]
+#[structopt(
+ name = "hdwallet",
+ about = "Hierarchical deterministic wallet for Ethereum",
+)]
+enum Options {
+ New(new::Options),
+}
+
fn main() {
- println!("Hello, world!");
+ match Options::from_args() {
+ Options::New(options) => new::run(options),
+ }
}
diff --git a/src/mnemonic.rs b/src/mnemonic.rs
new file mode 100644
index 0000000..61663b4
--- /dev/null
+++ b/src/mnemonic.rs
@@ -0,0 +1,6 @@
+//! BIP-0039 mnemonic phrase implementation.
+
+mod language;
+mod wordlist;
+
+pub use self::language::Language;
diff --git a/src/mnemonic/language.rs b/src/mnemonic/language.rs
new file mode 100644
index 0000000..87950d8
--- /dev/null
+++ b/src/mnemonic/language.rs
@@ -0,0 +1,60 @@
+//! Mnemonic language for selecting word lists.
+
+use anyhow::{bail, Result};
+use std::{
+ fmt::{self, Display, Formatter},
+ str::FromStr,
+};
+
+/// The mnemonic langage used to select the word list.
+#[derive(Clone, Copy, Debug)]
+pub enum Language {
+ English,
+ // TODO(nlordell): Support more languages. Note that this is not necessarily
+ // trivial as some have specific considerations (like 'ñ' being equivalent
+ // to 'n' in Spanish, and Japanese using '\u{3000}` for spaces).
+}
+
+impl Language {
+ /// Splits a mnemonic phrase into its words, returning the detected language
+ /// and a vector of **normalized** words.
+ pub fn split(phrase: impl AsRef<str>) -> Result<(Self, Vec<String>)> {
+ // TODO(nlordell): A lot to do here...
+ Ok((
+ Language::English,
+ phrase
+ .as_ref()
+ .trim()
+ .split_whitespace()
+ .filter(|word| !word.is_empty())
+ .map(|word| word.to_lowercase())
+ .collect(),
+ ))
+ }
+}
+
+impl Default for Language {
+ fn default() -> Self {
+ // TODO(nlordell): Read the default language from the system locale.
+ Language::English
+ }
+}
+
+impl Display for Language {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ f.write_str(match self {
+ Language::English => "english",
+ })
+ }
+}
+
+impl FromStr for Language {
+ type Err = anyhow::Error;
+
+ fn from_str(s: &str) -> Result<Self> {
+ Ok(match s.to_lowercase().as_str() {
+ "english" => Language::English,
+ _ => bail!("unsupported language '{}'", s),
+ })
+ }
+}
diff --git a/src/mnemonic/wordlist.rs b/src/mnemonic/wordlist.rs
new file mode 100644
index 0000000..b2e2383
--- /dev/null
+++ b/src/mnemonic/wordlist.rs
@@ -0,0 +1,67 @@
+//! Module containing the embedded BIP-0039 word lists.
+
+use std::borrow::Cow;
+
+/// A parsed word list.
+pub struct Wordlist<'a>(Vec<Cow<'a, str>>);
+
+/// The number of words in a list, as defined in BIP-0039.
+pub const WORD_COUNT: usize = 1024;
+
+impl<'a> Wordlist<'a> {
+ /// Parses a list of newline-separated words.
+ fn parse(words: &'a str) -> Wordlist<'a> {
+ let words = words
+ .trim()
+ .split('\n')
+ .map(|word| {
+ // TODO(nlordell): The words here need to be normalized for its
+ // language, like converting 'ñ' to 'n' in Spanish.
+ Cow::Borrowed(word.trim())
+ })
+ .collect::<Vec<_>>();
+
+ debug_assert_eq!(words.len(), WORD_COUNT);
+ debug_assert!(words
+ .iter()
+ .all(|word| word.chars().all(char::is_lowercase)));
+ debug_assert!(words.windows(2).all(|pair| pair[0] < pair[1]));
+
+ Wordlist(words)
+ }
+
+ /// Searches the word list for the specified word returning its numerical
+ /// value representing its index in the list. This method returns `None`
+ /// if the word does not belong to the list.
+ pub fn search(&self, word: impl AsRef<str>) -> Option<usize> {
+ // TODO(nlordell): It is possible to be generous here and fix common
+ // spelling mistakes as well as only consider the first letters of the
+ // word as long as it is unique.
+ self.0
+ .binary_search_by_key(&word.as_ref(), |word| &word)
+ .ok()
+ }
+
+ /// Returns the word for the specified index.
+ ///
+ /// # Panics
+ ///
+ /// This method panics if the index is out of range for the BIP-0039 word
+ /// list: it must be less than `WORD_COUNT` or `1024`.
+ pub fn word(&'a self, index: usize) -> &'a str {
+ assert!(index < WORD_COUNT, "invalid word index");
+ &self.0[index]
+ }
+}
+
+macro_rules! define {
+ ($(
+ $(#[$attr:meta])*
+ $vis:vis $lang:ident = $f:expr;
+ )*) => {$(
+ $(#[$attr])*
+ $vis fn $lang() -> &'static Wordlist;
+ )*};
+}
+
+define!();