Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keys: valid keys when added #1812

Merged
merged 10 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions FULL_HELP_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -961,8 +961,8 @@ Add a new identity (keypair, ledger, macOS keychain)

###### **Options:**

* `--secret-key` — Add using `secret_key` Can provide with `SOROBAN_SECRET_KEY`
* `--seed-phrase` — Add using 12 word seed phrase to generate `secret_key`
* `--secret-key` — (deprecated) Enter secret (S) key when prompted
* `--seed-phrase` — (deprecated) Enter key using 12-24 word seed phrase
* `--global` — Use global config
* `--config-dir <CONFIG_DIR>` — Location of config directory, default is "."

Expand Down
45 changes: 12 additions & 33 deletions cmd/soroban-cli/src/config/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use crate::{

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("invalid secret key")]
InvalidSecretKey,
// #[error("seed_phrase must be 12 words long, found {len}")]
// InvalidSeedPhrase { len: usize },
#[error("secret input error")]
Expand All @@ -23,52 +21,33 @@ pub enum Error {
SeedPhrase(#[from] sep5::error::Error),
#[error(transparent)]
Ed25519(#[from] ed25519_dalek::SignatureError),
#[error("Invalid address {0}")]
InvalidAddress(String),
#[error("cannot parse secret (S) or seed phrase (12 or 24 word)")]
InvalidSecretOrSeedPhrase,
#[error(transparent)]
Signer(#[from] signer::Error),
}

#[derive(Debug, clap::Args, Clone)]
#[group(skip)]
pub struct Args {
/// Add using `secret_key`
/// Can provide with `SOROBAN_SECRET_KEY`
#[arg(long, conflicts_with = "seed_phrase")]
/// (deprecated) Enter secret (S) key when prompted
#[arg(long)]
pub secret_key: bool,
/// Add using 12 word seed phrase to generate `secret_key`
#[arg(long, conflicts_with = "secret_key")]
/// (deprecated) Enter key using 12-24 word seed phrase
#[arg(long)]
pub seed_phrase: bool,
}

impl Args {
pub fn read_secret(&self) -> Result<Secret, Error> {
if let Ok(secret_key) = std::env::var("SOROBAN_SECRET_KEY") {
Ok(Secret::SecretKey { secret_key })
} else if self.secret_key {
println!("Type a secret key: ");
let secret_key = read_password()?;
let secret_key = PrivateKey::from_string(&secret_key)
.map_err(|_| Error::InvalidSecretKey)?
.to_string();
Ok(Secret::SecretKey { secret_key })
} else if self.seed_phrase {
println!("Type a 12 word seed phrase: ");
let seed_phrase = read_password()?;
let seed_phrase: Vec<&str> = seed_phrase.split_whitespace().collect();
// if seed_phrase.len() != 12 {
// let len = seed_phrase.len();
// return Err(Error::InvalidSeedPhrase { len });
// }
Ok(Secret::SeedPhrase {
seed_phrase: seed_phrase
.into_iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join(" "),
})
} else {
Err(Error::PasswordRead {})
println!("Type a secret key or 12/24 word seed phrase:");
let secret_key = read_password()?;
secret_key
.parse()
.map_err(|_| Error::InvalidSecretOrSeedPhrase)
}
}
}
Expand All @@ -93,7 +72,7 @@ impl FromStr for Secret {
seed_phrase: s.to_string(),
})
} else {
Err(Error::InvalidAddress(s.to_string()))
Err(Error::InvalidSecretOrSeedPhrase)
}
}
}
Expand Down
Loading