subduction_ephemeral 0.5.1

Ephemeral (non-persisted) messaging for Subduction
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
//! Ephemeral message types and wire encoding.
//!
//! # Wire Layout
//!
//! All ephemeral messages share the `SUE\x00` schema. Byte 4 (the
//! [`DISCRIMINANT`] for `Ephemeral`, or a tag byte for unsigned control
//! messages) distinguishes them:
//!
//! ## `Ephemeral` variant (discriminant `0x00`)
//!
//! The wire bytes _are_ the complete [`Signed<EphemeralPayload>`] —
//! including schema, discriminant, issuer, fields, and signature.
//! No stripping or reconstruction is performed; the bytes are passed
//! verbatim to [`Signed::try_decode`].
//!
//! ```text
//! ╔════════╦══════╦════════╦════════╦═══════╦═══════════╦════════════╦═════════╦═══════════╗
//! ║ Schema ║ Disc ║ Issuer ║   ID   ║ Nonce ║ Timestamp ║ PayloadLen ║ Payload ║ Signature ║
//! ║   4B   ║  1B  ║  32B   ║  32B   ║  8B   ║    8B     ║  bijou64   ║  var    ║   64B     ║
//! ╚════════╩══════╩════════╩════════╩═══════╩═══════════╩════════════╩═════════╩═══════════╝
//!  ↑──────────────────────── signed region ───────────────────────────────────↑
//! ```
//!
//! ## Control messages (tags `0x01`–`0x03`)
//!
//! ```text
//! ╔════════╦═════╦═════════════════════════════╗
//! ║ Schema ║ Tag ║         Payload             ║
//! ║   4B   ║ 1B  ║         variable            ║
//! ╚════════╩═════╩═════════════════════════════╝
//! ```
//!
//! | Tag    | Variant              | Payload                          |
//! |--------|----------------------|----------------------------------|
//! | `0x01` | Subscribe            | count(2) + topics(32 each)       |
//! | `0x02` | Unsubscribe          | count(2) + topics(32 each)       |
//! | `0x03` | SubscribeRejected    | count(2) + topics(32 each)       |
//!
//! [`DISCRIMINANT`]: sedimentree_core::codec::schema::Schema::DISCRIMINANT
//! [`Signed<EphemeralPayload>`]: subduction_crypto::signed::Signed
//! [`Signed::try_decode`]: subduction_crypto::signed::Signed::try_decode

use alloc::{boxed::Box, vec::Vec};

use nonempty::NonEmpty;
use sedimentree_core::codec::{
    decode::{self, Decode, DecodeFields},
    encode::{self, Encode, EncodeFields},
    error::{DecodeError, InvalidEnumTag, InvalidSchema, SizeMismatch},
    schema::{self, Schema},
};
use subduction_core::timestamp::TimestampSeconds;
use subduction_crypto::signed::Signed;

use crate::topic::Topic;

/// Schema header for [`EphemeralMessage`] envelope: **SU**bduction **E**phemeral v0.
pub const EPHEMERAL_SCHEMA: [u8; 4] = *b"SUE\x00";

/// Header size for unsigned control messages: `schema(4) + tag(1)`.
const CONTROL_HEADER_SIZE: usize = 4 + 1;

mod tags {
    use super::EphemeralPayload;

    pub(super) const EPHEMERAL: u8 = EphemeralPayload::TAG;
    pub(super) const SUBSCRIBE: u8 = 0x01;
    pub(super) const UNSUBSCRIBE: u8 = 0x02;
    pub(super) const SUBSCRIBE_REJECTED: u8 = 0x03;
}

/// Minimum payload sizes _after_ the tag byte (for control messages only).
/// The `Ephemeral` variant validates size via [`Signed::try_decode`].
mod min_sizes {
    // count(2) + topic(32) — at least one topic required (NonEmpty)
    pub(super) const SUBSCRIBE: usize = 2 + 32;
    // count(2) + topic(32)
    pub(super) const UNSUBSCRIBE: usize = 2 + 32;
    // count(2) + topic(32)
    pub(super) const SUBSCRIBE_REJECTED: usize = 2 + 32;
}

