diff --git a/pkgs/dht/Cargo.lock b/pkgs/dht/Cargo.lock index 717036154c..01bdd72465 100644 --- a/pkgs/dht/Cargo.lock +++ b/pkgs/dht/Cargo.lock @@ -698,8 +698,10 @@ name = "dht" version = "0.1.0" dependencies = [ "anyhow", + "dht-rotation", "distributed-topic-tracker", "ed25519-dalek", + "futures-lite", "iroh", "iroh-gossip", "rand 0.9.3", @@ -707,6 +709,13 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "dht-rotation" +version = "0.1.0" +dependencies = [ + "sha2 0.10.9", +] + [[package]] name = "diatomic-waker" version = "0.2.3" diff --git a/pkgs/dht/Cargo.toml b/pkgs/dht/Cargo.toml index d557168592..088fda981c 100644 --- a/pkgs/dht/Cargo.toml +++ b/pkgs/dht/Cargo.toml @@ -3,12 +3,18 @@ name = "dht" version = "0.1.0" edition = "2024" +[workspace] +members = [".", "dht-rotation"] +resolver = "3" + [dependencies] anyhow = "1.0.100" distributed-topic-tracker = { git = "https://github.com/rustonbsd/distributed-topic-tracker", version = "0.2.5" } ed25519-dalek = "3.0.0-pre.1" +futures-lite = "2" iroh = "0.95" iroh-gossip = "0.95" rand = "0.9.2" tokio = { version = "1.48.0", features = ["full"] } -tracing-subscriber = "0.3.20" +tracing-subscriber = { version = "0.3.20", features = ["env-filter"] } +dht-rotation = { path = "dht-rotation" } diff --git a/pkgs/dht/dht-rotation/Cargo.toml b/pkgs/dht/dht-rotation/Cargo.toml new file mode 100644 index 0000000000..911102df72 --- /dev/null +++ b/pkgs/dht/dht-rotation/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "dht-rotation" +version = "0.1.0" +edition = "2024" + +[dependencies] +sha2 = "0.10.9" diff --git a/pkgs/dht/dht-rotation/src/lib.rs b/pkgs/dht/dht-rotation/src/lib.rs new file mode 100644 index 0000000000..2c7048d0aa --- /dev/null +++ b/pkgs/dht/dht-rotation/src/lib.rs @@ -0,0 +1,45 @@ +use sha2::{Digest, Sha512}; + +/// Derive the 32-byte secret-rotation key for a topic and minute. +/// +/// This crate intentionally has no dependency on the DHT or its rotation +/// trait, keeping the deterministic derivation independently reusable. +pub fn derive(topic_hash: [u8; 32], unix_minute: u64, initial_secret_hash: [u8; 32]) -> [u8; 32] { + let mut hash = Sha512::new(); + hash.update(topic_hash); + hash.update(unix_minute.to_be_bytes()); + hash.update(initial_secret_hash); + hash.update(b"rotate"); + hash.finalize()[..32] + .try_into() + .expect("SHA-512 output is 64 bytes") +} + +#[cfg(test)] +mod tests { + use super::derive; + + #[test] + fn derive_matches_the_fixed_legacy_vector() { + let topic_hash = [0x11; 32]; + let unix_minute = 0x0102_0304_0506_0708; + let initial_secret_hash = [0x22; 32]; + let expected = [ + 0x70, 0xf6, 0xbe, 0x26, 0xf8, 0x91, 0x27, 0xd2, 0xba, 0x46, 0x9b, 0x80, 0x58, 0xe3, + 0x37, 0x17, 0x02, 0x6b, 0x27, 0x4c, 0x01, 0x49, 0xe3, 0x2e, 0x1f, 0x2b, 0x0a, 0xb3, + 0x42, 0x3c, 0x70, 0xf1, + ]; + + assert_eq!( + derive(topic_hash, unix_minute, initial_secret_hash), + expected + ); + } + + #[test] + fn derive_returns_exactly_32_bytes() { + let key = derive([0; 32], 0, [0; 32]); + + assert_eq!(key.len(), 32); + } +} diff --git a/pkgs/dht/src/main.rs b/pkgs/dht/src/main.rs index f43b85d2d1..fbc9fb237f 100644 --- a/pkgs/dht/src/main.rs +++ b/pkgs/dht/src/main.rs @@ -1,74 +1,83 @@ -use anyhow::result; -use iroh::endpoint::{endpoint, secretkey}; -use iroh_gossip::net::gossip; -use distributed_topic_tracker::{autodiscoverygossip, recordpublisher, topicid, secretrotation, rotationhandle}; -use ed25519_dalek::signingkey; -use futures_lite::streamext; +use anyhow::Result; +use distributed_topic_tracker::{ + AutoDiscoveryGossip, RecordPublisher, RotationHandle, SecretRotation, TopicId, +}; +use ed25519_dalek::SigningKey; +use iroh::{Endpoint, SecretKey}; +use iroh_gossip::{api::Event, net::Gossip}; -struct mysecretrotation; +struct MySecretRotation; -impl secretrotation for mysecretrotation { - fn derive(&self, topic_hash: [u8; 32], unix_minute: u64, initial_secret_hash: [u8; 32]) -> [u8; 32] { - use sha2::{sha512, digest::digest; - let mut hash = sha512::new(); - hash.update(topic_hash); - hash.update(unix_minute.to_be_bytes()); - hash.update(initial_secret_hash); - hash.update(b"rotate"); - hash.finalize()[..32].try_into().expect("hash") +impl SecretRotation for MySecretRotation { + fn derive( + &self, + topic_hash: [u8; 32], + unix_minute: u64, + initial_secret_hash: [u8; 32], + ) -> [u8; 32] { + dht_rotation::derive(topic_hash, unix_minute, initial_secret_hash) } } #[tokio::main] -async fn main() -> result<()> { +async fn main() -> Result<()> { tracing_subscriber::fmt() .with_thread_ids(true) .with_ansi(true) - .with_env_filter(tracing_subscriber::envfilter::try_from_default_env() - .unwrap_or_else(|_| "distributed_topic_tracker=info".parse().unwrap())) + .with_env_filter( + tracing_subscriber::EnvFilter::try_from_default_env() + .unwrap_or_else(|_| "distributed_topic_tracker=info".parse().unwrap()), + ) .init(); - let secret_key = secretkey::generate(&mut rand::rng()); - let signing_key = signingkey::from_bytes(&secret_key.to_bytes()); + let secret_key = SecretKey::generate(&mut rand::rng()); + let signing_key = SigningKey::from_bytes(&secret_key.to_bytes()); - let endpoint = endpoint::builder() + let endpoint = Endpoint::builder() .secret_key(secret_key.clone()) - .bind().await?; + .bind() + .await?; - let gossip = gossip::builder().spawn(endpoint.clone()); - let _router = iroh::protocol::router::builder(endpoint.clone()) - .accept(iroh_gossip::alpn, gossip.clone()) + let gossip = Gossip::builder().spawn(endpoint.clone()); + let _router = iroh::protocol::Router::builder(endpoint.clone()) + .accept(iroh_gossip::ALPN, gossip.clone()) .spawn(); - let topic_id = topicid::new("my-iroh-gossip-topic".to_string()); + let topic_id = TopicId::new("my-iroh-gossip-topic".to_string()); let initial_secret = b"my-initial-secret".to_vec(); - let record_publisher = recordpublisher::new( + let record_publisher = RecordPublisher::new( topic_id.clone(), signing_key.verifying_key(), signing_key.clone(), - Some(rotationhandle::new(mysecretrotation)), + Some(RotationHandle::new(MySecretRotation)), initial_secret, ); - let (gossip_sender, mut gossip_receiver) = gossip - .subscribe_and_join_with_auto_discovery(record_publisher).await? - .split()?; + let (gossip_sender, gossip_receiver) = gossip + .subscribe_and_join_with_auto_discovery(record_publisher) + .await? + .split() + .await?; - println!("joined topic: {}", &topic_id); - println!("node id: {}", &endpoint.node_id().to_string()[..8]); + println!("joined topic: {}", topic_id.raw()); + let node_id = endpoint.id().to_string(); + println!("node id: {}", &node_id[..8]); tokio::spawn(async move { while let Some(event) = gossip_receiver.next().await { match event { - Ok(gossip::event::Event::Received(msg)) => { - println!("\n{}: {}", &msg.delivered_from.to_string()[..8], - String::from_utf8_lossy(&msg.content)); + Ok(Event::Received(msg)) => { + println!( + "\n{}: {}", + &msg.delivered_from.to_string()[..8], + String::from_utf8_lossy(&msg.content) + ); } - Ok(gossip::event::Event::NeighborUp(peer)) => { + Ok(Event::NeighborUp(peer)) => { println!("\njoined: {}", &peer.to_string()[..8]); } - Ok(gossip::event::Event::NeighborDown(peer)) => { + Ok(Event::NeighborDown(peer)) => { println!("\nleft: {}", &peer.to_string()[..8]); } _ => {}