From 913aa05fc7213c937e6e24d5cc05af7b8d473ef7 Mon Sep 17 00:00:00 2001 From: Adam Mika Date: Mon, 4 Dec 2023 09:49:41 -0700 Subject: [PATCH 1/2] Rename KeyType -> KeyAlgorithm --- crates/crypto/src/key/key.rs | 4 ++-- crates/crypto/src/key_manager/key_manager.rs | 6 ++--- .../src/key_manager/local_key_manager.rs | 24 +++++++++---------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/crates/crypto/src/key/key.rs b/crates/crypto/src/key/key.rs index 66ada448..5e83ec24 100644 --- a/crates/crypto/src/key/key.rs +++ b/crates/crypto/src/key/key.rs @@ -1,8 +1,8 @@ use ssi_jwk::JWK; use ssi_jws::Error as JWSError; -/// Enum defining all supported cryptographic key types. -pub enum KeyType { +/// Enum defining all supported cryptographic algorithms for a [`Key`]. +pub enum KeyAlgorithm { Secp256k1, Secp256r1, Ed25519, diff --git a/crates/crypto/src/key_manager/key_manager.rs b/crates/crypto/src/key_manager/key_manager.rs index b4ba690d..f12e6522 100644 --- a/crates/crypto/src/key_manager/key_manager.rs +++ b/crates/crypto/src/key_manager/key_manager.rs @@ -1,4 +1,4 @@ -use crate::key::{KeyError, KeyType, PublicKey}; +use crate::key::{KeyError, KeyAlgorithm, PublicKey}; use crate::key_manager::key_store::KeyStoreError; use ssi_jwk::Error as JWKError; @@ -21,10 +21,10 @@ pub enum KeyManagerError { /// Systems (KMS), such as AWS KMS, Google Cloud KMD, Hardware Security Modules (HSM), or simple /// in-memory storage, each adhering to the same consistent API for usage within applications. pub trait KeyManager: Send + Sync { - /// Generates and securely stores a private key based on the provided `key_type`, + /// Generates and securely stores a private key based on the provided `key_algorithm`, /// returning a unique alias that can be utilized to reference the generated key for future /// operations. - fn generate_private_key(&self, key_type: KeyType) -> Result; + fn generate_private_key(&self, key_algorithm: KeyAlgorithm) -> Result; /// Returns the public key associated with the provided `key_alias`, if one exists. fn get_public_key(&self, key_alias: &str) -> Result, KeyManagerError>; diff --git a/crates/crypto/src/key_manager/local_key_manager.rs b/crates/crypto/src/key_manager/local_key_manager.rs index d814884f..dbe67549 100644 --- a/crates/crypto/src/key_manager/local_key_manager.rs +++ b/crates/crypto/src/key_manager/local_key_manager.rs @@ -1,4 +1,4 @@ -use crate::key::{KeyType, PrivateKey, PublicKey}; +use crate::key::{KeyAlgorithm, PrivateKey, PublicKey}; use crate::key_manager::key_store::{InMemoryKeyStore, KeyStore}; use crate::key_manager::{KeyManager, KeyManagerError}; use ssi_jwk::JWK; @@ -26,11 +26,11 @@ impl LocalKeyManager { } impl KeyManager for LocalKeyManager { - fn generate_private_key(&self, key_type: KeyType) -> Result { - let jwk = match key_type { - KeyType::Secp256k1 => JWK::generate_secp256k1(), - KeyType::Secp256r1 => JWK::generate_p256(), - KeyType::Ed25519 => JWK::generate_ed25519(), + fn generate_private_key(&self, key_algorithm: KeyAlgorithm) -> Result { + let jwk = match key_algorithm { + KeyAlgorithm::Secp256k1 => JWK::generate_secp256k1(), + KeyAlgorithm::Secp256r1 => JWK::generate_p256(), + KeyAlgorithm::Ed25519 => JWK::generate_ed25519(), }?; let private_key = PrivateKey(jwk); @@ -75,15 +75,15 @@ mod tests { let key_manager = LocalKeyManager::new_in_memory(); key_manager - .generate_private_key(KeyType::Ed25519) + .generate_private_key(KeyAlgorithm::Ed25519) .expect("Failed to generate Ed25519 key"); key_manager - .generate_private_key(KeyType::Secp256k1) + .generate_private_key(KeyAlgorithm::Secp256k1) .expect("Failed to generate secp256k1 key"); key_manager - .generate_private_key(KeyType::Secp256r1) + .generate_private_key(KeyAlgorithm::Secp256r1) .expect("Failed to generate secp256r1 key"); } @@ -91,7 +91,7 @@ mod tests { fn test_get_public_key() { let key_manager = LocalKeyManager::new_in_memory(); - let key_alias = key_manager.generate_private_key(KeyType::Ed25519).unwrap(); + let key_alias = key_manager.generate_private_key(KeyAlgorithm::Ed25519).unwrap(); key_manager .get_public_key(&key_alias) @@ -102,7 +102,7 @@ mod tests { #[test] fn test_sign() { let key_manager = LocalKeyManager::new_in_memory(); - let key_alias = key_manager.generate_private_key(KeyType::Ed25519).unwrap(); + let key_alias = key_manager.generate_private_key(KeyAlgorithm::Ed25519).unwrap(); // Sign a payload let payload: &[u8] = b"hello world"; @@ -117,7 +117,7 @@ mod tests { #[test] fn test_alias() { let key_manager = LocalKeyManager::new_in_memory(); - let key_alias = key_manager.generate_private_key(KeyType::Ed25519).unwrap(); + let key_alias = key_manager.generate_private_key(KeyAlgorithm::Ed25519).unwrap(); let public_key = key_manager.get_public_key(&key_alias).unwrap().unwrap(); let alias = key_manager.alias(&public_key).unwrap(); From 61fbcaba7e88b24f622a6eb8b3f0623c7401d71b Mon Sep 17 00:00:00 2001 From: Adam Mika Date: Mon, 4 Dec 2023 10:14:25 -0700 Subject: [PATCH 2/2] Forgot to turn my formatter back on after adding Github Actions --- crates/crypto/src/key_manager/key_manager.rs | 2 +- crates/crypto/src/key_manager/local_key_manager.rs | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/crates/crypto/src/key_manager/key_manager.rs b/crates/crypto/src/key_manager/key_manager.rs index f12e6522..21cdef52 100644 --- a/crates/crypto/src/key_manager/key_manager.rs +++ b/crates/crypto/src/key_manager/key_manager.rs @@ -1,4 +1,4 @@ -use crate::key::{KeyError, KeyAlgorithm, PublicKey}; +use crate::key::{KeyAlgorithm, KeyError, PublicKey}; use crate::key_manager::key_store::KeyStoreError; use ssi_jwk::Error as JWKError; diff --git a/crates/crypto/src/key_manager/local_key_manager.rs b/crates/crypto/src/key_manager/local_key_manager.rs index dbe67549..13640c5d 100644 --- a/crates/crypto/src/key_manager/local_key_manager.rs +++ b/crates/crypto/src/key_manager/local_key_manager.rs @@ -91,7 +91,9 @@ mod tests { fn test_get_public_key() { let key_manager = LocalKeyManager::new_in_memory(); - let key_alias = key_manager.generate_private_key(KeyAlgorithm::Ed25519).unwrap(); + let key_alias = key_manager + .generate_private_key(KeyAlgorithm::Ed25519) + .unwrap(); key_manager .get_public_key(&key_alias) @@ -102,7 +104,9 @@ mod tests { #[test] fn test_sign() { let key_manager = LocalKeyManager::new_in_memory(); - let key_alias = key_manager.generate_private_key(KeyAlgorithm::Ed25519).unwrap(); + let key_alias = key_manager + .generate_private_key(KeyAlgorithm::Ed25519) + .unwrap(); // Sign a payload let payload: &[u8] = b"hello world"; @@ -117,7 +121,9 @@ mod tests { #[test] fn test_alias() { let key_manager = LocalKeyManager::new_in_memory(); - let key_alias = key_manager.generate_private_key(KeyAlgorithm::Ed25519).unwrap(); + let key_alias = key_manager + .generate_private_key(KeyAlgorithm::Ed25519) + .unwrap(); let public_key = key_manager.get_public_key(&key_alias).unwrap().unwrap(); let alias = key_manager.alias(&public_key).unwrap();