nostr-pubsub 0.1.13

Neutral Nostr event and source routing primitives
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
use std::collections::HashSet;

use nostr::JsonUtil;
use serde_json::{Value, json};

use crate::{
    ClientMessage, EventId, Filter, PubsubError, PubsubPeerSubscription,
    PubsubPeerSubscriptionStore, PubsubSubscriptionUpdate, RelayMessage, Result, SourceId,
    SubscriptionId, VerifiedEvent,
};

pub const DEFAULT_FIPS_PUBSUB_MAX_FRAME_BYTES: usize = 64 * 1024;

/// A supported Nostr message carried by a FIPS pubsub transport frame.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FipsPubsubWireMessage {
    Req {
        subscription_id: SubscriptionId,
        filters: Vec<Filter>,
    },
    Close {
        subscription_id: SubscriptionId,
    },
    Event {
        subscription_id: Option<SubscriptionId>,
        event: VerifiedEvent,
    },
    /// A subscription-scoped live-event inventory announcement.
    Inv {
        subscription_ids: Vec<SubscriptionId>,
        event_id: EventId,
        event_kind: u16,
        payload_bytes: u32,
        hop_limit: u8,
    },
    /// A request for one advertised event. The response is an ordinary
    /// subscription-scoped `EVENT` frame.
    Want {
        event_id: EventId,
    },
}

impl FipsPubsubWireMessage {
    #[must_use]
    pub fn req(subscription_id: SubscriptionId, filters: Vec<Filter>) -> Self {
        Self::Req {
            subscription_id,
            filters,
        }
    }

    #[must_use]
    pub fn close(subscription_id: SubscriptionId) -> Self {
        Self::Close { subscription_id }
    }

    #[must_use]
    pub fn publish(event: VerifiedEvent) -> Self {
        Self::Event {
            subscription_id: None,
            event,
        }
    }

    #[must_use]
    pub fn deliver(subscription_id: SubscriptionId, event: VerifiedEvent) -> Self {
        Self::Event {
            subscription_id: Some(subscription_id),
            event,
        }
    }

    #[must_use]
    pub fn inv(
        subscription_ids: Vec<SubscriptionId>,
        event_id: EventId,
        event_kind: u16,
        payload_bytes: u32,
        hop_limit: u8,
    ) -> Self {
        Self::Inv {
            subscription_ids,
            event_id,
            event_kind,
            payload_bytes,
            hop_limit,
        }
    }

    #[must_use]
    pub fn want(event_id: EventId) -> Self {
        Self::Want { event_id }
    }
}

/// Encodes and decodes one transport-provided FIPS pubsub payload frame.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FipsPubsubWireCodec {
    max_frame_bytes: usize,
}

impl Default for FipsPubsubWireCodec {
    fn default() -> Self {
        Self {
            max_frame_bytes: DEFAULT_FIPS_PUBSUB_MAX_FRAME_BYTES,
        }
    }
}

impl FipsPubsubWireCodec {
    pub fn new(max_frame_bytes: usize) -> Result<Self> {
        if max_frame_bytes == 0 {
            return Err(PubsubError::Validation(
                "FIPS pubsub max frame bytes must be greater than zero".to_string(),
            ));
        }
        Ok(Self { max_frame_bytes })
    }

    #[must_use]
    pub const fn max_frame_bytes(self) -> usize {
        self.max_frame_bytes
    }

