paykit-lib 0.1.0

Paykit library implementation
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
use super::EncryptedLinkRecoveryContext;
use crate::{PaykitError, PublicKey, Result};

const HANDSHAKE_CHECKPOINT_VERSION: u8 = 1;
const HANDSHAKE_PACKET_LEN: usize = pubky_noise::snow_crypto::PUBKY_NOISE_CIPHERTEXT_LEN + 2;
const MAX_HANDSHAKE_PATH_LEN: usize = crate::PAYKIT_PRIVATE_PATH_PREFIX.len() + 1 + 64 + 1 + 10;
const MAX_HANDSHAKE_SNAPSHOT_LEN: usize =
    pubky_noise::serializer::MAX_SESSION_STATE_LEN + 32 + EncryptedLinkRecoveryContext::ENCODED_LEN;

struct PendingHandshakePacket {
    destination_path: String,
    packet: Vec<u8>,
}

fn invalid_handshake_checkpoint() -> PaykitError {
    PaykitError::InvalidData {
        context: "invalid Encrypted Link Handshake checkpoint".into(),
        source: None,
    }
}

/// Serializable snapshot of an established [`EncryptedLink`](crate::EncryptedLink).
///
/// Serialize with [`serialize`](Self::serialize) and restore with
/// [`restore_encrypted_link`](crate::restore_encrypted_link). Snapshot bytes
/// include sensitive key material and must be stored as secrets.
pub struct EncryptedLinkSnapshot {
    /// The underlying pubky-noise session state.
    state: pubky_noise::serializer::PubkyNoiseSessionState,
    /// The counterparty's public key (derived from `state.endpoint_pubkey`).
    recipient: PublicKey,
    /// The counterparty's identity-wide Noise public key.
    remote_noise_public_key: PublicKey,
    recovery_context: EncryptedLinkRecoveryContext,
}

fn recipient_from_snapshot_state(
    state: &pubky_noise::serializer::PubkyNoiseSessionState,
    snapshot_kind: &'static str,
) -> Result<PublicKey> {
    let pkarr_pk =
        pubky::pkarr::PublicKey::try_from(state.endpoint_pubkey.as_slice()).map_err(|err| {
            PaykitError::InvalidData {
                context: format!(
                    "failed to reconstruct recipient public key from {snapshot_kind}: {err}"
                ),
                source: Some(err.into()),
            }
        })?;
    Ok(PublicKey::from(pkarr_pk))
}

fn public_key_from_bytes(bytes: &[u8], context: &'static str) -> Result<PublicKey> {
    let pkarr_pk =
        pubky::pkarr::PublicKey::try_from(bytes).map_err(|err| PaykitError::InvalidData {
            context: format!("failed to reconstruct {context}: {err}"),
            source: Some(err.into()),
        })?;
    Ok(PublicKey::from(pkarr_pk))
}

fn serialize_snapshot(
    state: &pubky_noise::serializer::PubkyNoiseSessionState,
    remote_noise_public_key: &PublicKey,
    recovery_context: &EncryptedLinkRecoveryContext,
) -> Vec<u8> {
    let mut bytes = state.serialize();
    bytes.extend_from_slice(&remote_noise_public_key.to_bytes());
    recovery_context.append_bytes(&mut bytes, true);
    bytes
}

fn deserialize_snapshot(
    bytes: &[u8],
    snapshot_kind: &'static str,
) -> Result<(
    pubky_noise::serializer::PubkyNoiseSessionState,
    PublicKey,
    EncryptedLinkRecoveryContext,
)> {
    let Some(state_len) = bytes
        .len()
        .checked_sub(32 + EncryptedLinkRecoveryContext::ENCODED_LEN)
    else {
        return Err(PaykitError::InvalidData {
            context: format!("{snapshot_kind} is too short"),
            source: None,
        });
    };
    let state = deserialize_noise_state(&bytes[..state_len], snapshot_kind)?;
    if state.serialize().len() != state_len {
        return Err(PaykitError::InvalidData {
            context: format!("{snapshot_kind} has an invalid length"),
            source: None,
        });
    }
    let remote_noise_public_key =
        public_key_from_bytes(&bytes[state_len..state_len + 32], "remote Noise public key")?;
    let recovery_context = EncryptedLinkRecoveryContext::from_bytes(&bytes[state_len + 32..])?;
    Ok((state, remote_noise_public_key, recovery_context))
}

impl std::fmt::Debug for EncryptedLinkSnapshot {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EncryptedLinkSnapshot")
            .field("recipient", &self.recipient)
            .field("remote_noise_public_key", &self.remote_noise_public_key)
            .finish_non_exhaustive()
    }
}

impl EncryptedLinkSnapshot {
    pub(super) fn from_state(
        state: pubky_noise::serializer::PubkyNoiseSessionState,
        recipient: PublicKey,
        remote_noise_public_key: PublicKey,
        recovery_context: EncryptedLinkRecoveryContext,
    ) -> Self {
        Self {
            state,
            recipient,
            remote_noise_public_key,
            recovery_context,
        }
    }

    pub(super) fn phase(&self) -> pubky_noise::snow_crypto::NoisePhase {
        self.state.phase
    }

    pub(super) fn into_state(self) -> pubky_noise::serializer::PubkyNoiseSessionState {
        self.state
    }

    /// Serialize to a compact binary format for durable storage.
    ///
    /// The output contains the `pubky-noise` session state followed by the
    /// counterparty's 32-byte Noise public key and 72-byte recovery context.
    pub fn serialize(&self) -> Vec<u8> {
        serialize_snapshot(
            &self.state,
            &self.remote_noise_public_key,
            &self.recovery_context,
        )
    }

    /// Deserialize from bytes previously produced by [`serialize`](Self::serialize).
    ///
    /// Returns [`PaykitError::InvalidData`] if the bytes are malformed or the
    /// embedded public key cannot be reconstructed.
    pub fn deserialize(bytes: &[u8]) -> Result<Self> {
        let (state, remote_noise_public_key, recovery_context) =
            deserialize_snapshot(bytes, "Encrypted Link snapshot")?;
        let recipient = recipient_from_snapshot_state(&state, "Encrypted Link snapshot")?;

        Ok(Self {
            state,
            recipient,
            remote_noise_public_key,
            recovery_context,
        })
    }

    /// Access the counterparty's public key embedded in the snapshot.
    pub fn recipient(&self) -> &PublicKey {
        &self.recipient
    }

    /// Access the counterparty's identity-wide Noise public key.
    pub fn remote_noise_public_key(&self) -> &PublicKey {
        &self.remote_noise_public_key
    }

    /// Recovery attempts used to derive this connection's stream paths.
    pub fn recovery_context(&self) -> &EncryptedLinkRecoveryContext {
        &self.recovery_context
    }

    /// Stable identifier of the completed Noise handshake, unchanged by transport messages.
    pub fn link_id(&self) -> Option<[u8; 32]> {
        self.state.link_id
    }

    /// Check whether the next private message slot exists without restoring Noise.
    ///
    /// This is an advisory HEAD request. It does not authenticate a message or
    /// advance the snapshot. Before receiving, reload the authoritative state
    /// and use the ordinary locked receive workflow. Session authorization and
    /// key rotation remain the caller's responsibility.
    pub async fn has_pending_private_application_message(
        &self,
        public_storage: &pubky::PublicStorage,
        local_identity_public_key: &PublicKey,
        local_noise_secret_key: &[u8; 32],
    ) -> Result<bool> {
        if self.state.phase != pubky_noise::snow_crypto::NoisePhase::Transport
            || self.state.static_secret.as_ref() != Some(local_noise_secret_key)
            || self.state.read_counter >= u32::MAX - 1
            || self.state.receiving_nonce >= u64::MAX - 1
        {
            return Err(PaykitError::Validation(
                "snapshot cannot receive with the supplied Noise key".into(),
            ));
        }
        let (_, read_path) = super::paths::compute_private_payment_paths(
            local_noise_secret_key,
            local_identity_public_key,
            &self.recipient,
            &self.remote_noise_public_key,
            &self.recovery_context,
        );
        public_storage
            .exists(format!(
                "{}/{}/{}",
                self.recipient, read_path, self.state.read_counter
            ))
            .await
            .map_err(|err| PaykitError::Transport {
                context: "failed to check private message availability".into(),
                source: err.into(),
            })
    }
}

fn deserialize_noise_state(
    bytes: &[u8],
    snapshot_kind: &'static str,
) -> Result<pubky_noise::serializer::PubkyNoiseSessionState> {
    pubky_noise::serializer::PubkyNoiseSessionState::deserialize(bytes).map_err(|err| {
        PaykitError::InvalidData {
            context: format!("failed to deserialize {snapshot_kind}: {err:?}"),
            source: None,
        }
    })
}

