diff --git a/crates/node/src/sync.rs b/crates/node/src/sync.rs index cf4f00c94..0fcf00401 100644 --- a/crates/node/src/sync.rs +++ b/crates/node/src/sync.rs @@ -16,7 +16,7 @@ use crate::types::{InitPayload, StreamMessage}; use crate::Node; mod blobs; -mod key_share; +mod key; mod state; #[derive(Copy, Clone, Debug)] @@ -227,7 +227,7 @@ impl Node { ) .await? } - InitPayload::KeyShare {} => { + InitPayload::KeyShare => { self.handle_key_share_request(context, their_identity, stream) .await? } diff --git a/crates/node/src/sync/blobs.rs b/crates/node/src/sync/blobs.rs index 2ce868232..8e6803d5d 100644 --- a/crates/node/src/sync/blobs.rs +++ b/crates/node/src/sync/blobs.rs @@ -162,7 +162,7 @@ impl Node { party_id: our_identity, payload: InitPayload::BlobShare { blob_id }, }, - Some(shared_key), + None, ) .await?; diff --git a/crates/node/src/sync/key_share.rs b/crates/node/src/sync/key.rs similarity index 68% rename from crates/node/src/sync/key_share.rs rename to crates/node/src/sync/key.rs index d55e92d83..097893387 100644 --- a/crates/node/src/sync/key_share.rs +++ b/crates/node/src/sync/key.rs @@ -23,7 +23,7 @@ impl Node { &StreamMessage::Init { context_id: context.id, party_id: our_identity, - payload: InitPayload::KeyShare {}, + payload: InitPayload::KeyShare, }, None, ) @@ -33,15 +33,35 @@ impl Node { bail!("connection closed while awaiting state sync handshake"); }; - let (sender_key, their_identity) = match ack { + let their_identity = match ack { + StreamMessage::Init { + party_id, + payload: InitPayload::KeyShare, + .. + } => party_id, + unexpected @ (StreamMessage::Init { .. } + | StreamMessage::Message { .. } + | StreamMessage::OpaqueError) => { + bail!("unexpected message: {:?}", unexpected) + } + }; + + let private_key = self + .ctx_manager + .get_private_key(context.id, our_identity)? + .ok_or_eyre("expected own identity to have private key")?; + + let shared_key = SharedKey::new(&private_key, &their_identity); + + let Some(ack) = recv(stream, self.sync_config.timeout, Some(shared_key)).await? else { + bail!("connection closed while awaiting state sync handshake"); + }; + + let sender_key = match ack { StreamMessage::Message { - payload: - MessagePayload::KeyShare { - sender_key, - public_key: their_identity, - }, + payload: MessagePayload::KeyShare { sender_key }, .. - } => (sender_key, their_identity), + } => sender_key, unexpected @ (StreamMessage::Init { .. } | StreamMessage::Message { .. } | StreamMessage::OpaqueError) => { @@ -90,6 +110,17 @@ impl Node { bail!("no identities found for context: {}", context.id); }; + send( + stream, + &StreamMessage::Init { + context_id: context.id, + party_id: our_identity, + payload: InitPayload::KeyShare, + }, + None, + ) + .await?; + let sender_key = self .ctx_manager .get_sender_key(&context.id, &our_identity)? @@ -103,10 +134,7 @@ impl Node { stream, &StreamMessage::Message { sequence_id: sequencer.next(), - payload: MessagePayload::KeyShare { - sender_key, - public_key: our_identity, - }, + payload: MessagePayload::KeyShare { sender_key }, }, Some(shared_key), // or None? ) diff --git a/crates/node/src/sync/state.rs b/crates/node/src/sync/state.rs index b176adf8d..4a989d684 100644 --- a/crates/node/src/sync/state.rs +++ b/crates/node/src/sync/state.rs @@ -144,8 +144,7 @@ impl Node { application_id: context.application_id, }, }, - None, // I think it should be None here, - // because the first recv in the function above has to have some way of decrypting it? + None, ) .await?; diff --git a/crates/node/src/types.rs b/crates/node/src/types.rs index 2fd7beeef..00649b15c 100644 --- a/crates/node/src/types.rs +++ b/crates/node/src/types.rs @@ -47,21 +47,14 @@ pub enum InitPayload { root_hash: Hash, application_id: ApplicationId, }, - KeyShare {}, + KeyShare, } -// this I was encrypting + #[derive(Debug, BorshSerialize, BorshDeserialize)] pub enum MessagePayload<'a> { - StateSync { - artifact: Cow<'a, [u8]>, - }, - BlobShare { - chunk: Cow<'a, [u8]>, - }, - KeyShare { - sender_key: PrivateKey, - public_key: PublicKey, - }, + StateSync { artifact: Cow<'a, [u8]> }, + BlobShare { chunk: Cow<'a, [u8]> }, + KeyShare { sender_key: PrivateKey }, } #[derive(Deserialize)] diff --git a/file.patch b/file.patch deleted file mode 100644 index 95e0fa137..000000000 --- a/file.patch +++ /dev/null @@ -1,36 +0,0 @@ -diff --git a/crates/node/src/interactive_cli.rs b/crates/node/src/interactive_cli.rs -index 2d720da4..d8bddbbc 100644 ---- a/crates/node/src/interactive_cli.rs -+++ b/crates/node/src/interactive_cli.rs -@@ -12,6 +12,7 @@ pub mod peers; - pub mod state; - pub mod store; - -+use calimero_primitives::blobs::BlobId; - use clap::{Parser, Subcommand}; - - use crate::Node; -@@ -34,6 +35,7 @@ pub enum SubCommand { - Peers(peers::PeersCommand), - // Store(store::StoreCommand), - State(state::StateCommand), -+ Connect, - } - - pub async fn handle_line(node: &mut Node, line: String) -> eyre::Result<()> { -@@ -59,6 +61,14 @@ pub async fn handle_line(node: &mut Node, line: String) -> eyre::Result<()> { - SubCommand::Peers(peers) => peers.run(node.network_client.clone().into()).await?, - SubCommand::State(state) => state.run(node)?, - // SubCommand::Store(store) => store.run(node)?, -+ SubCommand::Connect => { -+ let addr = -+ "/ip4/18.156.18.6/udp/4001/quic-v1/p2p/12D3KooWMgoF9xzyeKJHtRvrYwdomheRbHPELagWZwTLmXb6bCVC"; -+ -+ let res = node.network_client.dial(addr.parse()?).await; -+ -+ dbg!(res); -+ } - } - - Ok(()) - } \ No newline at end of file