Skip to content

Commit

Permalink
Add private key generation and load from network dir.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmygchen committed Oct 31, 2024
1 parent 39d5980 commit 8f62a70
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
51 changes: 51 additions & 0 deletions anchor/network/src/keypair_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use libp2p::identity::{secp256k1, Keypair};
use std::fs::File;
use std::io::{Read, Write};
use std::path::PathBuf;
use tracing::{debug, warn};

pub const NETWORK_KEY_FILENAME: &str = "key";

/// Loads a private key from disk. If this fails, a new key is
/// generated and is then saved to disk.
///
/// Currently only secp256k1 keys are allowed, as these are the only keys supported by discv5.
pub fn load_private_key(network_dir: &PathBuf) -> Keypair {
// check for key from disk
let network_key_f = network_dir.join(NETWORK_KEY_FILENAME);
if let Ok(mut network_key_file) = File::open(network_key_f.clone()) {
let mut key_bytes: Vec<u8> = Vec::with_capacity(36);
match network_key_file.read_to_end(&mut key_bytes) {
Err(_) => debug!("Could not read network key file"),
Ok(_) => {
// only accept secp256k1 keys for now
if let Ok(secret_key) = secp256k1::SecretKey::try_from_bytes(&mut key_bytes) {
let kp: secp256k1::Keypair = secret_key.into();
debug!("Loaded network key from disk.");
return kp.into();
} else {
debug!("Network key file is not a valid secp256k1 key");
}
}
}
}

// if a key could not be loaded from disk, generate a new one and save it
let local_private_key = secp256k1::Keypair::generate();
let _ = std::fs::create_dir_all(network_dir);
match File::create(network_key_f.clone())
.and_then(|mut f| f.write_all(&local_private_key.secret().to_bytes()))
{
Ok(_) => {
debug!("New network key generated and written to disk");
}
Err(e) => {
warn!(
file = ?network_key_f,
error = ?e,
"Could not write node key to file"
);
}
}
local_private_key.into()
}
1 change: 1 addition & 0 deletions anchor/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod behaviour;
mod config;
mod keypair_utils;
mod network;
mod transport;
mod types;
Expand Down
8 changes: 3 additions & 5 deletions anchor/network/src/network.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::behaviour::AnchorBehaviour;
use crate::keypair_utils::load_private_key;
use crate::transport::build_transport;
use crate::Config;
use futures::{SinkExt, StreamExt};
use libp2p::core::muxing::StreamMuxerBox;
use libp2p::core::transport::Boxed;
use libp2p::identity::{secp256k1, Keypair};
use libp2p::identity::Keypair;
use libp2p::multiaddr::Protocol;
use libp2p::{futures, identify, ping, PeerId, Swarm, SwarmBuilder};
use std::num::{NonZeroU8, NonZeroUsize};
Expand All @@ -19,10 +20,7 @@ pub struct Network {

impl Network {
pub fn spawn(executor: TaskExecutor, config: &Config) {
// TODO: generate / load local key
let secp256k1_kp: secp256k1::Keypair = secp256k1::SecretKey::generate().into();
let local_keypair: Keypair = secp256k1_kp.into();

let local_keypair: Keypair = load_private_key(&config.network_dir);
let transport = build_transport(local_keypair.clone(), !config.disable_quic_support);
let behaviour = build_anchor_behaviour(local_keypair.clone());
let peer_id = local_keypair.public().to_peer_id();
Expand Down

0 comments on commit 8f62a70

Please sign in to comment.