diff --git a/cmd/crates/soroban-test/tests/it/config.rs b/cmd/crates/soroban-test/tests/it/config.rs index dd33713aa..598f65ec0 100644 --- a/cmd/crates/soroban-test/tests/it/config.rs +++ b/cmd/crates/soroban-test/tests/it/config.rs @@ -153,6 +153,36 @@ fn read_key() { .stdout(predicates::str::contains("test_id\n")); } +#[test] +fn cannot_generate_ledger_key() { + let sandbox = TestEnv::default(); + sandbox + .new_assert_cmd("keys") + .arg("generate") + .arg("ledger") + .assert() + .stdout("") + .stderr("error: Cannot name a Key ledger\n") + .failure(); +} + +#[test] +fn cannot_add_ledger_key() { + let sandbox = TestEnv::default(); + sandbox + .new_assert_cmd("keys") + .env( + "SOROBAN_SECRET_KEY", + "SDIY6AQQ75WMD4W46EYB7O6UYMHOCGQHLAQGQTKHDX4J2DYQCHVCQYFD", + ) + .arg("add") + .arg("ledger") + .assert() + .stdout("") + .stderr("error: Cannot name a Key ledger\n") + .failure(); +} + #[test] fn generate_key() { let sandbox = TestEnv::default(); diff --git a/cmd/soroban-cli/src/commands/config/locator.rs b/cmd/soroban-cli/src/commands/config/locator.rs index f4779523d..04c8c3eb2 100644 --- a/cmd/soroban-cli/src/commands/config/locator.rs +++ b/cmd/soroban-cli/src/commands/config/locator.rs @@ -1,11 +1,7 @@ use clap::arg; use serde::de::DeserializeOwned; use std::{ - ffi::OsStr, - fmt::Display, - fs, io, - path::{Path, PathBuf}, - str::FromStr, + ffi::OsStr, fmt::Display, fs, io, ops::Deref, path::{Path, PathBuf}, str::FromStr }; use crate::{utils::find_config_dir, Pwd}; @@ -60,6 +56,10 @@ pub enum Error { String(#[from] std::string::FromUtf8Error), #[error(transparent)] Secret(#[from] crate::commands::config::secret::Error), + #[error("Incorrect Key name")] + IncorrectKeyName, + #[error("Cannot name a Key ledger")] + LedgerKeyName, } #[derive(Debug, clap::Args, Default, Clone)] @@ -93,6 +93,28 @@ impl Display for Location { } } +pub struct KeyName(String); + +impl Deref for KeyName { + type Target = String; + + fn deref(&self) -> &Self::Target { + &self.0 + } + +} + +impl FromStr for KeyName { + type Err = Error; + + fn from_str(s: &str) -> Result { + if s == "ledger" { + return Err(Error::LedgerKeyName); + } + Ok(KeyName(s.to_string())) + } +} + impl AsRef for Location { fn as_ref(&self) -> &Path { match self { @@ -139,7 +161,7 @@ impl Args { ) } - pub fn write_identity(&self, name: &str, secret: &Signer) -> Result<(), Error> { + pub fn write_identity(&self, name: &KeyName, secret: &Signer) -> Result<(), Error> { KeyType::Identity.write(name, secret, &self.config_dir()?) } diff --git a/cmd/soroban-cli/src/commands/keys/add.rs b/cmd/soroban-cli/src/commands/keys/add.rs index 2868c7371..d250f30ea 100644 --- a/cmd/soroban-cli/src/commands/keys/add.rs +++ b/cmd/soroban-cli/src/commands/keys/add.rs @@ -28,6 +28,6 @@ impl Cmd { pub fn run(&self) -> Result<(), Error> { Ok(self .config_locator - .write_identity(&self.name, &self.secrets.read_secret()?)?) + .write_identity(&self.name.parse()?, &self.secrets.read_secret()?)?) } } diff --git a/cmd/soroban-cli/src/commands/keys/generate.rs b/cmd/soroban-cli/src/commands/keys/generate.rs index 75eb50e01..ea93ff460 100644 --- a/cmd/soroban-cli/src/commands/keys/generate.rs +++ b/cmd/soroban-cli/src/commands/keys/generate.rs @@ -62,7 +62,7 @@ impl Cmd { } else { seed_phrase }; - self.config_locator.write_identity(&self.name, &secret)?; + self.config_locator.write_identity(&self.name.parse()?, &secret)?; if !self.no_fund { let addr = secret.public_key(self.hd_path).await?; let network = self.network.get(&self.config_locator)?;