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: u8Version of the onion packet, currently 0
public_key: PublicKeyThe 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
impl OnionPacket
Sourcepub 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>
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.
If this returns SphinxError::InvalidBlindingFactor, retry with a different session key.
Sourcepub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn into_bytes(self) -> Vec<u8> ⓘ
Converts the onion packet into a byte vector.
Sourcepub fn from_bytes_with_packet_data_len(
bytes: Vec<u8>,
packet_data_len: usize,
) -> Result<Self, SphinxError>
pub fn from_bytes_with_packet_data_len( bytes: Vec<u8>, packet_data_len: usize, ) -> Result<Self, SphinxError>
Converts back from a byte vector with the expected packet data length.
An onion packet has the layout version || public_key || packet_data || hmac.
This function verifies that bytes is exactly
PACKET_VERSION_LEN + PACKET_PUBLIC_KEY_LEN + packet_data_len + PACKET_HMAC_LEN
bytes before copying the HMAC and packet data.
Sourcepub fn from_bytes(bytes: Vec<u8>) -> Result<Self, SphinxError>
👎Deprecated since 2.4.0: use OnionPacket::from_bytes_with_packet_data_len to verify the packet data length
pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, SphinxError>
use OnionPacket::from_bytes_with_packet_data_len to verify the packet data length
Converts back from a byte vector.
Deprecated because this function accepts any packet data length that can be
inferred from bytes. Use OnionPacket::from_bytes_with_packet_data_len
when the expected packet data length is known.
pub fn extract_public_key_from_slice( bytes: &[u8], ) -> Result<PublicKey, SphinxError>
Derives the shared secret using the node secret key and the ephemeral public key in the onion packet.
Sourcepub 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>
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>
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
impl Clone for OnionPacket
Source§fn clone(&self) -> OnionPacket
fn clone(&self) -> OnionPacket
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for OnionPacket
impl Debug for OnionPacket
Source§impl PartialEq for OnionPacket
impl PartialEq for OnionPacket
Source§fn eq(&self, other: &OnionPacket) -> bool
fn eq(&self, other: &OnionPacket) -> bool
self and other values to be equal, and is used by ==.