iris-chat-protocol 0.1.3

Reusable Iris Chat double-ratchet protocol engine
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
use std::path::Path;
use std::sync::{Arc, Mutex};

use nostr::{Event, Filter, Keys, PublicKey, UnsignedEvent};
use rusqlite::{params, Connection, OptionalExtension};

use crate::{
    is_app_keys_event, AppKeys, ProtocolDecryptedMessage, ProtocolEffect, ProtocolEngine,
    ProtocolRetryBatch, SharedConnection, SqliteStorageAdapter, UnixSeconds, APP_KEYS_EVENT_KIND,
    CHAT_MESSAGE_KIND, INVITE_EVENT_KIND, INVITE_RESPONSE_KIND, MESSAGE_EVENT_KIND,
};

const SCHEMA: &str = r#"
CREATE TABLE IF NOT EXISTS private_chat_threads (
    chat_id TEXT PRIMARY KEY,
    display_name TEXT NOT NULL,
    avatar_seed TEXT NOT NULL,
    updated_at_secs INTEGER NOT NULL DEFAULT 0
);

CREATE TABLE IF NOT EXISTS private_chat_messages (
    chat_id TEXT NOT NULL,
    id TEXT NOT NULL,
    body TEXT NOT NULL,
    is_outgoing INTEGER NOT NULL,
    created_at_secs INTEGER NOT NULL,
    delivery TEXT NOT NULL,
    source_event_id TEXT,
    PRIMARY KEY (chat_id, id)
);

CREATE INDEX IF NOT EXISTS private_chat_recent_idx
    ON private_chat_messages(chat_id, created_at_secs, id);

CREATE UNIQUE INDEX IF NOT EXISTS private_chat_source_event_idx
    ON private_chat_messages(source_event_id)
    WHERE source_event_id IS NOT NULL;

CREATE TABLE IF NOT EXISTS private_chat_seen_events (
    event_id TEXT PRIMARY KEY
);

CREATE TABLE IF NOT EXISTS ndr_kv (
    owner_pubkey_hex TEXT NOT NULL,
    device_pubkey_hex TEXT NOT NULL,
    key TEXT NOT NULL,
    value TEXT NOT NULL,
    PRIMARY KEY (owner_pubkey_hex, device_pubkey_hex, key)
);
"#;

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DirectMessageDelivery {
    Pending,
    Sent,
    Received,
    Failed,
}

