Skip to content

Commit

Permalink
Merge branch 'release/1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
doitian committed Sep 26, 2024
2 parents debc6bc + 5f8c9e3 commit fa2accf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fiber-sphinx"
version = "0.2.0"
version = "1.0.0"
edition = "2021"
license-file = "COPYING.md"
description = "A Rust implementation of the Sphinx mix network."
Expand Down
50 changes: 50 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,26 @@ impl OnionPacket {
bytes
}

/// Converts back from a byte vector.
pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, SphinxError> {
if bytes.len() < 66 {
return Err(SphinxError::PacketDataLenTooSmall);
}
let version = bytes[0];
let public_key =
PublicKey::from_slice(&bytes[1..34]).map_err(|_| SphinxError::PublicKeyInvalid)?;
let packet_data = (&bytes[34..(bytes.len() - 32)]).into();
let mut hmac = [0u8; 32];
hmac.copy_from_slice(&bytes[(bytes.len() - 32)..]);

Ok(Self {
version,
public_key,
packet_data,
hmac,
})
}

/// Derives the shared secret using the node secret key and the ephemeral public key in the onion packet.
pub fn shared_secret(&self, secret_key: &SecretKey) -> [u8; 32] {
SharedSecret::new(&self.public_key, secret_key).secret_bytes()
Expand Down Expand Up @@ -342,6 +362,10 @@ impl OnionErrorPacket {
pub fn into_bytes(self) -> Vec<u8> {
self.packet_data
}

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

#[derive(Error, Debug, Eq, PartialEq)]
Expand All @@ -360,6 +384,12 @@ pub enum SphinxError {

#[error("The parsed data len is larger than the onion packet len")]
HopDataLenTooLarge,

#[error("The parsed data len is too small")]
PacketDataLenTooSmall,

#[error("Invalid public key")]
PublicKeyInvalid,
}

/// Keys used to forward the onion packet.
Expand Down Expand Up @@ -703,6 +733,26 @@ mod tests {
]
}

#[test]
fn test_onion_packet_from_bytes() {
let public_key = PublicKey::from_slice(
Vec::from_hex("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619")
.expect("valid hex")
.as_ref(),
)
.expect("valid public key");
let packet = OnionPacket {
version: 1,
public_key,
packet_data: vec![2],
hmac: [3; 32],
};
let packet_from_bytes_res = OnionPacket::from_bytes(packet.clone().into_bytes());
assert!(packet_from_bytes_res.is_ok());
let packet_from_bytes = packet_from_bytes_res.unwrap();
assert_eq!(packet_from_bytes, packet);
}

#[test]
fn test_derive_hops_keys() {
let hops_path = get_test_hops_path();
Expand Down

0 comments on commit fa2accf

Please sign in to comment.