identity.rs
1 use super::*; 2 3 impl<S> MlsService<S> 4 where 5 S: DeMlsStorage<MlsStorage = MemoryStorage>, 6 { 7 // ══════════════════════════════════════════════════════════ 8 // Identity 9 // ══════════════════════════════════════════════════════════ 10 11 /// Initialize identity from wallet address. 12 /// 13 /// Creates MLS credentials and signing keys from the wallet address. 14 /// Call this once before using any other methods. 15 pub fn init(&self, wallet: Address) -> Result<()> { 16 { 17 let guard = self 18 .identity 19 .read() 20 .map_err(|e| StorageError::Lock(e.to_string()))?; 21 if guard.is_some() { 22 return Err(MlsError::Identity(IdentityError::AlreadyInitialized)); 23 } 24 } 25 26 let credential = BasicCredential::new(wallet.as_slice().to_vec()); 27 let signer = SignatureKeyPair::new(CIPHERSUITE.signature_algorithm())?; 28 29 // Store signer in OpenMLS storage 30 signer.store(self.storage.mls_storage())?; 31 32 let data = IdentityData { 33 wallet, 34 credential: CredentialWithKey { 35 credential: credential.into(), 36 signature_key: signer.to_public_vec().into(), 37 }, 38 signer, 39 }; 40 41 let mut guard = self 42 .identity 43 .write() 44 .map_err(|e| StorageError::Lock(e.to_string()))?; 45 *guard = Some(data); 46 Ok(()) 47 } 48 49 /// Get the wallet address as a checksummed hex string ("0x..."). 50 pub fn wallet_hex(&self) -> String { 51 self.identity 52 .read() 53 .ok() 54 .and_then(|guard| guard.as_ref().map(|id| id.wallet.to_checksum(None))) 55 .unwrap_or_default() 56 } 57 58 /// Get the wallet address as raw bytes. 59 pub fn wallet_bytes(&self) -> Vec<u8> { 60 self.identity 61 .read() 62 .ok() 63 .and_then(|guard| guard.as_ref().map(|id| id.wallet.as_slice().to_vec())) 64 .unwrap_or_default() 65 } 66 }