// ── EphemeralPayload ────────────────────────────────────────────────────

/// Size of `EphemeralPayload` fields: `id(32) + nonce(8) + timestamp(8) + payload_len(bijou64 min=1)`.
const EPHEMERAL_PAYLOAD_MIN_FIELDS_SIZE: usize = 32 + 8 + 8 + 1;

/// Minimum size of a `Signed<EphemeralPayload>`:
/// schema(4) + discriminant(1) + issuer(32) + fields + signature(64).
const EPHEMERAL_PAYLOAD_MIN_SIGNED_SIZE: usize =
    4 + 1 + 32 + EPHEMERAL_PAYLOAD_MIN_FIELDS_SIZE + 64;

/// The signable payload of an ephemeral message.
///
/// This type implements [`Schema`], [`EncodeFields`], and [`DecodeFields`]
/// so it can be used with [`Signed<EphemeralPayload>`] for signing,
/// verification, and wire encoding.
///
/// The issuer (sender) is stored in the [`Signed`] wrapper, not here.
///
/// [`Signed<EphemeralPayload>`]: subduction_crypto::signed::Signed
/// [`Signed`]: subduction_crypto::signed::Signed
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EphemeralPayload {
    /// The topic this message is published to.
    pub id: Topic,
    /// Random nonce for deduplication.
    pub nonce: u64,
    /// UTC time at message creation (seconds since epoch).
    pub timestamp: TimestampSeconds,
    /// Opaque application payload.
    pub payload: Vec<u8>,
}

impl EphemeralPayload {
    /// Variant tag within the `SUE\x00` ephemeral protocol.
    pub const TAG: u8 = 0x00;
}

impl Schema for EphemeralPayload {
    const PREFIX: [u8; 2] = schema::SUBDUCTION_PREFIX;
    const TYPE_BYTE: u8 = b'E';
    const VERSION: u8 = 0;
    const DISCRIMINANT: Option<u8> = Some(EphemeralPayload::TAG);
    // SCHEMA = b"SUE\x00"
}

impl EncodeFields for EphemeralPayload {
    fn encode_fields(&self, buf: &mut Vec<u8>) {
        encode::array(self.id.as_bytes(), buf);
        encode::u64(self.nonce, buf);
        encode::u64(self.timestamp.as_secs(), buf);
        #[allow(clippy::cast_possible_truncation)]
        bijou64::encode(self.payload.len() as u64, buf);
        buf.extend_from_slice(&self.payload);
    }

    fn fields_size(&self) -> usize {
        32 + 8 + 8 + bijou64::encoded_len(self.payload.len() as u64) + self.payload.len()
    }
}

impl DecodeFields for EphemeralPayload {
    const MIN_SIGNED_SIZE: usize = EPHEMERAL_PAYLOAD_MIN_SIGNED_SIZE;

    fn try_decode_fields(buf: &[u8]) -> Result<(Self, usize), DecodeError> {
        if buf.len() < EPHEMERAL_PAYLOAD_MIN_FIELDS_SIZE {
            return Err(DecodeError::MessageTooShort {
                type_name: "EphemeralPayload",
                need: EPHEMERAL_PAYLOAD_MIN_FIELDS_SIZE,
                have: buf.len(),
            });
        }

        let mut offset = 0;

        let id_bytes: [u8; 32] = decode::array(buf, offset)?;
        offset += 32;
        let id = Topic::new(id_bytes);

        let nonce = decode::u64(buf, offset)?;
        offset += 8;

        let timestamp_secs = decode::u64(buf, offset)?;
        offset += 8;
        let timestamp = TimestampSeconds::new(timestamp_secs);

        let remaining = buf.get(offset..).ok_or(DecodeError::MessageTooShort {
            type_name: "EphemeralPayload payload_len",
            need: offset + 1,
            have: buf.len(),
        })?;
        let (payload_len, consumed) = bijou64::decode(remaining)
            .map_err(|kind| sedimentree_core::codec::error::Bijou64Error { offset, kind })?;
        offset += consumed;

        #[allow(clippy::cast_possible_truncation)]
        let payload_len = payload_len as usize;
        let payload = buf
            .get(offset..offset + payload_len)
            .ok_or(DecodeError::MessageTooShort {
                type_name: "EphemeralPayload payload data",
                need: offset + payload_len,
                have: buf.len(),
            })?
            .to_vec();
        offset += payload_len;

        Ok((
            Self {
                id,
                nonce,
                timestamp,
                payload,
            },
            offset,
        ))
    }
}

