key_storage_impl.rs (3588B)
1 use enostr::{Keypair, Pubkey}; 2 3 use super::file_key_storage::FileKeyStorage; 4 use crate::Error; 5 6 #[cfg(target_os = "macos")] 7 use super::security_framework_key_storage::SecurityFrameworkKeyStorage; 8 9 #[derive(Debug, PartialEq)] 10 pub enum KeyStorageType { 11 None, 12 FileSystem(FileKeyStorage), 13 #[cfg(target_os = "macos")] 14 SecurityFramework(SecurityFrameworkKeyStorage), 15 } 16 17 #[allow(dead_code)] 18 #[derive(Debug)] 19 pub enum KeyStorageResponse<R> { 20 Waiting, 21 ReceivedResult(Result<R, KeyStorageError>), 22 } 23 24 impl<R: PartialEq> PartialEq for KeyStorageResponse<R> { 25 fn eq(&self, other: &Self) -> bool { 26 match (self, other) { 27 (KeyStorageResponse::Waiting, KeyStorageResponse::Waiting) => true, 28 ( 29 KeyStorageResponse::ReceivedResult(Ok(r1)), 30 KeyStorageResponse::ReceivedResult(Ok(r2)), 31 ) => r1 == r2, 32 ( 33 KeyStorageResponse::ReceivedResult(Err(_)), 34 KeyStorageResponse::ReceivedResult(Err(_)), 35 ) => true, 36 _ => false, 37 } 38 } 39 } 40 41 impl KeyStorageType { 42 pub fn get_keys(&self) -> KeyStorageResponse<Vec<Keypair>> { 43 match self { 44 Self::None => KeyStorageResponse::ReceivedResult(Ok(Vec::new())), 45 Self::FileSystem(f) => f.get_keys(), 46 #[cfg(target_os = "macos")] 47 Self::SecurityFramework(f) => f.get_keys(), 48 } 49 } 50 51 pub fn add_key(&self, key: &Keypair) -> KeyStorageResponse<()> { 52 let _ = key; 53 match self { 54 Self::None => KeyStorageResponse::ReceivedResult(Ok(())), 55 Self::FileSystem(f) => f.add_key(key), 56 #[cfg(target_os = "macos")] 57 Self::SecurityFramework(f) => f.add_key(key), 58 } 59 } 60 61 pub fn remove_key(&self, key: &Keypair) -> KeyStorageResponse<()> { 62 let _ = key; 63 match self { 64 Self::None => KeyStorageResponse::ReceivedResult(Ok(())), 65 Self::FileSystem(f) => f.remove_key(key), 66 #[cfg(target_os = "macos")] 67 Self::SecurityFramework(f) => f.remove_key(key), 68 } 69 } 70 71 pub fn get_selected_key(&self) -> KeyStorageResponse<Option<Pubkey>> { 72 match self { 73 Self::None => KeyStorageResponse::ReceivedResult(Ok(None)), 74 Self::FileSystem(f) => f.get_selected_key(), 75 #[cfg(target_os = "macos")] 76 Self::SecurityFramework(_) => unimplemented!(), 77 } 78 } 79 80 pub fn select_key(&self, key: Option<Pubkey>) -> KeyStorageResponse<()> { 81 match self { 82 Self::None => KeyStorageResponse::ReceivedResult(Ok(())), 83 Self::FileSystem(f) => f.select_key(key), 84 #[cfg(target_os = "macos")] 85 Self::SecurityFramework(_) => unimplemented!(), 86 } 87 } 88 } 89 90 #[allow(dead_code)] 91 #[derive(Debug)] 92 pub enum KeyStorageError { 93 Retrieval(Error), 94 Addition(Error), 95 Selection(Error), 96 Removal(Error), 97 OSError(Error), 98 } 99 100 impl std::fmt::Display for KeyStorageError { 101 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { 102 match self { 103 Self::Retrieval(e) => write!(f, "Failed to retrieve keys: {:?}", e), 104 Self::Addition(key) => write!(f, "Failed to add key: {:?}", key), 105 Self::Selection(pubkey) => write!(f, "Failed to select key: {:?}", pubkey), 106 Self::Removal(key) => write!(f, "Failed to remove key: {:?}", key), 107 Self::OSError(e) => write!(f, "OS had an error: {:?}", e), 108 } 109 } 110 } 111 112 impl std::error::Error for KeyStorageError {}