nrc-mls-sqlite-storage 0.1.0

Sqlite database for nostr-mls that implements the NostrMlsStorageProvider Trait
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Implementation of GroupStorage trait for SQLite storage.

use std::collections::BTreeSet;

use nostr::{PublicKey, RelayUrl};
use nrc_mls_storage::groups::error::GroupError;
use nrc_mls_storage::groups::types::{Group, GroupExporterSecret, GroupRelay};
use nrc_mls_storage::groups::GroupStorage;
use nrc_mls_storage::messages::types::Message;
use openmls::group::GroupId;
use rusqlite::{params, OptionalExtension};

use crate::{db, NostrMlsSqliteStorage};

#[inline]
fn into_group_err<T>(e: T) -> GroupError
where
    T: std::error::Error,
{
    GroupError::DatabaseError(e.to_string())
}

impl GroupStorage for NostrMlsSqliteStorage {
    fn all_groups(&self) -> Result<Vec<Group>, GroupError> {
        let conn_guard = self.db_connection.lock().map_err(into_group_err)?;

        let mut stmt = conn_guard
            .prepare("SELECT * FROM groups")
            .map_err(into_group_err)?;

        let groups_iter = stmt
            .query_map([], db::row_to_group)
            .map_err(into_group_err)?;

        let mut groups: Vec<Group> = Vec::new();

        for group_result in groups_iter {
            // TODO: simply skip parsing errors? Or log them? Instead of block the whole request
            let group: Group = group_result.map_err(into_group_err)?;
            groups.push(group);
        }

        Ok(groups)
    }

    fn find_group_by_mls_group_id(
        &self,
        mls_group_id: &GroupId,
    ) -> Result<Option<Group>, GroupError> {
        let conn_guard = self.db_connection.lock().map_err(into_group_err)?;

        let mut stmt = conn_guard
            .prepare("SELECT * FROM groups WHERE mls_group_id = ?")
            .map_err(into_group_err)?;

        stmt.query_row([mls_group_id.as_slice()], db::row_to_group)
            .optional()
            .map_err(into_group_err)
    }

    fn find_group_by_nostr_group_id(
        &self,
        nostr_group_id: &[u8; 32],
    ) -> Result<Option<Group>, GroupError> {
        let conn_guard = self.db_connection.lock().map_err(into_group_err)?;

        let mut stmt = conn_guard
            .prepare("SELECT * FROM groups WHERE nostr_group_id = ?")
            .map_err(into_group_err)?;

        stmt.query_row(params![nostr_group_id], db::row_to_group)
            .optional()
            .map_err(into_group_err)
    }

    fn save_group(&self, group: Group) -> Result<(), GroupError> {
        let conn_guard = self.db_connection.lock().map_err(into_group_err)?;

        let admin_pubkeys_json: String =
            serde_json::to_string(&group.admin_pubkeys).map_err(|e| {
                GroupError::DatabaseError(format!("Failed to serialize admin pubkeys: {}", e))
            })?;

        let last_message_id: Option<&[u8; 32]> =
            group.last_message_id.as_ref().map(|id| id.as_bytes());
        let last_message_at: Option<u64> = group.last_message_at.as_ref().map(|ts| ts.as_u64());

        conn_guard
            .execute(
                "INSERT INTO groups
             (mls_group_id, nostr_group_id, name, description, image_url, image_key, image_nonce, admin_pubkeys, last_message_id,
              last_message_at, epoch, state)
             VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
             ON CONFLICT(mls_group_id) DO UPDATE SET
                nostr_group_id = excluded.nostr_group_id,
                name = excluded.name,
                description = excluded.description,
                image_url = excluded.image_url,
                image_key = excluded.image_key,
                image_nonce = excluded.image_nonce,
                admin_pubkeys = excluded.admin_pubkeys,
                last_message_id = excluded.last_message_id,
                last_message_at = excluded.last_message_at,
                epoch = excluded.epoch,
                state = excluded.state",
                params![
                    &group.mls_group_id.as_slice(),
                    &group.nostr_group_id,
                    &group.name,
                    &group.description,
                    &group.image_url,
                    &group.image_key,
                    &group.image_nonce,
                    &admin_pubkeys_json,
                    last_message_id,
                    &last_message_at,
                    &(group.epoch as i64),
                    group.state.as_str()
                ],
            )
            .map_err(into_group_err)?;

        Ok(())
    }

