Skip to content

Commit

Permalink
feat: ensure that you cannot add a ledger key
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Jun 20, 2024
1 parent 5c5e641 commit 449d61d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
30 changes: 30 additions & 0 deletions cmd/crates/soroban-test/tests/it/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
34 changes: 28 additions & 6 deletions cmd/soroban-cli/src/commands/config/locator.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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<Self, Self::Err> {
if s == "ledger" {
return Err(Error::LedgerKeyName);
}
Ok(KeyName(s.to_string()))
}
}

impl AsRef<Path> for Location {
fn as_ref(&self) -> &Path {
match self {
Expand Down Expand Up @@ -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()?)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-cli/src/commands/keys/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?)?)
}
}
2 changes: 1 addition & 1 deletion cmd/soroban-cli/src/commands/keys/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down

0 comments on commit 449d61d

Please sign in to comment.