6 releases (stable)
new 2.1.0 | Nov 29, 2024 |
---|---|
2.0.0 | Nov 28, 2024 |
1.0.1 | Sep 29, 2024 |
0.2.0 | Sep 25, 2024 |
0.1.1 | Sep 23, 2024 |
#621 in Network programming
1,924 downloads per month
270KB
736 lines
Fiber Sphinx
A Rust implementation of Sphinx (a.k.a. Onion Message) for Fiber.
lib.rs
:
A Rust implementation of Sphinx (a.k.a. Onion Message) for Fiber.
See more in the Specification.
Example
use secp256k1::{PublicKey, SecretKey, Secp256k1};
use fiber_sphinx::OnionPacket;
let secp = Secp256k1::new();
let hops_keys = vec![
SecretKey::from_slice(&[0x20; 32]).expect("32 bytes, within curve order"),
SecretKey::from_slice(&[0x21; 32]).expect("32 bytes, within curve order"),
SecretKey::from_slice(&[0x22; 32]).expect("32 bytes, within curve order"),
];
let hops_path = hops_keys.iter().map(|sk| sk.public_key(&secp)).collect();
let session_key = SecretKey::from_slice(&[0x41; 32]).expect("32 bytes, within curve order");
// Use the first byte to indicate the data len
let hops_data = vec![vec![0], vec![1, 0], vec![5, 0, 1, 2, 3, 4]];
let get_length = |packet_data: &[u8]| Some(packet_data[0] as usize + 1);
let assoc_data = vec![0x42u8; 32];
let packet = OnionPacket::create(
session_key,
hops_path,
hops_data.clone(),
Some(assoc_data.clone()),
1300,
&secp,
).expect("new onion packet");
// Hop 0
let res = packet.peel(&hops_keys[0], Some(&assoc_data), &secp, get_length);
assert!(res.is_ok());
let (data, packet) = res.unwrap();
assert_eq!(data, hops_data[0]);
// Hop 1
let res = packet.peel(&hops_keys[1], Some(&assoc_data), &secp, get_length);
assert!(res.is_ok());
let (data, packet) = res.unwrap();
assert_eq!(data, hops_data[1]);
// Hop 2
let res = packet.peel(&hops_keys[2], Some(&assoc_data), &secp, get_length);
assert!(res.is_ok());
let (data, _packet) = res.unwrap();
assert_eq!(data, hops_data[2]);
Dependencies
~7MB
~86K SLoC