diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2021-08-21 07:35:13 +0000 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2021-08-21 07:35:13 +0000 |
| commit | f608cce748d80d46f49ba25999e4334fd959b070 (patch) | |
| tree | 9fc73440b321933cfd6e6ead10ee18d93bc99c89 | |
| parent | c05487949a5cb9808945b5bdd174e14fdd7e51ea (diff) | |
Refactor into lib+bin and start adding e2e tests
| -rw-r--r-- | src/account.rs | 3 | ||||
| -rw-r--r-- | src/cmd.rs | 2 | ||||
| -rw-r--r-- | src/cmd/hash.rs | 3 | ||||
| -rw-r--r-- | src/cmd/new.rs | 2 | ||||
| -rw-r--r-- | src/cmd/sign.rs | 6 | ||||
| -rw-r--r-- | src/hdk.rs | 4 | ||||
| -rw-r--r-- | src/lib.rs | 17 | ||||
| -rw-r--r-- | src/main.rs | 10 | ||||
| -rw-r--r-- | src/mnemonic.rs | 4 | ||||
| -rw-r--r-- | tests/new.rs | 20 | ||||
| -rw-r--r-- | tests/util/mod.rs | 29 |
11 files changed, 83 insertions, 17 deletions
diff --git a/src/account.rs b/src/account.rs index eda93bf..db80e17 100644 --- a/src/account.rs +++ b/src/account.rs @@ -9,8 +9,9 @@ pub use self::{ }; use crate::hash; use anyhow::Result; +pub use secp256k1::PublicKey; use secp256k1::{ - key::{PublicKey, SecretKey, ONE_KEY}, + key::{SecretKey, ONE_KEY}, Message, Secp256k1, }; use std::{ @@ -8,8 +8,8 @@ pub mod new; pub mod public_key; pub mod sign; -use crate::{account::PrivateKey, hdk, mnemonic::Mnemonic}; use anyhow::Result; +use hdwallet::{account::PrivateKey, hdk, mnemonic::Mnemonic}; use std::{ fs, io::{self, Read as _}, diff --git a/src/cmd/hash.rs b/src/cmd/hash.rs index aade76b..4a650ff 100644 --- a/src/cmd/hash.rs +++ b/src/cmd/hash.rs @@ -1,7 +1,8 @@ //! Module implementing the `hash` subcommand for `keccak256` hashing data. -use crate::{cmd, hash}; +use crate::cmd; use anyhow::Result; +use hdwallet::hash; use std::path::PathBuf; use structopt::StructOpt; diff --git a/src/cmd/new.rs b/src/cmd/new.rs index d247213..002e5d0 100644 --- a/src/cmd/new.rs +++ b/src/cmd/new.rs @@ -1,8 +1,8 @@ //! Module implementing the `new` subcommand for generating a mnemonic for a new //! hierarchical deterministic wallet. -use crate::mnemonic::{Language, Mnemonic}; use anyhow::Result; +use hdwallet::mnemonic::{Language, Mnemonic}; use structopt::StructOpt; #[derive(Debug, StructOpt)] diff --git a/src/cmd/sign.rs b/src/cmd/sign.rs index 85aec8b..290b1a3 100644 --- a/src/cmd/sign.rs +++ b/src/cmd/sign.rs @@ -1,11 +1,11 @@ //! Module implementing the `sign` subcommand for generating ECDSA signatures. -use crate::{ - cmd::{self, AccountOptions}, +use crate::cmd::{self, AccountOptions}; +use anyhow::{ensure, Context as _, Result}; +use hdwallet::{ hash, transaction::{LegacyTransaction, Transaction}, }; -use anyhow::{ensure, Context as _, Result}; use std::{convert::TryInto, path::PathBuf}; use structopt::StructOpt; @@ -17,6 +17,10 @@ const HARDENED: u32 = 0x8000_0000; /// Creates a new extended private key from a seed. pub fn derive(seed: impl AsRef<[u8]>, path: &Path) -> Result<PrivateKey> { + derive_slice(seed.as_ref(), path) +} + +fn derive_slice(seed: &[u8], path: &Path) -> Result<PrivateKey> { let mut extended_key = { let mut hmac = Hmac::<Sha512>::new_from_slice(b"Bitcoin seed")?; hmac.update(seed.as_ref()); diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..62474bd --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,17 @@ +//! Ethereum hierarchically deterministic wallet. +//! +//! This crate implements generating and parsing BIP-0039 mnemonic phrases, as +//! well as BIP-0032 private key derivation, and various utilities for using +//! these derived keys for signing various messages relative to Ethereum. + +pub mod account; +pub mod hash; +pub mod hdk; +pub mod mnemonic; +mod rand; +mod serialization; +pub mod transaction; +pub mod typeddata; + +#[cfg(test)] +mod ganache; diff --git a/src/main.rs b/src/main.rs index dcba758..211cfb1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,4 @@ -mod account; mod cmd; -mod hash; -mod hdk; -mod mnemonic; -mod rand; -mod serialization; -mod transaction; - -#[cfg(test)] -mod ganache; use std::process; use structopt::StructOpt; diff --git a/src/mnemonic.rs b/src/mnemonic.rs index 5121e40..1d553f0 100644 --- a/src/mnemonic.rs +++ b/src/mnemonic.rs @@ -59,6 +59,10 @@ impl Mnemonic { /// Parses a mnemonic from a phrase. pub fn from_phrase(mnemonic: impl AsRef<str>) -> Result<Self> { + Self::from_phrase_str(mnemonic.as_ref()) + } + + fn from_phrase_str(mnemonic: &str) -> Result<Self> { let (language, words) = Language::split(mnemonic.as_ref())?; let len = mnemonic_to_byte_length(words.len())?; diff --git a/tests/new.rs b/tests/new.rs new file mode 100644 index 0000000..4ddbf0c --- /dev/null +++ b/tests/new.rs @@ -0,0 +1,20 @@ +mod util; + +use hdwallet::mnemonic::Mnemonic; + +#[test] +fn generates_random_mnemonic() { + let mnemonic = util::exec("new", &[]); + assert!(Mnemonic::from_phrase(mnemonic).is_ok()) +} + +#[test] +fn errors_on_invalid_language() { + assert!(util::try_exec("new", &["--language", "klingon"]).is_err()); +} + +#[test] +fn errors_on_invalid_length() { + assert!(util::try_exec("new", &["--length", "1"]).is_err()); + assert!(util::try_exec("new", &["--length", "42"]).is_err()); +}
\ No newline at end of file diff --git a/tests/util/mod.rs b/tests/util/mod.rs new file mode 100644 index 0000000..3cc4c66 --- /dev/null +++ b/tests/util/mod.rs @@ -0,0 +1,29 @@ +#[path = "../../src/ganache.rs"] +mod ganache; + +use std::{str, process::Command}; +pub use ganache::*; + +/// Utility function for running the `hdwallet` command for integration tests. +pub fn exec(subcommand: &str, args: &[&str]) -> String { + try_exec(subcommand, args).unwrap() +} + +/// Try and execute a command, returning an error if there was one. +pub fn try_exec(subcommand: &str, args: &[&str]) -> Result<String, String> { + let output = Command::new("cargo") + .env("MNEMONIC", ganache::DETERMINISTIC_MNEMONIC) + .args(["run", "--", subcommand]) + .args(args) + .output() + .unwrap(); + if output.status.success() { + Ok(string_from_utf8(output.stdout)) + } else { + Err(string_from_utf8(output.stderr)) + } +} + +fn string_from_utf8(bytes: Vec<u8>) -> String { + str::from_utf8(&bytes).unwrap().trim().to_string() +}
\ No newline at end of file |
