Skip to content

Commit

Permalink
only overwrite when explicitly permitted
Browse files Browse the repository at this point in the history
  • Loading branch information
BlaineHeffron committed Aug 19, 2024
1 parent f051663 commit a8fefdc
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions cmd/soroban-cli/src/commands/keys/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub enum Error {
Secret(#[from] secret::Error),
#[error(transparent)]
Network(#[from] network::Error),
#[error("An identity with the name '{0}' already exists")]
IdentityAlreadyExists(String),
}

#[derive(Debug, clap::Parser, Clone)]
Expand Down Expand Up @@ -41,6 +43,10 @@ pub struct Cmd {
#[arg(long, short = 'd', conflicts_with = "seed")]
pub default_seed: bool,

/// Overwrite existing identity if it already exists
#[arg(long)]
pub overwrite: bool,

#[command(flatten)]
pub network: network::Args,
}
Expand Down Expand Up @@ -79,30 +85,22 @@ impl Cmd {
};

// Check if identity exists
let Ok(existing_secret) = self.config_locator.read_identity(&self.name) else {
// Identity doesn't exist, create new one
self.write_identity(&secret)?;
return Ok(());
};
if self.seed.is_some() || self.default_seed {
// Compare secrets only if seed is provided
match (
existing_secret.private_key(self.hd_path),
secret.private_key(self.hd_path),
) {
(Ok(existing_pk), Ok(new_pk)) if existing_pk == new_pk => {
self.handle_existing_identity();
}
_ => {
// Secrets don't match
eprintln!("An identity with the name {} already exists but has a different secret. Overwriting...", self.name);
self.write_identity(&secret)?;
}
if let Ok(_existing_secret) = self.config_locator.read_identity(&self.name) {
if self.overwrite {
eprintln!(
"Overwriting existing identity '{}' as requested.",
self.name
);
self.write_identity(&secret)?;
} else {
self.handle_existing_identity();
return Err(Error::IdentityAlreadyExists(self.name.clone()));
}
} else {
// No seed provided, inform user that identity already exists
self.handle_existing_identity();
// Identity doesn't exist, create new one
self.write_identity(&secret)?;
}

Ok(())
}
}

0 comments on commit a8fefdc

Please sign in to comment.