// ── EphemeralMessage ────────────────────────────────────────────────────

/// Wire message types for the ephemeral protocol.
///
/// These messages are scoped to the `SUE\x00` schema and are fully
/// independent of [`SyncMessage`]. Each variant maps to a tag byte
/// local to this schema.
///
/// [`SyncMessage`]: subduction_core::connection::message::SyncMessage
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EphemeralMessage {
    /// A signed ephemeral payload for a specific topic.
    ///
    /// Fire-and-forget delivery to all subscribers of the topic
    /// (minus the sender). The originator signs the message; relays
    /// forward it as-is.
    Ephemeral(Box<Signed<EphemeralPayload>>),

    /// Subscribe to ephemeral messages for the given topics.
    Subscribe {
        /// The topics to subscribe to.
        topics: NonEmpty<Topic>,
    },

    /// Unsubscribe from ephemeral messages for the given topics.
    Unsubscribe {
        /// The topics to unsubscribe from.
        topics: NonEmpty<Topic>,
    },

    /// Notification that some subscribe requests were rejected.
    ///
    /// Contains only the rejected topics. Accepted topics are implied
    /// by omission.
    SubscribeRejected {
        /// The topics that were rejected.
        topics: NonEmpty<Topic>,
    },
}

// ── Encode ──────────────────────────────────────────────────────────────

impl Encode for EphemeralMessage {
    fn encode(&self) -> Vec<u8> {
        match self {
            // The Signed<T> bytes ARE the wire message — schema + discriminant
            // + issuer + fields + signature. No outer envelope needed.
            Self::Ephemeral(signed) => signed.as_bytes().to_vec(),

            // Control messages: manual SUE\x00 + tag + payload.
            Self::Subscribe { topics } => {
                let mut buf = Vec::with_capacity(CONTROL_HEADER_SIZE + 2 + topics.len() * 32);
                buf.extend_from_slice(&EPHEMERAL_SCHEMA);
                buf.push(tags::SUBSCRIBE);
                encode_topic_list(&mut buf, topics);
                buf
            }
            Self::Unsubscribe { topics } => {
                let mut buf = Vec::with_capacity(CONTROL_HEADER_SIZE + 2 + topics.len() * 32);
                buf.extend_from_slice(&EPHEMERAL_SCHEMA);
                buf.push(tags::UNSUBSCRIBE);
                encode_topic_list(&mut buf, topics);
                buf
            }
            Self::SubscribeRejected { topics } => {
                let mut buf = Vec::with_capacity(CONTROL_HEADER_SIZE + 2 + topics.len() * 32);
                buf.extend_from_slice(&EPHEMERAL_SCHEMA);
                buf.push(tags::SUBSCRIBE_REJECTED);
                encode_topic_list(&mut buf, topics);
                buf
            }
        }
    }

    fn encoded_size(&self) -> usize {
        match self {
            Self::Ephemeral(signed) => signed.as_bytes().len(),
            Self::Subscribe { topics }
            | Self::Unsubscribe { topics }
            | Self::SubscribeRejected { topics } => CONTROL_HEADER_SIZE + 2 + topics.len() * 32,
        }
    }
}

fn encode_topic_list(buf: &mut Vec<u8>, topics: &NonEmpty<Topic>) {
    debug_assert!(
        u16::try_from(topics.len()).is_ok(),
        "topic list exceeds u16::MAX ({} topics)",
        topics.len()
    );
    #[allow(clippy::cast_possible_truncation)]
    buf.extend_from_slice(&(topics.len() as u16).to_be_bytes());
    for topic in topics {
        buf.extend_from_slice(topic.as_bytes());
    }
}

