Define the global KeySpace interface

Signed-off-by: Lee Smet <lee.smet@hotmail.com>
This commit is contained in:
Lee Smet 2025-05-14 11:08:37 +02:00
parent e44ee83e74
commit 78c0fd7871
Signed by untrusted user who does not match committer: lee
GPG Key ID: 72CBFB5FDA7FE025
5 changed files with 79 additions and 1 deletions

View File

@ -1,3 +1,20 @@
#[derive(Debug)]
/// Errors encountered while using the vault
pub enum Error {
/// An error during cryptographic operations
Crypto(CryptoError),
}
impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::Crypto(e) => f.write_fmt(format_args!("crypto: {e}")),
}
}
}
impl core::error::Error for Error {}
#[derive(Debug)]
/// Errors generated by the vault or keys.
///
@ -18,7 +35,7 @@ pub enum CryptoError {
}
impl core::fmt::Display for CryptoError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
CryptoError::InvalidKeySize => f.write_str("provided key is not the correct size"),
CryptoError::EncryptionFailed => f.write_str("encryption failure"),
@ -33,3 +50,9 @@ impl core::fmt::Display for CryptoError {
}
impl core::error::Error for CryptoError {}
impl From<CryptoError> for Error {
fn from(value: CryptoError) -> Self {
Self::Crypto(value)
}
}

50
vault/src/keyspace.rs Normal file
View File

@ -0,0 +1,50 @@
#[cfg(target_arch = "wasm32")]
mod wasm;
#[cfg(not(target_arch = "wasm32"))]
mod fallback;
#[cfg(target_arch = "wasm32")]
use wasm::KeySpace as KS;
#[cfg(not(target_arch = "wasm32"))]
use fallback::KeySpace as KS;
use crate::{error::Error, key::Key};
/// A keyspace represents a group of stored cryptographic keys. The storage is encrypted, a
/// password must be provided when opening the KeySpace to decrypt the keys.
pub struct KeySpace {
store: KS,
}
/// Wasm32 constructor
#[cfg(target_arch = "wasm32")]
impl KeySpace {}
/// Non-wasm constructor
#[cfg(not(target_arch = "wasm32"))]
impl KeySpace {}
/// Exposed methods, platform independant
impl KeySpace {
/// Get a [`Key`] previously stored under the provided name.
async fn get(&self, key: &str) -> Result<Option<Key>, Error> {
todo!();
}
/// Store a [`Key`] under the provided name.
async fn set(&self, key: &str, value: Key) -> Result<(), Error> {
todo!();
}
/// Delete the [`Key`] stored under the provided name.
async fn delete(&self, key: &str) -> Result<(), Error> {
todo!();
}
/// Iterate over all stored [`keys`](Key) in the KeySpace
async fn iter(&self) -> Result<impl Iterator<Item = (String, Key)>, Error> {
todo!()
}
}

View File

@ -0,0 +1,2 @@
/// A KeySpace using the filesystem as storage
pub mod KeySpace {}

View File

@ -0,0 +1,2 @@
/// KeySpace represents an IndexDB keyspace
pub struct KeySpace {}

View File

@ -1,3 +1,4 @@
pub mod error;
pub mod key;
pub mod keyspace;
pub mod kvs;