Skip to main content

OnionErrorPacket

Struct OnionErrorPacket 

Source
pub struct OnionErrorPacket {
    pub packet_data: Vec<u8>,
}
Expand description

Onion error packet to return errors to the origin node.

The nodes must store the shared secrets to forward OnionPacket locally and reuse them to obfuscate the error packet. See the section “Returning Errors” in the specification for details.

§Example

use secp256k1::{PublicKey, SecretKey, Secp256k1};
use std::str::FromStr;
use fiber_sphinx::{OnionErrorPacket, OnionPacket, OnionSharedSecretIter};

let secp = Secp256k1::new();
let hops_path = vec![
  PublicKey::from_str("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").expect("valid public key"),
  PublicKey::from_str("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").expect("valid public key"),
  PublicKey::from_str("027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007").expect("valid public key"),
];
let session_key = SecretKey::from_slice(&[0x41; 32]).expect("32 bytes, within curve order");
let hops_ss = OnionSharedSecretIter::new(hops_path.iter(), session_key, &secp).collect::<Vec<_>>();

// The node 0324653...0ab1c generates the error
let shared_secret = hops_ss[1];
let error_packet = OnionErrorPacket::create(&shared_secret, b"error message".to_vec());

Fields§

§packet_data: Vec<u8>

Encrypted error-returning packet data.

Implementations§

Source§

impl OnionErrorPacket

Source

pub fn create(shared_secret: &[u8; 32], payload: Vec<u8>) -> Self

Creates an onion error packet using the erring node shared secret.

The erring node should store the shared secrets to forward the onion packet locally and reuse them to obfuscate the error packet.

The shared secret can be obtained via OnionPacket::shared_secret.

Source

pub fn concat(hmac: [u8; 32], payload: Vec<u8>) -> Self

Concatenates HMAC and the payload without encryption.

Source

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

Encrypts or decrypts the packet data with the chacha20 stream.

Apply XOR on the packet data with the keystream generated by the chacha20 stream cipher.

Source

pub fn parse<F, T>( self, hops_path: Vec<PublicKey>, session_key: SecretKey, parse_payload: F, ) -> Option<(T, usize)>
where F: Fn(&[u8]) -> Option<T>,

Decrypts the packet data and parses the error message.

This method is for the origin node to decrypts the packet data node by node and try to parse the message.

  • 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.
  • parse_payload: A function to parse the error payload from the decrypted packet data. It should return Some(T) if the given buffer starts with a valid error payload, otherwise None.

Returns the parsed error message and the erring node index in hops_path if the HMAC is valid and the error message is successfully parsed by the function parse_payload.

Source

pub fn split(self) -> ([u8; 32], Vec<u8>)

Splits into HMAC and payload without decryption.

Source

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

Converts the onion packet into a byte vector.

Source

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

Trait Implementations§

Source§

impl Clone for OnionErrorPacket

Source§

fn clone(&self) -> OnionErrorPacket

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 OnionErrorPacket

Source§

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

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

impl PartialEq for OnionErrorPacket

Source§

fn eq(&self, other: &OnionErrorPacket) -> 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 OnionErrorPacket

Source§

impl StructuralPartialEq for OnionErrorPacket

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.