-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out a SecureStore mod for code reusability
between generate and add
- Loading branch information
1 parent
3cd96bc
commit 666b4b0
Showing
3 changed files
with
63 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |