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

Persist networking keystore #90

Merged
merged 9 commits into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
4 changes: 1 addition & 3 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ edition = "2018"
[dependencies]
network = { path = "network" }
ferret-libp2p = { path = "ferret-libp2p"}

utils = { path = "utils" }

libp2p = { git = "https://github.com/SigP/rust-libp2p", rev = "3f9b030e29c9b31f9fe6f2ed27be4a813e2b3701" }
tokio = "0.1.22"
futures = "0.1.29"
clap = "2.33.0"
dirs = "2.0.2"
toml = "0.5.5"
chrono = "0.4.9"
serde = "1.0"
log = "0.4.8"
Expand Down
1 change: 1 addition & 0 deletions node/ferret-libp2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2018"

[dependencies]
libp2p = { git = "https://github.com/SigP/rust-libp2p", rev = "3f9b030e29c9b31f9fe6f2ed27be4a813e2b3701" }
utils = { path = "../utils" }
tokio = "0.1.22"
futures = "0.1.29"
log = "0.4.8"
Expand Down
73 changes: 62 additions & 11 deletions node/ferret-libp2p/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ use super::behaviour::{MyBehaviour, MyBehaviourEvent};
use super::config::Libp2pConfig;
use futures::{Async, Stream};
use libp2p::{
core, core::muxing::StreamMuxerBox, core::nodes::Substream, core::transport::boxed::Boxed,
gossipsub::TopicHash, identity, mplex, secio, yamux, PeerId, Swarm, Transport,
core,
core::muxing::StreamMuxerBox,
core::nodes::Substream,
core::transport::boxed::Boxed,
gossipsub::TopicHash,
identity::{ed25519, Keypair},
mplex, secio, yamux, PeerId, Swarm, Transport,
};
use slog::{debug, error, info, Logger};
use slog::{debug, error, info, trace, Logger};
use std::io::{Error, ErrorKind};
use std::time::Duration;
use utils::{get_home_dir, read_file_to_vec, write_to_file};
type Libp2pStream = Boxed<(PeerId, StreamMuxerBox), Error>;
type Libp2pBehaviour = MyBehaviour<Substream<StreamMuxerBox>>;

Expand All @@ -19,16 +25,16 @@ pub struct Libp2pService {
impl Libp2pService {
/// Constructs a Libp2pService
pub fn new(log: &Logger, config: &Libp2pConfig) -> Self {
// TODO @Greg do local storage
let local_key = identity::Keypair::generate_ed25519();
let local_peer_id = PeerId::from(local_key.public());
info!(log, "Local peer id: {:?}", local_peer_id);
let net_keypair = get_keypair(log);
let peer_id = PeerId::from(net_keypair.public());

let transport = build_transport(local_key.clone());
info!(log, "Local peer id: {:?}", peer_id);

let transport = build_transport(net_keypair.clone());

let mut swarm = {
let be = MyBehaviour::new(&local_key);
Swarm::new(transport, be, local_peer_id)
let be = MyBehaviour::new(&net_keypair);
Swarm::new(transport, be, peer_id)
};

for node in config.bootstrap_peers.clone() {
Expand Down Expand Up @@ -102,7 +108,7 @@ pub enum NetworkEvent {
},
}

fn build_transport(local_key: identity::Keypair) -> Boxed<(PeerId, StreamMuxerBox), Error> {
fn build_transport(local_key: Keypair) -> Boxed<(PeerId, StreamMuxerBox), Error> {
let transport = libp2p::tcp::TcpConfig::new().nodelay(true);
let transport = libp2p::dns::DnsConfig::new(transport);

Expand All @@ -118,3 +124,48 @@ fn build_transport(local_key: identity::Keypair) -> Boxed<(PeerId, StreamMuxerBo
.map_err(|err| Error::new(ErrorKind::Other, err))
.boxed()
}

/// Fetch keypair from disk, or generate a new one if its not available
fn get_keypair(log: &Logger) -> Keypair {
let path_to_keystore = format!("{}{}", get_home_dir(), "/.ferret/libp2p/keypair");
let local_keypair = match read_file_to_vec(&path_to_keystore) {
Err(e) => {
info!(log, "Networking keystore not found!");
trace!(log, "Error {:?}", e);
return generate_new_peer_id(log);
}
Ok(mut vec) => {
// If decoding fails, generate new peer id
GregTheGreek marked this conversation as resolved.
Show resolved Hide resolved
// TODO rename old file to keypair.old(?)
match ed25519::Keypair::decode(&mut vec) {
Ok(kp) => {
info!(log, "Recovered keystore from {:?}", &path_to_keystore);
kp
}
Err(e) => {
info!(log, "Could not decode networking keystore!");
trace!(log, "Error {:?}", e);
return generate_new_peer_id(log);
}
}
}
GregTheGreek marked this conversation as resolved.
Show resolved Hide resolved
};

Keypair::Ed25519(local_keypair)
}

/// Generates a new libp2p keypair and saves to disk
fn generate_new_peer_id(log: &Logger) -> Keypair {
let path_to_keystore = format!("{}{}", get_home_dir(), "/.ferret/libp2p/");
GregTheGreek marked this conversation as resolved.
Show resolved Hide resolved
let generated_keypair = Keypair::generate_ed25519();
info!(log, "Generated new keystore!");

if let Keypair::Ed25519(key) = generated_keypair.clone() {
if let Err(e) = write_to_file(&key.encode().to_vec(), &path_to_keystore, "keypair") {
GregTheGreek marked this conversation as resolved.
Show resolved Hide resolved
info!(log, "Could not write keystore to disk!");
trace!(log, "Error {:?}", e);
};
}

generated_keypair
}
4 changes: 2 additions & 2 deletions node/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ mod config;
pub use config::Config;

use clap::{App, Arg};
use node::utils::{read_file, read_toml};
use slog::Logger;
use std::io;
use utils::{read_file_to_string, read_toml};

pub(super) fn cli(_log: &Logger) -> Result<Config, io::Error> {
let app = App::new("Ferret")
Expand All @@ -26,7 +26,7 @@ pub(super) fn cli(_log: &Logger) -> Result<Config, io::Error> {

if let Some(config_file) = app.value_of("config") {
// Read from config file
let toml = read_file(config_file.to_string())?;
let toml = read_file_to_string(config_file)?;

// Parse and return the configuration file
return Ok(read_toml(&toml)?);
Expand Down
1 change: 0 additions & 1 deletion node/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod clock;
pub mod utils;
13 changes: 13 additions & 0 deletions node/utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "utils"
version = "0.1.0"
authors = ["Gregory Markou <gregorymarkou@gmail.com>"]
GregTheGreek marked this conversation as resolved.
Show resolved Hide resolved
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
dirs = "2.0.2"
toml = "0.5.5"
serde = "1.0"
serde_derive = "1.0"
28 changes: 19 additions & 9 deletions node/src/utils/mod.rs → node/utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(dead_code)]

use dirs::home_dir;
use serde;
use std::fs::{create_dir_all, File};
Expand All @@ -14,25 +12,37 @@ use toml;
/// * `message` - Contents that will be written to the file
/// * `path` - Filesystem path of where the file will be written to
/// * `file_name` - Desired filename
pub fn write_string_to_file(message: &str, path: &str, file_name: &str) -> Result<()> {
pub fn write_to_file(message: &[u8], path: &str, file_name: &str) -> Result<()> {
// Create path if it doesn't exist
create_dir_all(Path::new(&path))?;
GregTheGreek marked this conversation as resolved.
Show resolved Hide resolved
let join = format!("{}{}", path, file_name);
let mut file = File::create(join.to_owned())?;
file.write_all(&message.as_bytes())?;
file.write_all(&message)?;
GregTheGreek marked this conversation as resolved.
Show resolved Hide resolved
Ok(())
}

/// Read file if it exists in the filesystem
/// Read file as a Vec<u8>
///
/// # Arguments
///
/// * `path` - A String representing the path to a file
pub fn read_file_to_vec(path: &str) -> Result<Vec<u8>> {
let mut file = File::open(path)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
Ok(buffer)
}

/// Read file as a String
///
/// # Arguments
///
/// * `path` - A String representing the path to a file
pub fn read_file(path: String) -> Result<String> {
pub fn read_file_to_string(path: &str) -> Result<String> {
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
let mut string = String::new();
file.read_to_string(&mut string)?;
Ok(string)
}

/// Gets the home directory of the current system.
Expand Down
42 changes: 32 additions & 10 deletions node/tests/utils_test.rs → node/utils/tests/files.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use node::utils::{read_file, read_toml, write_string_to_file};
use utils::{read_file_to_string, read_file_to_vec, read_toml, write_to_file};

use serde_derive::Deserialize;
use std::fs::remove_dir_all;

// Please use with caution, remove_dir_all will completely delete a directory
fn cleanup_file(path: &str) {
if let Err(e) = remove_dir_all(path) {
panic!("cleanup_file() failed: {:?}", e);
panic!("cleanup_file() path: {:?} failed: {:?}", path, e);
}
}

#[test]
fn write_to_file() {
fn write_to_file_to_path() {
let msg = "Hello World!".as_bytes().to_vec();
let path = "./test-write/";
let file = "test.txt";
match write_string_to_file("message", path, file) {
match write_to_file(&msg, &path, file) {
Ok(_) => cleanup_file(path),
Err(e) => {
cleanup_file(path);
Expand All @@ -24,10 +25,11 @@ fn write_to_file() {
}

#[test]
fn write_string_to_file_nested_dir() {
fn write_to_file_nested_dir() {
let msg = "Hello World!".as_bytes().to_vec();
let root = "./test_missing";
let path = format!("{}{}", root, "/test_write_string/");
match write_string_to_file("message", &path, "test-file") {
match write_to_file(&msg, &path, "test-file") {
Ok(_) => cleanup_file(root),
Err(e) => {
cleanup_file(root);
Expand All @@ -37,14 +39,34 @@ fn write_string_to_file_nested_dir() {
}

#[test]
fn read_from_file() {
let msg = "Hello World!";
fn read_from_file_vec() {
let msg = "Hello World!".as_bytes().to_vec();
let path = "./test_read_file/";
let file_name = "out.keystore";
if let Err(e) = write_string_to_file(msg, path, file_name) {
if let Err(e) = write_to_file(&msg, &path, file_name) {
panic!(e);
}
match read_file_to_vec(&format!("{}{}", path, file_name)) {
Ok(contents) => {
cleanup_file(path);
assert_eq!(contents, msg)
}
Err(e) => {
cleanup_file(path);
panic!("{:?}", e);
}
}
}

#[test]
fn read_from_file_string() {
let msg = "Hello World!";
let path = "./test_string_read_file/";
let file_name = "out.keystore";
if let Err(e) = write_to_file(&msg.as_bytes().to_vec(), &path, file_name) {
panic!(e);
}
match read_file(format!("{}{}", path, file_name)) {
match read_file_to_string(&format!("{}{}", path, file_name)) {
Ok(contents) => {
cleanup_file(path);
assert_eq!(contents, msg)
Expand Down