From 0ecd7e13cad8250eb6dac2a8b89b7ee44472faec Mon Sep 17 00:00:00 2001 From: Nicolas Luck Date: Fri, 8 Feb 2019 22:17:02 +0100 Subject: [PATCH 01/15] Added command: hc keygen --- cli/Cargo.toml | 4 ++++ cli/src/cli/keygen.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ cli/src/cli/mod.rs | 2 ++ cli/src/main.rs | 11 +++++++++++ hc_dpki/src/bundle.rs | 1 + hc_dpki/src/lib.rs | 5 +++-- 6 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 cli/src/cli/keygen.rs diff --git a/cli/Cargo.toml b/cli/Cargo.toml index a88a4dfd44..ed35c7acbf 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -9,6 +9,8 @@ holochain_core_types = { path = "../core_types" } holochain_core = { path = "../core" } holochain_cas_implementations = { path = "../cas_implementations" } holochain_conductor_api = { path = "../conductor_api" } +holochain_dpki = { path = "../hc_dpki" } +holochain_sodium = { path = "../sodium" } holochain_wasm_utils = { path = "../wasm_utils" } structopt = "0.2" failure = "^0.1" @@ -25,3 +27,5 @@ dir-diff = "0.3.1" colored = "1.6" ignore = "0.4.3" rustyline = "^2.1" +rpassword = "2.1.0" +directories = "1.0" \ No newline at end of file diff --git a/cli/src/cli/keygen.rs b/cli/src/cli/keygen.rs new file mode 100644 index 0000000000..ad0c75a9ca --- /dev/null +++ b/cli/src/cli/keygen.rs @@ -0,0 +1,42 @@ +use error::DefaultResult; +use holochain_dpki::{bundle::KeyBundle, keypair::{Keypair, SEEDSIZE}, util::PwHashConfig}; +use holochain_sodium::{pwhash, random::random_secbuf, secbuf::SecBuf}; +use rpassword; +use std::fs::File; +use std::io::prelude::*; +use std::path::PathBuf; +use std::fs::create_dir_all; + +pub fn keygen() -> DefaultResult<()> { + let passphrase = rpassword::read_password_from_tty(Some("Passphrase: ")).unwrap(); + + let mut seed = SecBuf::with_secure(SEEDSIZE); + random_secbuf(&mut seed); + let mut keypair = Keypair::new_from_seed(&mut seed).unwrap(); + let passphrase_bytes = passphrase.as_bytes(); + let mut passphrase_buf = SecBuf::with_insecure(passphrase_bytes.len()); + passphrase_buf.write(0, passphrase_bytes).expect("SecBuf must be writeable"); + + let bundle: KeyBundle = keypair + .get_bundle(&mut passphrase_buf, "hint".to_string(), Some(PwHashConfig( + pwhash::OPSLIMIT_INTERACTIVE, + pwhash::MEMLIMIT_INTERACTIVE, + pwhash::ALG_ARGON2ID13, + ))) + .unwrap(); + + let path = match directories::UserDirs::new() { + Some(user_dirs) => user_dirs + .home_dir() + .join(".holochain") + .join("keys"), + None => PathBuf::new(), + }; + + create_dir_all(path.clone())?; + let path = path.join(keypair.pub_keys); + let mut file = File::create(path.clone())?; + file.write_all(serde_json::to_string(&bundle).unwrap().as_bytes())?; + println!("Wrote {}.", path.to_str().unwrap()); + Ok(()) +} \ No newline at end of file diff --git a/cli/src/cli/mod.rs b/cli/src/cli/mod.rs index d8fe0eb6d9..10c4f631e2 100644 --- a/cli/src/cli/mod.rs +++ b/cli/src/cli/mod.rs @@ -1,6 +1,7 @@ mod agent; mod generate; mod init; +mod keygen; pub mod package; mod run; mod scaffold; @@ -11,6 +12,7 @@ pub use self::{ agent::agent, generate::generate, init::init, + keygen::keygen, package::{package, unpack}, run::run, test::{test, TEST_DIR_NAME}, diff --git a/cli/src/main.rs b/cli/src/main.rs index 55b63760b5..965dc44179 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -2,7 +2,9 @@ extern crate holochain_cas_implementations; extern crate holochain_conductor_api; extern crate holochain_core; extern crate holochain_core_types; +extern crate holochain_dpki; extern crate holochain_net; +extern crate holochain_sodium; extern crate holochain_wasm_utils; extern crate structopt; #[macro_use] @@ -22,6 +24,7 @@ extern crate ignore; extern crate rustyline; extern crate tempfile; extern crate uuid; +extern crate rpassword; mod cli; mod config_files; @@ -142,6 +145,12 @@ enum Cli { #[structopt(long = "skip-package", short = "s", help = "Skip packaging DNA")] skip_build: bool, }, + #[structopt( + name = "keygen", + alias = "k", + about = "Creates a new agent key pair, asks for a passphrase and writes an encrypted key bundle to ~/.holochain/keys", + )] + KeyGen, } fn main() { @@ -183,6 +192,8 @@ fn run() -> HolochainResult<()> { cli::test(¤t_path, &dir, &testfile, skip_build) } .map_err(HolochainError::Default)?, + Cli::KeyGen => cli::keygen() + .map_err(|e| HolochainError::Default(format_err!("{}", e)))?, } Ok(()) diff --git a/hc_dpki/src/bundle.rs b/hc_dpki/src/bundle.rs index 19555b6619..2024f9e798 100644 --- a/hc_dpki/src/bundle.rs +++ b/hc_dpki/src/bundle.rs @@ -3,6 +3,7 @@ /// The bundle_type tells if the bundle is a RootSeed bundle | DeviceSeed bundle | DevicePINSeed Bundle | ApplicationKeys Bundle /// /// the data includes a base64 encoded string of the ReturnBundleData Struct that was created by combining all the keys in one SecBuf +#[derive(Serialize, Deserialize)] pub struct KeyBundle { pub bundle_type: String, pub hint: String, diff --git a/hc_dpki/src/lib.rs b/hc_dpki/src/lib.rs index fd4f684e2b..4896eb14ff 100644 --- a/hc_dpki/src/lib.rs +++ b/hc_dpki/src/lib.rs @@ -4,10 +4,11 @@ extern crate holochain_sodium; #[macro_use] extern crate arrayref; extern crate base64; -extern crate rustc_serialize; - extern crate bip39; extern crate boolinator; +extern crate rustc_serialize; +#[macro_use] +extern crate serde; pub mod bundle; pub mod error; From 8e7e8c2d6f08ccdd43083548fe272a35b28be6c8 Mon Sep 17 00:00:00 2001 From: Nicolas Luck Date: Fri, 8 Feb 2019 22:18:38 +0100 Subject: [PATCH 02/15] rustfmt --- cli/src/cli/keygen.rs | 40 ++++++++++++++++++++++++---------------- cli/src/main.rs | 11 +++++------ 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/cli/src/cli/keygen.rs b/cli/src/cli/keygen.rs index ad0c75a9ca..946666c58b 100644 --- a/cli/src/cli/keygen.rs +++ b/cli/src/cli/keygen.rs @@ -1,11 +1,16 @@ use error::DefaultResult; -use holochain_dpki::{bundle::KeyBundle, keypair::{Keypair, SEEDSIZE}, util::PwHashConfig}; +use holochain_dpki::{ + bundle::KeyBundle, + keypair::{Keypair, SEEDSIZE}, + util::PwHashConfig, +}; use holochain_sodium::{pwhash, random::random_secbuf, secbuf::SecBuf}; use rpassword; -use std::fs::File; -use std::io::prelude::*; -use std::path::PathBuf; -use std::fs::create_dir_all; +use std::{ + fs::{create_dir_all, File}, + io::prelude::*, + path::PathBuf, +}; pub fn keygen() -> DefaultResult<()> { let passphrase = rpassword::read_password_from_tty(Some("Passphrase: ")).unwrap(); @@ -15,21 +20,24 @@ pub fn keygen() -> DefaultResult<()> { let mut keypair = Keypair::new_from_seed(&mut seed).unwrap(); let passphrase_bytes = passphrase.as_bytes(); let mut passphrase_buf = SecBuf::with_insecure(passphrase_bytes.len()); - passphrase_buf.write(0, passphrase_bytes).expect("SecBuf must be writeable"); + passphrase_buf + .write(0, passphrase_bytes) + .expect("SecBuf must be writeable"); let bundle: KeyBundle = keypair - .get_bundle(&mut passphrase_buf, "hint".to_string(), Some(PwHashConfig( - pwhash::OPSLIMIT_INTERACTIVE, - pwhash::MEMLIMIT_INTERACTIVE, - pwhash::ALG_ARGON2ID13, - ))) + .get_bundle( + &mut passphrase_buf, + "hint".to_string(), + Some(PwHashConfig( + pwhash::OPSLIMIT_INTERACTIVE, + pwhash::MEMLIMIT_INTERACTIVE, + pwhash::ALG_ARGON2ID13, + )), + ) .unwrap(); let path = match directories::UserDirs::new() { - Some(user_dirs) => user_dirs - .home_dir() - .join(".holochain") - .join("keys"), + Some(user_dirs) => user_dirs.home_dir().join(".holochain").join("keys"), None => PathBuf::new(), }; @@ -39,4 +47,4 @@ pub fn keygen() -> DefaultResult<()> { file.write_all(serde_json::to_string(&bundle).unwrap().as_bytes())?; println!("Wrote {}.", path.to_str().unwrap()); Ok(()) -} \ No newline at end of file +} diff --git a/cli/src/main.rs b/cli/src/main.rs index 965dc44179..5d4f016337 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -21,10 +21,10 @@ extern crate toml; #[macro_use] extern crate serde_json; extern crate ignore; +extern crate rpassword; extern crate rustyline; extern crate tempfile; extern crate uuid; -extern crate rpassword; mod cli; mod config_files; @@ -146,9 +146,9 @@ enum Cli { skip_build: bool, }, #[structopt( - name = "keygen", - alias = "k", - about = "Creates a new agent key pair, asks for a passphrase and writes an encrypted key bundle to ~/.holochain/keys", + name = "keygen", + alias = "k", + about = "Creates a new agent key pair, asks for a passphrase and writes an encrypted key bundle to ~/.holochain/keys" )] KeyGen, } @@ -192,8 +192,7 @@ fn run() -> HolochainResult<()> { cli::test(¤t_path, &dir, &testfile, skip_build) } .map_err(HolochainError::Default)?, - Cli::KeyGen => cli::keygen() - .map_err(|e| HolochainError::Default(format_err!("{}", e)))?, + Cli::KeyGen => cli::keygen().map_err(|e| HolochainError::Default(format_err!("{}", e)))?, } Ok(()) From b71a1c998aff3dabca8ec61c3c452c0f17548762 Mon Sep 17 00:00:00 2001 From: Nicolas Luck Date: Fri, 8 Feb 2019 22:23:31 +0100 Subject: [PATCH 03/15] Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc9b8af0bf..8381fad7fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Changed +- Added command `hc keygen` which creates a new key pair, asks for a passphrase and writes an encrypted key bundle file to `~/.holochain/keys`. - core now depends on `pretty_assertions` crate - `ChainHeader::sources()` is now `ChainHeader::provenances()` - Headers from other agents are stored in the EAV From 3bf977f62b7dc3b0e3840b349b6db13aaec76dd6 Mon Sep 17 00:00:00 2001 From: Nicolas Luck Date: Mon, 11 Feb 2019 14:33:14 +0100 Subject: [PATCH 04/15] Add crate holochain_common with paths module and use it in cli::kegyen --- Cargo.toml | 1 + cli/Cargo.toml | 1 + cli/src/cli/keygen.rs | 8 ++------ cli/src/main.rs | 1 + common/Cargo.toml | 8 ++++++++ common/src/lib.rs | 1 + common/src/paths.rs | 20 ++++++++++++++++++++ 7 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 common/Cargo.toml create mode 100644 common/src/lib.rs create mode 100644 common/src/paths.rs diff --git a/Cargo.toml b/Cargo.toml index c6ea0c5fe7..311093c432 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "cli", + "common", "conductor", "conductor_api", "core_api_c_binding", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index ed35c7acbf..74f0b34dea 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Holochain Core Dev Team "] [dependencies] holochain_net = { path = "../net" } +holochain_common = { path = "../common" } holochain_core_types = { path = "../core_types" } holochain_core = { path = "../core" } holochain_cas_implementations = { path = "../cas_implementations" } diff --git a/cli/src/cli/keygen.rs b/cli/src/cli/keygen.rs index 946666c58b..7582ea358b 100644 --- a/cli/src/cli/keygen.rs +++ b/cli/src/cli/keygen.rs @@ -1,4 +1,5 @@ use error::DefaultResult; +use holochain_common::paths::keys_directory; use holochain_dpki::{ bundle::KeyBundle, keypair::{Keypair, SEEDSIZE}, @@ -9,7 +10,6 @@ use rpassword; use std::{ fs::{create_dir_all, File}, io::prelude::*, - path::PathBuf, }; pub fn keygen() -> DefaultResult<()> { @@ -36,11 +36,7 @@ pub fn keygen() -> DefaultResult<()> { ) .unwrap(); - let path = match directories::UserDirs::new() { - Some(user_dirs) => user_dirs.home_dir().join(".holochain").join("keys"), - None => PathBuf::new(), - }; - + let path = keys_directory(); create_dir_all(path.clone())?; let path = path.join(keypair.pub_keys); let mut file = File::create(path.clone())?; diff --git a/cli/src/main.rs b/cli/src/main.rs index 5d4f016337..b69c0ef5a5 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,4 +1,5 @@ extern crate holochain_cas_implementations; +extern crate holochain_common; extern crate holochain_conductor_api; extern crate holochain_core; extern crate holochain_core_types; diff --git a/common/Cargo.toml b/common/Cargo.toml new file mode 100644 index 0000000000..8906c89d69 --- /dev/null +++ b/common/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "holochain_common" +version = "0.0.3" +authors = ["Holochain Core Dev Team "] +edition = "2018" + +[dependencies] +directories = "1.0" \ No newline at end of file diff --git a/common/src/lib.rs b/common/src/lib.rs new file mode 100644 index 0000000000..06f949561e --- /dev/null +++ b/common/src/lib.rs @@ -0,0 +1 @@ +pub mod paths; \ No newline at end of file diff --git a/common/src/paths.rs b/common/src/paths.rs new file mode 100644 index 0000000000..0ea25ace9d --- /dev/null +++ b/common/src/paths.rs @@ -0,0 +1,20 @@ +use std::path::PathBuf; + +pub const CONFIG_DIRECTORY: &'static str = "holochain"; +pub const KEYS_DIRECTORY: &'static str = "keys"; + +/// Returns the path to the root config directory for all of Holochain. +/// If we can get a user directory it will be a dot-directory in ~, like "/home/peter/.holochain". +/// If it can't get a user directory it will default to /etc, i.e. "/etc/holochain". +pub fn config_root() -> PathBuf { + directories::UserDirs::new() + .and_then(|user_dirs| Some(user_dirs.home_dir().join(format!(".{}", CONFIG_DIRECTORY)))) + .or(Some(PathBuf::new().join("/etc").join(CONFIG_DIRECTORY))) + .unwrap() +} + +/// Returns the path to where agent keys are stored and looked for by default. +/// Something like "~/.holochain/keys". +pub fn keys_directory() -> PathBuf { + config_root().join(KEYS_DIRECTORY) +} \ No newline at end of file From d84ad41109e6caafbdb1f84377de10f5e061abc1 Mon Sep 17 00:00:00 2001 From: Nicolas Luck Date: Mon, 11 Feb 2019 14:40:30 +0100 Subject: [PATCH 05/15] XDG compliant paths --- common/src/paths.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/common/src/paths.rs b/common/src/paths.rs index 0ea25ace9d..82bc3b7074 100644 --- a/common/src/paths.rs +++ b/common/src/paths.rs @@ -1,20 +1,23 @@ use std::path::PathBuf; -pub const CONFIG_DIRECTORY: &'static str = "holochain"; +pub const QUALIFIER: &'static str = "org"; +pub const ORGANIZATION: &'static str = "holochain"; +pub const APPLICATION: &'static str = "holochain"; pub const KEYS_DIRECTORY: &'static str = "keys"; /// Returns the path to the root config directory for all of Holochain. -/// If we can get a user directory it will be a dot-directory in ~, like "/home/peter/.holochain". -/// If it can't get a user directory it will default to /etc, i.e. "/etc/holochain". +/// If we can get a user directory it will be an XDG compliant path +/// like "/home/peter/.config/holochain". +/// If it can't get a user directory it will default to "/etc/holochain". pub fn config_root() -> PathBuf { - directories::UserDirs::new() - .and_then(|user_dirs| Some(user_dirs.home_dir().join(format!(".{}", CONFIG_DIRECTORY)))) - .or(Some(PathBuf::new().join("/etc").join(CONFIG_DIRECTORY))) + directories::ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION) + .and_then(|dirs| Some(dirs.config_dir().to_owned())) + .or(Some(PathBuf::new().join("/etc").join(APPLICATION))) .unwrap() } /// Returns the path to where agent keys are stored and looked for by default. -/// Something like "~/.holochain/keys". +/// Something like "~/.config/holochain/keys". pub fn keys_directory() -> PathBuf { config_root().join(KEYS_DIRECTORY) } \ No newline at end of file From 3f5f301fca8fd02bedaf36a11a85165fa7471693 Mon Sep 17 00:00:00 2001 From: Nicolas Luck Date: Mon, 11 Feb 2019 14:43:24 +0100 Subject: [PATCH 06/15] Output++ --- cli/src/cli/keygen.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cli/src/cli/keygen.rs b/cli/src/cli/keygen.rs index 7582ea358b..f2e64470fd 100644 --- a/cli/src/cli/keygen.rs +++ b/cli/src/cli/keygen.rs @@ -38,9 +38,10 @@ pub fn keygen() -> DefaultResult<()> { let path = keys_directory(); create_dir_all(path.clone())?; - let path = path.join(keypair.pub_keys); + let path = path.join(keypair.pub_keys.clone()); let mut file = File::create(path.clone())?; file.write_all(serde_json::to_string(&bundle).unwrap().as_bytes())?; - println!("Wrote {}.", path.to_str().unwrap()); + println!("Agent keys with public address: {}", keypair.pub_keys); + println!("written to: {}.", path.to_str().unwrap()); Ok(()) } From c25acb8b22e7cfa1c6d02420ff627e2b6667c36c Mon Sep 17 00:00:00 2001 From: Nicolas Luck Date: Mon, 11 Feb 2019 15:18:52 +0100 Subject: [PATCH 07/15] Test keygen --- cli/src/cli/keygen.rs | 53 +++++++++++++++++++++++++++++++++++++++---- cli/src/main.rs | 2 +- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/cli/src/cli/keygen.rs b/cli/src/cli/keygen.rs index f2e64470fd..2bb3440019 100644 --- a/cli/src/cli/keygen.rs +++ b/cli/src/cli/keygen.rs @@ -10,10 +10,13 @@ use rpassword; use std::{ fs::{create_dir_all, File}, io::prelude::*, + path::PathBuf, }; -pub fn keygen() -> DefaultResult<()> { - let passphrase = rpassword::read_password_from_tty(Some("Passphrase: ")).unwrap(); +pub fn keygen(path: Option, passphrase: Option) -> DefaultResult<()> { + let passphrase = passphrase.unwrap_or_else(|| + rpassword::read_password_from_tty(Some("Passphrase: ")).unwrap() + ); let mut seed = SecBuf::with_secure(SEEDSIZE); random_secbuf(&mut seed); @@ -36,12 +39,52 @@ pub fn keygen() -> DefaultResult<()> { ) .unwrap(); - let path = keys_directory(); - create_dir_all(path.clone())?; - let path = path.join(keypair.pub_keys.clone()); + let path = if None == path { + let p = keys_directory(); + create_dir_all(p.clone())?; + p.join(keypair.pub_keys.clone()) + } else { + path.unwrap() + }; + let mut file = File::create(path.clone())?; file.write_all(serde_json::to_string(&bundle).unwrap().as_bytes())?; println!("Agent keys with public address: {}", keypair.pub_keys); println!("written to: {}.", path.to_str().unwrap()); Ok(()) } + + +pub mod test { + use super::*; + use holochain_dpki::bundle::KeyBundle; + use std::{ + fs::{File, remove_file}, + path::PathBuf + }; + + #[test] + fn keygen_roundtrip() { + let path = PathBuf::new().join("test.key"); + let passphrase = String::from("secret"); + + keygen(Some(path.clone()), Some(passphrase.clone())) + .expect("Keygen should work"); + + let mut file = File::open(path.clone()).unwrap(); + let mut contents = String::new(); + file.read_to_string(&mut contents).unwrap(); + + let bundle: KeyBundle = serde_json::from_str(&contents).unwrap(); + let mut passphrase = SecBuf::with_insecure_from_string(passphrase); + let keypair = Keypair::from_bundle(&bundle, &mut passphrase, Some(PwHashConfig( + pwhash::OPSLIMIT_INTERACTIVE, + pwhash::MEMLIMIT_INTERACTIVE, + pwhash::ALG_ARGON2ID13, + ))); + + assert!(keypair.is_ok()); + + let _ = remove_file(path); + } +} \ No newline at end of file diff --git a/cli/src/main.rs b/cli/src/main.rs index b69c0ef5a5..7c9d7a9e51 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -193,7 +193,7 @@ fn run() -> HolochainResult<()> { cli::test(¤t_path, &dir, &testfile, skip_build) } .map_err(HolochainError::Default)?, - Cli::KeyGen => cli::keygen().map_err(|e| HolochainError::Default(format_err!("{}", e)))?, + Cli::KeyGen => cli::keygen(None, None).map_err(|e| HolochainError::Default(format_err!("{}", e)))?, } Ok(()) From c5e45009947bf5bbd60347bad5475f4e9626301c Mon Sep 17 00:00:00 2001 From: Nicolas Luck Date: Mon, 11 Feb 2019 15:21:35 +0100 Subject: [PATCH 08/15] rustfmt --- cli/src/cli/keygen.rs | 29 +++++++++++++++-------------- cli/src/main.rs | 4 +++- common/src/lib.rs | 2 +- common/src/paths.rs | 2 +- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/cli/src/cli/keygen.rs b/cli/src/cli/keygen.rs index 2bb3440019..90efb41799 100644 --- a/cli/src/cli/keygen.rs +++ b/cli/src/cli/keygen.rs @@ -14,9 +14,8 @@ use std::{ }; pub fn keygen(path: Option, passphrase: Option) -> DefaultResult<()> { - let passphrase = passphrase.unwrap_or_else(|| - rpassword::read_password_from_tty(Some("Passphrase: ")).unwrap() - ); + let passphrase = passphrase + .unwrap_or_else(|| rpassword::read_password_from_tty(Some("Passphrase: ")).unwrap()); let mut seed = SecBuf::with_secure(SEEDSIZE); random_secbuf(&mut seed); @@ -54,13 +53,12 @@ pub fn keygen(path: Option, passphrase: Option) -> DefaultResul Ok(()) } - pub mod test { use super::*; use holochain_dpki::bundle::KeyBundle; use std::{ - fs::{File, remove_file}, - path::PathBuf + fs::{remove_file, File}, + path::PathBuf, }; #[test] @@ -68,8 +66,7 @@ pub mod test { let path = PathBuf::new().join("test.key"); let passphrase = String::from("secret"); - keygen(Some(path.clone()), Some(passphrase.clone())) - .expect("Keygen should work"); + keygen(Some(path.clone()), Some(passphrase.clone())).expect("Keygen should work"); let mut file = File::open(path.clone()).unwrap(); let mut contents = String::new(); @@ -77,14 +74,18 @@ pub mod test { let bundle: KeyBundle = serde_json::from_str(&contents).unwrap(); let mut passphrase = SecBuf::with_insecure_from_string(passphrase); - let keypair = Keypair::from_bundle(&bundle, &mut passphrase, Some(PwHashConfig( - pwhash::OPSLIMIT_INTERACTIVE, - pwhash::MEMLIMIT_INTERACTIVE, - pwhash::ALG_ARGON2ID13, - ))); + let keypair = Keypair::from_bundle( + &bundle, + &mut passphrase, + Some(PwHashConfig( + pwhash::OPSLIMIT_INTERACTIVE, + pwhash::MEMLIMIT_INTERACTIVE, + pwhash::ALG_ARGON2ID13, + )), + ); assert!(keypair.is_ok()); let _ = remove_file(path); } -} \ No newline at end of file +} diff --git a/cli/src/main.rs b/cli/src/main.rs index 7c9d7a9e51..3b52a387ce 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -193,7 +193,9 @@ fn run() -> HolochainResult<()> { cli::test(¤t_path, &dir, &testfile, skip_build) } .map_err(HolochainError::Default)?, - Cli::KeyGen => cli::keygen(None, None).map_err(|e| HolochainError::Default(format_err!("{}", e)))?, + Cli::KeyGen => { + cli::keygen(None, None).map_err(|e| HolochainError::Default(format_err!("{}", e)))? + } } Ok(()) diff --git a/common/src/lib.rs b/common/src/lib.rs index 06f949561e..8118b2968e 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -1 +1 @@ -pub mod paths; \ No newline at end of file +pub mod paths; diff --git a/common/src/paths.rs b/common/src/paths.rs index 82bc3b7074..5b0fa92c93 100644 --- a/common/src/paths.rs +++ b/common/src/paths.rs @@ -20,4 +20,4 @@ pub fn config_root() -> PathBuf { /// Something like "~/.config/holochain/keys". pub fn keys_directory() -> PathBuf { config_root().join(KEYS_DIRECTORY) -} \ No newline at end of file +} From e7a5637fc41899e218599bb51dfaf5f48c8cb096 Mon Sep 17 00:00:00 2001 From: Julian Laubstein Date: Mon, 11 Feb 2019 16:22:42 +0100 Subject: [PATCH 09/15] Update common/src/paths.rs Co-Authored-By: lucksus --- common/src/paths.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/paths.rs b/common/src/paths.rs index 5b0fa92c93..ba45cb52a6 100644 --- a/common/src/paths.rs +++ b/common/src/paths.rs @@ -11,7 +11,7 @@ pub const KEYS_DIRECTORY: &'static str = "keys"; /// If it can't get a user directory it will default to "/etc/holochain". pub fn config_root() -> PathBuf { directories::ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION) - .and_then(|dirs| Some(dirs.config_dir().to_owned())) + .map(|dirs| dirs.config_dir().to_owned()) .or(Some(PathBuf::new().join("/etc").join(APPLICATION))) .unwrap() } From debf294bca468bd53e90ab525165dafe25f90bb7 Mon Sep 17 00:00:00 2001 From: Nicolas Luck Date: Mon, 11 Feb 2019 16:24:30 +0100 Subject: [PATCH 10/15] style --- common/src/paths.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/common/src/paths.rs b/common/src/paths.rs index ba45cb52a6..fe329b43ef 100644 --- a/common/src/paths.rs +++ b/common/src/paths.rs @@ -12,8 +12,7 @@ pub const KEYS_DIRECTORY: &'static str = "keys"; pub fn config_root() -> PathBuf { directories::ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION) .map(|dirs| dirs.config_dir().to_owned()) - .or(Some(PathBuf::new().join("/etc").join(APPLICATION))) - .unwrap() + .unwrap_or_else(|| PathBuf::new().join("/etc").join(APPLICATION)) } /// Returns the path to where agent keys are stored and looked for by default. From 3a192bab4db9d4372df908805b0fd50a9fe6508c Mon Sep 17 00:00:00 2001 From: Nicolas Luck Date: Mon, 11 Feb 2019 16:24:36 +0100 Subject: [PATCH 11/15] warnings-- --- cli/src/cli/keygen.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/src/cli/keygen.rs b/cli/src/cli/keygen.rs index 90efb41799..aac1d67466 100644 --- a/cli/src/cli/keygen.rs +++ b/cli/src/cli/keygen.rs @@ -53,6 +53,7 @@ pub fn keygen(path: Option, passphrase: Option) -> DefaultResul Ok(()) } +#[cfg(test)] pub mod test { use super::*; use holochain_dpki::bundle::KeyBundle; From 1b35b04461947de6f86e6693284bd0843d422ab4 Mon Sep 17 00:00:00 2001 From: Nicolas Luck Date: Mon, 11 Feb 2019 16:29:29 +0100 Subject: [PATCH 12/15] keygen -> cli README --- cli/README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cli/README.md b/cli/README.md index 7021bf1d5c..7eb2daac08 100644 --- a/cli/README.md +++ b/cli/README.md @@ -69,6 +69,7 @@ If you want to use `hc run` with real (as opposed to mock) networking, you will | unpack | Unpacks a Holochain bundle into its original file system structure | | test | Runs tests written in the test folder | | run | Starts a websocket server for the current Holochain app | +| keygen | Creates a new passphrase encrypted agent key bundle | | agent (u) | Starts a Holochain node as an agent | ### hc init & hc generate: How To Get Started Building An App @@ -89,6 +90,11 @@ To read about `hc test`, used for running tests over your source code, see [http To read about `hc run`, used for spinning up a quick developement version of your app with an HTTP or Websocket interface, that you can connect to from a UI, or any client, see [https://developer.holochain.org/guide/latest/development_conductor.html](https://developer.holochain.org/guide/latest/development_conductor.html). +### hc keygen: Create agent key pair + +Every agent is represented by a private/public key pair and its source chain. +This command creates a new key pair, asks for a passphrase and writes a key bundle file that the Holochain Conductor +can read and when starting up an instance. ## Contribute Holochain is an open source project. We welcome all sorts of participation and are actively working on increasing surface area to accept it. Please see our [contributing guidelines](../CONTRIBUTING.md) for our general practices and protocols on participating in the community. From 515eed81076a42a0462d003ecfb7c08f572da610 Mon Sep 17 00:00:00 2001 From: Julian Laubstein Date: Mon, 11 Feb 2019 16:36:51 +0100 Subject: [PATCH 13/15] Update common/src/paths.rs Co-Authored-By: lucksus --- common/src/paths.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/paths.rs b/common/src/paths.rs index fe329b43ef..3de7d21db3 100644 --- a/common/src/paths.rs +++ b/common/src/paths.rs @@ -12,7 +12,7 @@ pub const KEYS_DIRECTORY: &'static str = "keys"; pub fn config_root() -> PathBuf { directories::ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION) .map(|dirs| dirs.config_dir().to_owned()) - .unwrap_or_else(|| PathBuf::new().join("/etc").join(APPLICATION)) + .unwrap_or_else(|| PathBuf::from("/etc").join(APPLICATION)) } /// Returns the path to where agent keys are stored and looked for by default. From 29073ed45104300029b9910c6669111a8a4a98bb Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Mon, 11 Feb 2019 10:43:42 -0500 Subject: [PATCH 14/15] [skip travis] [ci skip] wording tweaks --- cli/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cli/README.md b/cli/README.md index 7eb2daac08..d0d6fb4fb0 100644 --- a/cli/README.md +++ b/cli/README.md @@ -92,9 +92,9 @@ To read about `hc run`, used for spinning up a quick developement version of you ### hc keygen: Create agent key pair -Every agent is represented by a private/public key pair and its source chain. -This command creates a new key pair, asks for a passphrase and writes a key bundle file that the Holochain Conductor -can read and when starting up an instance. +Every agent is represented by a private/public key pair, which are used to author source chains. +This command creates a new key pair by asking for a passphrase and writing a key bundle file that a Holochain Conductor +can read when starting up an instance. ## Contribute Holochain is an open source project. We welcome all sorts of participation and are actively working on increasing surface area to accept it. Please see our [contributing guidelines](../CONTRIBUTING.md) for our general practices and protocols on participating in the community. From b4908c6362b7e3ca69c428457b6fdff9e4b5b5d1 Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Mon, 11 Feb 2019 17:01:59 +0100 Subject: [PATCH 15/15] Update cli/src/main.rs Co-Authored-By: lucksus --- cli/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 3b52a387ce..634962bce7 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -149,7 +149,7 @@ enum Cli { #[structopt( name = "keygen", alias = "k", - about = "Creates a new agent key pair, asks for a passphrase and writes an encrypted key bundle to ~/.holochain/keys" + about = "Creates a new agent key pair, asks for a passphrase and writes an encrypted key bundle to ~/.config/holochain/keys" )] KeyGen, }