// ── Decode ──────────────────────────────────────────────────────────────

impl Decode for EphemeralMessage {
    const MIN_SIZE: usize = CONTROL_HEADER_SIZE;

    fn try_decode(buf: &[u8]) -> Result<Self, DecodeError> {
        decode_message(buf)
    }
}

fn decode_message(bytes: &[u8]) -> Result<EphemeralMessage, DecodeError> {
    if bytes.len() < CONTROL_HEADER_SIZE {
        return Err(DecodeError::MessageTooShort {
            type_name: "EphemeralMessage",
            need: CONTROL_HEADER_SIZE,
            have: bytes.len(),
        });
    }

    let schema: [u8; 4] =
        bytes
            .get(0..4)
            .and_then(|s| s.try_into().ok())
            .ok_or(DecodeError::MessageTooShort {
                type_name: "EphemeralMessage schema",
                need: 4,
                have: bytes.len(),
            })?;
    if schema != EPHEMERAL_SCHEMA {
        return Err(InvalidSchema {
            expected: EPHEMERAL_SCHEMA,
            got: schema,
        }
        .into());
    }

    // Byte 4 is either:
    // - A discriminant (0x00) for Signed<EphemeralPayload>
    // - A control message tag (0x01-0x03)
    let tag = *bytes.get(4).ok_or(DecodeError::MessageTooShort {
        type_name: "EphemeralMessage tag",
        need: CONTROL_HEADER_SIZE,
        have: bytes.len(),
    })?;

    match tag {
        tags::EPHEMERAL => {
            // The full bytes are a Signed<EphemeralPayload>.
            // The discriminant at byte 4 is validated by Signed::try_decode.
            let signed = Signed::<EphemeralPayload>::try_decode(bytes.to_vec())?;
            Ok(EphemeralMessage::Ephemeral(Box::new(signed)))
        }
        tags::SUBSCRIBE | tags::UNSUBSCRIBE | tags::SUBSCRIBE_REJECTED => {
            let payload = bytes
                .get(CONTROL_HEADER_SIZE..)
                .ok_or(DecodeError::MessageTooShort {
                    type_name: "EphemeralMessage control payload",
                    need: CONTROL_HEADER_SIZE + 1,
                    have: bytes.len(),
                })?;

            let (min_size, type_name) = match tag {
                tags::SUBSCRIBE => (min_sizes::SUBSCRIBE, "EphemeralSubscribe"),
                tags::UNSUBSCRIBE => (min_sizes::UNSUBSCRIBE, "EphemeralUnsubscribe"),
                tags::SUBSCRIBE_REJECTED => {
                    (min_sizes::SUBSCRIBE_REJECTED, "EphemeralSubscribeRejected")
                }
                _ => unreachable!(),
            };

            if payload.len() < min_size {
                return Err(DecodeError::MessageTooShort {
                    type_name,
                    need: CONTROL_HEADER_SIZE + min_size,
                    have: bytes.len(),
                });
            }

            let topics = decode_topic_list(payload)?;
            match tag {
                tags::SUBSCRIBE => Ok(EphemeralMessage::Subscribe { topics }),
                tags::UNSUBSCRIBE => Ok(EphemeralMessage::Unsubscribe { topics }),
                tags::SUBSCRIBE_REJECTED => Ok(EphemeralMessage::SubscribeRejected { topics }),
                _ => unreachable!(),
            }
        }
        _ => Err(InvalidEnumTag {
            tag,
            type_name: "EphemeralMessage",
        }
        .into()),
    }
}

