From 7829bc42d789f55fe3d06bc9ae64d6dcb2980ffa Mon Sep 17 00:00:00 2001 From: Scott Dn Date: Mon, 18 Sep 2023 21:32:45 +0700 Subject: [PATCH] feat(cli): generate key --- core/cli/src/cli.rs | 2 +- core/cli/src/commands/key.rs | 66 +++++++++++++++++++++++++++++++++--- core/cli/src/main.rs | 2 ++ 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/core/cli/src/cli.rs b/core/cli/src/cli.rs index d7baab09d..16d8c469e 100644 --- a/core/cli/src/cli.rs +++ b/core/cli/src/cli.rs @@ -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, } diff --git a/core/cli/src/commands/key.rs b/core/cli/src/commands/key.rs index e65d76dbf..b78670560 100644 --- a/core/cli/src/commands/key.rs +++ b/core/cli/src/commands/key.rs @@ -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::(config_path).await, } } -async fn generate_key() -> Result<()> { +async fn generate_key>>( + config_path: ResolvedPathBuf, +) -> Result<()> { + let config = Arc::new(load_or_write_config::(config_path).await?); + let signer_config = config.get::(); + + 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::::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::::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( + config_path: ResolvedPathBuf, +) -> Result> { + let config = TomlConfigProvider::open(&config_path)?; + Node::::fill_configuration(&config); + + if !config_path.exists() { + std::fs::write(&config_path, config.serialize_config())?; + } + + Ok(config) +} diff --git a/core/cli/src/main.rs b/core/cli/src/main.rs index b337561fa..57ad36a4a 100644 --- a/core/cli/src/main.rs +++ b/core/cli/src/main.rs @@ -1,6 +1,8 @@ mod args; mod cli; mod commands; +mod config; +mod node; mod utils; use std::process::exit;