Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Network keypair to enclave init. #209

Merged
merged 9 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update repositories
  • Loading branch information
hmzakhalid committed Dec 19, 2024
commit a8fd998d81f0ddb04c191388cb86e3bb1c7a9d9d
2 changes: 1 addition & 1 deletion packages/ciphernode/enclave_node/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub async fn setup_aggregator(
&cipher,
config.quic_port(),
config.enable_mdns(),
repositories.libp2pid(),
repositories.libp2p_keypair(),
)
.await?;

Expand Down
2 changes: 1 addition & 1 deletion packages/ciphernode/enclave_node/src/ciphernode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub async fn setup_ciphernode(
&cipher,
config.quic_port(),
config.enable_mdns(),
repositories.libp2pid(),
repositories.libp2p_keypair(),
)
.await?;

Expand Down
33 changes: 14 additions & 19 deletions packages/ciphernode/net/src/network_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use crate::NetworkPeer;
/// Actor for connecting to an libp2p client via it's mpsc channel interface
/// This Actor should be responsible for
use actix::prelude::*;
use anyhow::anyhow;
use anyhow::Result;
use anyhow::{anyhow, bail, Result};
use cipher::Cipher;
use data::Repository;
use enclave_core::{EnclaveEvent, EventBus, EventId, Subscribe};
Expand Down Expand Up @@ -75,35 +74,31 @@ impl NetworkManager {
enable_mdns: bool,
repository: Repository<Vec<u8>>,
) -> Result<(Addr<Self>, tokio::task::JoinHandle<Result<()>>, String)> {
info!("Reading from repository");
let mut bytes = if let Some(bytes) = repository.read().await? {
let decrypted = cipher.decrypt_data(&bytes)?;
info!("Found keypair in repository");
decrypted
} else {
let kp = libp2p::identity::Keypair::generate_ed25519();
info!("Generated new keypair {}", kp.public().to_peer_id());
let innerkp = kp.try_into_ed25519()?;
let bytes = innerkp.to_bytes().to_vec();

// We need to clone here so that returned bytes are not zeroized
repository.write(&cipher.encrypt_data(&mut bytes.clone())?);
info!("Saved new keypair to repository");
bytes
// Get existing keypair or generate a new one
let mut bytes = match repository.read().await? {
Some(bytes) => {
info!("Found keypair in repository");
cipher.decrypt_data(&bytes)?
}
None => bail!("No network keypair found in repository, please generate a new one using `enclave net generate-key`"),
};

let ed25519_keypair = ed25519::Keypair::try_from_bytes(&mut bytes)?;
let keypair: libp2p::identity::Keypair = ed25519_keypair.try_into()?;
// Create peer from keypair
let keypair: libp2p::identity::Keypair =
ed25519::Keypair::try_from_bytes(&mut bytes)?.try_into()?;
let mut peer = NetworkPeer::new(
&keypair,
peers,
Some(quic_port),
"tmp-enclave-gossip-topic",
enable_mdns,
)?;

// Setup and start network manager
let rx = peer.rx().ok_or(anyhow!("Peer rx already taken"))?;
let p2p_addr = NetworkManager::setup(bus, peer.tx(), rx);
let handle = tokio::spawn(async move { Ok(peer.start().await?) });

Ok((p2p_addr, handle, keypair.public().to_peer_id().to_string()))
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/ciphernode/router/src/repositories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ impl Repositories {
Repository::new(self.store.scope(format!("//eth_private_key")))
}

pub fn libp2pid(&self) -> Repository<Vec<u8>> {
Repository::new(self.store.scope(format!("//libp2pid")))
pub fn libp2p_keypair(&self) -> Repository<Vec<u8>> {
Repository::new(self.store.scope(format!("//libp2p/keypair")))
}

pub fn enclave_sol_reader(&self, chain_id: u64) -> Repository<EvmEventReaderState> {
Expand Down