hopr_crypto_packet/
packet.rs

1use std::fmt::{Display, Formatter};
2
3use hopr_crypto_sphinx::prelude::*;
4use hopr_crypto_types::prelude::*;
5use hopr_internal_types::prelude::*;
6#[cfg(feature = "rayon")]
7use hopr_parallelize::cpu::rayon::prelude::*;
8use hopr_primitive_types::prelude::*;
9
10use crate::{
11    HoprPseudonym, HoprReplyOpener, HoprSphinxHeaderSpec, HoprSphinxSuite, HoprSurb, PAYLOAD_SIZE_INT,
12    errors::{
13        PacketError::{PacketConstructionError, PacketDecodingError},
14        Result,
15    },
16    por::{
17        ProofOfRelayString, ProofOfRelayValues, SurbReceiverInfo, derive_ack_key_share, generate_proof_of_relay,
18        pre_verify,
19    },
20    types::{HoprPacketMessage, HoprPacketParts, HoprSenderId, HoprSurbId, PacketSignals},
21};
22
23/// Represents an outgoing packet that has been only partially instantiated.
24///
25/// It contains [`PartialPacket`], required Proof-of-Relay
26/// fields, and the [`Ticket`], but it does not contain the payload.
27///
28/// This can be used to pre-compute packets for certain destinations,
29/// and [convert](PartialHoprPacket::into_hopr_packet) them to full packets
30/// once the payload is known.
31#[derive(Clone)]
32#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
33pub struct PartialHoprPacket {
34    partial_packet: PartialPacket<HoprSphinxSuite, HoprSphinxHeaderSpec>,
35    surbs: Vec<HoprSurb>,
36    openers: Vec<HoprReplyOpener>,
37    ticket: Ticket,
38    next_hop: OffchainPublicKey,
39    ack_challenge: HalfKeyChallenge,
40}
41
42/// Shared key data for a path.
43///
44/// This contains the derived shared secrets and Proof of Relay data for a path.
45struct PathKeyData {
46    /// Shared secrets for the path.
47    pub shared_keys: SharedKeys<<HoprSphinxSuite as SphinxSuite>::E, <HoprSphinxSuite as SphinxSuite>::G>,
48    /// Proof of Relay data for each hop on the path.
49    pub por_strings: Vec<ProofOfRelayString>,
50    /// Proof of Relay values for the first ticket on the path.
51    pub por_values: ProofOfRelayValues,
52}
53
54impl PathKeyData {
55    fn new(path: &[OffchainPublicKey]) -> Result<Self> {
56        let shared_keys = HoprSphinxSuite::new_shared_keys(path)?;
57        let (por_strings, por_values) = generate_proof_of_relay(&shared_keys.secrets)?;
58
59        Ok(Self {
60            shared_keys,
61            por_strings,
62            por_values,
63        })
64    }
65
66    /// Computes `PathKeyData` for the given paths.
67    ///
68    /// Uses parallel processing if the `rayon` feature is enabled.
69    fn iter_from_paths(paths: Vec<&[OffchainPublicKey]>) -> Result<impl Iterator<Item = Self> + use<>> {
70        #[cfg(not(feature = "rayon"))]
71        let paths = paths.into_iter();
72
73        #[cfg(feature = "rayon")]
74        let paths = paths.into_par_iter();
75
76        paths
77            .map(Self::new)
78            .collect::<Result<Vec<_>>>()
79            .map(|paths| paths.into_iter())
80    }
81}
82
83impl PartialHoprPacket {
84    /// Instantiates a new partial HOPR packet.
85    ///
86    /// # Arguments
87    ///
88    /// * `pseudonym` our pseudonym as packet sender.
89    /// * `routing` routing to the destination.
90    /// * `chain_keypair` private key of the local node.
91    /// * `ticket` ticket builder for the first hop on the path.
92    /// * `mapper` of the public key identifiers.
93    /// * `domain_separator` channels contract domain separator.
94    pub fn new<M: KeyIdMapper<HoprSphinxSuite, HoprSphinxHeaderSpec>, P: NonEmptyPath<OffchainPublicKey> + Send>(
95        pseudonym: &HoprPseudonym,
96        routing: PacketRouting<P>,
97        chain_keypair: &ChainKeypair,
98        ticket: TicketBuilder,
99        mapper: &M,
100        domain_separator: &Hash,
101    ) -> Result<Self> {
102        match routing {
103            PacketRouting::ForwardPath {
104                forward_path,
105                return_paths,
106            } => {
107                // Create shared secrets and PoR challenge chain for forward and return paths
108                let mut key_data = PathKeyData::iter_from_paths(
109                    std::iter::once(forward_path.hops())
110                        .chain(return_paths.iter().map(|p| p.hops()))
111                        .collect(),
112                )?;
113
114                let PathKeyData {
115                    shared_keys,
116                    por_strings,
117                    por_values,
118                } = key_data
119                    .next()
120                    .ok_or_else(|| PacketConstructionError("empty path".into()))?;
121
122                let receiver_data = HoprSenderId::new(pseudonym);
123
124                // Create SURBs if some return paths were specified
125                // Possibly makes little sense to parallelize this iterator via rayon,
126                // as in most cases the number of return paths is 1.
127                let (surbs, openers): (Vec<_>, Vec<_>) = key_data
128                    .zip(return_paths)
129                    .zip(receiver_data.into_sequence())
130                    .map(|((key_data, rp), data)| create_surb_for_path((rp, key_data), data, mapper))
131                    .collect::<Result<Vec<_>>>()?
132                    .into_iter()
133                    .unzip();
134
135                // Update the ticket with the challenge
136                let ticket = ticket
137                    .eth_challenge(por_values.ticket_challenge())
138                    .build_signed(chain_keypair, domain_separator)?
139                    .leak();
140
141                Ok(Self {
142                    partial_packet: PartialPacket::<HoprSphinxSuite, HoprSphinxHeaderSpec>::new(
143                        MetaPacketRouting::ForwardPath {
144                            shared_keys,
145                            forward_path: &forward_path,
146                            receiver_data: &receiver_data,
147                            additional_data_relayer: &por_strings,
148                            no_ack: false,
149                        },
150                        mapper,
151                    )?,
152                    surbs,
153                    openers,
154                    ticket,
155                    next_hop: forward_path[0],
156                    ack_challenge: por_values.acknowledgement_challenge(),
157                })
158            }
159            PacketRouting::Surb(id, surb) => {
160                // Update the ticket with the challenge
161                let ticket = ticket
162                    .eth_challenge(surb.additional_data_receiver.proof_of_relay_values().ticket_challenge())
163                    .build_signed(chain_keypair, domain_separator)?
164                    .leak();
165
166                Ok(Self {
167                    ticket,
168                    next_hop: mapper.map_id_to_public(&surb.first_relayer).ok_or_else(|| {
169                        PacketConstructionError(format!(
170                            "failed to map key id {} to public key",
171                            surb.first_relayer.to_hex()
172                        ))
173                    })?,
174                    ack_challenge: surb
175                        .additional_data_receiver
176                        .proof_of_relay_values()
177                        .acknowledgement_challenge(),
178                    partial_packet: PartialPacket::<HoprSphinxSuite, HoprSphinxHeaderSpec>::new(
179                        MetaPacketRouting::Surb(surb, &HoprSenderId::from_pseudonym_and_id(pseudonym, id)),
180                        mapper,
181                    )?,
182                    surbs: vec![],
183                    openers: vec![],
184                })
185            }
186            PacketRouting::NoAck(destination) => {
187                // Create shared secrets and PoR challenge chain
188                let PathKeyData {
189                    shared_keys,
190                    por_strings,
191                    por_values,
192                    ..
193                } = PathKeyData::new(&[destination])?;
194
195                // Update the ticket with the challenge
196                let ticket = ticket
197                    .eth_challenge(por_values.ticket_challenge())
198                    .build_signed(chain_keypair, domain_separator)?
199                    .leak();
200
201                Ok(Self {
202                    partial_packet: PartialPacket::<HoprSphinxSuite, HoprSphinxHeaderSpec>::new(
203                        MetaPacketRouting::ForwardPath {
204                            shared_keys,
205                            forward_path: &[destination],
206                            receiver_data: &HoprSenderId::new(pseudonym),
207                            additional_data_relayer: &por_strings,
208                            no_ack: true, // Indicate this is a no-acknowledgement probe packet
209                        },
210                        mapper,
211                    )?,
212                    ticket,
213                    next_hop: destination,
214                    ack_challenge: por_values.acknowledgement_challenge(),
215                    surbs: vec![],
216                    openers: vec![],
217                })
218            }
219        }
220    }
221
222    /// Turns this partial HOPR packet into a full [`Outgoing`](HoprPacket::Outgoing) [`HoprPacket`] by
223    /// attaching the given payload `msg` and optional packet `signals` for the recipient.
224    ///
225    /// No `signals` are equivalent to `0`.
226    pub fn into_hopr_packet<S: Into<PacketSignals>>(
227        self,
228        msg: &[u8],
229        signals: S,
230    ) -> Result<(HoprPacket, Vec<HoprReplyOpener>)> {
231        let msg = HoprPacketMessage::try_from(HoprPacketParts {
232            surbs: self.surbs,
233            payload: msg.into(),
234            signals: signals.into(),
235        })?;
236        Ok((
237            HoprPacket::Outgoing(
238                HoprOutgoingPacket {
239                    packet: self.partial_packet.into_meta_packet(msg.into()),
240                    ticket: self.ticket,
241                    next_hop: self.next_hop,
242                    ack_challenge: self.ack_challenge,
243                }
244                .into(),
245            ),
246            self.openers,
247        ))
248    }
249}
250
251/// Represents a packet incoming to its final destination.
252#[derive(Clone)]
253pub struct HoprIncomingPacket {
254    /// Packet's authentication tag.
255    pub packet_tag: PacketTag,
256    /// Acknowledgement to be sent to the previous hop.
257    ///
258    /// In case an acknowledgement is not required, this field is `None`. This arises specifically
259    /// in case the message payload is used to send one or more acknowledgements in the payload.
260    pub ack_key: Option<HalfKey>,
261    /// Address of the previous hop.
262    pub previous_hop: OffchainPublicKey,
263    /// Decrypted packet payload.
264    pub plain_text: Box<[u8]>,
265    /// Pseudonym of the packet creator.
266    pub sender: HoprPseudonym,
267    /// List of [`SURBs`](SURB) to be used for replies sent to the packet creator.
268    pub surbs: Vec<(HoprSurbId, HoprSurb)>,
269    /// Additional packet signals from the lower protocol layer passed from the packet sender.
270    ///
271    /// Zero if no signal flags were specified.
272    pub signals: PacketSignals,
273}
274
275impl std::fmt::Debug for HoprIncomingPacket {
276    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
277        f.debug_struct("HoprIncomingPacket")
278            .field("packet_tag", &self.packet_tag)
279            .field("ack_key", &self.ack_key)
280            .field("previous_hop", &self.previous_hop)
281            .field("sender", &self.sender)
282            .field("signals", &self.signals)
283            .finish_non_exhaustive()
284    }
285}
286
287/// Represents a packet destined for another node.
288#[derive(Clone)]
289pub struct HoprOutgoingPacket {
290    /// Encrypted packet.
291    pub packet: MetaPacket<HoprSphinxSuite, HoprSphinxHeaderSpec, PAYLOAD_SIZE_INT>,
292    /// Ticket for this node.
293    pub ticket: Ticket,
294    /// Next hop this packet should be sent to.
295    pub next_hop: OffchainPublicKey,
296    /// Acknowledgement challenge solved once the next hop sends us an acknowledgement.
297    pub ack_challenge: HalfKeyChallenge,
298}
299
300impl std::fmt::Debug for HoprOutgoingPacket {
301    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
302        f.debug_struct("HoprOutgoingPacket")
303            .field("ticket", &self.ticket)
304            .field("next_hop", &self.next_hop)
305            .field("ack_challenge", &self.ack_challenge)
306            .finish_non_exhaustive()
307    }
308}
309
310/// Represents a [`HoprOutgoingPacket`] with additional forwarding information.
311#[derive(Clone)]
312pub struct HoprForwardedPacket {
313    /// Packet to be sent.
314    pub outgoing: HoprOutgoingPacket,
315    /// Authentication tag of the packet's header.
316    pub packet_tag: PacketTag,
317    /// Acknowledgement to be sent to the previous hop.
318    pub ack_key: HalfKey,
319    /// Sender of this packet.
320    pub previous_hop: OffchainPublicKey,
321    /// Key used to verify our challenge.
322    pub own_key: HalfKey,
323    /// Challenge for the next hop.
324    pub next_challenge: EthereumChallenge,
325    /// Our position in the path.
326    pub path_pos: u8,
327}
328
329impl std::fmt::Debug for HoprForwardedPacket {
330    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
331        f.debug_struct("HoprForwardedPacket")
332            .field("outgoing", &self.outgoing)
333            .field("packet_tag", &hex::encode(self.packet_tag))
334            .field("ack_key", &self.ack_key)
335            .field("previous_hop", &self.previous_hop)
336            .field("own_key", &self.own_key)
337            .field("next_challenge", &self.next_challenge)
338            .field("path_pos", &self.path_pos)
339            .finish_non_exhaustive()
340    }
341}
342
343/// Contains HOPR packet and its variants.
344///
345/// See [`HoprIncomingPacket`], [`HoprForwardedPacket`] and [`HoprOutgoingPacket`] for details.
346///
347/// The members are intentionally boxed to equalize the variant sizes.
348#[derive(Clone, Debug, strum::EnumTryAs, strum::EnumIs)]
349pub enum HoprPacket {
350    /// The packet is intended for us
351    Final(Box<HoprIncomingPacket>),
352    /// The packet must be forwarded
353    Forwarded(Box<HoprForwardedPacket>),
354    /// The packet that is being sent out by us
355    Outgoing(Box<HoprOutgoingPacket>),
356}
357
358impl HoprPacket {
359    /// Returns the [`PacketTag`] of forwarded or final packets, or `None` for outgoing packets.
360    pub fn packet_tag(&self) -> Option<&PacketTag> {
361        match self {
362            HoprPacket::Final(packet) => Some(&packet.packet_tag),
363            HoprPacket::Forwarded(packet) => Some(&packet.packet_tag),
364            HoprPacket::Outgoing(_) => None,
365        }
366    }
367}
368
369impl Display for HoprPacket {
370    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
371        match &self {
372            Self::Final(_) => write!(f, "Final"),
373            Self::Forwarded(_) => write!(f, "Forwarded"),
374            Self::Outgoing(_) => write!(f, "Outgoing"),
375        }
376    }
377}
378
379/// Determines options on how HOPR packet can be routed to its destination.
380#[derive(Clone)]
381pub enum PacketRouting<P: NonEmptyPath<OffchainPublicKey> = TransportPath> {
382    /// The packet is routed directly via the given path.
383    /// Optionally, return paths for
384    /// attached SURBs can be specified.
385    ForwardPath { forward_path: P, return_paths: Vec<P> },
386    /// The packet is routed via an existing SURB that corresponds to a pseudonym.
387    Surb(HoprSurbId, HoprSurb),
388    /// No acknowledgement packet: a special type of 0-hop packet that is not going to be acknowledged but can carry a
389    /// payload.
390    NoAck(OffchainPublicKey),
391}
392
393fn create_surb_for_path<M: KeyIdMapper<HoprSphinxSuite, HoprSphinxHeaderSpec>, P: NonEmptyPath<OffchainPublicKey>>(
394    return_path: (P, PathKeyData),
395    recv_data: HoprSenderId,
396    mapper: &M,
397) -> Result<(HoprSurb, HoprReplyOpener)> {
398    let (
399        return_path,
400        PathKeyData {
401            shared_keys,
402            por_strings,
403            por_values,
404        },
405    ) = return_path;
406
407    Ok(create_surb::<HoprSphinxSuite, HoprSphinxHeaderSpec>(
408        shared_keys,
409        &return_path
410            .iter()
411            .map(|k| {
412                mapper
413                    .map_key_to_id(k)
414                    .ok_or_else(|| PacketConstructionError(format!("failed to map key {} to id", k.to_hex())))
415            })
416            .collect::<Result<Vec<_>>>()?,
417        &por_strings,
418        recv_data,
419        SurbReceiverInfo::new(por_values, [0u8; 32]),
420    )
421    .map(|(s, r)| (s, (recv_data.surb_id(), r)))?)
422}
423
424impl HoprPacket {
425    /// The maximum number of SURBs that fit into a packet that contains no message.
426    pub const MAX_SURBS_IN_PACKET: usize = HoprPacket::PAYLOAD_SIZE / HoprSurb::SIZE;
427    /// Maximum message size when no SURBs are present in the packet.
428    ///
429    /// See [`HoprPacket::max_surbs_with_message`].
430    pub const PAYLOAD_SIZE: usize = PAYLOAD_SIZE_INT - HoprPacketMessage::HEADER_LEN;
431    /// The size of the packet including header, padded payload, ticket, and ack challenge.
432    pub const SIZE: usize =
433        MetaPacket::<HoprSphinxSuite, HoprSphinxHeaderSpec, PAYLOAD_SIZE_INT>::PACKET_LEN + Ticket::SIZE;
434
435    /// Constructs a new outgoing packet with the given path.
436    ///
437    /// # Arguments
438    /// * `msg` packet payload.
439    /// * `pseudonym` our pseudonym as packet sender.
440    /// * `routing` routing to the destination.
441    /// * `chain_keypair` private key of the local node.
442    /// * `ticket` ticket builder for the first hop on the path.
443    /// * `mapper` of the public key identifiers.
444    /// * `domain_separator` channels contract domain separator.
445    /// * `signals` optional signals passed to the packet's final destination.
446    ///
447    /// **NOTE**
448    /// For the given pseudonym, the [`ReplyOpener`] order matters.
449    #[allow(clippy::too_many_arguments)] // TODO: needs refactoring (perhaps introduce a builder pattern?)
450    pub fn into_outgoing<
451        M: KeyIdMapper<HoprSphinxSuite, HoprSphinxHeaderSpec>,
452        P: NonEmptyPath<OffchainPublicKey> + Send,
453        S: Into<PacketSignals>,
454    >(
455        msg: &[u8],
456        pseudonym: &HoprPseudonym,
457        routing: PacketRouting<P>,
458        chain_keypair: &ChainKeypair,
459        ticket: TicketBuilder,
460        mapper: &M,
461        domain_separator: &Hash,
462        signals: S,
463    ) -> Result<(Self, Vec<HoprReplyOpener>)> {
464        PartialHoprPacket::new(pseudonym, routing, chain_keypair, ticket, mapper, domain_separator)?
465            .into_hopr_packet(msg, signals)
466    }
467
468    /// Calculates how many SURBs can be fitted into a packet that
469    /// also carries a message of the given length.
470    pub const fn max_surbs_with_message(msg_len: usize) -> usize {
471        HoprPacket::PAYLOAD_SIZE.saturating_sub(msg_len) / HoprSurb::SIZE
472    }
473
474    /// Calculates the maximum length of the message that can be carried by a packet
475    /// with the given number of SURBs.
476    pub const fn max_message_with_surbs(num_surbs: usize) -> usize {
477        HoprPacket::PAYLOAD_SIZE.saturating_sub(num_surbs * HoprSurb::SIZE)
478    }
479
480    /// Deserializes the packet and performs the forward-transformation, so the
481    /// packet can be further delivered (relayed to the next hop or read).
482    pub fn from_incoming<M, F>(
483        data: &[u8],
484        node_keypair: &OffchainKeypair,
485        previous_hop: OffchainPublicKey,
486        mapper: &M,
487        reply_openers: F,
488    ) -> Result<Self>
489    where
490        M: KeyIdMapper<HoprSphinxSuite, HoprSphinxHeaderSpec>,
491        F: FnMut(&HoprSenderId) -> Option<ReplyOpener>,
492    {
493        if data.len() == Self::SIZE {
494            let (pre_packet, pre_ticket) =
495                data.split_at(MetaPacket::<HoprSphinxSuite, HoprSphinxHeaderSpec, PAYLOAD_SIZE_INT>::PACKET_LEN);
496
497            let mp: MetaPacket<HoprSphinxSuite, HoprSphinxHeaderSpec, PAYLOAD_SIZE_INT> =
498                MetaPacket::try_from(pre_packet)?;
499
500            match mp.into_forwarded(node_keypair, mapper, reply_openers)? {
501                ForwardedMetaPacket::Relayed {
502                    packet,
503                    derived_secret,
504                    additional_info,
505                    packet_tag,
506                    next_node,
507                    path_pos,
508                    ..
509                } => {
510                    let ack_key = derive_ack_key_share(&derived_secret);
511
512                    let ticket = Ticket::try_from(pre_ticket)?;
513                    let verification_output = pre_verify(&derived_secret, &additional_info, &ticket.challenge)?;
514                    Ok(Self::Forwarded(
515                        HoprForwardedPacket {
516                            outgoing: HoprOutgoingPacket {
517                                packet,
518                                ticket,
519                                next_hop: next_node,
520                                ack_challenge: verification_output.ack_challenge,
521                            },
522                            packet_tag,
523                            ack_key,
524                            previous_hop,
525                            path_pos,
526                            own_key: verification_output.own_key,
527                            next_challenge: verification_output.next_ticket_challenge,
528                        }
529                        .into(),
530                    ))
531                }
532                ForwardedMetaPacket::Final {
533                    packet_tag,
534                    plain_text,
535                    derived_secret,
536                    receiver_data,
537                    no_ack,
538                } => {
539                    // The pre_ticket is not parsed nor verified on the final hop
540                    let HoprPacketParts {
541                        surbs,
542                        payload,
543                        signals,
544                    } = HoprPacketMessage::from(plain_text).try_into()?;
545                    let should_acknowledge = !no_ack;
546                    Ok(Self::Final(
547                        HoprIncomingPacket {
548                            packet_tag,
549                            ack_key: should_acknowledge.then(|| derive_ack_key_share(&derived_secret)),
550                            previous_hop,
551                            plain_text: payload.into(),
552                            surbs: receiver_data.into_sequence().map(|d| d.surb_id()).zip(surbs).collect(),
553                            sender: receiver_data.pseudonym(),
554                            signals,
555                        }
556                        .into(),
557                    ))
558                }
559            }
560        } else {
561            Err(PacketDecodingError("packet has invalid size".into()))
562        }
563    }
564}
565
566#[cfg(test)]
567mod tests {
568    use anyhow::{Context, bail};
569    use bimap::BiHashMap;
570    use hex_literal::hex;
571    use hopr_crypto_random::Randomizable;
572    use parameterized::parameterized;
573
574    use super::*;
575    use crate::types::PacketSignal;
576
577    lazy_static::lazy_static! {
578        static ref PEERS: [(ChainKeypair, OffchainKeypair); 5] = [
579            (hex!("a7c486ceccf5ab53bd428888ab1543dc2667abd2d5e80aae918da8d4b503a426"), hex!("5eb212d4d6aa5948c4f71574d45dad43afef6d330edb873fca69d0e1b197e906")),
580            (hex!("9a82976f7182c05126313bead5617c623b93d11f9f9691c87b1a26f869d569ed"), hex!("e995db483ada5174666c46bafbf3628005aca449c94ebdc0c9239c3f65d61ae0")),
581            (hex!("ca4bdfd54a8467b5283a0216288fdca7091122479ccf3cfb147dfa59d13f3486"), hex!("9dec751c00f49e50fceff7114823f726a0425a68a8dc6af0e4287badfea8f4a4")),
582            (hex!("e306ebfb0d01d0da0952c9a567d758093a80622c6cb55052bf5f1a6ebd8d7b5c"), hex!("9a82976f7182c05126313bead5617c623b93d11f9f9691c87b1a26f869d569ed")),
583            (hex!("492057cf93e99b31d2a85bc5e98a9c3aa0021feec52c227cc8170e8f7d047775"), hex!("e0bf93e9c916104da00b1850adc4608bd7e9087bbd3f805451f4556aa6b3fd6e")),
584        ].map(|(p1,p2)| (ChainKeypair::from_secret(&p1).expect("lazy static keypair should be valid"), OffchainKeypair::from_secret(&p2).expect("lazy static keypair should be valid")));
585
586        static ref MAPPER: bimap::BiMap<KeyIdent, OffchainPublicKey> = PEERS
587            .iter()
588            .enumerate()
589            .map(|(i, (_, k))| (KeyIdent::from(i as u32), *k.public()))
590            .collect::<BiHashMap<_, _>>();
591    }
592
593    fn forward(
594        mut packet: HoprPacket,
595        chain_keypair: &ChainKeypair,
596        next_ticket: TicketBuilder,
597        domain_separator: &Hash,
598    ) -> HoprPacket {
599        if let HoprPacket::Forwarded(fwd) = &mut packet {
600            fwd.outgoing.ticket = next_ticket
601                .eth_challenge(fwd.next_challenge)
602                .build_signed(chain_keypair, domain_separator)
603                .expect("ticket should create")
604                .leak();
605        }
606
607        packet
608    }
609
610    impl HoprPacket {
611        pub fn to_bytes(&self) -> Box<[u8]> {
612            let dummy_ticket = hex!("67f0ca18102feec505e5bfedcc25963e9c64a6f8a250adcad7d2830dd607585700000000000000000000000000000000000000000000000000000000000000003891bf6fd4a78e868fc7ad477c09b16fc70dd01ea67e18264d17e3d04f6d8576de2e6472b0072e510df6e9fa1dfcc2727cc7633edfeb9ec13860d9ead29bee71d68de3736c2f7a9f42de76ccd57a5f5847bc7349");
613            let (packet, ticket) = match self {
614                Self::Final(packet) => (packet.plain_text.clone(), dummy_ticket.as_ref().into()),
615                Self::Forwarded(fwd) => (
616                    Vec::from(fwd.outgoing.packet.as_ref()).into_boxed_slice(),
617                    fwd.outgoing.ticket.clone().into_boxed(),
618                ),
619                Self::Outgoing(out) => (
620                    Vec::from(out.packet.as_ref()).into_boxed_slice(),
621                    out.ticket.clone().into_boxed(),
622                ),
623            };
624
625            let mut ret = Vec::with_capacity(Self::SIZE);
626            ret.extend_from_slice(packet.as_ref());
627            ret.extend_from_slice(&ticket);
628            ret.into_boxed_slice()
629        }
630    }
631
632    fn mock_ticket(next_peer_channel_key: &PublicKey, path_len: usize) -> anyhow::Result<TicketBuilder> {
633        assert!(path_len > 0);
634        let price_per_packet: U256 = 10000000000000000u128.into();
635
636        if path_len > 1 {
637            Ok(TicketBuilder::default()
638                .counterparty(next_peer_channel_key.to_address())
639                .amount(price_per_packet.div_f64(1.0)? * U256::from(path_len as u64 - 1))
640                .index(1)
641                .win_prob(WinningProbability::ALWAYS)
642                .channel_epoch(1)
643                .eth_challenge(Default::default()))
644        } else {
645            Ok(TicketBuilder::zero_hop().counterparty(next_peer_channel_key.to_address()))
646        }
647    }
648
649    const FLAGS: PacketSignal = PacketSignal::OutOfSurbs;
650
651    fn create_packet(
652        forward_hops: usize,
653        pseudonym: HoprPseudonym,
654        return_hops: Vec<usize>,
655        msg: &[u8],
656    ) -> anyhow::Result<(HoprPacket, Vec<HoprReplyOpener>)> {
657        assert!((0..=3).contains(&forward_hops), "forward hops must be between 1 and 3");
658        assert!(
659            return_hops.iter().all(|h| (0..=3).contains(h)),
660            "return hops must be between 1 and 3"
661        );
662
663        let ticket = mock_ticket(&PEERS[1].0.public(), forward_hops + 1)?;
664        let forward_path = TransportPath::new(PEERS[1..=forward_hops + 1].iter().map(|kp| *kp.1.public()))?;
665
666        let return_paths = return_hops
667            .into_iter()
668            .map(|h| TransportPath::new(PEERS[0..=h].iter().rev().map(|kp| *kp.1.public())))
669            .collect::<std::result::Result<Vec<_>, hopr_internal_types::errors::PathError>>()?;
670
671        Ok(HoprPacket::into_outgoing(
672            msg,
673            &pseudonym,
674            PacketRouting::ForwardPath {
675                forward_path,
676                return_paths,
677            },
678            &PEERS[0].0,
679            ticket,
680            &*MAPPER,
681            &Hash::default(),
682            FLAGS,
683        )?)
684    }
685
686    fn create_packet_from_surb(
687        sender_node: usize,
688        surb_id: HoprSurbId,
689        surb: HoprSurb,
690        hopr_pseudonym: &HoprPseudonym,
691        msg: &[u8],
692    ) -> anyhow::Result<HoprPacket> {
693        assert!((1..=4).contains(&sender_node), "sender_node must be between 1 and 4");
694
695        let ticket = mock_ticket(
696            &PEERS[sender_node - 1].0.public(),
697            surb.additional_data_receiver.proof_of_relay_values().chain_length() as usize,
698        )?;
699
700        Ok(HoprPacket::into_outgoing(
701            msg,
702            hopr_pseudonym,
703            PacketRouting::<TransportPath>::Surb(surb_id, surb),
704            &PEERS[sender_node].0,
705            ticket,
706            &*MAPPER,
707            &Hash::default(),
708            FLAGS,
709        )?
710        .0)
711    }
712
713    fn process_packet_at_node<F>(
714        path_len: usize,
715        node_pos: usize,
716        is_reply: bool,
717        packet: HoprPacket,
718        openers: F,
719    ) -> anyhow::Result<HoprPacket>
720    where
721        F: FnMut(&HoprSenderId) -> Option<ReplyOpener>,
722    {
723        assert!((0..=4).contains(&node_pos), "node position must be between 1 and 3");
724
725        let prev_hop = match (node_pos, is_reply) {
726            (1, false) => *PEERS[0].1.public(),
727            (_, false) => *PEERS[node_pos - 1].1.public(),
728            (3, true) => *PEERS[4].1.public(),
729            (_, true) => *PEERS[node_pos + 1].1.public(),
730        };
731
732        let packet = HoprPacket::from_incoming(&packet.to_bytes(), &PEERS[node_pos].1, prev_hop, &*MAPPER, openers)
733            .context(format!("deserialization failure at hop {node_pos}"))?;
734
735        match &packet {
736            HoprPacket::Final(_) => Ok(packet),
737            HoprPacket::Forwarded(_) => {
738                let next_hop = match (node_pos, is_reply) {
739                    (3, false) => PEERS[4].0.public().clone(),
740                    (_, false) => PEERS[node_pos + 1].0.public().clone(),
741                    (1, true) => PEERS[0].0.public().clone(),
742                    (_, true) => PEERS[node_pos - 1].0.public().clone(),
743                };
744
745                let next_ticket = mock_ticket(&next_hop, path_len)?;
746                Ok(forward(
747                    packet.clone(),
748                    &PEERS[node_pos].0,
749                    next_ticket,
750                    &Hash::default(),
751                ))
752            }
753            HoprPacket::Outgoing(_) => bail!("invalid packet state"),
754        }
755    }
756
757    #[parameterized(hops = { 0,1,2,3 })]
758    fn test_packet_forward_message_no_surb(hops: usize) -> anyhow::Result<()> {
759        let msg = b"some testing forward message";
760        let pseudonym = SimplePseudonym::random();
761        let (mut packet, opener) = create_packet(hops, pseudonym, vec![], msg)?;
762
763        assert!(opener.is_empty());
764        match &packet {
765            HoprPacket::Outgoing { .. } => {}
766            _ => bail!("invalid packet initial state"),
767        }
768
769        let mut actual_plain_text = Box::default();
770        for hop in 1..=hops + 1 {
771            packet = process_packet_at_node(hops + 1, hop, false, packet, |_| None)
772                .context(format!("packet decoding failed at hop {hop}"))?;
773
774            match &packet {
775                HoprPacket::Final(packet) => {
776                    assert_eq!(hop - 1, hops, "final packet must be at the last hop");
777                    assert!(packet.ack_key.is_some(), "must not be a no-ack packet");
778                    assert_eq!(PacketSignals::from(FLAGS), packet.signals);
779                    actual_plain_text = packet.plain_text.clone();
780                }
781                HoprPacket::Forwarded(fwd) => {
782                    assert_eq!(PEERS[hop - 1].1.public(), &fwd.previous_hop, "invalid previous hop");
783                    assert_eq!(PEERS[hop + 1].1.public(), &fwd.outgoing.next_hop, "invalid next hop");
784                    assert_eq!(hops + 1 - hop, fwd.path_pos as usize, "invalid path position");
785                }
786                HoprPacket::Outgoing(_) => bail!("invalid packet state at hop {hop}"),
787            }
788        }
789
790        assert_eq!(actual_plain_text.as_ref(), msg, "invalid plaintext");
791        Ok(())
792    }
793
794    #[parameterized(forward_hops = { 0,1,2,3 }, return_hops = { 0, 1, 2, 3})]
795    fn test_packet_forward_message_with_surb(forward_hops: usize, return_hops: usize) -> anyhow::Result<()> {
796        let msg = b"some testing forward message";
797        let pseudonym = SimplePseudonym::random();
798        let (mut packet, openers) = create_packet(forward_hops, pseudonym, vec![return_hops], msg)?;
799
800        assert_eq!(1, openers.len(), "invalid number of openers");
801        match &packet {
802            HoprPacket::Outgoing { .. } => {}
803            _ => bail!("invalid packet initial state"),
804        }
805
806        let mut received_plain_text = Box::default();
807        let mut received_surbs = vec![];
808        for hop in 1..=forward_hops + 1 {
809            packet = process_packet_at_node(forward_hops + 1, hop, false, packet, |_| None)
810                .context(format!("packet decoding failed at hop {hop}"))?;
811
812            match &packet {
813                HoprPacket::Final(packet) => {
814                    assert_eq!(hop - 1, forward_hops, "final packet must be at the last hop");
815                    assert_eq!(pseudonym, packet.sender, "invalid sender");
816                    assert!(packet.ack_key.is_some(), "must not be a no-ack packet");
817                    assert_eq!(PacketSignals::from(FLAGS), packet.signals);
818                    received_plain_text = packet.plain_text.clone();
819                    received_surbs.extend(packet.surbs.clone());
820                }
821                HoprPacket::Forwarded(fwd) => {
822                    assert_eq!(PEERS[hop - 1].1.public(), &fwd.previous_hop, "invalid previous hop");
823                    assert_eq!(PEERS[hop + 1].1.public(), &fwd.outgoing.next_hop, "invalid next hop");
824                    assert_eq!(forward_hops + 1 - hop, fwd.path_pos as usize, "invalid path position");
825                }
826                HoprPacket::Outgoing(_) => bail!("invalid packet state at hop {hop}"),
827            }
828        }
829
830        assert_eq!(received_plain_text.as_ref(), msg, "invalid plaintext");
831        assert_eq!(1, received_surbs.len(), "invalid number of surbs");
832        assert_eq!(
833            return_hops as u8 + 1,
834            received_surbs[0]
835                .1
836                .additional_data_receiver
837                .proof_of_relay_values()
838                .chain_length(),
839            "surb has invalid por chain length"
840        );
841
842        Ok(())
843    }
844
845    #[parameterized(
846        forward_hops = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 },
847        return_hops  = { 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 }
848    )]
849    fn test_packet_forward_and_reply_message(forward_hops: usize, return_hops: usize) -> anyhow::Result<()> {
850        let pseudonym = SimplePseudonym::random();
851
852        // Forward packet
853        let fwd_msg = b"some testing forward message";
854        let (mut fwd_packet, mut openers) = create_packet(forward_hops, pseudonym, vec![return_hops], fwd_msg)?;
855
856        assert_eq!(1, openers.len(), "invalid number of openers");
857        match &fwd_packet {
858            HoprPacket::Outgoing { .. } => {}
859            _ => bail!("invalid packet initial state"),
860        }
861
862        let mut received_fwd_plain_text = Box::default();
863        let mut received_surbs = vec![];
864        for hop in 1..=forward_hops + 1 {
865            fwd_packet = process_packet_at_node(forward_hops + 1, hop, false, fwd_packet, |_| None)
866                .context(format!("packet decoding failed at hop {hop}"))?;
867
868            match &fwd_packet {
869                HoprPacket::Final(incoming) => {
870                    assert_eq!(hop - 1, forward_hops, "final packet must be at the last hop");
871                    assert_eq!(pseudonym, incoming.sender, "invalid sender");
872                    assert!(incoming.ack_key.is_some(), "must not be a no-ack packet");
873                    assert_eq!(PacketSignals::from(FLAGS), incoming.signals);
874                    received_fwd_plain_text = incoming.plain_text.clone();
875                    received_surbs.extend(incoming.surbs.clone());
876                }
877                HoprPacket::Forwarded(fwd) => {
878                    assert_eq!(PEERS[hop - 1].1.public(), &fwd.previous_hop, "invalid previous hop");
879                    assert_eq!(PEERS[hop + 1].1.public(), &fwd.outgoing.next_hop, "invalid next hop");
880                    assert_eq!(forward_hops + 1 - hop, fwd.path_pos as usize, "invalid path position");
881                }
882                HoprPacket::Outgoing { .. } => bail!("invalid packet state at hop {hop}"),
883            }
884        }
885
886        assert_eq!(received_fwd_plain_text.as_ref(), fwd_msg, "invalid plaintext");
887        assert_eq!(1, received_surbs.len(), "invalid number of surbs");
888        assert_eq!(
889            return_hops as u8 + 1,
890            received_surbs[0]
891                .1
892                .additional_data_receiver
893                .proof_of_relay_values()
894                .chain_length(),
895            "surb has invalid por chain length"
896        );
897
898        // The reply packet
899        let re_msg = b"some testing reply message";
900        let mut re_packet = create_packet_from_surb(
901            forward_hops + 1,
902            received_surbs[0].0,
903            received_surbs[0].1.clone(),
904            &pseudonym,
905            re_msg,
906        )?;
907
908        let mut openers_fn = |p: &HoprSenderId| {
909            assert_eq!(p.pseudonym(), pseudonym);
910            let opener = openers.pop();
911            assert!(opener.as_ref().is_none_or(|(id, _)| id == &p.surb_id()));
912            opener.map(|(_, opener)| opener)
913        };
914
915        match &re_packet {
916            HoprPacket::Outgoing { .. } => {}
917            _ => bail!("invalid packet initial state"),
918        }
919
920        let mut received_re_plain_text = Box::default();
921        for hop in (0..=return_hops).rev() {
922            re_packet = process_packet_at_node(return_hops + 1, hop, true, re_packet, &mut openers_fn)
923                .context(format!("packet decoding failed at hop {hop}"))?;
924
925            match &re_packet {
926                HoprPacket::Final(incoming) => {
927                    assert_eq!(hop, 0, "final packet must be at the last hop");
928                    assert_eq!(pseudonym, incoming.sender, "invalid sender");
929                    assert!(incoming.ack_key.is_some(), "must not be a no-ack packet");
930                    assert!(incoming.surbs.is_empty(), "must not receive surbs on reply");
931                    assert_eq!(PacketSignals::from(FLAGS), incoming.signals);
932                    received_re_plain_text = incoming.plain_text.clone();
933                }
934                HoprPacket::Forwarded(fwd) => {
935                    assert_eq!(PEERS[hop + 1].1.public(), &fwd.previous_hop, "invalid previous hop");
936                    assert_eq!(PEERS[hop - 1].1.public(), &fwd.outgoing.next_hop, "invalid next hop");
937                    assert_eq!(hop, fwd.path_pos as usize, "invalid path position");
938                }
939                HoprPacket::Outgoing(_) => bail!("invalid packet state at hop {hop}"),
940            }
941        }
942
943        assert_eq!(received_re_plain_text.as_ref(), re_msg, "invalid plaintext");
944        Ok(())
945    }
946
947    #[parameterized(
948        forward_hops = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 },
949        return_hops  = { 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 }
950    )]
951    fn test_packet_surbs_only_and_reply_message(forward_hops: usize, return_hops: usize) -> anyhow::Result<()> {
952        let pseudonym = SimplePseudonym::random();
953
954        // Forward packet
955        let (mut fwd_packet, mut openers) = create_packet(forward_hops, pseudonym, vec![return_hops; 2], &[])?;
956
957        assert_eq!(2, openers.len(), "invalid number of openers");
958        match &fwd_packet {
959            HoprPacket::Outgoing { .. } => {}
960            _ => bail!("invalid packet initial state"),
961        }
962
963        let mut received_surbs = vec![];
964        for hop in 1..=forward_hops + 1 {
965            fwd_packet = process_packet_at_node(forward_hops + 1, hop, false, fwd_packet, |_| None)
966                .context(format!("packet decoding failed at hop {hop}"))?;
967
968            match &fwd_packet {
969                HoprPacket::Final(incoming) => {
970                    assert_eq!(hop - 1, forward_hops, "final packet must be at the last hop");
971                    assert!(
972                        incoming.plain_text.is_empty(),
973                        "must not receive plaintext on surbs only packet"
974                    );
975                    assert!(incoming.ack_key.is_some(), "must not be a no-ack packet");
976                    assert_eq!(2, incoming.surbs.len(), "invalid number of received surbs per packet");
977                    assert_eq!(pseudonym, incoming.sender, "invalid sender");
978                    assert_eq!(PacketSignals::from(FLAGS), incoming.signals);
979                    received_surbs.extend(incoming.surbs.clone());
980                }
981                HoprPacket::Forwarded(fwd) => {
982                    assert_eq!(PEERS[hop - 1].1.public(), &fwd.previous_hop, "invalid previous hop");
983                    assert_eq!(PEERS[hop + 1].1.public(), &fwd.outgoing.next_hop, "invalid next hop");
984                    assert_eq!(forward_hops + 1 - hop, fwd.path_pos as usize, "invalid path position");
985                }
986                HoprPacket::Outgoing { .. } => bail!("invalid packet state at hop {hop}"),
987            }
988        }
989
990        assert_eq!(2, received_surbs.len(), "invalid number of surbs");
991        for recv_surb in &received_surbs {
992            assert_eq!(
993                return_hops as u8 + 1,
994                recv_surb
995                    .1
996                    .additional_data_receiver
997                    .proof_of_relay_values()
998                    .chain_length(),
999                "surb has invalid por chain length"
1000            );
1001        }
1002
1003        let mut openers_fn = |p: &HoprSenderId| {
1004            assert_eq!(p.pseudonym(), pseudonym);
1005            let (id, opener) = openers.remove(0);
1006            assert_eq!(id, p.surb_id());
1007            Some(opener)
1008        };
1009
1010        // The reply packet
1011        for (i, recv_surb) in received_surbs.into_iter().enumerate() {
1012            let re_msg = format!("some testing reply message {i}");
1013            let mut re_packet = create_packet_from_surb(
1014                forward_hops + 1,
1015                recv_surb.0,
1016                recv_surb.1,
1017                &pseudonym,
1018                re_msg.as_bytes(),
1019            )?;
1020
1021            match &re_packet {
1022                HoprPacket::Outgoing { .. } => {}
1023                _ => bail!("invalid packet initial state in reply {i}"),
1024            }
1025
1026            let mut received_re_plain_text = Box::default();
1027            for hop in (0..=return_hops).rev() {
1028                re_packet = process_packet_at_node(return_hops + 1, hop, true, re_packet, &mut openers_fn)
1029                    .context(format!("packet decoding failed at hop {hop} in reply {i}"))?;
1030
1031                match &re_packet {
1032                    HoprPacket::Final(incoming) => {
1033                        assert_eq!(hop, 0, "final packet must be at the last hop for reply {i}");
1034                        assert!(incoming.ack_key.is_some(), "must not be a no-ack packet");
1035                        assert!(
1036                            incoming.surbs.is_empty(),
1037                            "must not receive surbs on reply for reply {i}"
1038                        );
1039                        assert_eq!(PacketSignals::from(FLAGS), incoming.signals);
1040                        received_re_plain_text = incoming.plain_text.clone();
1041                    }
1042                    HoprPacket::Forwarded(fwd) => {
1043                        assert_eq!(
1044                            PEERS[hop + 1].1.public(),
1045                            &fwd.previous_hop,
1046                            "invalid previous hop in reply {i}"
1047                        );
1048                        assert_eq!(
1049                            PEERS[hop - 1].1.public(),
1050                            &fwd.outgoing.next_hop,
1051                            "invalid next hop in reply {i}"
1052                        );
1053                        assert_eq!(hop, fwd.path_pos as usize, "invalid path position in reply {i}");
1054                    }
1055                    HoprPacket::Outgoing(_) => bail!("invalid packet state at hop {hop} in reply {i}"),
1056                }
1057            }
1058
1059            assert_eq!(
1060                received_re_plain_text.as_ref(),
1061                re_msg.as_bytes(),
1062                "invalid plaintext in reply {i}"
1063            );
1064        }
1065        Ok(())
1066    }
1067}