    pub fn encode_frame(&self, message: &FipsPubsubWireMessage) -> Result<Vec<u8>> {
        let json = match message {
            FipsPubsubWireMessage::Req {
                subscription_id,
                filters,
            } => ClientMessage::req(subscription_id.clone(), filters.clone()).as_json(),
            FipsPubsubWireMessage::Close { subscription_id } => {
                ClientMessage::close(subscription_id.clone()).as_json()
            }
            FipsPubsubWireMessage::Event {
                subscription_id: None,
                event,
            } => ClientMessage::event(event.as_event().clone()).as_json(),
            FipsPubsubWireMessage::Event {
                subscription_id: Some(subscription_id),
                event,
            } => RelayMessage::event(subscription_id.clone(), event.as_event().clone()).as_json(),
            FipsPubsubWireMessage::Inv {
                subscription_ids,
                event_id,
                event_kind,
                payload_bytes,
                hop_limit,
            } => json!([
                "INV",
                subscription_ids
                    .iter()
                    .map(ToString::to_string)
                    .collect::<Vec<_>>(),
                event_id.to_hex(),
                event_kind,
                payload_bytes,
                hop_limit,
            ])
            .to_string(),
            FipsPubsubWireMessage::Want { event_id } => {
                json!(["WANT", event_id.to_hex()]).to_string()
            }
        };
        let frame = json.into_bytes();
        self.check_frame_size(frame.len())?;
        Ok(frame)
    }

    pub fn decode_frame(&self, frame: &[u8]) -> Result<FipsPubsubWireMessage> {
        self.check_frame_size(frame.len())?;
        if frame.is_empty() {
            return Err(invalid_frame("frame is empty"));
        }

        let value: Value = serde_json::from_slice(frame)
            .map_err(|error| invalid_frame(format!("invalid JSON: {error}")))?;
        let (message_type, field_count) = {
            let fields = value
                .as_array()
                .ok_or_else(|| invalid_frame("message must be a JSON array"))?;
            let message_type = fields
                .first()
                .and_then(Value::as_str)
                .ok_or_else(|| invalid_frame("message type must be a string"))?;
            (message_type.to_string(), fields.len())
        };

        match (message_type.as_str(), field_count) {
            ("REQ", 3..) => decode_req(value),
            ("CLOSE", 2) => decode_close(value),
            ("EVENT", 2) => decode_published_event(value),
            ("EVENT", 3) => decode_delivered_event(value),
            ("INV", 6) => decode_inv(&value),
            ("WANT", 2) => decode_want(&value),
            ("REQ", _) => Err(invalid_frame("REQ requires an id and at least one filter")),
            ("CLOSE", _) => Err(invalid_frame("CLOSE requires exactly an id")),
            ("EVENT", _) => Err(invalid_frame(
                "EVENT requires an event and optional subscription id",
            )),
            ("INV", _) => Err(invalid_frame(
                "INV requires subscription ids, event id, kind, byte size, and hop limit",
            )),
            ("WANT", _) => Err(invalid_frame("WANT requires exactly an event id")),
            (other, _) => Err(invalid_frame(format!(
                "unsupported Nostr message type {other}"
            ))),
        }
    }

