diff --git a/cmd/soroban-cli/src/commands/keys/generate.rs b/cmd/soroban-cli/src/commands/keys/generate.rs index 8ec0158bb..20dc8d5a3 100644 --- a/cmd/soroban-cli/src/commands/keys/generate.rs +++ b/cmd/soroban-cli/src/commands/keys/generate.rs @@ -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)] @@ -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)] @@ -124,23 +121,10 @@ impl Cmd { fn secret(&self, print: &Print) -> Result { let seed_phrase = self.seed_phrase()?; if self.secure_store { - // secure_store:org.stellar.cli: - 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() @@ -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)] diff --git a/cmd/soroban-cli/src/lib.rs b/cmd/soroban-cli/src/lib.rs index 302ecab00..655947680 100644 --- a/cmd/soroban-cli/src/lib.rs +++ b/cmd/soroban-cli/src/lib.rs @@ -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; diff --git a/cmd/soroban-cli/src/secure_store.rs b/cmd/soroban-cli/src/secure_store.rs new file mode 100644 index 000000000..ecc5f10ae --- /dev/null +++ b/cmd/soroban-cli/src/secure_store.rs @@ -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 { + 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(()) + } +} \ No newline at end of file