Add mnenomic keypair generation and recovery to cli (#5889)

* Add mnenomic keypair generation and recovery to cli

* Use password input to retrieve mnemonic phrase

* Direct users without keypair file to use solana-keygen
This commit is contained in:
Justin Starry
2019-09-12 18:37:29 -07:00
committed by GitHub
parent 92a5979558
commit 8f5a1535af
6 changed files with 150 additions and 8 deletions

View File

@ -133,8 +133,8 @@ pub fn read_keypair(path: &str) -> Result<Keypair, Box<dyn error::Error>> {
Ok(keypair)
}
pub fn gen_keypair_file(outfile: &str) -> Result<String, Box<dyn error::Error>> {
let keypair_bytes = Keypair::new().to_bytes();
pub fn write_keypair(keypair: &Keypair, outfile: &str) -> Result<String, Box<dyn error::Error>> {
let keypair_bytes = keypair.to_bytes();
let serialized = serde_json::to_string(&keypair_bytes.to_vec())?;
if outfile != "-" {
@ -147,6 +147,21 @@ pub fn gen_keypair_file(outfile: &str) -> Result<String, Box<dyn error::Error>>
Ok(serialized)
}
pub fn keypair_from_seed(seed: &[u8]) -> Result<Keypair, Box<dyn error::Error>> {
if seed.len() < ed25519_dalek::SECRET_KEY_LENGTH {
return Err("Seed is too short".into());
}
let secret = ed25519_dalek::SecretKey::from_bytes(&seed[..ed25519_dalek::SECRET_KEY_LENGTH])
.map_err(|e| e.to_string())?;
let public = ed25519_dalek::PublicKey::from(&secret);
let keypair = Keypair { secret, public };
Ok(keypair)
}
pub fn gen_keypair_file(outfile: &str) -> Result<String, Box<dyn error::Error>> {
write_keypair(&Keypair::new(), outfile)
}
#[cfg(test)]
mod tests {
use super::*;
@ -178,6 +193,15 @@ mod tests {
assert!(!Path::new(&outfile).exists());
}
#[test]
fn test_keypair_from_seed() {
let good_seed = vec![0; 32];
assert!(keypair_from_seed(&good_seed).is_ok());
let too_short_seed = vec![0; 31];
assert!(keypair_from_seed(&too_short_seed).is_err());
}
#[test]
fn test_signature_fromstr() {
let signature = Keypair::new().sign_message(&[0u8]);