Skip to main content

OnionPacket

Struct OnionPacket 

Source
pub struct OnionPacket {
    pub version: u8,
    pub public_key: PublicKey,
    pub packet_data: Vec<u8>,
    pub hmac: [u8; 32],
}
Expand description

Onion packet to send encrypted message via multiple hops.

Fields§

§version: u8

Version of the onion packet, currently 0

§public_key: PublicKey

The public key of the next hop. Alpha in the specification.

§packet_data: Vec<u8>

Encrypted packet data. Beta in the specification.

§hmac: [u8; 32]

HMAC of the packet data. Gamma in the specification.

Implementations§

Source§

impl OnionPacket

Source

pub fn create<C: Signing>( session_key: SecretKey, hops_path: Vec<PublicKey>, hops_data: Vec<Vec<u8>>, assoc_data: Option<Vec<u8>>, packet_data_len: usize, secp_ctx: &Secp256k1<C>, ) -> Result<OnionPacket, SphinxError>

Creates the new onion packet for the first hop.

  • hops_path: The public keys for each hop. These are yi in the specification.
  • session_key: The ephemeral secret key for the onion packet. It must be generated securely using a random process. This is x in the specification.
  • hops_data: The unencrypted data for each hop. Attention that the data for each hop will be concatenated with the remaining encrypted data. To extract the data, the receiver must know the data length. For example, the hops data can include its length at the beginning. These are mi in the specification.
  • assoc_data: The associated data. It will not be included in the packet itself but will be covered by the packet’s HMAC. This allows each hop to verify that the associated data has not been tampered with. This is A in the specification.
  • onion_packet_len: The length of the onion packet. The packet has the same size for each hop.
Source

pub fn into_bytes(self) -> Vec<u8>

Converts the onion packet into a byte vector.

Source

pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, SphinxError>

Converts back from a byte vector.

Source

pub fn extract_public_key_from_slice( bytes: &[u8], ) -> Result<PublicKey, SphinxError>

Source

pub fn shared_secret(&self, secret_key: &SecretKey) -> [u8; 32]

Derives the shared secret using the node secret key and the ephemeral public key in the onion packet.

Source

pub fn peel<C, F>( self, secret_key: &SecretKey, assoc_data: Option<&[u8]>, secp_ctx: &Secp256k1<C>, get_hop_data_len: F, ) -> Result<(Vec<u8>, Self), SphinxError>
where C: Verification, F: FnOnce(&[u8]) -> Option<usize>,

Peels the onion packet at the current hop.

  • secret_key: the node private key. xi in the specification.
  • assoc_data: The associated data. It was covered by the onion packet’s HMAC. A in the specification.
  • get_hop_data_len: Tell the hop data len given the decrypted packet data for the current hop.

Returns a tuple (m, p) where m is the hop data for the current hop, and p is remaining onion packet for the next hop.

Trait Implementations§

Source§

impl Clone for OnionPacket

Source§

fn clone(&self) -> OnionPacket

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for OnionPacket

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for OnionPacket

Source§

fn eq(&self, other: &OnionPacket) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for OnionPacket

Source§

impl StructuralPartialEq for OnionPacket

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.