diff --git a/conversations/src/chat.rs b/conversations/src/chat.rs index 4d7ce6b..295f3ba 100644 --- a/conversations/src/chat.rs +++ b/conversations/src/chat.rs @@ -379,8 +379,8 @@ mod tests { let key_hex = hex::encode(intro.ephemeral_key.as_bytes()); // Key should be persisted - let loaded_key = manager.storage.load_inbox_key(&key_hex).unwrap(); - assert!(loaded_key.is_some()); + let all_keys = manager.storage.load_all_inbox_keys().unwrap(); + assert!(all_keys.contains_key(&key_hex)); } #[test] diff --git a/conversations/src/storage/db.rs b/conversations/src/storage/db.rs index 313edfc..9cb2abb 100644 --- a/conversations/src/storage/db.rs +++ b/conversations/src/storage/db.rs @@ -40,7 +40,7 @@ const CHAT_SCHEMA: &str = " /// /// This struct wraps a SqliteDb and provides domain-specific /// storage operations for chat state (identity, inbox keys, chat metadata). -/// +/// /// Note: Ratchet state persistence is delegated to double_ratchets::RatchetStorage. pub struct ChatStorage { db: SqliteDb, @@ -53,11 +53,6 @@ impl ChatStorage { Self::run_migration(db) } - /// Creates an in-memory storage (useful for testing). - pub fn in_memory() -> Result { - Self::new(StorageConfig::InMemory) - } - /// Creates a new chat storage with the given database. fn run_migration(db: SqliteDb) -> Result { db.connection().execute_batch(CHAT_SCHEMA)?; @@ -116,33 +111,6 @@ impl ChatStorage { Ok(()) } - /// Loads an inbox ephemeral key by its public key hex. - pub fn load_inbox_key( - &self, - public_key_hex: &str, - ) -> Result, StorageError> { - let mut stmt = self - .db - .connection() - .prepare("SELECT secret_key FROM inbox_keys WHERE public_key_hex = ?1")?; - - let result = stmt.query_row(params![public_key_hex], |row| { - let secret_key: Vec = row.get(0)?; - Ok(secret_key) - }); - - match result { - Ok(secret_key) => { - let bytes: [u8; 32] = secret_key - .try_into() - .map_err(|_| StorageError::InvalidData("Invalid secret key length".into()))?; - Ok(Some(StaticSecret::from(bytes))) - } - Err(RusqliteError::QueryReturnedNoRows) => Ok(None), - Err(e) => Err(e.into()), - } - } - /// Loads all inbox ephemeral keys. pub fn load_all_inbox_keys(&self) -> Result, StorageError> { let mut stmt = self @@ -168,15 +136,6 @@ impl ChatStorage { Ok(keys) } - /// Deletes an inbox ephemeral key (after it has been used). - pub fn delete_inbox_key(&mut self, public_key_hex: &str) -> Result<(), StorageError> { - self.db.connection().execute( - "DELETE FROM inbox_keys WHERE public_key_hex = ?1", - params![public_key_hex], - )?; - Ok(()) - } - // ==================== Chat Metadata Operations ==================== /// Saves a chat record. @@ -195,47 +154,6 @@ impl ChatStorage { Ok(()) } - /// Loads a chat record by ID. - pub fn load_chat(&self, chat_id: &str) -> Result, StorageError> { - let mut stmt = self.db.connection().prepare( - "SELECT chat_id, chat_type, remote_public_key, remote_address, created_at - FROM chats WHERE chat_id = ?1", - )?; - - let result = stmt.query_row(params![chat_id], |row| { - let chat_id: String = row.get(0)?; - let chat_type: String = row.get(1)?; - let remote_public_key: Option> = row.get(2)?; - let remote_address: String = row.get(3)?; - let created_at: i64 = row.get(4)?; - Ok(( - chat_id, - chat_type, - remote_public_key, - remote_address, - created_at, - )) - }); - - match result { - Ok((chat_id, chat_type, remote_public_key, remote_address, created_at)) => { - let remote_public_key = remote_public_key.map(|bytes| { - let arr: [u8; 32] = bytes.try_into().expect("Invalid key length"); - arr - }); - Ok(Some(ChatRecord { - chat_id, - chat_type, - remote_public_key, - remote_address, - created_at, - })) - } - Err(RusqliteError::QueryReturnedNoRows) => Ok(None), - Err(e) => Err(e.into()), - } - } - /// Lists all chat IDs. pub fn list_chat_ids(&self) -> Result, StorageError> { let mut stmt = self.db.connection().prepare("SELECT chat_id FROM chats")?; @@ -276,7 +194,7 @@ mod tests { #[test] fn test_identity_roundtrip() { - let mut storage = ChatStorage::in_memory().unwrap(); + let mut storage = ChatStorage::new(StorageConfig::InMemory).unwrap(); // Initially no identity assert!(storage.load_identity().unwrap().is_none()); @@ -293,7 +211,7 @@ mod tests { #[test] fn test_chat_roundtrip() { - let mut storage = ChatStorage::in_memory().unwrap(); + let mut storage = ChatStorage::new(StorageConfig::InMemory).unwrap(); let secret = x25519_dalek::StaticSecret::random(); let remote_key = x25519_dalek::PublicKey::from(&secret);