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 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #974 from holochain/hc-keygen
Signing 2 - hc keygen
- Loading branch information
Showing
12 changed files
with
155 additions
and
2 deletions.
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