Skip to content

Commit

Permalink
Simplify secure_store
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethengelman committed Jan 14, 2025
1 parent 8e234bc commit b59fc3d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 38 deletions.
4 changes: 2 additions & 2 deletions cmd/soroban-cli/src/commands/keys/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use sep5::SeedPhrase;
use crate::{
commands::global,
config::{address::KeyName, locator, secret::{self, Secret}},
print::{self, Print}, signer::secure_store::{self, SecureStore},
print::Print, signer::secure_store,
};

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

let seed_phrase: SeedPhrase = secret_key.parse()?;

Ok(SecureStore::save_secret(print, &self.name, seed_phrase)?)
Ok(secure_store::save_secret(print, &self.name, seed_phrase)?)
} else {
let prompt = "Type a secret key or 12/24 word seed phrase:";
let secret_key = read_password(print, prompt)?;
Expand Down
4 changes: 2 additions & 2 deletions cmd/soroban-cli/src/commands/keys/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
commands::global,
config::address::KeyName,
print::Print,
signer::secure_store::{self, SecureStore},
signer::secure_store,
};

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -124,7 +124,7 @@ impl Cmd {
fn secret(&self, print: &Print) -> Result<Secret, Error> {
let seed_phrase = self.seed_phrase()?;
if self.secure_store {
Ok(SecureStore::save_secret(print, &self.name, seed_phrase)?)
Ok(secure_store::save_secret(print, &self.name, seed_phrase)?)
} else if self.as_secret {
let secret: Secret = seed_phrase.into();
Ok(secret.private_key(self.hd_path)?.into())
Expand Down
64 changes: 30 additions & 34 deletions cmd/soroban-cli/src/signer/secure_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use crate::{
config::{address::KeyName, locator, secret::{self, Secret}}, print::Print, signer::keyring::{self, StellarEntry}
};

pub struct SecureStore {}

#[derive(thiserror::Error, Debug)]
pub enum Error{
#[error(transparent)]
Expand All @@ -24,40 +22,38 @@ pub enum Error{
SeedPhrase(#[from] sep5::Error)
}

impl SecureStore {
pub fn save_secret(print: &Print, entry_name: &KeyName, seed_phrase: SeedPhrase) -> Result<Secret, Error> {
// secure_store:org.stellar.cli:<key name>
let entry_name_with_prefix = format!(
"{}{}-{}",
keyring::SECURE_STORE_ENTRY_PREFIX,
keyring::SECURE_STORE_ENTRY_SERVICE,
entry_name
);

//checking that the entry name is valid before writing to the secure store
let secret: Secret = entry_name_with_prefix.parse()?;
pub fn save_secret(print: &Print, entry_name: &KeyName, seed_phrase: SeedPhrase) -> Result<Secret, Error> {
// secure_store:org.stellar.cli:<key name>
let entry_name_with_prefix = format!(
"{}{}-{}",
keyring::SECURE_STORE_ENTRY_PREFIX,
keyring::SECURE_STORE_ENTRY_SERVICE,
entry_name
);

if let Secret::SecureStore { entry_name } = &secret {
Self::write_to_secure_store(entry_name, seed_phrase, print)?;
}
//checking that the entry name is valid before writing to the secure store
let secret: Secret = entry_name_with_prefix.parse()?;

return Ok(secret);
if let Secret::SecureStore { entry_name } = &secret {
write_to_secure_store(entry_name, seed_phrase, print)?;
}

fn write_to_secure_store(
entry_name: &String,
seed_phrase: SeedPhrase,
print: &Print,
) -> Result<(), Error> {
print.infoln(format!("Writing to secure store: {entry_name}"));
let entry = StellarEntry::new(entry_name)?;
Ok(if let Ok(key) = entry.get_public_key(None) {
print.warnln(format!("A key for {entry_name} already exists in your operating system's secure store: {key}"));
} else {
print.infoln(format!(
"Saving a new key to your operating system's secure store: {entry_name}"
));
entry.set_seed_phrase(seed_phrase)?;
})
}
return Ok(secret);
}

fn write_to_secure_store(
entry_name: &String,
seed_phrase: SeedPhrase,
print: &Print,
) -> Result<(), Error> {
print.infoln(format!("Writing to secure store: {entry_name}"));
let entry = StellarEntry::new(entry_name)?;
Ok(if let Ok(key) = entry.get_public_key(None) {
print.warnln(format!("A key for {entry_name} already exists in your operating system's secure store: {key}"));
} else {
print.infoln(format!(
"Saving a new key to your operating system's secure store: {entry_name}"
));
entry.set_seed_phrase(seed_phrase)?;
})
}

0 comments on commit b59fc3d

Please sign in to comment.