Skip to content

Commit

Permalink
Lint and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Diane Huxley authored and KendallWeihe committed May 16, 2024
1 parent 061a552 commit 040cd90
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 47 deletions.
2 changes: 1 addition & 1 deletion crates/credentials/src/vc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ mod test {
use uuid::Uuid;

fn create_bearer_did() -> BearerDid {
let key_manager = Arc::new(LocalKeyManager::new_in_memory());
let key_manager = Arc::new(LocalKeyManager::new());
let options = DidJwkCreateOptions {
curve: Curve::Ed25519,
};
Expand Down
8 changes: 4 additions & 4 deletions crates/dids/src/bearer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ mod test {

#[tokio::test]
async fn test_from_key_manager() {
let key_manager = Arc::new(LocalKeyManager::new_in_memory());
let key_manager = Arc::new(LocalKeyManager::new());
let options = DidJwkCreateOptions {
curve: Curve::Ed25519,
};
let did_jwk_bearer_did = DidJwk::create(key_manager.clone(), options).unwrap();
let private_keys = key_manager.export_private_keys().unwrap();

let bearer_did =
BearerDid::from_key_manager(&did_jwk_bearer_did.identifier.uri, key_manager)
BearerDid::from_key_manager(&did_jwk_bearer_did.identifier.uri, key_manager.clone())
.await
.unwrap();
let bearer_did_private_keys = bearer_did.key_manager.export_private_keys().unwrap();
let bearer_did_private_keys = key_manager.export_private_keys().unwrap();

assert_eq!(bearer_did.identifier.uri, did_jwk_bearer_did.identifier.uri);
assert_eq!(private_keys.len(), bearer_did_private_keys.len());
Expand All @@ -100,7 +100,7 @@ mod test {

#[test]
fn test_sign() {
let key_manager = Arc::new(LocalKeyManager::new_in_memory());
let key_manager = Arc::new(LocalKeyManager::new());
let options = DidJwkCreateOptions {
curve: Curve::Ed25519,
};
Expand Down
2 changes: 1 addition & 1 deletion crates/dids/src/methods/jwk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ mod tests {
use keys::key_manager::local_key_manager::LocalKeyManager;

fn create_did_jwk() -> BearerDid {
let key_manager = Arc::new(LocalKeyManager::new_in_memory());
let key_manager = Arc::new(LocalKeyManager::new());
let options = DidJwkCreateOptions {
curve: Curve::Ed25519,
};
Expand Down
40 changes: 20 additions & 20 deletions crates/jws/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ mod tests {

#[tokio::test]
async fn test_jws_sign_and_verify() {
let key_manager = LocalKeyManager::new_in_memory();
let key_manager = Arc::new(LocalKeyManager::new());
let bearer_did = DidJwk::create(
Arc::new(key_manager),
key_manager,
DidJwkCreateOptions {
curve: Curve::Ed25519,
},
Expand Down Expand Up @@ -191,9 +191,9 @@ mod tests {

#[test]
fn test_jws_decode() {
let key_manager = LocalKeyManager::new_in_memory();
let key_manager = Arc::new(LocalKeyManager::new());
let bearer_did = DidJwk::create(
Arc::new(key_manager),
key_manager,
DidJwkCreateOptions {
curve: Curve::Ed25519,
},
Expand Down Expand Up @@ -242,9 +242,9 @@ mod tests {

#[tokio::test]
async fn test_jws_verify_malformed_header_alg_error() {
let key_manager = LocalKeyManager::new_in_memory();
let key_manager = Arc::new(LocalKeyManager::new());
let bearer_did = DidJwk::create(
Arc::new(key_manager),
key_manager,
DidJwkCreateOptions {
curve: Curve::Ed25519,
},
Expand Down Expand Up @@ -283,9 +283,9 @@ mod tests {

#[tokio::test]
async fn test_jws_verify_malformed_header_kid_error() {
let key_manager = LocalKeyManager::new_in_memory();
let key_manager = Arc::new(LocalKeyManager::new());
let bearer_did = DidJwk::create(
Arc::new(key_manager),
key_manager,
DidJwkCreateOptions {
curve: Curve::Ed25519,
},
Expand Down Expand Up @@ -324,9 +324,9 @@ mod tests {

#[tokio::test]
async fn test_jws_verify_algorithm_not_supported_error() {
let key_manager = LocalKeyManager::new_in_memory();
let key_manager = Arc::new(LocalKeyManager::new());
let bearer_did = DidJwk::create(
Arc::new(key_manager),
key_manager,
DidJwkCreateOptions {
curve: Curve::Ed25519,
},
Expand Down Expand Up @@ -365,9 +365,9 @@ mod tests {

#[tokio::test]
async fn test_jws_verify_resolution_error() {
let key_manager = LocalKeyManager::new_in_memory();
let key_manager = Arc::new(LocalKeyManager::new());
let bearer_did = DidJwk::create(
Arc::new(key_manager),
key_manager,
DidJwkCreateOptions {
curve: Curve::Ed25519,
},
Expand Down Expand Up @@ -406,9 +406,9 @@ mod tests {

#[tokio::test]
async fn test_jws_verify_document_verification_method_error() {
let key_manager = LocalKeyManager::new_in_memory();
let key_manager = Arc::new(LocalKeyManager::new());
let bearer_did = DidJwk::create(
Arc::new(key_manager),
key_manager,
DidJwkCreateOptions {
curve: Curve::Ed25519,
},
Expand Down Expand Up @@ -447,9 +447,9 @@ mod tests {

#[tokio::test]
async fn test_jws_verify_signature_decode_error() {
let key_manager = LocalKeyManager::new_in_memory();
let key_manager = Arc::new(LocalKeyManager::new());
let bearer_did = DidJwk::create(
Arc::new(key_manager),
key_manager,
DidJwkCreateOptions {
curve: Curve::Ed25519,
},
Expand Down Expand Up @@ -489,9 +489,9 @@ mod tests {

#[tokio::test]
async fn test_jws_verify_signature_crypto_error() {
let key_manager = LocalKeyManager::new_in_memory();
let key_manager = Arc::new(LocalKeyManager::new());
let bearer_did = DidJwk::create(
Arc::new(key_manager),
key_manager,
DidJwkCreateOptions {
curve: Curve::Ed25519,
},
Expand All @@ -513,9 +513,9 @@ mod tests {
.to_string()
.into_bytes();

let invalid_key_manager = LocalKeyManager::new_in_memory();
let invalid_key_manager = Arc::new(LocalKeyManager::new());
let invalid_bearer_did = DidJwk::create(
Arc::new(invalid_key_manager),
invalid_key_manager,
DidJwkCreateOptions {
curve: Curve::Ed25519,
},
Expand Down
4 changes: 2 additions & 2 deletions crates/jwt/src/jws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ mod tests {

#[tokio::test]
async fn test_sign_and_verify() {
let key_manager = LocalKeyManager::new_in_memory();
let key_manager = Arc::new(LocalKeyManager::new());
let bearer_did = DidJwk::create(
Arc::new(key_manager),
key_manager,
DidJwkCreateOptions {
curve: Curve::Ed25519,
},
Expand Down
46 changes: 32 additions & 14 deletions crates/keys/src/key_manager/local_key_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ impl LocalKeyManager {
map: RwLock::new(HashMap::new()),
}
}

pub fn export_private_keys(&self) -> Result<Vec<Arc<dyn PrivateKey>>, KeyManagerError> {
let map_lock = self.map.read().map_err(|e| {
KeyManagerError::InternalKeyStoreError(format!("Unable to acquire Mutex lock: {}", e))
})?;
let private_keys = map_lock.values().cloned().collect();
Ok(private_keys)
}
}

impl KeyManager for LocalKeyManager {
Expand Down Expand Up @@ -57,9 +65,13 @@ impl KeyManager for LocalKeyManager {
}

fn sign(&self, key_alias: &str, payload: &[u8]) -> Result<Vec<u8>, KeyManagerError> {
let map_lock: std::sync::RwLockReadGuard<HashMap<String, Arc<dyn PrivateKey>>> = self.map.read().map_err(|e| {
KeyManagerError::InternalKeyStoreError(format!("Unable to acquire Mutex lock: {}", e))
})?;
let map_lock: std::sync::RwLockReadGuard<HashMap<String, Arc<dyn PrivateKey>>> =
self.map.read().map_err(|e| {
KeyManagerError::InternalKeyStoreError(format!(
"Unable to acquire Mutex lock: {}",
e
))
})?;
let private_key = map_lock
.get(key_alias)
.ok_or(KeyManagerError::KeyNotFound(key_alias.to_string()))?;
Expand All @@ -86,15 +98,12 @@ impl KeyImporter for LocalKeyManager {
}
}



impl Default for LocalKeyManager {
fn default() -> Self {
Self::new()
}
}


#[cfg(test)]
mod tests {
use crate::key::Key;
Expand Down Expand Up @@ -135,7 +144,7 @@ mod tests {

#[test]
fn test_sign() {
let key_manager: LocalKeyManager = LocalKeyManager::new();
let key_manager = LocalKeyManager::new();
let key_alias = key_manager
.generate_private_key(Curve::Ed25519, None)
.unwrap();
Expand All @@ -154,15 +163,21 @@ mod tests {
let key_manager = LocalKeyManager::new();
let private_key = Arc::new(Ed25519::generate().unwrap());

let key_alias = key_manager.import(private_key.clone()).expect("Failed to import private key");
let default_alias = private_key.alias().expect("Failed to generate private key alias");
let key_alias = key_manager
.import(private_key.clone())
.expect("Failed to import private key");
let default_alias = private_key
.alias()
.expect("Failed to generate private key alias");
assert_eq!(key_alias, default_alias);

let key_manager_public_key = key_manager.get_public_key(&key_alias)
let key_manager_public_key = key_manager
.get_public_key(&key_alias)
.expect("Failed to get public key")
.jwk()
.expect("Failed to get alias of public key");
let public_key = private_key.to_public()
let public_key = private_key
.to_public()
.expect("Failed to convert private key to public")
.jwk()
.expect("Failed to get alias of public key");
Expand All @@ -175,14 +190,17 @@ mod tests {
let private_key = Arc::new(Ed25519::generate().unwrap());

let key_alias = "1234".to_string();
key_manager.import_with_alias(private_key.clone(), &key_alias)
key_manager
.import_with_alias(private_key.clone(), &key_alias)
.expect("Failed to import private key with alias");

let key_manager_public_key = key_manager.get_public_key(&key_alias)
let key_manager_public_key = key_manager
.get_public_key(&key_alias)
.expect("Failed to get public key")
.jwk()
.expect("Failed to get alias of public key");
let public_key = private_key.to_public()
let public_key = private_key
.to_public()
.expect("Failed to convert private key to public")
.jwk()
.expect("Failed to get alias of public key");
Expand Down
7 changes: 2 additions & 5 deletions crates/keys/src/key_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,9 @@ pub trait KeyImporter: KeyManager {

/// Imports a private key into the key manager using private_key.alias() as the alias
/// Returns the key alias
fn import(
&self,
private_key: Arc<dyn PrivateKey>
) -> Result<String, KeyManagerError> {
fn import(&self, private_key: Arc<dyn PrivateKey>) -> Result<String, KeyManagerError> {
let key_alias = private_key.alias()?;
self.import_with_alias(private_key, &key_alias)?;
Ok(key_alias)
}
}
}

0 comments on commit 040cd90

Please sign in to comment.