mdk-core 0.8.0

A simplified interface to build secure messaging apps on nostr with MLS.
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
use std::collections::BTreeMap;
use std::collections::HashSet;
use std::ops::Range;

use base64::Engine;
use nostr::nips::nip44;
use nostr::{
    Event, EventBuilder, JsonUtil, Keys, Kind, PublicKey, Tag, TagKind, Timestamp, UnsignedEvent,
};

use super::{
    BASE64_ENCODING, ENCODING_TAG_NAME, ENCRYPTED_TOKEN_LEN, MAX_NOTIFICATION_REQUEST_TOKENS,
    Mip05Error, NOTIFICATION_REQUEST_KIND, NOTIFICATION_REQUEST_VERSION, NotificationEventBatch,
    NotificationRequest, TokenTag, VERSION_TAG_NAME,
};

// NIP-59 recommends randomizing wrapper timestamps across a two-day window.
const RANGE_RANDOM_TIMESTAMP_TWEAK: Range<u64> = 0..172800;

/// Build an unsigned `kind:446` MIP-05 notification request rumor.
///
/// The `pubkey` should be a fresh ephemeral key for the request. Callers that
/// want MDK to handle the ephemeral-key lifecycle should prefer
/// [`build_notification_batches`].
pub fn build_notification_request_rumor(
    pubkey: PublicKey,
    created_at: Timestamp,
    tokens: Vec<super::EncryptedToken>,
) -> Result<UnsignedEvent, Mip05Error> {
    if tokens.is_empty() {
        return Err(Mip05Error::NotificationRequestMustIncludeToken);
    }

    let mut content_bytes = Vec::with_capacity(tokens.len() * ENCRYPTED_TOKEN_LEN);
    for token in &tokens {
        content_bytes.extend_from_slice(token.as_bytes());
    }

    let mut rumor = UnsignedEvent::new(
        pubkey,
        created_at,
        Kind::from(NOTIFICATION_REQUEST_KIND),
        [
            Tag::custom(
                TagKind::Custom(VERSION_TAG_NAME.into()),
                [NOTIFICATION_REQUEST_VERSION],
            ),
            Tag::custom(TagKind::Custom(ENCODING_TAG_NAME.into()), [BASE64_ENCODING]),
        ],
        base64::engine::general_purpose::STANDARD.encode(content_bytes),
    );
    rumor.ensure_id();
    Ok(rumor)
}

/// Parse a typed `kind:446` notification request rumor.
pub fn parse_notification_request_rumor(
    event: &UnsignedEvent,
) -> Result<NotificationRequest, Mip05Error> {
    if event.kind != Kind::from(NOTIFICATION_REQUEST_KIND) {
        return Err(Mip05Error::UnexpectedRumorKind);
    }

    let mut version_tag_seen = false;
    let mut encoding_tag_seen = false;

    for tag in event.tags.iter() {
        let values = tag.as_slice();
        match values.first().map(String::as_str) {
            Some(VERSION_TAG_NAME) => {
                if version_tag_seen {
                    return Err(Mip05Error::DuplicateNotificationRequestVersionTag);
                }
                if values.len() != 2 {
                    return Err(Mip05Error::InvalidNotificationRequestVersionTag);
                }
                if values.get(1).map(String::as_str) != Some(NOTIFICATION_REQUEST_VERSION) {
                    return Err(Mip05Error::InvalidNotificationRequestVersionTag);
                }
                version_tag_seen = true;
            }
            Some(ENCODING_TAG_NAME) => {
                if encoding_tag_seen {
                    return Err(Mip05Error::DuplicateNotificationRequestEncodingTag);
                }
                if values.len() != 2 {
                    return Err(Mip05Error::InvalidNotificationRequestEncodingTag);
                }
                if values.get(1).map(String::as_str) != Some(BASE64_ENCODING) {
                    return Err(Mip05Error::InvalidNotificationRequestEncodingTag);
                }
                encoding_tag_seen = true;
            }
            // Intentionally reject unknown tags to match the current MIP-05 draft exactly.
            _ => return Err(Mip05Error::UnsupportedNotificationRequestTags),
        }
    }

    if !version_tag_seen {
        return Err(Mip05Error::MissingNotificationRequestVersionTag);
    }
    if !encoding_tag_seen {
        return Err(Mip05Error::MissingNotificationRequestEncodingTag);
    }

    let content = base64::engine::general_purpose::STANDARD
        .decode(&event.content)
        .map_err(|_| Mip05Error::InvalidNotificationRequestBase64)?;

    if content.is_empty() {
        return Err(Mip05Error::NotificationRequestMustIncludeToken);
    }
    if content.len() % ENCRYPTED_TOKEN_LEN != 0 {
        return Err(Mip05Error::InvalidNotificationRequestContentLength);
    }

    let tokens = content
        .chunks_exact(ENCRYPTED_TOKEN_LEN)
        .map(super::EncryptedToken::from_slice)
        .collect::<Result<Vec<_>, _>>()?;

    Ok(NotificationRequest { tokens })
}