fn decode_topic_list(payload: &[u8]) -> Result<NonEmpty<Topic>, DecodeError> {
    let mut offset = 0;

    let count = read_u16(payload, &mut offset)? as usize;
    if count == 0 {
        return Err(DecodeError::MessageTooShort {
            type_name: "EphemeralTopicList",
            need: 2 + 32, // count + at least one topic
            have: payload.len(),
        });
    }

    let first = Topic::new(read_array::<32>(payload, &mut offset)?);
    let mut rest = Vec::with_capacity(count - 1);
    for _ in 1..count {
        rest.push(Topic::new(read_array::<32>(payload, &mut offset)?));
    }

    // Reject trailing bytes — the payload should be exactly count(2) + count * topic(32).
    if offset != payload.len() {
        return Err(SizeMismatch {
            declared: offset,
            actual: payload.len(),
        }
        .into());
    }

    Ok(NonEmpty {
        head: first,
        tail: rest,
    })
}

// ── Decode helpers ──────────────────────────────────────────────────────

fn read_u16(buf: &[u8], offset: &mut usize) -> Result<u16, DecodeError> {
    let bytes: [u8; 2] = buf
        .get(*offset..*offset + 2)
        .and_then(|s| s.try_into().ok())
        .ok_or(DecodeError::MessageTooShort {
            type_name: "u16",
            need: *offset + 2,
            have: buf.len(),
        })?;
    *offset += 2;
    Ok(u16::from_be_bytes(bytes))
}

fn read_array<const N: usize>(buf: &[u8], offset: &mut usize) -> Result<[u8; N], DecodeError> {
    let bytes: [u8; N] = buf
        .get(*offset..*offset + N)
        .and_then(|s| s.try_into().ok())
        .ok_or(DecodeError::MessageTooShort {
            type_name: "array",
            need: *offset + N,
            have: buf.len(),
        })?;
    *offset += N;
    Ok(bytes)
}

#[cfg(test)]
#[allow(
    clippy::expect_used,
    clippy::unwrap_used,
    clippy::indexing_slicing,
    clippy::panic
)]
mod tests {
    use super::*;
    use subduction_core::peer::id::PeerId;

    /// Helper: create a signed ephemeral for roundtrip tests.
    /// Uses `MemorySigner` for a real signature.
    async fn make_signed_ephemeral(payload: Vec<u8>) -> EphemeralMessage {
        use subduction_crypto::signer::memory::MemorySigner;
        let signer = MemorySigner::from_bytes(&[0xAA; 32]);

        let ep = EphemeralPayload {
            id: Topic::new([0xBB; 32]),
            nonce: 0x1234_5678_9ABC_DEF0,
            timestamp: TimestampSeconds::new(1_700_000_000),
            payload,
        };

        let verified = Signed::seal::<future_form::Sendable, _>(&signer, ep).await;
        EphemeralMessage::Ephemeral(Box::new(verified.into_signed()))
    }

    #[tokio::test]
    async fn ephemeral_roundtrip() {
        let msg = make_signed_ephemeral(vec![1, 2, 3, 4, 5]).await;

        let encoded = msg.encode();
        let decoded = EphemeralMessage::try_decode(&encoded).expect("decode");

        assert_eq!(decoded, msg);
    }

    #[tokio::test]
    async fn empty_payload_roundtrip() {
        let msg = make_signed_ephemeral(vec![]).await;

        let encoded = msg.encode();
        let decoded = EphemeralMessage::try_decode(&encoded).expect("decode");

        assert_eq!(decoded, msg);
    }

    #[test]
    fn subscribe_roundtrip() {
        let mut topics = NonEmpty::new(Topic::new([0x01; 32]));
        topics.push(Topic::new([0x02; 32]));
        let msg = EphemeralMessage::Subscribe { topics };

        let encoded = msg.encode();
        let decoded = EphemeralMessage::try_decode(&encoded).expect("decode");

        assert_eq!(decoded, msg);
    }

    #[test]
    fn unsubscribe_roundtrip() {
        let topics = NonEmpty::new(Topic::new([0xFF; 32]));
        let msg = EphemeralMessage::Unsubscribe { topics };

        let encoded = msg.encode();
        let decoded = EphemeralMessage::try_decode(&encoded).expect("decode");

        assert_eq!(decoded, msg);
    }

    #[test]
    fn subscribe_rejected_roundtrip() {
        let topics = NonEmpty::new(Topic::new([0x42; 32]));
        let msg = EphemeralMessage::SubscribeRejected { topics };

        let encoded = msg.encode();
        let decoded = EphemeralMessage::try_decode(&encoded).expect("decode");

        assert_eq!(decoded, msg);
    }