    fn messages(&self, mls_group_id: &GroupId) -> Result<Vec<Message>, GroupError> {
        // First verify the group exists
        if self.find_group_by_mls_group_id(mls_group_id)?.is_none() {
            return Err(GroupError::InvalidParameters(format!(
                "Group with MLS ID {:?} not found",
                mls_group_id
            )));
        }

        let conn_guard = self.db_connection.lock().map_err(into_group_err)?;

        let mut stmt = conn_guard
            .prepare("SELECT * FROM messages WHERE mls_group_id = ? ORDER BY created_at DESC")
            .map_err(into_group_err)?;

        let messages_iter = stmt
            .query_map(params![mls_group_id.as_slice()], db::row_to_message)
            .map_err(into_group_err)?;

        let mut messages: Vec<Message> = Vec::new();

        for message_result in messages_iter {
            let message: Message = message_result.map_err(into_group_err)?;
            messages.push(message);
        }

        Ok(messages)
    }

    fn admins(&self, mls_group_id: &GroupId) -> Result<BTreeSet<PublicKey>, GroupError> {
        // Get the group which contains the admin_pubkeys
        match self.find_group_by_mls_group_id(mls_group_id)? {
            Some(group) => Ok(group.admin_pubkeys),
            None => Err(GroupError::InvalidParameters(format!(
                "Group with MLS ID {:?} not found",
                mls_group_id
            ))),
        }
    }

    fn group_relays(&self, mls_group_id: &GroupId) -> Result<BTreeSet<GroupRelay>, GroupError> {
        // First verify the group exists
        if self.find_group_by_mls_group_id(mls_group_id)?.is_none() {
            return Err(GroupError::InvalidParameters(format!(
                "Group with MLS ID {:?} not found",
                mls_group_id
            )));
        }

        let conn_guard = self.db_connection.lock().map_err(into_group_err)?;

        let mut stmt = conn_guard
            .prepare("SELECT * FROM group_relays WHERE mls_group_id = ?")
            .map_err(into_group_err)?;

        let relays_iter = stmt
            .query_map(params![mls_group_id.as_slice()], db::row_to_group_relay)
            .map_err(into_group_err)?;

        let mut relays: BTreeSet<GroupRelay> = BTreeSet::new();

        for relay_result in relays_iter {
            let relay: GroupRelay = relay_result.map_err(into_group_err)?;
            relays.insert(relay);
        }

        Ok(relays)
    }

    fn replace_group_relays(
        &self,
        group_id: &GroupId,
        relays: BTreeSet<RelayUrl>,
    ) -> Result<(), GroupError> {
        // First verify the group exists
        if self.find_group_by_mls_group_id(group_id)?.is_none() {
            return Err(GroupError::InvalidParameters(format!(
                "Group with MLS ID {:?} not found",
                group_id
            )));
        }

        let conn_guard = self.db_connection.lock().map_err(into_group_err)?;

        // Use a transaction for atomicity
        let tx = conn_guard.unchecked_transaction().map_err(into_group_err)?;

        // Clear existing relays for this group
        tx.execute(
            "DELETE FROM group_relays WHERE mls_group_id = ?",
            params![group_id.as_slice()],
        )
        .map_err(into_group_err)?;

        // Insert new relays
        for relay_url in relays {
            tx.execute(
                "INSERT INTO group_relays (mls_group_id, relay_url) VALUES (?, ?)",
                params![group_id.as_slice(), relay_url.as_str()],
            )
            .map_err(into_group_err)?;
        }

        // Commit the transaction
        tx.commit().map_err(into_group_err)?;

        Ok(())
    }

    fn get_group_exporter_secret(
        &self,
        mls_group_id: &GroupId,
        epoch: u64,
    ) -> Result<Option<GroupExporterSecret>, GroupError> {
        // First verify the group exists
        if self.find_group_by_mls_group_id(mls_group_id)?.is_none() {
            return Err(GroupError::InvalidParameters(format!(
                "Group with MLS ID {:?} not found",
                mls_group_id
            )));
        }

        let conn_guard = self.db_connection.lock().map_err(into_group_err)?;

        let mut stmt = conn_guard
            .prepare("SELECT * FROM group_exporter_secrets WHERE mls_group_id = ? AND epoch = ?")
            .map_err(into_group_err)?;

        stmt.query_row(
            params![mls_group_id.as_slice(), epoch],
            db::row_to_group_exporter_secret,
        )
        .optional()
        .map_err(into_group_err)
    }