/// Build ready-to-publish NIP-59 gift-wrapped `kind:446` notification requests.
///
/// Input token tags are grouped by notification server, relay hints are preserved
/// per server, and each server batch is chunked into requests of at most 100 tokens.
pub fn build_notification_batches(
    tokens: Vec<TokenTag>,
) -> Result<Vec<NotificationEventBatch>, Mip05Error> {
    if tokens.is_empty() {
        return Err(Mip05Error::NotificationRequestMustIncludeToken);
    }

    let grouped_tokens = group_tokens_by_server(tokens);

    grouped_tokens
        .into_iter()
        .map(|(server_pubkey, server_tokens)| {
            build_notification_batch_for_server(server_pubkey, server_tokens)
        })
        .collect()
}

fn group_tokens_by_server(tokens: Vec<TokenTag>) -> BTreeMap<PublicKey, Vec<TokenTag>> {
    let mut grouped_tokens: BTreeMap<PublicKey, Vec<TokenTag>> = BTreeMap::new();

    for token in tokens {
        grouped_tokens
            .entry(token.server_pubkey)
            .or_default()
            .push(token);
    }

    grouped_tokens
}

fn build_notification_batch_for_server(
    server_pubkey: PublicKey,
    server_tokens: Vec<TokenTag>,
) -> Result<NotificationEventBatch, Mip05Error> {
    validate_unique_encrypted_tokens(&server_tokens)?;
    let relay_hints = collect_relay_hints(&server_tokens);
    let events = server_tokens
        .chunks(MAX_NOTIFICATION_REQUEST_TOKENS)
        .map(|chunk| {
            let encrypted_tokens = chunk
                .iter()
                .map(|token| token.encrypted_token.clone())
                .collect::<Vec<_>>();
            build_notification_event_chunk(&server_pubkey, encrypted_tokens)
        })
        .collect::<Result<Vec<_>, _>>()?;

    Ok(NotificationEventBatch {
        server_pubkey,
        relay_hints,
        events,
    })
}

fn validate_unique_encrypted_tokens(tokens: &[TokenTag]) -> Result<(), Mip05Error> {
    let mut unique_tokens = HashSet::with_capacity(tokens.len());

    for token in tokens {
        if !unique_tokens.insert(token.encrypted_token.clone()) {
            return Err(Mip05Error::DuplicateEncryptedToken);
        }
    }

    Ok(())
}

fn collect_relay_hints(tokens: &[TokenTag]) -> Vec<nostr::RelayUrl> {
    let mut relay_hints = Vec::new();

    for token in tokens {
        if !relay_hints.contains(&token.relay_hint) {
            relay_hints.push(token.relay_hint.clone());
        }
    }

    relay_hints
}

fn build_notification_event_chunk(
    server_pubkey: &PublicKey,
    encrypted_tokens: Vec<super::EncryptedToken>,
) -> Result<Event, Mip05Error> {
    let sender_keys = Keys::generate();
    let rumor = build_notification_request_rumor(
        sender_keys.public_key(),
        Timestamp::now(),
        encrypted_tokens,
    )?;
    let seal = build_notification_request_seal(&sender_keys, server_pubkey, rumor)?;
    EventBuilder::gift_wrap_from_seal(server_pubkey, &seal, []).map_err(|e| {
        tracing::warn!(
            target: "mdk_core::mip05::notifications",
            error = %e,
            "Failed to gift-wrap notification request"
        );
        Mip05Error::NotificationRequestGiftWrapFailed
    })
}

