Skip to content

Commit

Permalink
refactor(delegations-cli): seperate cli config for local and keystore
Browse files Browse the repository at this point in the history
  • Loading branch information
namn-grg committed Oct 15, 2024
1 parent c849466 commit 5553a3b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
47 changes: 26 additions & 21 deletions bolt-delegations-cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,33 @@ pub struct Opts {
#[derive(Subcommand, Debug, Clone)]
pub enum Commands {
/// Generate delegation messages.
Generate {
/// The source of the validator key (local or keystore).
#[clap(long, env = "SOURCE")]
source: SourceType,
GenerateLocal {
/// The private key in hex format (required if source is local).
/// Multiple secret keys must be seperated by commas.
#[clap(
long,
env = "SECRET_KEYS",
value_parser,
value_delimiter = ',',
hide_env_values = true,
conflicts_with("keystore_path")
)]
secret_key: Option<Vec<String>>,

/// The BLS public key to which the delegation message should be signed.
#[clap(long, env = "DELEGATEE_PUBKEY")]
delegatee_pubkey: String,

/// The output file for the delegations.
#[clap(long, env = "OUTPUT_FILE_PATH", default_value = "delegations.json")]
out: String,

/// The chain for which the delegation message is intended.
#[clap(long, env = "CHAIN", default_value = "mainnet")]
chain: Chain,
},

GenerateKeystore {
/// Path to the keystore file (required if source is keystore).
#[clap(long, env = "KEY_PATH", conflicts_with("secret_key"))]
keystore_path: Option<String>,
Expand All @@ -33,17 +55,6 @@ pub enum Commands {
)]
keystore_password: String,

/// The private key in hex format (required if source is local).
/// Multiple secret keys must be seperated by commas.
#[clap(
long,
env = "SECRET_KEYS",
value_parser,
value_delimiter = ',',
conflicts_with("keystore_path")
)]
secret_key: Option<Vec<String>>,

/// The BLS public key to which the delegation message should be signed.
#[clap(long, env = "DELEGATEE_PUBKEY")]
delegatee_pubkey: String,
Expand All @@ -58,12 +69,6 @@ pub enum Commands {
},
}

#[derive(ValueEnum, Debug, Clone)]
pub enum SourceType {
Local,
Keystore,
}

/// Supported chains for the CLI
#[derive(Debug, Clone, Copy, ValueEnum)]
#[clap(rename_all = "kebab_case")]
Expand Down
35 changes: 16 additions & 19 deletions bolt-delegations-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use eyre::Result;
use lighthouse_eth2_keystore::Keystore;

pub mod config;
use config::{Chain, Commands, Opts, SourceType};
use config::{Chain, Commands, Opts};

pub mod types;
use types::{DelegationMessage, KeystoreError, SignedDelegation};
Expand All @@ -20,31 +20,28 @@ fn main() -> Result<()> {
let cli = Opts::parse();

match &cli.command {
Commands::Generate {
source,
Commands::GenerateLocal { secret_key, delegatee_pubkey, out, chain } => {
let secret_keys = secret_key.as_ref().unwrap();
let delegatee_pubkey = parse_public_key(delegatee_pubkey)?;
let signed_delegation = generate_from_local_key(secret_keys, delegatee_pubkey, chain)?;

write_delegations_to_file(out, &signed_delegation)?;
println!("Signed delegation messages generated and saved to {}", out);
}
Commands::GenerateKeystore {
keystore_path,
secret_key,
keystore_password,
delegatee_pubkey,
out,
chain,
} => {
let delegatee_pubkey = parse_public_key(delegatee_pubkey)?;
let signed_delegation = match source {
SourceType::Local => {
// Secret key is expected from CLI argument or env variable
generate_from_local_key(secret_key.as_ref().unwrap(), delegatee_pubkey, chain)?
}
SourceType::Keystore => {
// Keystore path and password is expected
generate_from_keystore(
keystore_path.as_deref(),
keystore_password.as_bytes(),
delegatee_pubkey,
chain,
)?
}
};
let signed_delegation = generate_from_keystore(
keystore_path.as_deref(),
keystore_password.as_bytes(),
delegatee_pubkey,
chain,
)?;

write_delegations_to_file(out, &signed_delegation)?;
println!("Signed delegation messages generated and saved to {}", out);
Expand Down

0 comments on commit 5553a3b

Please sign in to comment.