This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 267
Signing 2 - hc keygen #974
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
0ecd7e1
Added command: hc keygen
8e7e8c2
rustfmt
b71a1c9
Changelog
b470fcd
Merge branch 'develop' into hc-keygen
25c2611
Merge branch 'develop' into hc-keygen
zippy dcb6c41
Merge branch 'develop' into hc-keygen
Connoropolous 2ccdacb
Merge branch 'develop' into hc-keygen
3bf977f
Add crate holochain_common with paths module and use it in cli::kegyen
d84ad41
XDG compliant paths
3f5f301
Output++
c25acb8
Test keygen
c5e4500
rustfmt
e7a5637
Update common/src/paths.rs
sphinxc0re debf294
style
3a192ba
warnings--
1b35b04
keygen -> cli README
515eed8
Update common/src/paths.rs
sphinxc0re 29073ed
[skip travis] [ci skip] wording tweaks
b4908c6
Update cli/src/main.rs
Connoropolous File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
members = [ | ||
"cli", | ||
"common", | ||
"conductor", | ||
"conductor_api", | ||
"core_api_c_binding", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use error::DefaultResult; | ||
use holochain_common::paths::keys_directory; | ||
use holochain_dpki::{ | ||
bundle::KeyBundle, | ||
keypair::{Keypair, SEEDSIZE}, | ||
util::PwHashConfig, | ||
}; | ||
use holochain_sodium::{pwhash, random::random_secbuf, secbuf::SecBuf}; | ||
use rpassword; | ||
use std::{ | ||
fs::{create_dir_all, File}, | ||
io::prelude::*, | ||
path::PathBuf, | ||
}; | ||
|
||
pub fn keygen(path: Option<PathBuf>, passphrase: Option<String>) -> 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); | ||
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 = 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(()) | ||
} | ||
|
||
#[cfg(test)] | ||
pub mod test { | ||
use super::*; | ||
use holochain_dpki::bundle::KeyBundle; | ||
use std::{ | ||
fs::{remove_file, 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "holochain_common" | ||
version = "0.0.3" | ||
authors = ["Holochain Core Dev Team <devcore@holochain.org>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
directories = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod paths; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use std::path::PathBuf; | ||
|
||
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 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::ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION) | ||
.map(|dirs| dirs.config_dir().to_owned()) | ||
.unwrap_or_else(|| PathBuf::from("/etc").join(APPLICATION)) | ||
} | ||
|
||
/// Returns the path to where agent keys are stored and looked for by default. | ||
/// Something like "~/.config/holochain/keys". | ||
pub fn keys_directory() -> PathBuf { | ||
config_root().join(KEYS_DIRECTORY) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this needed again?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know. I've just moved this by two lines for alphanumeric sorting.