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
//! Client → relay messages (NIP-01 + NIP-42 AUTH + NIP-45 COUNT + NIP-77).
use alloc::borrow::Cow;
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;
use serde::de::{Error as DeError, SeqAccess, Visitor};
use serde::ser::SerializeSeq;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use super::{
Error, Result, SubscriptionId, end, event_item, filter_item, parse_neg_hex, read_type,
string_item, sub_id,
};
use crate::event::{Event, canonical};
use crate::filter::Filter;
/// Client → relay messages (NIP-01 + NIP-42 AUTH + NIP-45 COUNT + NIP-77).
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ClientMessage<'a> {
/// `["EVENT", <event>]` — publish a signed event.
Event(Cow<'a, Event>),
/// `["REQ", <id>, <filter>, ...]` — open a subscription.
Req {
/// Subscription id.
subscription_id: Cow<'a, SubscriptionId>,
/// At least one filter on the wire; `encode` of an empty slice emits
/// `["REQ","<id>"]` unchanged.
filters: Cow<'a, [Filter]>,
},
/// `["COUNT", <id>, <filter>, ...]` — a NIP-45 count request.
Count {
/// Subscription id.
subscription_id: Cow<'a, SubscriptionId>,
/// Same rules as [`ClientMessage::Req::filters`].
filters: Cow<'a, [Filter]>,
},
/// `["CLOSE", <id>]` — close a subscription.
Close(Cow<'a, SubscriptionId>),
/// `["AUTH", <event>]` — a NIP-42 kind-22242 auth event.
Auth(Cow<'a, Event>),
/// `["NEG-OPEN", <id>, <filter>, <hex>]` — open a NIP-77 sync.
NegOpen {
/// Subscription id.
subscription_id: Cow<'a, SubscriptionId>,
/// The sync filter.
filter: Cow<'a, Filter>,
/// The initial negentropy message (non-empty, even-length hex).
message: Cow<'a, str>,
},
/// `["NEG-MSG", <id>, <hex>]` — continue a NIP-77 sync.
NegMsg {
/// Subscription id.
subscription_id: Cow<'a, SubscriptionId>,
/// The negentropy message (non-empty, even-length hex).
message: Cow<'a, str>,
},
/// `["NEG-CLOSE", <id>]` — end a NIP-77 sync.
NegClose(Cow<'a, SubscriptionId>),
}
const TYPES: &[&str] = &[
"EVENT",
"REQ",
"COUNT",
"CLOSE",
"AUTH",
"NEG-OPEN",
"NEG-MSG",
"NEG-CLOSE",
];
impl ClientMessage<'_> {
/// The canonical NIP-01 JSON wire form.
#[must_use]
pub fn to_json(&self) -> String {
let mut out = String::new();
match self {
Self::Event(event) => {
out.push_str("[\"EVENT\",");
canonical::write_signed(event, &mut out);
out.push(']');
}
Self::Req {
subscription_id,
filters,
}
| Self::Count {
subscription_id,
filters,
} => {
let kind = if matches!(self, Self::Req { .. }) {
"REQ"
} else {
"COUNT"
};
out.push_str("[\"");
out.push_str(kind);
out.push_str("\",");
canonical::push_json_string(subscription_id.as_str(), &mut out);
for filter in filters.iter() {
out.push(',');
filter.write_canonical(&mut out);
}
out.push(']');
}
Self::Close(subscription_id) => {
out.push_str("[\"CLOSE\",");
canonical::push_json_string(subscription_id.as_str(), &mut out);
out.push(']');
}
Self::Auth(event) => {
out.push_str("[\"AUTH\",");
canonical::write_signed(event, &mut out);
out.push(']');
}
Self::NegOpen {
subscription_id,
filter,
message,
} => {
out.push_str("[\"NEG-OPEN\",");
canonical::push_json_string(subscription_id.as_str(), &mut out);
out.push(',');
filter.write_canonical(&mut out);
out.push(',');
canonical::push_json_string(message, &mut out);
out.push(']');
}
Self::NegMsg {
subscription_id,
message,
} => {
out.push_str("[\"NEG-MSG\",");
canonical::push_json_string(subscription_id.as_str(), &mut out);
out.push(',');
canonical::push_json_string(message, &mut out);
out.push(']');
}
Self::NegClose(subscription_id) => {
out.push_str("[\"NEG-CLOSE\",");
canonical::push_json_string(subscription_id.as_str(), &mut out);
out.push(']');
}
}
out
}
/// Parses a client → relay JSON message.
///
/// # Errors
///
/// [`Error::InvalidJson`] when the text is not JSON,
/// [`Error::NotAMessageArray`] when it is not a non-empty array starting
/// with the type string, [`Error::UnknownType`] for an unrecognized
/// type, [`Error::InvalidMessage`] for wrong arity or field types,
/// [`Error::InvalidPayload`] when an embedded event or filter fails
/// wire validation, [`Error::InvalidSubscriptionId`] for a bad
/// subscription id, [`Error::ObsoleteNegOpen`] for the 5-element
/// `NEG-OPEN`, and [`Error::InvalidNegMessage`] for malformed NEG hex.
pub fn from_json(raw: &str) -> Result<ClientMessage<'static>> {
serde_json::from_str(raw).map_err(|e| Error::InvalidJson {
source: crate::json::JsonError::from(e),
})
}
/// Converts every borrowed field to owned, giving a `'static` message.
#[must_use]
pub fn into_owned(self) -> ClientMessage<'static> {
match self {
Self::Event(event) => ClientMessage::Event(Cow::Owned(event.into_owned())),
Self::Req {
subscription_id,
filters,
} => ClientMessage::Req {
subscription_id: Cow::Owned(subscription_id.into_owned()),
filters: Cow::Owned(filters.into_owned()),
},
Self::Count {
subscription_id,
filters,
} => ClientMessage::Count {
subscription_id: Cow::Owned(subscription_id.into_owned()),
filters: Cow::Owned(filters.into_owned()),
},
Self::Close(id) => ClientMessage::Close(Cow::Owned(id.into_owned())),
Self::Auth(event) => ClientMessage::Auth(Cow::Owned(event.into_owned())),
Self::NegOpen {
subscription_id,
filter,
message,
} => ClientMessage::NegOpen {
subscription_id: Cow::Owned(subscription_id.into_owned()),
filter: Cow::Owned(filter.into_owned()),
message: Cow::Owned(message.into_owned()),
},
Self::NegMsg {
subscription_id,
message,
} => ClientMessage::NegMsg {
subscription_id: Cow::Owned(subscription_id.into_owned()),
message: Cow::Owned(message.into_owned()),
},
Self::NegClose(id) => ClientMessage::NegClose(Cow::Owned(id.into_owned())),
}
}
}
impl Serialize for ClientMessage<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
Self::Event(event) => {
let mut seq = serializer.serialize_seq(Some(2))?;
seq.serialize_element("EVENT")?;
seq.serialize_element(&**event)?;
seq.end()
}
Self::Req {
subscription_id,
filters,
}
| Self::Count {
subscription_id,
filters,
} => {
let ty = if matches!(self, Self::Req { .. }) {
"REQ"
} else {
"COUNT"
};
let mut seq = serializer.serialize_seq(Some(2 + filters.len()))?;
seq.serialize_element(ty)?;
seq.serialize_element(&**subscription_id)?;
for filter in filters.iter() {
seq.serialize_element(filter)?;
}
seq.end()
}
Self::Close(subscription_id) => {
let mut seq = serializer.serialize_seq(Some(2))?;
seq.serialize_element("CLOSE")?;
seq.serialize_element(&**subscription_id)?;
seq.end()
}
Self::Auth(event) => {
let mut seq = serializer.serialize_seq(Some(2))?;
seq.serialize_element("AUTH")?;
seq.serialize_element(&**event)?;
seq.end()
}
Self::NegOpen {
subscription_id,
filter,
message,
} => {
let mut seq = serializer.serialize_seq(Some(4))?;
seq.serialize_element("NEG-OPEN")?;
seq.serialize_element(&**subscription_id)?;
seq.serialize_element(&**filter)?;
seq.serialize_element(&**message)?;
seq.end()
}
Self::NegMsg {
subscription_id,
message,
} => {
let mut seq = serializer.serialize_seq(Some(3))?;
seq.serialize_element("NEG-MSG")?;
seq.serialize_element(&**subscription_id)?;
seq.serialize_element(&**message)?;
seq.end()
}
Self::NegClose(subscription_id) => {
let mut seq = serializer.serialize_seq(Some(2))?;
seq.serialize_element("NEG-CLOSE")?;
seq.serialize_element(&**subscription_id)?;
seq.end()
}
}
}
}
impl<'de> Deserialize<'de> for ClientMessage<'static> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct MessageVisitor;
impl<'de> Visitor<'de> for MessageVisitor {
type Value = ClientMessage<'static>;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("a client message array")
}
#[allow(
clippy::excessive_nesting,
reason = "a message is a labelled seq of variants"
)]
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
let ty = read_type(&mut seq, TYPES)?;
match ty {
"EVENT" => {
let event = event_item(&mut seq, ty)?;
end(&mut seq, ty)?;
Ok(ClientMessage::Event(Cow::Owned(event)))
}
"REQ" | "COUNT" => {
let subscription_id = sub_id(&mut seq, ty)?;
let mut filters = Vec::new();
while let Some(filter) = seq.next_element::<Filter>()? {
filters.push(filter);
}
if filters.is_empty() {
return Err(DeError::custom(Error::InvalidMessage { ty }));
}
let filters = Cow::Owned(filters);
Ok(if ty == "REQ" {
ClientMessage::Req {
subscription_id: Cow::Owned(subscription_id),
filters,
}
} else {
ClientMessage::Count {
subscription_id: Cow::Owned(subscription_id),
filters,
}
})
}
"CLOSE" => {
let id = sub_id(&mut seq, ty)?;
end(&mut seq, ty)?;
Ok(ClientMessage::Close(Cow::Owned(id)))
}
"AUTH" => {
let event = event_item(&mut seq, ty)?;
end(&mut seq, ty)?;
Ok(ClientMessage::Auth(Cow::Owned(event)))
}
"NEG-OPEN" => {
let subscription_id = sub_id(&mut seq, ty)?;
let filter = filter_item(&mut seq, ty)?;
let message =
parse_neg_hex(&string_item(&mut seq, ty)?).map_err(DeError::custom)?;
if seq.next_element::<serde::de::IgnoredAny>()?.is_some() {
return Err(DeError::custom(Error::ObsoleteNegOpen));
}
Ok(ClientMessage::NegOpen {
subscription_id: Cow::Owned(subscription_id),
filter: Cow::Owned(filter),
message: Cow::Owned(message),
})
}
"NEG-MSG" => {
let subscription_id = sub_id(&mut seq, ty)?;
let message =
parse_neg_hex(&string_item(&mut seq, ty)?).map_err(DeError::custom)?;
end(&mut seq, ty)?;
Ok(ClientMessage::NegMsg {
subscription_id: Cow::Owned(subscription_id),
message: Cow::Owned(message),
})
}
"NEG-CLOSE" => {
let id = sub_id(&mut seq, ty)?;
end(&mut seq, ty)?;
Ok(ClientMessage::NegClose(Cow::Owned(id)))
}
_ => Err(DeError::custom(Error::InvalidMessage { ty })),
}
}
}
deserializer.deserialize_seq(MessageVisitor)
}
}