/// Serializable snapshot of an in-progress
/// [`EncryptedLinkHandshake`](crate::EncryptedLinkHandshake).
///
/// Serialize with [`serialize`](Self::serialize) and restore with
/// [`restore_encrypted_link_handshake`](crate::restore_encrypted_link_handshake).
/// Snapshot bytes include sensitive key material and must be stored as secrets.
pub struct EncryptedLinkHandshakeSnapshot {
    /// The underlying pubky-noise session state.
    state: pubky_noise::serializer::PubkyNoiseSessionState,
    /// The counterparty's public key (derived from `state.endpoint_pubkey`).
    recipient: PublicKey,
    /// The counterparty's identity-wide Noise public key.
    remote_noise_public_key: PublicKey,
    recovery_context: EncryptedLinkRecoveryContext,
    pending: Option<PendingHandshakePacket>,
}

impl std::fmt::Debug for EncryptedLinkHandshakeSnapshot {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EncryptedLinkHandshakeSnapshot")
            .field("recipient", &self.recipient)
            .field("remote_noise_public_key", &self.remote_noise_public_key)
            .finish_non_exhaustive()
    }
}

impl EncryptedLinkHandshakeSnapshot {
    pub(super) fn from_state(
        state: pubky_noise::serializer::PubkyNoiseSessionState,
        recipient: PublicKey,
        remote_noise_public_key: PublicKey,
        recovery_context: EncryptedLinkRecoveryContext,
    ) -> Self {
        Self {
            state,
            recipient,
            remote_noise_public_key,
            recovery_context,
            pending: None,
        }
    }

    pub(super) fn phase(&self) -> pubky_noise::snow_crypto::NoisePhase {
        self.state.phase
    }

    pub(super) fn into_state(self) -> Result<pubky_noise::serializer::PubkyNoiseSessionState> {
        if self.pending.is_some() {
            return Err(PaykitError::Validation(
                "handshake publication must be acknowledged before restoration".into(),
            ));
        }
        Ok(self.state)
    }

    /// Serialize to a compact binary format for durable storage.
    ///
    /// The versioned envelope contains a length-delimited snapshot and at most
    /// one destination path with its exact, complete handshake packet.
    pub fn serialize(&self) -> Vec<u8> {
        let snapshot = serialize_snapshot(
            &self.state,
            &self.remote_noise_public_key,
            &self.recovery_context,
        );
        let mut bytes = vec![HANDSHAKE_CHECKPOINT_VERSION];
        bytes.extend_from_slice(&(snapshot.len() as u16).to_be_bytes());
        bytes.extend_from_slice(&snapshot);
        let path = self
            .pending
            .as_ref()
            .map_or("", |pending| pending.destination_path.as_str());
        bytes.extend_from_slice(&(path.len() as u16).to_be_bytes());
        bytes.extend_from_slice(path.as_bytes());
        if let Some(pending) = &self.pending {
            bytes.extend_from_slice(&pending.packet);
        }
        bytes
    }

    /// Deserialize from bytes previously produced by [`serialize`](Self::serialize).
    ///
    /// Returns [`PaykitError::InvalidData`] if the bytes are malformed or the
    /// embedded public key cannot be reconstructed.
    pub fn deserialize(bytes: &[u8]) -> Result<Self> {
        if bytes.len() < 5
            || bytes.len()
                > 5 + MAX_HANDSHAKE_SNAPSHOT_LEN + MAX_HANDSHAKE_PATH_LEN + HANDSHAKE_PACKET_LEN
            || bytes[0] != HANDSHAKE_CHECKPOINT_VERSION
        {
            return Err(invalid_handshake_checkpoint());
        }
        let snapshot_len = u16::from_be_bytes([bytes[1], bytes[2]]) as usize;
        if snapshot_len > MAX_HANDSHAKE_SNAPSHOT_LEN || bytes.len() < 5 + snapshot_len {
            return Err(invalid_handshake_checkpoint());
        }
        let path_offset = 3 + snapshot_len;
        let path_len = u16::from_be_bytes([bytes[path_offset], bytes[path_offset + 1]]) as usize;
        let tail = &bytes[path_offset + 2..];
        if path_len > MAX_HANDSHAKE_PATH_LEN
            || tail.len()
                != path_len
                    + if path_len == 0 {
                        0
                    } else {
                        HANDSHAKE_PACKET_LEN
                    }
        {
            return Err(invalid_handshake_checkpoint());
        }
        let (state, remote_noise_public_key, recovery_context) =
            deserialize_snapshot(&bytes[3..path_offset], "Encrypted Link Handshake snapshot")?;
        if state.phase != pubky_noise::snow_crypto::NoisePhase::HandShake {
            return Err(invalid_handshake_checkpoint());
        }
        let recipient = recipient_from_snapshot_state(&state, "Encrypted Link Handshake snapshot")?;
        let mut snapshot = Self {
            state,
            recipient,
            remote_noise_public_key,
            recovery_context,
            pending: None,
        };
        if path_len != 0 {
            let path = std::str::from_utf8(&tail[..path_len])
                .map_err(|_| invalid_handshake_checkpoint())?;
            snapshot.validate_pending_packet(path, &tail[path_len..])?;
            snapshot.pending = Some(PendingHandshakePacket {
                destination_path: path.to_owned(),
                packet: tail[path_len..].to_vec(),
            });
        }
        Ok(snapshot)
    }