impl DirectMessageDelivery {
    fn as_str(&self) -> &'static str {
        match self {
            Self::Pending => "pending",
            Self::Sent => "sent",
            Self::Received => "received",
            Self::Failed => "failed",
        }
    }

    fn from_str(value: &str) -> Self {
        match value {
            "sent" => Self::Sent,
            "received" => Self::Received,
            "failed" => Self::Failed,
            _ => Self::Pending,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DirectMessageSnapshot {
    pub id: String,
    pub chat_id: String,
    pub body: String,
    pub is_outgoing: bool,
    pub created_at_secs: u64,
    pub delivery: DirectMessageDelivery,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DirectChatSnapshot {
    pub chat_id: String,
    pub last_message_preview: String,
    pub last_message_at: u64,
    pub unread_count: u32,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DirectThreadSnapshot {
    pub chat: DirectChatSnapshot,
    pub messages: Vec<DirectMessageSnapshot>,
}

#[derive(Clone, Debug)]
pub enum DirectMessageCommand {
    Publish(Event),
    Subscribe {
        subscription_id: String,
        filters: Vec<Filter>,
        durable: bool,
    },
}

pub struct DirectMessageService {
    conn: SharedConnection,
    protocol_engine: Option<ProtocolEngine>,
    owner_public_key: Option<PublicKey>,
    relay_subscription_key: Option<String>,
    fetch_subscription_counter: u64,
    last_error: Option<String>,
}

impl DirectMessageService {
    pub fn memory() -> Self {
        let service = Self {
            conn: Arc::new(Mutex::new(Connection::open_in_memory().unwrap())),
            protocol_engine: None,
            owner_public_key: None,
            relay_subscription_key: None,
            fetch_subscription_counter: 0,
            last_error: None,
        };
        service.ensure_schema();
        service
    }

    pub fn open(data_dir: &Path, owner_keys: Option<&Keys>) -> Self {
        let path = data_dir.join("private-chat.sqlite3");
        let conn = Connection::open(path).or_else(|_| Connection::open_in_memory());
        let conn = match conn {
            Ok(conn) => conn,
            Err(error) => {
                return Self {
                    conn: Arc::new(Mutex::new(Connection::open_in_memory().unwrap())),
                    protocol_engine: None,
                    owner_public_key: None,
                    relay_subscription_key: None,
                    fetch_subscription_counter: 0,
                    last_error: Some(format!("Direct message store open failed: {error}")),
                };
            }
        };
        let service = Self {
            conn: Arc::new(Mutex::new(conn)),
            protocol_engine: None,
            owner_public_key: None,
            relay_subscription_key: None,
            fetch_subscription_counter: 0,
            last_error: None,
        };
        service.ensure_schema();
        if let Some(keys) = owner_keys {
            service.with_protocol_engine(keys)
        } else {
            service
        }
    }

    pub fn activate(&mut self, keys: &Keys) -> Vec<DirectMessageCommand> {
        let next = Self {
            conn: Arc::clone(&self.conn),
            protocol_engine: None,
            owner_public_key: None,
            relay_subscription_key: self.relay_subscription_key.clone(),
            fetch_subscription_counter: self.fetch_subscription_counter,
            last_error: self.last_error.clone(),
        }
        .with_protocol_engine(keys);
        self.protocol_engine = next.protocol_engine;
        self.owner_public_key = next.owner_public_key;
        self.protocol_subscription_commands()
    }

    pub fn last_error(&self) -> Option<String> {
        self.last_error.clone()
    }

    pub fn chats(&self) -> Vec<DirectChatSnapshot> {
        let Ok(conn) = self.conn.lock() else {
            return Vec::new();
        };
        let mut stmt = match conn.prepare(
            "SELECT t.chat_id,
                    COALESCE(m.body, ''), COALESCE(m.created_at_secs, t.updated_at_secs)
             FROM private_chat_threads t
             LEFT JOIN private_chat_messages m
               ON m.chat_id = t.chat_id
              AND m.created_at_secs = (
                    SELECT MAX(created_at_secs)
                    FROM private_chat_messages
                    WHERE chat_id = t.chat_id
              )
             ORDER BY COALESCE(m.created_at_secs, t.updated_at_secs) DESC, t.chat_id ASC",
        ) {
            Ok(stmt) => stmt,
            Err(_) => return Vec::new(),
        };
        let rows = match stmt.query_map([], |row| {
            Ok(DirectChatSnapshot {
                chat_id: row.get(0)?,
                last_message_preview: row.get(1)?,
                last_message_at: row.get::<_, i64>(2)?.max(0) as u64,
                unread_count: 0,
            })
        }) {
            Ok(rows) => rows,
            Err(_) => return Vec::new(),
        };
        rows.filter_map(Result::ok).collect()
    }

    pub fn thread(&self, chat_id: &str) -> Option<DirectThreadSnapshot> {
        let chat_id = normalize_pubkey(chat_id).ok()?;
        let chat = self
            .chats()
            .into_iter()
            .find(|chat| chat.chat_id == chat_id)
            .unwrap_or_else(|| chat_snapshot_for_pubkey(&chat_id));
        let messages = self.messages(&chat_id, 160);
        Some(DirectThreadSnapshot { chat, messages })
    }

    pub fn open_chat(
        &mut self,
        peer_input: &str,
        _keys: &Keys,
    ) -> Result<(DirectThreadSnapshot, Vec<DirectMessageCommand>), String> {
        let public_key = PublicKey::parse(peer_input).map_err(|error| error.to_string())?;
        let chat_id = public_key.to_hex();
        self.ensure_thread(&chat_id, unix_now());
        let commands = self.protocol_subscription_commands();
        let thread = self
            .thread(&chat_id)
            .ok_or_else(|| "Chat open failed".to_string())?;
        Ok((thread, commands))
    }

    pub fn send_message(
        &mut self,
        chat_id: &str,
        body: &str,
        _keys: &Keys,
    ) -> Result<Vec<DirectMessageCommand>, String> {
        let body = body.trim();
        if body.is_empty() {
            return Ok(Vec::new());
        }
        let public_key = PublicKey::parse(chat_id).map_err(|error| error.to_string())?;
        let chat_id = public_key.to_hex();
        self.ensure_thread(&chat_id, unix_now());
        let engine = self
            .protocol_engine
            .as_mut()
            .ok_or_else(|| "Direct message runtime is not ready".to_string())?;
        let result = engine
            .send_direct_text(public_key, &chat_id, body, None, UnixSeconds(unix_now()))
            .map_err(|error| error.to_string())?;
        let delivery = if result.event_ids.is_empty() {
            DirectMessageDelivery::Pending
        } else {
            DirectMessageDelivery::Sent
        };
        self.insert_message(
            &chat_id,
            &result.message_id,
            body,
            true,
            unix_now(),
            delivery,
            None,
        );
        Ok(self.commands_from_effects(result.effects))
    }

    pub fn process_event(&mut self, event: Event, _keys: &Keys) -> Vec<DirectMessageCommand> {
        let event_id = event.id.to_hex();
        if self.seen_event(&event_id) {
            return Vec::new();
        }
        let Some(engine) = self.protocol_engine.as_mut() else {
            return Vec::new();
        };
        let kind = event.kind.as_u16() as u32;
        let mut effects = Vec::new();
        let mut retry_batch = ProtocolRetryBatch::default();
        let mut decrypted = None;

        let processed = match kind {
            APP_KEYS_EVENT_KIND if is_app_keys_event(&event) => match AppKeys::from_event(&event) {
                Ok(app_keys) => match engine.ingest_app_keys_snapshot(
                    event.pubkey,
                    app_keys,
                    event.created_at.as_secs(),
                ) {
                    Ok(batch) => {
                        retry_batch = batch;
                        true
                    }
                    Err(error) => {
                        self.last_error = Some(format!("Direct message app keys failed: {error}"));
                        false
                    }
                },
                Err(_) => false,
            },
            INVITE_EVENT_KIND => match engine.observe_invite_event(&event) {
                Ok(batch) => {
                    retry_batch = batch;
                    true
                }
                Err(_) => false,
            },
            INVITE_RESPONSE_KIND => match engine.observe_invite_response_event(&event) {
                Ok(batch) => {
                    retry_batch = batch;
                    true
                }
                Err(_) => false,
            },
            MESSAGE_EVENT_KIND => match engine.process_direct_message_event(&event) {
                Ok(message) => {
                    decrypted = message;
                    true
                }
                Err(_) => false,
            },
            _ => false,
        };

        if !processed {
            return Vec::new();
        }
        self.mark_seen_event(&event_id);
        if let Some(message) = decrypted {
            self.apply_decrypted_protocol_message(message);
        }
        effects.extend(self.effects_from_retry_batch(retry_batch));
        self.commands_from_effects(effects)
    }

    pub fn mobile_push_message_author_pubkeys(&self) -> Vec<String> {
        let Some(engine) = self.protocol_engine.as_ref() else {
            return Vec::new();
        };
        let mut authors = engine
            .known_message_author_pubkeys()
            .into_iter()
            .map(|pubkey| pubkey.to_hex())
            .collect::<Vec<_>>();
        authors.sort();
        authors.dedup();
        authors
    }

    fn subscription_command(&mut self) -> Option<DirectMessageCommand> {
        let engine = self.protocol_engine.as_ref()?;
        let authors = engine
            .known_message_author_pubkeys()
            .into_iter()
            .chain(self.owner_public_key)
            .collect::<Vec<_>>();
        let mut author_hexes = authors.iter().map(PublicKey::to_hex).collect::<Vec<_>>();
        author_hexes.sort();
        author_hexes.dedup();
        let key = author_hexes.join(",");
        if key.is_empty() || self.relay_subscription_key.as_deref() == Some(key.as_str()) {
            return None;
        }
        self.relay_subscription_key = Some(key);

        let public_keys = author_hexes
            .iter()
            .filter_map(|hex| PublicKey::parse(hex).ok())
            .collect::<Vec<_>>();
        let filter = Filter::new()
            .authors(public_keys)
            .kinds([
                nostr::Kind::from(MESSAGE_EVENT_KIND as u16),
                nostr::Kind::from(INVITE_EVENT_KIND as u16),
                nostr::Kind::from(INVITE_RESPONSE_KIND as u16),
                nostr::Kind::from(APP_KEYS_EVENT_KIND as u16),
            ])
            .limit(500);
        Some(DirectMessageCommand::Subscribe {
            subscription_id: "iris-native-private-chat".to_string(),
            filters: vec![filter],
            durable: true,
        })
    }

    fn with_protocol_engine(mut self, keys: &Keys) -> Self {
        let owner = keys.public_key();
        let owner_hex = owner.to_hex();
        let storage = Arc::new(SqliteStorageAdapter::new(
            Arc::clone(&self.conn),
            owner_hex.clone(),
            owner_hex,
        ));
        match ProtocolEngine::load_or_create_for_local_device(storage, owner, keys) {
            Ok(engine) => {
                self.protocol_engine = Some(engine);
                self.owner_public_key = Some(owner);
            }
            Err(error) => self.last_error = Some(format!("Direct message init failed: {error}")),
        }
        self
    }

    fn protocol_subscription_commands(&mut self) -> Vec<DirectMessageCommand> {
        self.subscription_command().into_iter().collect()
    }

    fn commands_from_effects(&mut self, effects: Vec<ProtocolEffect>) -> Vec<DirectMessageCommand> {
        let mut commands = Vec::new();
        for effect in effects {
            match effect {
                ProtocolEffect::Publish(publish) => {
                    commands.push(DirectMessageCommand::Publish(publish.event));
                }
                ProtocolEffect::FetchProtocolState { filters, reason } => {
                    self.fetch_subscription_counter =
                        self.fetch_subscription_counter.saturating_add(1);
                    let subscription_id = format!(
                        "iris-native-private-chat-fetch-{reason}-{}",
                        self.fetch_subscription_counter
                    );
                    commands.push(DirectMessageCommand::Subscribe {
                        subscription_id,
                        filters,
                        durable: false,
                    });
                }
            }
        }
        commands
    }

    fn effects_from_retry_batch(&mut self, batch: ProtocolRetryBatch) -> Vec<ProtocolEffect> {
        let mut effects = batch.effects;
        for result in batch.direct_results {
            if !result.event_ids.is_empty() {
                self.mark_message_sent(&result.chat_id, &result.message_id);
            }
            effects.extend(result.effects);
        }
        effects.extend(batch.group_result.effects);
        for message in batch.direct_messages {
            self.apply_decrypted_protocol_message(message);
        }
        effects
    }

    fn apply_decrypted_protocol_message(&mut self, message: ProtocolDecryptedMessage) {
        self.apply_decrypted(
            message.sender,
            message.conversation_owner,
            &message.content,
            message.event_id,
        );
    }

    fn apply_decrypted(
        &mut self,
        sender: PublicKey,
        conversation_owner: Option<PublicKey>,
        content: &str,
        source_event_id: Option<String>,
    ) {
        let Some(rumor) = parse_runtime_rumor(content) else {
            return;
        };
        if rumor.kind != CHAT_MESSAGE_KIND {
            return;
        }
        let local_owner = self.owner_public_key;
        let peer = if local_owner == Some(sender) {
            conversation_owner.unwrap_or(sender)
        } else {
            sender
        };
        let chat_id = peer.to_hex();
        self.ensure_thread(&chat_id, rumor.created_at_secs);
        self.insert_message(
            &chat_id,
            &rumor.id,
            &rumor.content,
            local_owner == Some(sender),
            rumor.created_at_secs,
            if local_owner == Some(sender) {
                DirectMessageDelivery::Sent
            } else {
                DirectMessageDelivery::Received
            },
            source_event_id.as_deref(),
        );
    }

    fn ensure_schema(&self) {
        if let Ok(conn) = self.conn.lock() {
            let _ = conn.execute_batch(SCHEMA);
        }
    }

    fn ensure_thread(&self, chat_id: &str, updated_at: u64) {
        if let Ok(conn) = self.conn.lock() {
            let _ = conn.execute(
                "INSERT INTO private_chat_threads (chat_id, display_name, avatar_seed, updated_at_secs)
                 VALUES (?1, '', '', ?2)
                 ON CONFLICT(chat_id) DO UPDATE SET updated_at_secs = MAX(updated_at_secs, excluded.updated_at_secs)",
                params![chat_id, updated_at as i64],
            );
        }
    }

    fn messages(&self, chat_id: &str, limit: usize) -> Vec<DirectMessageSnapshot> {
        let Ok(conn) = self.conn.lock() else {
            return Vec::new();
        };
        let mut stmt = match conn.prepare(
            "SELECT id, body, is_outgoing, created_at_secs, delivery
             FROM private_chat_messages
             WHERE chat_id = ?1
             ORDER BY created_at_secs DESC, id DESC
             LIMIT ?2",
        ) {
            Ok(stmt) => stmt,
            Err(_) => return Vec::new(),
        };
        let rows = match stmt.query_map(params![chat_id, limit as i64], |row| {
            Ok(DirectMessageSnapshot {
                id: row.get(0)?,
                chat_id: chat_id.to_string(),
                body: row.get(1)?,
                is_outgoing: row.get::<_, i64>(2)? != 0,
                created_at_secs: row.get::<_, i64>(3)?.max(0) as u64,
                delivery: DirectMessageDelivery::from_str(&row.get::<_, String>(4)?),
            })
        }) {
            Ok(rows) => rows,
            Err(_) => return Vec::new(),
        };
        let mut messages = rows.filter_map(Result::ok).collect::<Vec<_>>();
        messages.reverse();
        messages
    }

    fn insert_message(
        &self,
        chat_id: &str,
        id: &str,
        body: &str,
        is_outgoing: bool,
        created_at: u64,
        delivery: DirectMessageDelivery,
        source_event_id: Option<&str>,
    ) {
        if id.is_empty() {
            return;
        }
        if let Ok(conn) = self.conn.lock() {
            let _ = conn.execute(
                "INSERT OR IGNORE INTO private_chat_messages
                 (chat_id, id, body, is_outgoing, created_at_secs, delivery, source_event_id)
                 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
                params![
                    chat_id,
                    id,
                    body,
                    is_outgoing as i64,
                    created_at as i64,
                    delivery.as_str(),
                    source_event_id,
                ],
            );
            let _ = conn.execute(
                "UPDATE private_chat_threads SET updated_at_secs = MAX(updated_at_secs, ?2)
                 WHERE chat_id = ?1",
                params![chat_id, created_at as i64],
            );
        }
    }

    fn seen_event(&self, event_id: &str) -> bool {
        let Ok(conn) = self.conn.lock() else {
            return true;
        };
        conn.query_row(
            "SELECT 1 FROM private_chat_seen_events WHERE event_id = ?1",
            [event_id],
            |_| Ok(()),
        )
        .optional()
        .ok()
        .flatten()
        .is_some()
    }

    fn mark_seen_event(&self, event_id: &str) {
        if let Ok(conn) = self.conn.lock() {
            let _ = conn.execute(
                "INSERT OR IGNORE INTO private_chat_seen_events (event_id) VALUES (?1)",
                [event_id],
            );
        }
    }

    fn mark_message_sent(&self, chat_id: &str, id: &str) {
        if id.is_empty() {
            return;
        }
        if let Ok(conn) = self.conn.lock() {
            let _ = conn.execute(
                "UPDATE private_chat_messages
                 SET delivery = ?3
                 WHERE chat_id = ?1 AND id = ?2",
                params![chat_id, id, DirectMessageDelivery::Sent.as_str()],
            );
        }
    }
}

struct RuntimeRumor {
    id: String,
    kind: u32,
    content: String,
    created_at_secs: u64,
}

fn parse_runtime_rumor(content: &str) -> Option<RuntimeRumor> {
    let mut event = serde_json::from_str::<UnsignedEvent>(content).ok()?;
    event.ensure_id();
    event.verify_id().ok()?;
    Some(RuntimeRumor {
        id: event.id.as_ref()?.to_string(),
        kind: event.kind.as_u16() as u32,
        content: event.content,
        created_at_secs: event.created_at.as_secs(),
    })
}

fn chat_snapshot_for_pubkey(chat_id: &str) -> DirectChatSnapshot {
    DirectChatSnapshot {
        chat_id: chat_id.to_string(),
        last_message_preview: String::new(),
        last_message_at: 0,
        unread_count: 0,
    }
}

fn normalize_pubkey(input: &str) -> Result<String, String> {
    PublicKey::parse(input)
        .map(|pubkey| pubkey.to_hex())
        .map_err(|error| error.to_string())
}

fn unix_now() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|duration| duration.as_secs())
        .unwrap_or_default()
}