    fn save_group_exporter_secret(
        &self,
        group_exporter_secret: GroupExporterSecret,
    ) -> Result<(), GroupError> {
        if self
            .find_group_by_mls_group_id(&group_exporter_secret.mls_group_id)?
            .is_none()
        {
            return Err(GroupError::InvalidParameters(format!(
                "Group with MLS ID {:?} not found",
                group_exporter_secret.mls_group_id
            )));
        }

        let conn_guard = self.db_connection.lock().map_err(into_group_err)?;

        conn_guard.execute(
            "INSERT OR REPLACE INTO group_exporter_secrets (mls_group_id, epoch, secret) VALUES (?, ?, ?)",
            params![&group_exporter_secret.mls_group_id.as_slice(), &group_exporter_secret.epoch, &group_exporter_secret.secret],
        )
        .map_err(into_group_err)?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use aes_gcm::aead::OsRng;
    use aes_gcm::{Aes128Gcm, KeyInit};
    use nrc_mls_storage::groups::types::GroupState;

    use super::*;

    pub fn generate_encryption_key() -> Vec<u8> {
        Aes128Gcm::generate_key(OsRng).to_vec()
    }

    #[test]
    fn test_save_and_find_group() {
        let storage = NostrMlsSqliteStorage::new_in_memory().unwrap();

        // Create a test group
        let mls_group_id = GroupId::from_slice(&[1, 2, 3, 4]);
        let mut nostr_group_id = [0u8; 32];
        nostr_group_id[0..13].copy_from_slice(b"test_group_12");
        let image_url = Some("http://blossom_server:4531/fake_img.png".to_owned());
        let image_key = Some(generate_encryption_key());
        let image_nonce = Some(vec![8u8; 12]);

        let group = Group {
            mls_group_id: mls_group_id.clone(),
            nostr_group_id,
            name: "Test Group".to_string(),
            description: "A test group".to_string(),
            admin_pubkeys: BTreeSet::new(),
            last_message_id: None,
            last_message_at: None,
            epoch: 0,
            state: GroupState::Active,
            image_url,
            image_key,
            image_nonce,
        };

        // Save the group
        let result = storage.save_group(group);
        assert!(result.is_ok());

        // Find by MLS group ID
        let found_group = storage
            .find_group_by_mls_group_id(&mls_group_id)
            .unwrap()
            .unwrap();
        assert_eq!(found_group.nostr_group_id[0..13], b"test_group_12"[..]);

        // Find by Nostr group ID
        let found_group = storage
            .find_group_by_nostr_group_id(&nostr_group_id)
            .unwrap()
            .unwrap();
        assert_eq!(found_group.mls_group_id, mls_group_id);

        // Get all groups
        let all_groups = storage.all_groups().unwrap();
        assert_eq!(all_groups.len(), 1);
    }

    // Note: Comprehensive storage functionality tests are now in nostr-mls-storage/tests/
    // using shared test functions to ensure consistency between storage implementations

    #[test]
    fn test_group_exporter_secret() {
        let storage = NostrMlsSqliteStorage::new_in_memory().unwrap();

        // Create a test group
        let mls_group_id = GroupId::from_slice(&[1, 2, 3, 4]);
        let mut nostr_group_id = [0u8; 32];
        nostr_group_id[0..13].copy_from_slice(b"test_group_12");

        let group = Group {
            mls_group_id: mls_group_id.clone(),
            nostr_group_id,
            name: "Test Group".to_string(),
            description: "A test group".to_string(),
            admin_pubkeys: BTreeSet::new(),
            last_message_id: None,
            last_message_at: None,
            epoch: 0,
            state: GroupState::Active,
            image_url: None,
            image_key: None,
            image_nonce: None,
        };

        // Save the group
        storage.save_group(group).unwrap();

        // Create a group exporter secret
        let secret1 = GroupExporterSecret {
            mls_group_id: mls_group_id.clone(),
            epoch: 1,
            secret: [0u8; 32],
        };

        // Save the secret
        storage.save_group_exporter_secret(secret1).unwrap();

        // Get the secret and verify it was saved correctly
        let retrieved_secret = storage
            .get_group_exporter_secret(&mls_group_id, 1)
            .unwrap()
            .unwrap();
        assert_eq!(retrieved_secret.secret, [0u8; 32]);

        // Create a second secret with same group_id and epoch but different secret value
        let secret2 = GroupExporterSecret {
            mls_group_id: mls_group_id.clone(),
            epoch: 1,
            secret: [0u8; 32],
        };

        // Save the second secret - this should replace the first one due to the "OR REPLACE" in the SQL
        storage.save_group_exporter_secret(secret2).unwrap();

        // Get the secret again and verify it was updated
        let retrieved_secret = storage
            .get_group_exporter_secret(&mls_group_id, 1)
            .unwrap()
            .unwrap();
        assert_eq!(retrieved_secret.secret, [0u8; 32]);

        // Verify we can still save a different epoch
        let secret3 = GroupExporterSecret {
            mls_group_id: mls_group_id.clone(),
            epoch: 2,
            secret: [0u8; 32],
        };

        storage.save_group_exporter_secret(secret3).unwrap();

        // Verify both epochs exist
        let retrieved_secret1 = storage
            .get_group_exporter_secret(&mls_group_id, 1)
            .unwrap()
            .unwrap();
        let retrieved_secret2 = storage
            .get_group_exporter_secret(&mls_group_id, 2)
            .unwrap()
            .unwrap();

        assert_eq!(retrieved_secret1.secret, [0u8; 32]);
        assert_eq!(retrieved_secret2.secret, [0u8; 32]);
    }
}