Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Rename node binary's key generation command (#92)
Browse files Browse the repository at this point in the history
`gevulot generate node-key` -> `gevulot generate key`

The `node-key` command still exists, but will be phased out. The new
command name allows for more ubiquitous use of gevulot container e.g. in
prover/verifier development purposes.
  • Loading branch information
tuommaki authored Feb 20, 2024
1 parent 460f026 commit a125950
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ This guide uses `/var/lib/gevulot`, which is also the default, but it is configu

Each Gevulot node requires a keypair for operation. It can be generated with Gevulot node container:
```
podman run -it -v /var/lib/gevulot:/var/lib/gevulot:z quay.io/gevulot/node:latest generate node-key
podman run -it -v /var/lib/gevulot:/var/lib/gevulot:z quay.io/gevulot/node:latest generate key
```

## Gevulot node systemd unit
Expand Down
13 changes: 9 additions & 4 deletions crates/node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ pub struct Config {
}

#[derive(Debug, Args)]
pub struct NodeKeyOptions {
pub struct KeyOptions {
#[arg(
long,
long_help = "Node key filename",
long_help = "Key filename",
default_value_os_t = PathBuf::from("/var/lib/gevulot/node.key"),
)]
pub node_key_file: PathBuf,
pub key_file: PathBuf,
}

#[derive(Debug, Subcommand)]
Expand Down Expand Up @@ -174,9 +174,14 @@ pub struct P2PBeaconConfig {

#[derive(Debug, Subcommand)]
pub enum GenerateCommand {
Key {
#[command(flatten)]
options: KeyOptions,
},
// NOTE: Depracated. Will be eventually removed. Use `Key` instead.
NodeKey {
#[command(flatten)]
options: NodeKeyOptions,
options: KeyOptions,
},
}

Expand Down
25 changes: 18 additions & 7 deletions crates/node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use asset_manager::AssetManager;
use async_trait::async_trait;
use clap::Parser;
use cli::{
Cli, Command, Config, GenerateCommand, NodeKeyOptions, P2PBeaconConfig, PeerCommand,
ShowCommand,
Cli, Command, Config, GenerateCommand, KeyOptions, P2PBeaconConfig, PeerCommand, ShowCommand,
};
use eyre::Result;
use gevulot_node::types;
Expand Down Expand Up @@ -67,7 +66,11 @@ async fn main() -> Result<()> {

match cli.subcommand {
Command::Generate { target } => match target {
GenerateCommand::NodeKey { options } => generate_node_key(options),
GenerateCommand::NodeKey { options } => {
eprintln!("WARNING: `node-key` command is deprecated. Please use `key` instead.");
generate_key(options)
}
GenerateCommand::Key { options } => generate_key(options),
},
Command::Migrate { db_url } => {
let pool = PgPoolOptions::new()
Expand Down Expand Up @@ -104,22 +107,23 @@ async fn main() -> Result<()> {
}
}

fn generate_node_key(opts: NodeKeyOptions) -> Result<()> {
fn generate_key(opts: KeyOptions) -> Result<()> {
let key = SecretKey::random(&mut StdRng::from_entropy());
let public_key = PublicKey::from_secret_key(&key);
let mut fd = match std::fs::OpenOptions::new()
.read(true)
.write(true)
.create_new(true)
.open(&opts.node_key_file)
.open(&opts.key_file)
{
Ok(fd) => fd,
Err(err) => match err.kind() {
ErrorKind::NotFound => {
eprintln!("directory for {:#?} doesn't exist", &opts.node_key_file);
eprintln!("directory for {:#?} doesn't exist", &opts.key_file);
std::process::exit(1);
}
ErrorKind::AlreadyExists => {
eprintln!("file {:#?} already exists", &opts.node_key_file);
eprintln!("file {:#?} already exists", &opts.key_file);
std::process::exit(1);
}
_ => return Err(err.into()),
Expand All @@ -128,6 +132,13 @@ fn generate_node_key(opts: NodeKeyOptions) -> Result<()> {

fd.write_all(&key.serialize()[..])?;
fd.flush()?;

println!(
"Key generated and saved in file {}\nPublic key: {}",
&opts.key_file.to_str().unwrap_or(""),
hex::encode(public_key.serialize()),
);

Ok(())
}

Expand Down

0 comments on commit a125950

Please sign in to comment.