Skip to content

Commit

Permalink
feat(signer): save keys to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-wright committed Jun 28, 2023
1 parent d2434c2 commit 4a874bb
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
32 changes: 30 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2027,7 +2027,16 @@ version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059"
dependencies = [
"dirs-sys",
"dirs-sys 0.3.7",
]

[[package]]
name = "dirs"
version = "5.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225"
dependencies = [
"dirs-sys 0.4.1",
]

[[package]]
Expand All @@ -2051,6 +2060,18 @@ dependencies = [
"winapi",
]

[[package]]
name = "dirs-sys"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c"
dependencies = [
"libc",
"option-ext",
"redox_users",
"windows-sys 0.48.0",
]

[[package]]
name = "dirs-sys-next"
version = "0.1.2"
Expand Down Expand Up @@ -2333,6 +2354,7 @@ dependencies = [
"affair",
"anyhow",
"async-trait",
"dirs 5.0.1",
"draco-application",
"draco-interfaces",
"draco-test-utils",
Expand Down Expand Up @@ -4799,6 +4821,12 @@ dependencies = [
"vcpkg",
]

[[package]]
name = "option-ext"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"

[[package]]
name = "os_str_bytes"
version = "6.5.1"
Expand Down Expand Up @@ -5761,7 +5789,7 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "321e5e41b3b192dab6f1e75b9deacb6688b4b8c5e68906a78e8f43e7c2887bb5"
dependencies = [
"dirs",
"dirs 4.0.0",
]

[[package]]
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ zeroize = "1.6"
scc = "1.8.1"
num-traits = "0.2.15"
num-derive = "0.3.3"
dirs = "5.0.1"


# Our libraries
Expand Down
1 change: 1 addition & 0 deletions core/signer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ anyhow.workspace = true
async-trait.workspace = true
serde.workspace = true
tokio.workspace = true
dirs.workspace = true

fleek-crypto.workspace = true
affair.workspace = true
Expand Down
14 changes: 9 additions & 5 deletions core/signer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod config;
#[cfg(test)]
mod tests;
mod utils;
use std::{
collections::VecDeque,
sync::{Arc, Mutex},
Expand Down Expand Up @@ -193,22 +194,25 @@ struct SignerInner {

impl SignerInner {
fn new(config: Config) -> Self {
// TODO: load private keys from file if they exist
let node_secret_key =
match NodeSecretKey::decode_pem(config.node_key_path.to_str().unwrap()) {
Some(node_secret_key) => node_secret_key,
None => {
NodeSecretKey::generate()
// TODO(matthias): save file to disk
let node_secret_key = NodeSecretKey::generate();
utils::save(&config.node_key_path, node_secret_key.encode_pem())
.expect("Failed to save NodeSecretKey to disk.");
node_secret_key
},
};
let node_public_key = node_secret_key.to_pk();
let network_secret_key =
match NodeNetworkingSecretKey::decode_pem(config.network_key_path.to_str().unwrap()) {
Some(network_secret_key) => network_secret_key,
None => {
NodeNetworkingSecretKey::generate()
// TODO(matthias): save file to disk
let network_secret_key = NodeNetworkingSecretKey::generate();
utils::save(&config.network_key_path, network_secret_key.encode_pem())
.expect("Failed to save NodeNetworkingSecretKey to disk.");
network_secret_key
},
};
let network_public_key = network_secret_key.to_pk();
Expand Down
32 changes: 32 additions & 0 deletions core/signer/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::{
fs::{self, create_dir_all, File},
io::Write,
os::unix::fs::PermissionsExt,
path::{Path, PathBuf},
};

use anyhow::anyhow;

pub(crate) fn save<T: AsRef<[u8]>>(path: &Path, data: T) -> anyhow::Result<()> {
// Mostly taken from: https://github.com/fleek-network/ursa/blob/feat/pod/crates/ursa/src/ursa/identity.rs
let path = expand_path(path)?;
create_dir_all(path.parent().unwrap())?;
let mut file = File::create(path.as_path())?;
file.write_all(data.as_ref())?;
file.sync_all()?;
let mut perms = file.metadata()?.permissions();
perms.set_mode(0o600);
fs::set_permissions(path, perms)?;
Ok(())
}

fn expand_path(path: &Path) -> anyhow::Result<PathBuf> {
if path.starts_with("~") {
let path = path.strip_prefix("~")?;
let home_dir = dirs::home_dir().ok_or(anyhow!("Failed to obtain home directory."))?;
let full_path = home_dir.join(path);
Ok(full_path)
} else {
Ok(path.to_owned())
}
}

0 comments on commit 4a874bb

Please sign in to comment.