Skip to content

Commit

Permalink
feat: add info logs for key generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mriise committed Jul 6, 2023
1 parent c8e30ef commit cbe4298
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion homestar-runtime/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{
io::Read,
path::{Path, PathBuf},
};
use tracing::info;

/// Server settings.
#[derive(Clone, Debug, Deserialize)]
Expand Down Expand Up @@ -76,19 +77,26 @@ impl PubkeyConfig {
/// Calling this function will access the filesystem if configured to import a key.
pub(crate) fn keypair(&self) -> anyhow::Result<identity::Keypair> {
match self {
PubkeyConfig::Random => Ok(identity::Keypair::generate_ed25519()),
PubkeyConfig::Random => {
info!("generating random ed25519 key");
Ok(identity::Keypair::generate_ed25519())
}
PubkeyConfig::GenerateFromSeed(PupkeyRNGSeed { key_type, seed }) => {
// seed RNG with supplied seed
let mut r = rand::prelude::StdRng::from_seed(*seed);
let mut new_key: [u8; 32] = r.gen();

match key_type {
KeyType::Ed25519 => {
info!("generating radom ed25519 key from seed");

identity::Keypair::ed25519_from_bytes(new_key).map_err(|e| {
anyhow!("failed to generate ed25519 key from random: {:?}", e)
})
}
KeyType::Secp256k1 => {
info!("generating radom secp256k1 key from seed");

let sk =
secp256k1::SecretKey::try_from_bytes(&mut new_key).map_err(|e| {
anyhow!("failed to generate secp256k1 key from random: {:?}", e)
Expand All @@ -108,6 +116,8 @@ impl PubkeyConfig {

match key_type {
KeyType::Ed25519 => {
info!("importing ed25519 key from: {}", path.display());

let (tag, mut key) = sec1::der::pem::decode_vec(&buf)
.map_err(|e| anyhow!("key file must be PEM formatted: {:?}", e))?;
if tag != "PRIVATE KEY" {
Expand All @@ -122,6 +132,8 @@ impl PubkeyConfig {
.with_context(|| "imported key material was invalid for ed25519")
}
KeyType::Secp256k1 => {
info!("importing secp256k1 key from: {}", path.display());

let sk = match path.extension().and_then(|ext| ext.to_str()) {
Some("der") => sec1::EcPrivateKey::from_der(buf.as_slice()).map_err(|e| anyhow!("failed to parse DER encoded secp256k1 key: {e:?}")),
Some("pem") => {
Expand Down

0 comments on commit cbe4298

Please sign in to comment.