generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add linter to CI and applied some fixes (#35)
* Apply clippy fixes * Setup clippy CI * Apply cargo fmt * Fix for 'module_inception' clippy rule This error resulted from the fact that we had a files that matched their folder's name. E.g: We had crypto/src/key/key.rs. The module's name is `key`, and it contained a module named `key` within it. To fix, I removed all of this inception. `key.rs`'s code now lives in `mod.rs`, same with `key_manager.rs`. Subsequently, this allows us to r emove all of the global use statements like `pub use key::*;`! This also brings consistency with the approach taken in the `tbdex-rs` repo, as discussed here: TBD54566975/tbdex-rs#31 (comment) * Apply suggestions from code review Co-authored-by: Adam Mika <88001738+amika-sq@users.noreply.github.com> --------- Co-authored-by: Adam Mika <amika@squareup.com> Co-authored-by: Adam Mika <88001738+amika-sq@users.noreply.github.com>
- Loading branch information
1 parent
7e12405
commit b1f36ad
Showing
12 changed files
with
119 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,25 @@ | ||
mod key; | ||
pub use key::*; | ||
pub mod private_key; | ||
pub mod public_key; | ||
|
||
mod private_key; | ||
pub use private_key::*; | ||
use ssi_jwk::JWK; | ||
use ssi_jws::Error as JWSError; | ||
|
||
mod public_key; | ||
pub use public_key::*; | ||
/// Enum defining all supported cryptographic key types. | ||
pub enum KeyType { | ||
Secp256k1, | ||
Secp256r1, | ||
Ed25519, | ||
} | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
pub enum KeyError { | ||
#[error(transparent)] | ||
JWSError(#[from] JWSError), | ||
#[error("Algorithm not found on JWK")] | ||
AlgorithmNotFound, | ||
} | ||
|
||
/// Trait defining all common behavior for cryptographic keys. | ||
pub trait Key { | ||
fn jwk(&self) -> &JWK; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,17 @@ | ||
mod key_store; | ||
pub use key_store::*; | ||
pub mod in_memory_key_store; | ||
|
||
mod in_memory_key_store; | ||
pub use in_memory_key_store::*; | ||
use crate::key::private_key::PrivateKey; | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
pub enum KeyStoreError { | ||
#[error("{0}")] | ||
InternalKeyStoreError(String), | ||
} | ||
|
||
// Trait for storing and retrieving private keys. | ||
// | ||
// Implementations of this trait should be thread-safe and allow for concurrent access. | ||
pub trait KeyStore: Send + Sync { | ||
fn get(&self, key_alias: &str) -> Result<Option<PrivateKey>, KeyStoreError>; | ||
fn insert(&self, key_alias: &str, private_key: PrivateKey) -> Result<(), KeyStoreError>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,41 @@ | ||
mod key_manager; | ||
pub use key_manager::*; | ||
pub mod key_store; | ||
pub mod local_key_manager; | ||
|
||
mod local_key_manager; | ||
pub use local_key_manager::*; | ||
use crate::key::public_key::PublicKey; | ||
use crate::key::{KeyError, KeyType}; | ||
use crate::key_manager::key_store::KeyStoreError; | ||
use ssi_jwk::Error as JWKError; | ||
|
||
pub mod key_store; | ||
#[derive(thiserror::Error, Debug)] | ||
pub enum KeyManagerError { | ||
#[error("Signing key not found in KeyManager")] | ||
SigningKeyNotFound, | ||
#[error(transparent)] | ||
JWKError(#[from] JWKError), | ||
#[error(transparent)] | ||
KeyError(#[from] KeyError), | ||
#[error(transparent)] | ||
KeyStoreError(#[from] KeyStoreError), | ||
} | ||
|
||
/// A key management trait for generating, storing, and utilizing keys private keys and their | ||
/// associated public keys. | ||
/// | ||
/// Implementations of this trait might provide key management through various Key Management | ||
/// 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`, | ||
/// 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<String, KeyManagerError>; | ||
|
||
/// Returns the public key associated with the provided `key_alias`, if one exists. | ||
fn get_public_key(&self, key_alias: &str) -> Result<Option<PublicKey>, KeyManagerError>; | ||
|
||
/// Signs the provided payload using the private key identified by the provided `key_alias`. | ||
fn sign(&self, key_alias: &str, payload: &[u8]) -> Result<Vec<u8>, KeyManagerError>; | ||
|
||
/// Returns the key alias of a public key, as was originally returned by `generate_private_key`. | ||
fn alias(&self, public_key: &PublicKey) -> Result<String, KeyManagerError>; | ||
} |