chore: add wasm console demo

This commit is contained in:
Sameh Abouel-saad
2025-05-26 13:22:42 +03:00
parent 1e52c572d2
commit 44b4dfd6a7
14 changed files with 1138 additions and 14 deletions

View File

@@ -7,6 +7,7 @@ edition = "2021"
path = "src/lib.rs"
[dependencies]
instant = { version = "0.1", features = ["wasm-bindgen"] }
once_cell = "1.18"
tokio = { version = "1.37", features = ["rt", "macros"] }
kvstore = { path = "../kvstore" }

View File

@@ -9,6 +9,8 @@ use zeroize::Zeroize;
/// SessionManager: Ergonomic, stateful wrapper over the Vault stateless API.
#[cfg(not(target_arch = "wasm32"))]
pub struct SessionManager<S: KVStore + Send + Sync> {
// ... existing fields
vault: Vault<S>,
unlocked_keyspace: Option<(String, Vec<u8>, KeyspaceData)>, // (name, password, data)
current_keypair: Option<String>,
@@ -38,7 +40,12 @@ impl<S: KVStore + Send + Sync> SessionManager<S> {
}
}
pub async fn create_keyspace(&mut self, name: &str, password: &[u8], tags: Option<Vec<String>>) -> Result<(), VaultError> {
pub async fn create_keyspace(
&mut self,
name: &str,
password: &[u8],
tags: Option<Vec<String>>,
) -> Result<(), VaultError> {
self.vault.create_keyspace(name, password, tags).await?;
self.unlock_keyspace(name, password).await
}
@@ -90,12 +97,34 @@ impl<S: KVStore + Send + Sync> SessionManager<S> {
self.unlocked_keyspace.as_ref().map(|(_, _, data)| data)
}
/// Returns the name of the currently unlocked keyspace, if any.
pub fn current_keyspace_name(&self) -> Option<&str> {
self.unlocked_keyspace
.as_ref()
.map(|(name, _, _)| name.as_str())
}
pub fn current_keypair(&self) -> Option<&KeyEntry> {
let keyspace = self.current_keyspace()?;
let key_id = self.current_keypair.as_ref()?;
keyspace.keypairs.iter().find(|k| &k.id == key_id)
}
/// Returns the metadata of the current selected keypair, if any.
pub fn current_keypair_metadata(&self) -> Option<crate::KeyMetadata> {
self.current_keypair().and_then(|k| k.metadata.clone())
}
/// Returns the public key of the current selected keypair, if any.
pub fn current_keypair_public_key(&self) -> Option<Vec<u8>> {
self.current_keypair().map(|k| k.public_key.clone())
}
/// Returns true if a keyspace is currently unlocked.
pub fn is_unlocked(&self) -> bool {
self.unlocked_keyspace.is_some()
}
pub async fn sign(&self, message: &[u8]) -> Result<Vec<u8>, VaultError> {
let (name, password, _) = self
.unlocked_keyspace
@@ -137,7 +166,12 @@ impl<S: KVStore> SessionManager<S> {
}
}
pub async fn create_keyspace(&mut self, name: &str, password: &[u8], tags: Option<Vec<String>>) -> Result<(), VaultError> {
pub async fn create_keyspace(
&mut self,
name: &str,
password: &[u8],
tags: Option<Vec<String>>,
) -> Result<(), VaultError> {
self.vault.create_keyspace(name, password, tags).await?;
self.unlock_keyspace(name, password).await
}
@@ -189,12 +223,34 @@ impl<S: KVStore> SessionManager<S> {
self.unlocked_keyspace.as_ref().map(|(_, _, data)| data)
}
/// Returns the name of the currently unlocked keyspace, if any.
pub fn current_keyspace_name(&self) -> Option<&str> {
self.unlocked_keyspace
.as_ref()
.map(|(name, _, _)| name.as_str())
}
pub fn current_keypair(&self) -> Option<&KeyEntry> {
let keyspace = self.current_keyspace()?;
let key_id = self.current_keypair.as_ref()?;
keyspace.keypairs.iter().find(|k| &k.id == key_id)
}
/// Returns the metadata of the current selected keypair, if any.
pub fn current_keypair_metadata(&self) -> Option<crate::KeyMetadata> {
self.current_keypair().and_then(|k| k.metadata.clone())
}
/// Returns the public key of the current selected keypair, if any.
pub fn current_keypair_public_key(&self) -> Option<Vec<u8>> {
self.current_keypair().map(|k| k.public_key.clone())
}
/// Returns true if a keyspace is currently unlocked.
pub fn is_unlocked(&self) -> bool {
self.unlocked_keyspace.is_some()
}
pub async fn sign(&self, message: &[u8]) -> Result<Vec<u8>, VaultError> {
let (name, password, _) = self
.unlocked_keyspace