    fn validate_pending_packet(&self, path: &str, packet: &[u8]) -> Result<()> {
        use pubky_noise::snow_crypto::{full_handshake_actions, HandshakeAction};
        let slot = self
            .state
            .counter
            .checked_sub(1)
            .ok_or_else(invalid_handshake_checkpoint)?;
        let (directory, suffix) = path
            .rsplit_once('/')
            .ok_or_else(invalid_handshake_checkpoint)?;
        let hash = directory
            .strip_prefix(crate::PAYKIT_PRIVATE_PATH_PREFIX)
            .and_then(|path| path.strip_prefix('/'))
            .ok_or_else(invalid_handshake_checkpoint)?;
        if hash.len() != 64
            || !hash
                .bytes()
                .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
            || suffix != slot.to_string()
            || full_handshake_actions(self.state.pattern, self.state.initiator)
                .map_err(|_| invalid_handshake_checkpoint())?
                .get(slot as usize)
                != Some(&HandshakeAction::Write)
            || packet.len() != HANDSHAKE_PACKET_LEN
        {
            return Err(invalid_handshake_checkpoint());
        }
        let payload_len = u16::from_be_bytes([packet[0], packet[1]]) as usize;
        if payload_len == 0
            || payload_len > HANDSHAKE_PACKET_LEN - 2
            || packet[2 + payload_len..].iter().any(|byte| *byte != 0)
        {
            return Err(invalid_handshake_checkpoint());
        }
        Ok(())
    }

    /// Attach one exact Noise-prepared packet to its post-write snapshot.
    ///
    /// Persist the returned checkpoint before publication. Session authorization
    /// and key rotation remain the caller's responsibility.
    pub(super) fn with_pending_publication(
        mut self,
        local_identity: &PublicKey,
        destination_path: String,
        packet: Vec<u8>,
    ) -> Result<Self> {
        if self.pending.is_some() {
            return Err(invalid_handshake_checkpoint());
        }
        self.validate_pending_packet(&destination_path, &packet)?;
        self.pending = Some(PendingHandshakePacket {
            destination_path,
            packet,
        });
        let secret = self
            .state
            .static_secret
            .ok_or_else(invalid_handshake_checkpoint)?;
        self.pending_publication(local_identity, &secret)?;
        Ok(self)
    }

    /// Read pending bytes only after checking the local key and identity-bound path.
    ///
    /// This does not authorize publication: the caller must validate current
    /// authorization, recovery context, source checkpoint and lease atomically.
    pub fn pending_publication(
        &self,
        local_identity: &PublicKey,
        local_noise_secret: &[u8; 32],
    ) -> Result<Option<(&str, &[u8])>> {
        if self.state.static_secret.as_ref() != Some(local_noise_secret) {
            return Err(PaykitError::Validation(
                "handshake checkpoint Noise key mismatch".into(),
            ));
        }
        let Some(pending) = &self.pending else {
            return Ok(None);
        };
        let (write_path, _) = super::paths::compute_private_payment_paths(
            local_noise_secret,
            local_identity,
            &self.recipient,
            &self.remote_noise_public_key,
            &self.recovery_context,
        );
        if pending.destination_path != format!("{write_path}/{}", self.state.counter - 1) {
            return Err(PaykitError::Validation(
                "handshake publication identity path mismatch".into(),
            ));
        }
        Ok(Some((&pending.destination_path, &pending.packet)))
    }

