Skip to content

Commit

Permalink
KeyShare process
Browse files Browse the repository at this point in the history
  • Loading branch information
petarjuki7 committed Nov 12, 2024
1 parent 6833399 commit d2394e9
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 5 deletions.
4 changes: 2 additions & 2 deletions crates/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ calimero-crypto = { path = "../crypto" }
calimero-blobstore = { path = "../store/blobs" }
calimero-network = { path = "../network" }
calimero-node-primitives = { path = "../node-primitives" }
calimero-primitives = { path = "../primitives" }
calimero-primitives = { path = "../primitives", features = ["borsh"] }
calimero-runtime = { path = "../runtime" }
calimero-server = { path = "../server", features = ["jsonrpc", "websocket", "admin"] }
calimero-store = { path = "../store", features = ["datatypes"] }
calimero-context-config = { path = "../context/config" }
calimero-context-config = { path = "../context/config" }

[lints]
workspace = true
10 changes: 10 additions & 0 deletions crates/node/src/interactive_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<()> {
Expand All @@ -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(())
Expand Down
8 changes: 8 additions & 0 deletions crates/node/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::types::{InitPayload, StreamMessage};
use crate::Node;

mod blobs;
mod key_share;
mod state;

#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -118,6 +119,9 @@ impl Node {

let mut stream = self.network_client.open_stream(chosen_peer).await?;

self.initiate_key_share_process(&mut context, our_identity, &mut stream)
.await?;

if !self.ctx_manager.has_blob_available(application.blob)? {
self.initiate_blob_share_process(
&context,
Expand Down Expand Up @@ -223,6 +227,10 @@ impl Node {
)
.await?
}
InitPayload::KeyShare {} => {
self.handle_key_share_request(context, their_identity, stream)
.await?
}
};

Ok(Some(()))
Expand Down
92 changes: 92 additions & 0 deletions crates/node/src/sync/key_share.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use calimero_crypto::SharedKey;
use calimero_network::stream::Stream;
use calimero_primitives::context::Context;
use calimero_primitives::identity::PublicKey;
use eyre::{bail, OptionExt};
use rand::seq::IteratorRandom;
use rand::thread_rng;
use tracing::debug;

use crate::sync::{recv, send, Sequencer};
use crate::types::{InitPayload, MessagePayload, StreamMessage};
use crate::Node;

impl Node {
pub(super) async fn initiate_key_share_process(
&self,
context: &mut Context,
our_identity: PublicKey,
stream: &mut Stream,
) -> eyre::Result<()> {
send(
stream,
&StreamMessage::Init {
context_id: context.id,
party_id: our_identity,
payload: InitPayload::KeyShare {},
},
None,
)
.await?;

let Some(ack) = recv(stream, self.sync_config.timeout, None).await? else {
bail!("connection closed while awaiting state sync handshake");
};

let sender_key = match ack {
StreamMessage::Message {
payload: MessagePayload::KeyShare { sender_key },
..
} => sender_key,
unexpected @ (StreamMessage::Init { .. }
| StreamMessage::Message { .. }
| StreamMessage::OpaqueError) => {
bail!("unexpected message: {:?}", unexpected)
}
};

// Do I store "his" SenderKey somewhere?

Ok(())
}

pub(super) async fn handle_key_share_request(
&self,
context: Context,
their_identity: PublicKey,
stream: &mut Stream,
) -> eyre::Result<()> {
debug!(
context_id=%context.id,
their_identity=%their_identity,
"Received key share request",
);

let identities = self.ctx_manager.get_context_owned_identities(context.id)?;

let Some(our_identity) = identities.into_iter().choose(&mut thread_rng()) else {
bail!("no identities found for context: {}", context.id);
};

let sender_key = self
.ctx_manager
.get_sender_key(&context.id, &our_identity)?
.ok_or_eyre("expected own identity to have sender key")?;

let mut sequencer = Sequencer::default();

let shared_key = SharedKey::new(&sender_key, &our_identity);

send(
stream,
&StreamMessage::Message {
sequence_id: sequencer.next(),
payload: MessagePayload::KeyShare { sender_key },
},
Some(shared_key), // or None?
)
.await?;

Ok(())
}
}
3 changes: 2 additions & 1 deletion crates/node/src/sync/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ impl Node {
application_id: context.application_id,
},
},
Some(shared_key),
None, // I think it should be None here,
// because the first recv in the function above has to have some way of decrypting it?
)
.await?;

Expand Down
6 changes: 4 additions & 2 deletions crates/node/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use calimero_primitives::application::ApplicationId;
use calimero_primitives::blobs::BlobId;
use calimero_primitives::context::ContextId;
use calimero_primitives::hash::Hash;
use calimero_primitives::identity::PublicKey;
use calimero_primitives::identity::{PrivateKey, PublicKey};
use serde::Deserialize;

#[derive(Debug, BorshSerialize, BorshDeserialize)]
Expand Down Expand Up @@ -47,12 +47,14 @@ pub enum InitPayload {
root_hash: Hash,
application_id: ApplicationId,
},
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 },
}

#[derive(Deserialize)]
Expand Down
4 changes: 4 additions & 0 deletions crates/primitives/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ use crate::context::ContextId;
use crate::hash::{Hash, HashError};

#[derive(Eq, Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshDeserialize, borsh::BorshSerialize)
)]
pub struct PrivateKey(Hash);

impl From<[u8; 32]> for PrivateKey {
Expand Down
36 changes: 36 additions & 0 deletions file.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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(())
}

0 comments on commit d2394e9

Please sign in to comment.