Skip to content

Commit

Permalink
feat(cli): generate key
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-dn committed Sep 18, 2023
1 parent bc985d8 commit 7829bc4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion core/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Cli {

match self.args.cmd {
Command::Run => run::exec().await,
Command::Key(cmd) => key::exec(cmd).await,
Command::Key(cmd) => key::exec(cmd, config_path).await,
Command::PrintConfig { default } => print_config::exec(default, config_path).await,
Command::Dev(cmd) => dev::exec(cmd).await,
}
Expand Down
66 changes: 62 additions & 4 deletions core/cli/src/commands/key.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,76 @@
use anyhow::Result;
use std::fs::remove_file;
use std::sync::Arc;

use anyhow::{anyhow, Result};
use lightning_interfaces::config::ConfigProviderInterface;
use lightning_interfaces::infu_collection::{Collection, Node};
use lightning_interfaces::signer::SignerInterface;
use lightning_signer::Signer;
use resolved_pathbuf::ResolvedPathBuf;

use crate::args::KeySubCmd;
use crate::config::TomlConfigProvider;
use crate::node::FinalTypes;

pub async fn exec(key: KeySubCmd) -> Result<()> {
pub async fn exec(key: KeySubCmd, config_path: ResolvedPathBuf) -> Result<()> {
match key {
KeySubCmd::Show => show_key().await,
KeySubCmd::Generate => generate_key().await,
KeySubCmd::Generate => generate_key::<FinalTypes>(config_path).await,
}
}

async fn generate_key() -> Result<()> {
async fn generate_key<C: Collection<SignerInterface = Signer<C>>>(
config_path: ResolvedPathBuf,
) -> Result<()> {
let config = Arc::new(load_or_write_config::<C>(config_path).await?);
let signer_config = config.get::<C::SignerInterface>();

if signer_config.node_key_path.exists() {
return Err(anyhow!(
"Node secret key exists at specified path. Not generating keys."
));
}

if signer_config.consensus_key_path.exists() {
return Err(anyhow!(
"Consensus secret key exists at specified path. Not generating keys."
));
}

match Signer::<C>::generate_node_key(&signer_config.node_key_path) {
Ok(_) => println!(
"Successfully created node secret key at: {:?}",
signer_config.node_key_path
),
Err(err) => return Err(anyhow!("Failed to create node secret key: {err:?}")),
};

match Signer::<C>::generate_consensus_key(&signer_config.consensus_key_path) {
Ok(_) => println!(
"Successfully created consensus secret key at: {:?}",
signer_config.consensus_key_path
),
Err(err) => {
remove_file(signer_config.node_key_path)?;
return Err(anyhow!("Failed to create consensus secret key: {err:?}"));
},
};
Ok(())
}

async fn show_key() -> Result<()> {
Ok(())
}

async fn load_or_write_config<C: Collection>(
config_path: ResolvedPathBuf,
) -> Result<TomlConfigProvider<C>> {
let config = TomlConfigProvider::open(&config_path)?;
Node::<C>::fill_configuration(&config);

if !config_path.exists() {
std::fs::write(&config_path, config.serialize_config())?;
}

Ok(config)
}
2 changes: 2 additions & 0 deletions core/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod args;
mod cli;
mod commands;
mod config;
mod node;
mod utils;

use std::process::exit;
Expand Down

0 comments on commit 7829bc4

Please sign in to comment.