    fn check_frame_size(self, frame_bytes: usize) -> Result<()> {
        if frame_bytes > self.max_frame_bytes {
            return Err(invalid_frame(format!(
                "frame has {frame_bytes} bytes, limit is {}",
                self.max_frame_bytes
            )));
        }
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FipsPubsubInbound {
    pub message: FipsPubsubWireMessage,
    pub subscription_update: PubsubSubscriptionUpdate,
}

/// Bridges transport frames to verified events and bounded peer subscriptions.
#[derive(Debug, Clone)]
pub struct FipsPubsubWireAdapter {
    codec: FipsPubsubWireCodec,
    subscriptions: PubsubPeerSubscriptionStore,
}

impl Default for FipsPubsubWireAdapter {
    fn default() -> Self {
        Self::new(
            FipsPubsubWireCodec::default(),
            PubsubPeerSubscriptionStore::default(),
        )
    }
}

impl FipsPubsubWireAdapter {
    #[must_use]
    pub fn new(codec: FipsPubsubWireCodec, subscriptions: PubsubPeerSubscriptionStore) -> Self {
        Self {
            codec,
            subscriptions,
        }
    }

    #[must_use]
    pub const fn codec(&self) -> FipsPubsubWireCodec {
        self.codec
    }

    #[must_use]
    pub fn subscriptions(&self) -> &PubsubPeerSubscriptionStore {
        &self.subscriptions
    }

    /// Drop every subscription retained for a transport peer that disconnected.
    ///
    /// Transport integrations should call this after a connection is
    /// permanently removed so stale filters cannot consume routing state.
    pub fn disconnect_peer(&mut self, peer_id: &SourceId) -> Vec<PubsubPeerSubscription> {
        self.subscriptions.remove_peer(peer_id)
    }

    pub fn decode_inbound(&mut self, peer_id: SourceId, frame: &[u8]) -> Result<FipsPubsubInbound> {
        let message = self.codec.decode_frame(frame)?;
        let subscription_update = match &message {
            FipsPubsubWireMessage::Req {
                subscription_id,
                filters,
            } => {
                self.subscriptions.upsert_filters(
                    peer_id,
                    subscription_id.to_string(),
                    filters.clone(),
                )?;
                PubsubSubscriptionUpdate::Subscribed
            }
            FipsPubsubWireMessage::Close { subscription_id } => {
                self.subscriptions
                    .remove(&peer_id, &subscription_id.to_string());
                PubsubSubscriptionUpdate::Closed
            }
            FipsPubsubWireMessage::Event { .. }
            | FipsPubsubWireMessage::Inv { .. }
            | FipsPubsubWireMessage::Want { .. } => PubsubSubscriptionUpdate::Ignored,
        };
        Ok(FipsPubsubInbound {
            message,
            subscription_update,
        })
    }

    pub fn encode_outbound(&self, message: &FipsPubsubWireMessage) -> Result<Vec<u8>> {
        self.codec.encode_frame(message)
    }
}

fn decode_req(value: Value) -> Result<FipsPubsubWireMessage> {
    let message = ClientMessage::from_value(value).map_err(|error| message_error(&error))?;
    let ClientMessage::Req {
        subscription_id,
        filters,
    } = message
    else {
        return Err(invalid_frame("expected REQ message"));
    };
    Ok(FipsPubsubWireMessage::req(
        subscription_id.into_owned(),
        filters
            .into_iter()
            .map(std::borrow::Cow::into_owned)
            .collect(),
    ))
}

fn decode_close(value: Value) -> Result<FipsPubsubWireMessage> {
    let message = ClientMessage::from_value(value).map_err(|error| message_error(&error))?;
    let ClientMessage::Close(subscription_id) = message else {
        return Err(invalid_frame("expected CLOSE message"));
    };
    Ok(FipsPubsubWireMessage::close(subscription_id.into_owned()))
}

fn decode_published_event(value: Value) -> Result<FipsPubsubWireMessage> {
    let message = ClientMessage::from_value(value).map_err(|error| message_error(&error))?;
    let ClientMessage::Event(event) = message else {
        return Err(invalid_frame("expected client EVENT message"));
    };
    Ok(FipsPubsubWireMessage::publish(VerifiedEvent::try_from(
        event.into_owned(),
    )?))
}

fn decode_delivered_event(value: Value) -> Result<FipsPubsubWireMessage> {
    let message = RelayMessage::from_value(value).map_err(|error| message_error(&error))?;
    let RelayMessage::Event {
        subscription_id,
        event,
    } = message
    else {
        return Err(invalid_frame("expected subscription EVENT message"));
    };
    Ok(FipsPubsubWireMessage::deliver(
        subscription_id.into_owned(),
        VerifiedEvent::try_from(event.into_owned())?,
    ))
}

fn decode_inv(value: &Value) -> Result<FipsPubsubWireMessage> {
    let fields = value
        .as_array()
        .ok_or_else(|| invalid_frame("INV must be a JSON array"))?;
    let subscription_ids = decode_subscription_ids(fields, 1)?;
    let event_id = decode_event_id(fields, 2, "INV")?;
    let event_kind = decode_unsigned::<u16>(fields, 3, "INV event kind")?;
    let payload_bytes = decode_unsigned::<u32>(fields, 4, "INV payload bytes")?;
    let hop_limit = decode_unsigned::<u8>(fields, 5, "INV hop limit")?;
    if hop_limit == 0 {
        return Err(invalid_frame("INV hop limit must be greater than zero"));
    }
    Ok(FipsPubsubWireMessage::inv(
        subscription_ids,
        event_id,
        event_kind,
        payload_bytes,
        hop_limit,
    ))
}

fn decode_subscription_ids(fields: &[Value], index: usize) -> Result<Vec<SubscriptionId>> {
    let values = fields
        .get(index)
        .and_then(Value::as_array)
        .ok_or_else(|| invalid_frame("INV subscription ids must be an array"))?;
    if values.is_empty() {
        return Err(invalid_frame("INV requires at least one subscription id"));
    }
    let mut seen = HashSet::with_capacity(values.len());
    values
        .iter()
        .map(|value| {
            let id = value
                .as_str()
                .ok_or_else(|| invalid_frame("INV subscription id must be a string"))?;
            if !seen.insert(id) {
                return Err(invalid_frame("INV subscription ids must be unique"));
            }
            Ok(SubscriptionId::new(id))
        })
        .collect()
}

fn decode_want(value: &Value) -> Result<FipsPubsubWireMessage> {
    let fields = value
        .as_array()
        .ok_or_else(|| invalid_frame("WANT must be a JSON array"))?;
    Ok(FipsPubsubWireMessage::want(decode_event_id(
        fields, 1, "WANT",
    )?))
}

fn decode_event_id(fields: &[Value], index: usize, message_type: &str) -> Result<EventId> {
    let value = fields
        .get(index)
        .and_then(Value::as_str)
        .ok_or_else(|| invalid_frame(format!("{message_type} event id must be a string")))?;
    EventId::from_hex(value)
        .map_err(|error| invalid_frame(format!("{message_type} event id is invalid: {error}")))
}

fn decode_unsigned<T>(fields: &[Value], index: usize, name: &str) -> Result<T>
where
    T: TryFrom<u64>,
{
    let value = fields
        .get(index)
        .and_then(Value::as_u64)
        .ok_or_else(|| invalid_frame(format!("{name} must be an unsigned integer")))?;
    T::try_from(value).map_err(|_| invalid_frame(format!("{name} is out of range")))
}

fn message_error(error: &nostr::message::MessageHandleError) -> PubsubError {
    invalid_frame(error.to_string())
}

fn invalid_frame(message: impl Into<String>) -> PubsubError {
    PubsubError::Validation(format!("invalid FIPS pubsub frame: {}", message.into()))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn subscription_scoped_inv_and_want_round_trip() {
        let codec = FipsPubsubWireCodec::default();
        let subscription_id = SubscriptionId::new("live-feed");
        let event_id = EventId::from_byte_array([7; 32]);
        let inv = FipsPubsubWireMessage::inv(
            vec![subscription_id.clone(), SubscriptionId::new("alerts")],
            event_id,
            1,
            4_096,
            4,
        );
        let want = FipsPubsubWireMessage::want(event_id);

        assert_eq!(
            codec
                .decode_frame(&codec.encode_frame(&inv).unwrap())
                .unwrap(),
            inv
        );
        assert_eq!(
            codec
                .decode_frame(&codec.encode_frame(&want).unwrap())
                .unwrap(),
            want
        );
    }

    #[test]
    fn inv_rejects_empty_subscriptions_zero_hops_and_noncanonical_event_ids() {
        let codec = FipsPubsubWireCodec::default();
        assert!(
            codec
                .decode_frame(
                    br#"["INV",["live"],"0707070707070707070707070707070707070707070707070707070707070707",1,10,0]"#,
                )
                .is_err()
        );
        assert!(
            codec
                .decode_frame(
                    br#"["INV",[],"0707070707070707070707070707070707070707070707070707070707070707",1,10,4]"#,
                )
                .is_err()
        );
        assert!(
            codec
                .decode_frame(br#"["WANT","not-an-event-id"]"#)
                .is_err()
        );
    }
}