chore: add wasm console demo
This commit is contained in:
@@ -7,7 +7,9 @@ edition = "2021"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
instant = { version = "0.1", features = ["wasm-bindgen"] }
|
||||
web-sys = { version = "0.3", features = ["console"] }
|
||||
js-sys = "0.3"
|
||||
kvstore = { path = "../kvstore" }
|
||||
hex = "0.4"
|
||||
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
|
||||
|
@@ -9,6 +9,7 @@ use vault::rhai_bindings as vault_rhai_bindings;
|
||||
use vault::session::SessionManager;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::JsValue;
|
||||
use js_sys::Uint8Array;
|
||||
|
||||
thread_local! {
|
||||
static ENGINE: Lazy<RefCell<Engine>> = Lazy::new(|| RefCell::new(Engine::new()));
|
||||
@@ -21,6 +22,34 @@ pub use vault::session_singleton::SESSION_MANAGER;
|
||||
// Session Lifecycle
|
||||
// =====================
|
||||
|
||||
/// Create and unlock a new keyspace with the given name and password
|
||||
#[wasm_bindgen]
|
||||
pub async fn create_keyspace(keyspace: &str, password: &str) -> Result<(), JsValue> {
|
||||
let keyspace = keyspace.to_string();
|
||||
let password_vec = password.as_bytes().to_vec();
|
||||
match WasmStore::open("vault").await {
|
||||
Ok(store) => {
|
||||
let vault = vault::Vault::new(store);
|
||||
let mut manager = SessionManager::new(vault);
|
||||
match manager.create_keyspace(&keyspace, &password_vec, None).await {
|
||||
Ok(_) => {
|
||||
SESSION_MANAGER.with(|cell| cell.replace(Some(manager)));
|
||||
}
|
||||
Err(e) => {
|
||||
web_sys::console::error_1(&format!("Failed to create keyspace: {e}").into());
|
||||
return Err(JsValue::from_str(&format!("Failed to create keyspace: {e}")));
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
web_sys::console::error_1(&format!("Failed to open WasmStore: {e}").into());
|
||||
return Err(JsValue::from_str(&format!("Failed to open WasmStore: {e}")));
|
||||
}
|
||||
}
|
||||
SESSION_PASSWORD.with(|cell| cell.replace(Some(password.as_bytes().to_vec())));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Initialize session with keyspace and password
|
||||
#[wasm_bindgen]
|
||||
pub async fn init_session(keyspace: &str, password: &str) -> Result<(), JsValue> {
|
||||
@@ -61,6 +90,36 @@ pub fn lock_session() {
|
||||
// Keypair Management
|
||||
// =====================
|
||||
|
||||
/// Get metadata of the currently selected keypair
|
||||
#[wasm_bindgen]
|
||||
pub fn current_keypair_metadata() -> Result<JsValue, JsValue> {
|
||||
SESSION_MANAGER.with(|cell| {
|
||||
cell.borrow().as_ref()
|
||||
.and_then(|session| session.current_keypair_metadata())
|
||||
.map(|meta| wasm_bindgen::JsValue::from_serde(&meta).unwrap())
|
||||
.ok_or_else(|| JsValue::from_str("No keypair selected or no keyspace unlocked"))
|
||||
})
|
||||
}
|
||||
|
||||
/// Get public key of the currently selected keypair as Uint8Array
|
||||
#[wasm_bindgen]
|
||||
pub fn current_keypair_public_key() -> Result<JsValue, JsValue> {
|
||||
SESSION_MANAGER.with(|cell| {
|
||||
cell.borrow().as_ref()
|
||||
.and_then(|session| session.current_keypair_public_key())
|
||||
.map(|pk| js_sys::Uint8Array::from(pk.as_slice()).into())
|
||||
.ok_or_else(|| JsValue::from_str("No keypair selected or no keyspace unlocked"))
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns true if a keyspace is currently unlocked
|
||||
#[wasm_bindgen]
|
||||
pub fn is_unlocked() -> bool {
|
||||
SESSION_MANAGER.with(|cell| {
|
||||
cell.borrow().as_ref().map(|session| session.is_unlocked()).unwrap_or(false)
|
||||
})
|
||||
}
|
||||
|
||||
/// Get all keypairs from the current session
|
||||
/// Returns an array of keypair objects with id, type, and metadata
|
||||
// #[wasm_bindgen]
|
||||
@@ -118,18 +177,12 @@ pub async fn add_keypair(
|
||||
let password = SESSION_PASSWORD
|
||||
.with(|pw| pw.borrow().clone())
|
||||
.ok_or_else(|| JsValue::from_str("Session password not set"))?;
|
||||
let (keyspace_name, session_exists) = SESSION_MANAGER.with(|cell| {
|
||||
if let Some(ref session) = cell.borrow().as_ref() {
|
||||
let keyspace_name = session.current_keyspace().map(|_| "".to_string()); // TODO: replace with actual keyspace name if available;
|
||||
(keyspace_name, true)
|
||||
} else {
|
||||
(None, false)
|
||||
}
|
||||
let keyspace_name = SESSION_MANAGER.with(|cell| {
|
||||
cell.borrow().as_ref().and_then(|session| {
|
||||
session.current_keyspace_name().map(|name| name.to_string())
|
||||
})
|
||||
});
|
||||
let keyspace_name = keyspace_name.ok_or_else(|| JsValue::from_str("No keyspace selected"))?;
|
||||
if !session_exists {
|
||||
return Err(JsValue::from_str("Session not initialized"));
|
||||
}
|
||||
let key_type = key_type
|
||||
.as_deref()
|
||||
.map(|s| match s {
|
||||
@@ -153,6 +206,9 @@ pub async fn add_keypair(
|
||||
.add_keypair(&keyspace_name, &password, Some(key_type), metadata)
|
||||
.await
|
||||
.map_err(|e| JsValue::from_str(&format!("add_keypair error: {e}")))?;
|
||||
// Refresh in-memory keyspace data so list_keypairs reflects the new keypair immediately
|
||||
session.unlock_keyspace(&keyspace_name, &password).await
|
||||
.map_err(|e| JsValue::from_str(&format!("refresh keyspace after add_keypair error: {e}")))?;
|
||||
// Put session back
|
||||
SESSION_MANAGER.with(|cell| *cell.borrow_mut() = Some(session_opt.take().unwrap()));
|
||||
Ok(JsValue::from_str(&key_id))
|
||||
|
Reference in New Issue
Block a user