    /// Clear only the exact published packet; conditionally persist this result
    /// against the original pending checkpoint before advancing or exposing a link.
    pub fn acknowledge_publication(
        mut self,
        destination_path: &str,
        packet: &[u8],
    ) -> Result<Self> {
        if !self.pending.as_ref().is_some_and(|pending| {
            pending.destination_path == destination_path && pending.packet == packet
        }) {
            return Err(PaykitError::Validation(
                "handshake publication acknowledgement mismatch".into(),
            ));
        }
        self.pending = None;
        Ok(self)
    }

    /// Access the counterparty's public key embedded in the snapshot.
    pub fn recipient(&self) -> &PublicKey {
        &self.recipient
    }

    /// Access the counterparty's identity-wide Noise public key.
    pub fn remote_noise_public_key(&self) -> &PublicKey {
        &self.remote_noise_public_key
    }

    /// Recovery attempts used to derive this handshake's stream paths.
    pub fn recovery_context(&self) -> &EncryptedLinkRecoveryContext {
        &self.recovery_context
    }

    /// Return the next remote handshake resource without restoring Noise.
    ///
    /// `None` means local work is required, not that the handshake is idle. The
    /// returned path includes the counterparty identity and can be checked with
    /// `PublicStorage::exists` after validating authorization and recovery state.
    /// This advisory lookup neither authenticates a transcript nor advances it.
    /// Before advancing, reload authoritative state under the ordinary lease.
    /// Session authorization and key rotation remain the caller's responsibility.
    pub fn next_handshake_read_path(
        &self,
        local_identity_public_key: &PublicKey,
        local_noise_secret_key: &[u8; 32],
    ) -> Result<Option<String>> {
        if self.state.phase != pubky_noise::snow_crypto::NoisePhase::HandShake
            || self.state.static_secret.as_ref() != Some(local_noise_secret_key)
        {
            return Err(PaykitError::Validation(
                "snapshot cannot advance with the supplied Noise key".into(),
            ));
        }
        if self.pending.is_some() {
            return Ok(None);
        }
        let slot =
            self.state
                .next_handshake_read_slot()
                .map_err(|err| PaykitError::InvalidData {
                    context: format!("invalid Encrypted Link Handshake cursor: {err:?}"),
                    source: None,
                })?;
        let Some(slot) = slot else {
            return Ok(None);
        };
        let (_, read_path) = super::paths::compute_private_payment_paths(
            local_noise_secret_key,
            local_identity_public_key,
            &self.recipient,
            &self.remote_noise_public_key,
            &self.recovery_context,
        );
        Ok(Some(format!("{}/{read_path}/{slot}", self.recipient)))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use pubky_noise::{
        serializer::PubkyNoiseSessionState,
        snow_crypto::{HandshakePattern, NoisePhase, NoiseStep},
    };

    fn checkpoint(final_write: bool) -> EncryptedLinkHandshakeSnapshot {
        let recipient = pubky::Keypair::from_secret(&[4; 32]).public_key();
        EncryptedLinkHandshakeSnapshot::from_state(
            PubkyNoiseSessionState {
                version: pubky_noise::serializer::SESSION_STATE_VERSION,
                phase: NoisePhase::HandShake,
                pattern: HandshakePattern::PatternXX,
                initiator: true,
                ephemeral_secret: [1; 32],
                static_secret: Some([2; 32]),
                counter: if final_write { 3 } else { 1 },
                noise_step: if final_write {
                    NoiseStep::Final
                } else {
                    NoiseStep::StepTwo
                },
                sub_step_index: 0,
                handshake_hash: Some([3; 32]),
                link_id: None,
                sending_nonce: 0,
                receiving_nonce: 0,
                write_counter: 0,
                read_counter: 0,
                endpoint_pubkey: recipient.to_bytes(),
                handshake_messages: if final_write {
                    vec![vec![5; 96]]
                } else {
                    Vec::new()
                },
            },
            recipient,
            pubky::Keypair::from_secret(&[5; 32]).public_key(),
            EncryptedLinkRecoveryContext::default(),
        )
    }

    fn pending(final_write: bool) -> (EncryptedLinkHandshakeSnapshot, PublicKey, String, Vec<u8>) {
        let snapshot = checkpoint(final_write);
        let local = pubky::Keypair::from_secret(&[6; 32]).public_key();
        let (path, _) = super::super::paths::compute_private_payment_paths(
            &[2; 32],
            &local,
            snapshot.recipient(),
            snapshot.remote_noise_public_key(),
            snapshot.recovery_context(),
        );
        let path = format!("{path}/{}", snapshot.state.counter - 1);
        let mut packet = vec![0; HANDSHAKE_PACKET_LEN];
        packet[1] = 32;
        packet[2..34].fill(7);
        (
            snapshot
                .with_pending_publication(&local, path.clone(), packet.clone())
                .unwrap(),
            local,
            path,
            packet,
        )
    }

    #[test]
    fn test_handshake_checkpoint_keeps_exact_publication_until_acknowledged() {
        for final_write in [false, true] {
            let plain = checkpoint(final_write).serialize();
            let (snapshot, local, path, packet) = pending(final_write);
            let bytes = snapshot.serialize();
            let load = || EncryptedLinkHandshakeSnapshot::deserialize(&bytes).unwrap();
            assert_eq!(load().serialize(), bytes);
            assert_eq!(
                snapshot.pending_publication(&local, &[2; 32]).unwrap(),
                Some((path.as_str(), packet.as_slice()))
            );
            assert!(snapshot
                .next_handshake_read_path(&local, &[2; 32])
                .unwrap()
                .is_none());
            assert!(load().into_state().is_err());
            for (wrong_path, wrong_packet) in [
                (path.as_str(), &[0; HANDSHAKE_PACKET_LEN][..]),
                ("wrong-path", packet.as_slice()),
            ] {
                assert!(load()
                    .acknowledge_publication(wrong_path, wrong_packet)
                    .is_err());
            }
            assert!(load()
                .with_pending_publication(&local, path.clone(), packet.clone())
                .is_err());
            assert!(snapshot.pending_publication(&local, &[9; 32]).is_err());
            assert!(snapshot
                .pending_publication(
                    &pubky::Keypair::from_secret(&[9; 32]).public_key(),
                    &[2; 32]
                )
                .is_err());
            let acknowledged = load().acknowledge_publication(&path, &packet).unwrap();
            assert_eq!(acknowledged.serialize(), plain);
            assert_eq!(
                acknowledged.into_state().unwrap().phase,
                NoisePhase::HandShake
            );
        }
    }

    #[test]
    fn test_handshake_checkpoint_rejects_malformed_and_unbounded_envelopes() {
        let (mut snapshot, _, path, packet) = pending(false);
        let bytes = snapshot.serialize();
        let path_offset = 3 + u16::from_be_bytes([bytes[1], bytes[2]]) as usize;
        for end in [0, 1, 2, 3, path_offset, path_offset + 1, bytes.len() - 1] {
            assert!(EncryptedLinkHandshakeSnapshot::deserialize(&bytes[..end]).is_err());
        }
        for mutation in [
            "version",
            "state-length",
            "path-length",
            "path",
            "hash",
            "slot",
            "length",
            "empty",
            "padding",
            "trailing",
            "oversized",
            "transport",
        ] {
            let mut malformed = bytes.clone();
            let packet_offset = bytes.len() - HANDSHAKE_PACKET_LEN;
            match mutation {
                "version" => malformed[0] += 1,
                "state-length" => malformed[1..3].fill(255),
                "path-length" => malformed[path_offset..path_offset + 2].fill(255),
                "path" => malformed[path_offset + 2] = 255,
                "hash" => {
                    malformed[path_offset + 3 + crate::PAYKIT_PRIVATE_PATH_PREFIX.len()] = b'/'
                }
                "slot" => malformed[packet_offset - 1] = b'1',
                "length" => malformed[packet_offset..packet_offset + 2].fill(255),
                "empty" => malformed[packet_offset..packet_offset + 2].fill(0),
                "padding" => *malformed.last_mut().unwrap() = 1,
                "trailing" => malformed.push(0),
                "oversized" => malformed.resize(
                    6 + MAX_HANDSHAKE_SNAPSHOT_LEN + MAX_HANDSHAKE_PATH_LEN + HANDSHAKE_PACKET_LEN,
                    0,
                ),
                "transport" => malformed[4] = 1,
                _ => unreachable!(),
            }
            assert!(
                EncryptedLinkHandshakeSnapshot::deserialize(&malformed).is_err(),
                "{mutation}"
            );
        }
        snapshot.state.counter = 2;
        snapshot.state.sub_step_index = 1;
        snapshot.state.handshake_messages = vec![vec![5; 96]];
        let directory = path.rsplit_once('/').unwrap().0;
        assert!(snapshot
            .validate_pending_packet(&format!("{directory}/1"), &packet)
            .is_err());
    }
}