fn build_notification_request_seal(
    sender_keys: &Keys,
    server_pubkey: &PublicKey,
    rumor: UnsignedEvent,
) -> Result<Event, Mip05Error> {
    let content = nip44::encrypt(
        sender_keys.secret_key(),
        server_pubkey,
        rumor.as_json(),
        nip44::Version::default(),
    )
    .map_err(|e| {
        tracing::warn!(
            target: "mdk_core::mip05::notifications",
            error = %e,
            "Failed to encrypt notification request"
        );
        Mip05Error::NotificationRequestEncryptionFailed
    })?;

    EventBuilder::new(Kind::Seal, content)
        .custom_created_at(Timestamp::tweaked(RANGE_RANDOM_TIMESTAMP_TWEAK))
        .sign_with_keys(sender_keys)
        .map_err(|e| {
            tracing::warn!(
                target: "mdk_core::mip05::notifications",
                error = %e,
                "Failed to sign notification request seal"
            );
            Mip05Error::NotificationRequestSealFailed
        })
}

#[cfg(test)]
mod tests {
    use nostr::TagStandard;
    use nostr::nips::nip59;

    use super::*;

    fn make_token_tag(server_pubkey: PublicKey, relay_hint: &str, byte: u8) -> TokenTag {
        TokenTag {
            encrypted_token: super::super::EncryptedToken::from([byte; ENCRYPTED_TOKEN_LEN]),
            server_pubkey,
            relay_hint: nostr::RelayUrl::parse(relay_hint).unwrap(),
        }
    }

    #[test]
    fn test_build_and_parse_notification_request_rumor() {
        let sender_keys = Keys::generate();
        let rumor = build_notification_request_rumor(
            sender_keys.public_key(),
            Timestamp::from(123u64),
            vec![
                super::super::EncryptedToken::from([1u8; ENCRYPTED_TOKEN_LEN]),
                super::super::EncryptedToken::from([2u8; ENCRYPTED_TOKEN_LEN]),
            ],
        )
        .unwrap();

        assert_eq!(rumor.kind, Kind::from(NOTIFICATION_REQUEST_KIND));
        let parsed = parse_notification_request_rumor(&rumor).unwrap();
        assert_eq!(parsed.tokens.len(), 2);
        assert_eq!(parsed.tokens[0].as_bytes(), &[1u8; ENCRYPTED_TOKEN_LEN]);
        assert_eq!(parsed.tokens[1].as_bytes(), &[2u8; ENCRYPTED_TOKEN_LEN]);
    }

    #[test]
    fn test_parse_notification_request_rejects_invalid_content_length() {
        let mut rumor = UnsignedEvent::new(
            Keys::generate().public_key(),
            Timestamp::from(123u64),
            Kind::from(NOTIFICATION_REQUEST_KIND),
            [
                Tag::custom(
                    TagKind::Custom(VERSION_TAG_NAME.into()),
                    [NOTIFICATION_REQUEST_VERSION],
                ),
                Tag::custom(TagKind::Custom(ENCODING_TAG_NAME.into()), [BASE64_ENCODING]),
            ],
            base64::engine::general_purpose::STANDARD.encode(vec![7u8; ENCRYPTED_TOKEN_LEN - 1]),
        );
        rumor.ensure_id();

        assert_eq!(
            parse_notification_request_rumor(&rumor).unwrap_err(),
            Mip05Error::InvalidNotificationRequestContentLength
        );
    }

    #[test]
    fn test_parse_notification_request_rejects_duplicate_version_tag() {
        let mut rumor = UnsignedEvent::new(
            Keys::generate().public_key(),
            Timestamp::from(123u64),
            Kind::from(NOTIFICATION_REQUEST_KIND),
            [
                Tag::custom(
                    TagKind::Custom(VERSION_TAG_NAME.into()),
                    [NOTIFICATION_REQUEST_VERSION],
                ),
                Tag::custom(
                    TagKind::Custom(VERSION_TAG_NAME.into()),
                    [NOTIFICATION_REQUEST_VERSION],
                ),
                Tag::custom(TagKind::Custom(ENCODING_TAG_NAME.into()), [BASE64_ENCODING]),
            ],
            base64::engine::general_purpose::STANDARD.encode([1u8; ENCRYPTED_TOKEN_LEN]),
        );
        rumor.ensure_id();

        assert_eq!(
            parse_notification_request_rumor(&rumor).unwrap_err(),
            Mip05Error::DuplicateNotificationRequestVersionTag
        );
    }

    #[test]
    fn test_parse_notification_request_rejects_extra_tag_values() {
        let mut rumor = UnsignedEvent::new(
            Keys::generate().public_key(),
            Timestamp::from(123u64),
            Kind::from(NOTIFICATION_REQUEST_KIND),
            [
                Tag::custom(
                    TagKind::Custom(VERSION_TAG_NAME.into()),
                    [NOTIFICATION_REQUEST_VERSION, "extra"],
                ),
                Tag::custom(TagKind::Custom(ENCODING_TAG_NAME.into()), [BASE64_ENCODING]),
            ],
            base64::engine::general_purpose::STANDARD.encode([1u8; ENCRYPTED_TOKEN_LEN]),
        );
        rumor.ensure_id();

        assert_eq!(
            parse_notification_request_rumor(&rumor).unwrap_err(),
            Mip05Error::InvalidNotificationRequestVersionTag
        );
    }

