Skip to content

Commit

Permalink
Factor out a SecureStore mod for code reusability
Browse files Browse the repository at this point in the history
between generate and add
  • Loading branch information
elizabethengelman committed Jan 8, 2025
1 parent 3cd96bc commit 666b4b0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 39 deletions.
44 changes: 5 additions & 39 deletions cmd/soroban-cli/src/commands/keys/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use super::super::config::{
secret::{self, Secret},
};
use crate::{
commands::global,
config::address::KeyName,
print::Print,
signer::keyring::{self, StellarEntry},
commands::global, config::address::KeyName, print::Print, secure_store::{self, SecureStore}
};

#[derive(thiserror::Error, Debug)]
Expand All @@ -27,7 +24,7 @@ pub enum Error {
IdentityAlreadyExists(String),

#[error(transparent)]
Keyring(#[from] keyring::Error),
SecureStore(#[from] secure_store::Error)
}

#[derive(Debug, clap::Parser, Clone)]
Expand Down Expand Up @@ -124,23 +121,10 @@ impl Cmd {
fn secret(&self, print: &Print) -> Result<Secret, Error> {
let seed_phrase = self.seed_phrase()?;
if self.secure_store {
// secure_store:org.stellar.cli:<key name>
let entry_name_with_prefix = format!(
"{}{}-{}",
keyring::SECURE_STORE_ENTRY_PREFIX,
keyring::SECURE_STORE_ENTRY_SERVICE,
self.name
);

//checking that the entry name is valid before writing to the secure store
let secret: Secret = entry_name_with_prefix.parse()?;

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

return Ok(secret);
let secret = SecureStore::secret(print, self.name.clone(), seed_phrase)?;
return Ok(secret)
}

let secret: Secret = seed_phrase.into();
Ok(if self.as_secret {
secret.private_key(self.hd_path)?.into()
Expand All @@ -156,24 +140,6 @@ impl Cmd {
secret::seed_phrase_from_seed(self.seed.as_deref())
}?)
}

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)?;
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)?;
}
Ok(())
}
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions cmd/soroban-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod get_spec;
pub mod key;
pub mod log;
pub mod print;
pub mod secure_store;
pub mod signer;
pub mod toid;
pub mod tx;
Expand Down
57 changes: 57 additions & 0 deletions cmd/soroban-cli/src/secure_store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use sep5::SeedPhrase;

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)]
Config(#[from] locator::Error),

#[error(transparent)]
Secret(#[from] secret::Error),

#[error(transparent)]
Keyring(#[from] keyring::Error),
}

impl SecureStore {
pub fn secret(print: &Print, entry_name: KeyName, seed_phrase: SeedPhrase) -> Result<Secret, Error> {
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()?;

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

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)?;
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)?;
}
Ok(())
}
}

0 comments on commit 666b4b0

Please sign in to comment.