1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use crate::keyring::SecretKeyEncoding;
use abscissa::{Command, Runnable};
use signatory::{ed25519, Encode};
use std::{env, process};

/// Options for the `keygen` command
#[derive(Command, Debug, Default, Options)]
pub struct KeygenCommand {
    #[options(free, help = "path where generated key should be created")]
    output_paths: Vec<String>,
}

impl Runnable for KeygenCommand {
    /// Generate an Ed25519 secret key for use with a software provider (i.e. ed25519-dalek)
    fn run(&self) {
        if self.output_paths.len() != 1 {
            eprintln!("Usage: {} keygen [PATH]", env::args().next().unwrap());
            process::exit(2);
        }

        let output_path = &self.output_paths[0];

        let seed = ed25519::Seed::generate();
        seed.encode_to_file(output_path, &SecretKeyEncoding::default())
            .unwrap_or_else(|e| {
                status_err!("couldn't write to {}: {}", output_path, e);
                process::exit(1);
            });

        info!("Wrote random Ed25519 private key to {}", output_path);
    }
}