    #[test]
    fn test_build_notification_batches_rejects_empty_input() {
        assert_eq!(
            build_notification_batches(vec![]).unwrap_err(),
            Mip05Error::NotificationRequestMustIncludeToken
        );
    }

    #[test]
    fn test_build_notification_batches_rejects_duplicate_tokens_per_server() {
        let server_keys = Keys::generate();
        let token = make_token_tag(server_keys.public_key(), "wss://relay.example.com", 7);

        assert_eq!(
            build_notification_batches(vec![token.clone(), token]).unwrap_err(),
            Mip05Error::DuplicateEncryptedToken
        );
    }

    #[test]
    fn test_build_notification_batches_chunks_tokens_per_server() {
        let server_keys = Keys::generate();
        // Generate more tokens than MAX_NOTIFICATION_REQUEST_TOKENS to verify chunking.
        let count = MAX_NOTIFICATION_REQUEST_TOKENS + 1;
        let tokens = (0..count)
            .map(|index| {
                make_token_tag(
                    server_keys.public_key(),
                    "wss://relay.example.com",
                    index as u8,
                )
            })
            .collect();

        let batches = build_notification_batches(tokens).unwrap();

        assert_eq!(batches.len(), 1);
        assert_eq!(batches[0].server_pubkey, server_keys.public_key());
        assert_eq!(batches[0].events.len(), 2);
        assert_eq!(batches[0].relay_hints.len(), 1);
    }

    #[test]
    fn test_build_notification_batches_groups_by_server_and_collects_relay_hints() {
        let first_server = Keys::generate();
        let second_server = Keys::generate();
        let tokens = vec![
            make_token_tag(first_server.public_key(), "wss://relay.one.example", 1),
            make_token_tag(first_server.public_key(), "wss://relay.two.example", 2),
            make_token_tag(first_server.public_key(), "wss://relay.one.example", 3),
            make_token_tag(second_server.public_key(), "wss://relay.three.example", 4),
        ];

        let batches = build_notification_batches(tokens).unwrap();
        let first_batch = batches
            .iter()
            .find(|batch| batch.server_pubkey == first_server.public_key())
            .unwrap();
        let second_batch = batches
            .iter()
            .find(|batch| batch.server_pubkey == second_server.public_key())
            .unwrap();

        assert_eq!(batches.len(), 2);
        assert_eq!(first_batch.relay_hints.len(), 2);
        assert_eq!(second_batch.relay_hints.len(), 1);
        assert_eq!(first_batch.events.len(), 1);
        assert_eq!(second_batch.events.len(), 1);
    }

    #[tokio::test]
    async fn test_build_notification_batches_roundtrip_with_gift_wrap() {
        let server_keys = Keys::generate();
        let tokens = vec![
            make_token_tag(server_keys.public_key(), "wss://relay.one.example", 9),
            make_token_tag(server_keys.public_key(), "wss://relay.two.example", 10),
        ];

        let batches = build_notification_batches(tokens.clone()).unwrap();

        assert_eq!(batches.len(), 1);
        assert_eq!(batches[0].events.len(), 1);
        assert_eq!(batches[0].server_pubkey, server_keys.public_key());
        assert_eq!(batches[0].events[0].kind, Kind::GiftWrap);

        let receiver_tag = batches[0].events[0]
            .tags
            .iter()
            .find_map(|tag| match tag.as_standardized() {
                Some(TagStandard::PublicKey {
                    public_key,
                    relay_url: None,
                    alias: None,
                    uppercase: false,
                }) => Some(*public_key),
                _ => None,
            })
            .unwrap();
        assert_eq!(receiver_tag, server_keys.public_key());

        let unwrapped = nip59::extract_rumor(&server_keys, &batches[0].events[0])
            .await
            .unwrap();
        let parsed = parse_notification_request_rumor(&unwrapped.rumor).unwrap();

        assert_eq!(
            parsed.tokens,
            tokens
                .iter()
                .map(|token| token.encrypted_token.clone())
                .collect::<Vec<_>>()
        );
        assert_eq!(unwrapped.rumor.pubkey, unwrapped.sender);
    }
}