    #[test]
    fn empty_topic_list_rejected() {
        // Manually construct: SUE\x00 || Subscribe tag || count=0
        let mut buf = Vec::new();
        buf.extend_from_slice(&EPHEMERAL_SCHEMA);
        buf.push(tags::SUBSCRIBE);
        buf.extend_from_slice(&0_u16.to_be_bytes());

        let err = EphemeralMessage::try_decode(&buf).unwrap_err();
        assert!(
            matches!(err, DecodeError::MessageTooShort { .. }),
            "expected MessageTooShort for empty topic list, got {err:?}"
        );
    }

    #[test]
    fn wrong_schema_rejected() {
        let topics = NonEmpty::new(Topic::new([0x01; 32]));
        let msg = EphemeralMessage::Subscribe { topics };
        let mut encoded = msg.encode();
        encoded[2] = b'M';

        let err = EphemeralMessage::try_decode(&encoded).unwrap_err();
        assert!(
            matches!(err, DecodeError::InvalidSchema(_)),
            "expected InvalidSchema, got {err:?}"
        );
    }

    #[test]
    fn invalid_tag_rejected() {
        let topics = NonEmpty::new(Topic::new([0x01; 32]));
        let msg = EphemeralMessage::Subscribe { topics };
        let mut encoded = msg.encode();
        encoded[4] = 0xFF; // tag byte is at position 4 (after schema)

        let err = EphemeralMessage::try_decode(&encoded).unwrap_err();
        assert!(
            matches!(err, DecodeError::InvalidEnumTag(_)),
            "expected InvalidEnumTag, got {err:?}"
        );
    }

    #[test]
    fn schema_matches_ephemeral_payload() {
        assert_eq!(EphemeralPayload::SCHEMA, EPHEMERAL_SCHEMA);
    }

    #[tokio::test]
    async fn signed_ephemeral_verifies() {
        let msg = make_signed_ephemeral(vec![0xDE, 0xAD]).await;

        let EphemeralMessage::Ephemeral(ref signed) = msg else {
            panic!("expected Ephemeral");
        };

        let verified = signed.try_verify();
        assert!(verified.is_ok(), "signature should verify");

        let payload = verified.expect("verified").into_payload();
        assert_eq!(payload.id, Topic::new([0xBB; 32]));
        assert_eq!(payload.nonce, 0x1234_5678_9ABC_DEF0);
        assert_eq!(payload.payload, vec![0xDE, 0xAD]);
    }

    #[tokio::test]
    async fn tampered_signature_fails_verify() {
        let msg = make_signed_ephemeral(vec![1, 2, 3]).await;

        let EphemeralMessage::Ephemeral(signed) = msg else {
            panic!("expected Ephemeral");
        };

        // Tamper with the wire bytes to invalidate the signature.
        let mut bytes = signed.into_bytes();
        // Flip a bit in the signature (last 64 bytes).
        let sig_start = bytes.len() - 64;
        bytes[sig_start] ^= 0xFF;

        let tampered = Signed::<EphemeralPayload>::try_decode(bytes);
        if let Ok(s) = tampered {
            assert!(
                s.try_verify().is_err(),
                "tampered signature should fail verification"
            );
        }
        // Decode failure is also acceptable — the bytes are corrupt.
    }

    #[tokio::test]
    async fn issuer_is_sender() {
        use subduction_crypto::signer::memory::MemorySigner;
        let signer = MemorySigner::from_bytes(&[0x42; 32]);
        let expected_peer = PeerId::from(signer.verifying_key());

        let ep = EphemeralPayload {
            id: Topic::new([0x01; 32]),
            nonce: 1,
            timestamp: TimestampSeconds::new(100),
            payload: vec![],
        };

        let verified = Signed::seal::<future_form::Sendable, _>(&signer, ep).await;
        let sealed = verified.into_signed();

        assert_eq!(PeerId::from(sealed.issuer()), expected_peer);
    }
}