Skip to content

Commit

Permalink
fix: revert enum to Secret
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Jul 30, 2024
1 parent c84f8c2 commit f0e6e01
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
6 changes: 3 additions & 3 deletions cmd/crates/soroban-test/tests/it/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::Path;

use soroban_cli::{
commands::contract,
config::{locator::KeyType, secret::SignerKind},
config::{locator::KeyType, secret::Secret},
};
use soroban_test::{TestEnv, Wasm, TEST_ACCOUNT};

Expand All @@ -17,10 +17,10 @@ pub enum SecretKind {
#[allow(clippy::needless_pass_by_value)]
pub fn add_key(dir: &Path, name: &str, kind: SecretKind, data: &str) {
let secret = match kind {
SecretKind::Seed => SignerKind::SeedPhrase {
SecretKind::Seed => Secret::SeedPhrase {
seed_phrase: data.to_string(),
},
SecretKind::Key => SignerKind::SecretKey {
SecretKind::Key => Secret::SecretKey {
secret_key: data.to_string(),
},
};
Expand Down
6 changes: 3 additions & 3 deletions cmd/soroban-cli/src/commands/keys/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::{arg, command};

use super::super::config::{
locator, network,
secret::{self, SignerKind},
secret::{self, Secret},
};

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -51,9 +51,9 @@ pub struct Cmd {
impl Cmd {
pub async fn run(&self) -> Result<(), Error> {
let seed_phrase = if self.default_seed {
SignerKind::test_seed_phrase()
Secret::test_seed_phrase()
} else {
SignerKind::from_seed(self.seed.as_deref())
Secret::from_seed(self.seed.as_deref())
}?;
let secret = if self.as_secret {
seed_phrase.private_key(self.hd_path)?.into()
Expand Down
10 changes: 5 additions & 5 deletions cmd/soroban-cli/src/config/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{utils::find_config_dir, Pwd};
use super::{
alias,
network::{self, Network},
secret::SignerKind,
secret::Secret,
};

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -149,7 +149,7 @@ impl Args {
)
}

pub fn write_identity(&self, name: &str, secret: &SignerKind) -> Result<(), Error> {
pub fn write_identity(&self, name: &str, secret: &Secret) -> Result<(), Error> {
KeyType::Identity.write(name, secret, &self.config_dir()?)
}

Expand Down Expand Up @@ -207,12 +207,12 @@ impl Args {
Ok(saved_networks.chain(default_networks).collect())
}

pub fn read_identity(&self, name: &str) -> Result<SignerKind, Error> {
pub fn read_identity(&self, name: &str) -> Result<Secret, Error> {
KeyType::Identity.read_with_global(name, &self.local_config()?)
}

pub fn account(&self, account_str: &str) -> Result<SignerKind, Error> {
if let Ok(signer) = account_str.parse::<SignerKind>() {
pub fn account(&self, account_str: &str) -> Result<Secret, Error> {
if let Ok(signer) = account_str.parse::<Secret>() {
Ok(signer)
} else {
self.read_identity(account_str)
Expand Down
30 changes: 15 additions & 15 deletions cmd/soroban-cli/src/config/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ pub struct Args {
}

impl Args {
pub fn kind(&self) -> Result<SignerKind, Error> {
pub fn kind(&self) -> Result<Secret, Error> {
if let Ok(secret_key) = std::env::var("SOROBAN_SECRET_KEY") {
Ok(SignerKind::SecretKey { 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(SignerKind::SecretKey { secret_key })
Ok(Secret::SecretKey { secret_key })
} else if self.seed_phrase {
println!("Type a 12 word seed phrase: ");
let seed_phrase = read_password()?;
Expand All @@ -61,7 +61,7 @@ impl Args {
// let len = seed_phrase.len();
// return Err(Error::InvalidSeedPhrase { len });
// }
Ok(SignerKind::SeedPhrase {
Ok(Secret::SeedPhrase {
seed_phrase: seed_phrase
.into_iter()
.map(ToString::to_string)
Expand All @@ -76,21 +76,21 @@ impl Args {

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SignerKind {
pub enum Secret {
SecretKey { secret_key: String },
SeedPhrase { seed_phrase: String },
}

impl FromStr for SignerKind {
impl FromStr for Secret {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if PrivateKey::from_string(s).is_ok() {
Ok(SignerKind::SecretKey {
Ok(Secret::SecretKey {
secret_key: s.to_string(),
})
} else if sep5::SeedPhrase::from_str(s).is_ok() {
Ok(SignerKind::SeedPhrase {
Ok(Secret::SeedPhrase {
seed_phrase: s.to_string(),
})
} else {
Expand All @@ -99,19 +99,19 @@ impl FromStr for SignerKind {
}
}

impl From<PrivateKey> for SignerKind {
impl From<PrivateKey> for Secret {
fn from(value: PrivateKey) -> Self {
SignerKind::SecretKey {
Secret::SecretKey {
secret_key: value.to_string(),
}
}
}

impl SignerKind {
impl Secret {
pub fn private_key(&self, index: Option<usize>) -> Result<PrivateKey, Error> {
Ok(match self {
SignerKind::SecretKey { secret_key } => PrivateKey::from_string(secret_key)?,
SignerKind::SeedPhrase { seed_phrase } => PrivateKey::from_payload(
Secret::SecretKey { secret_key } => PrivateKey::from_string(secret_key)?,
Secret::SeedPhrase { seed_phrase } => PrivateKey::from_payload(
&sep5::SeedPhrase::from_str(seed_phrase)?
.from_path_index(index.unwrap_or_default(), None)?
.private()
Expand All @@ -127,7 +127,7 @@ impl SignerKind {

pub fn signer(&self, index: Option<usize>, prompt: bool) -> Result<StellarSigner, Error> {
match self {
SignerKind::SecretKey { .. } | SignerKind::SeedPhrase { .. } => Ok(
Secret::SecretKey { .. } | Secret::SeedPhrase { .. } => Ok(
StellarSigner::Local(LocalKey::new(self.key_pair(index)?, prompt)),
),
}
Expand All @@ -145,7 +145,7 @@ impl SignerKind {
}?
.seed_phrase
.into_phrase();
Ok(SignerKind::SeedPhrase { seed_phrase })
Ok(Secret::SeedPhrase { seed_phrase })
}

pub fn test_seed_phrase() -> Result<Self, Error> {
Expand Down

0 comments on commit f0e6e01

Please sign in to comment.