refactor: migrate extension to TypeScript and add Material-UI components
This commit is contained in:
@@ -221,15 +221,10 @@ pub async fn sign(message: &[u8]) -> Result<JsValue, JsValue> {
|
||||
// SAFETY: We only use this pointer synchronously within this function, and SESSION_MANAGER outlives this scope.
|
||||
let session_ptr =
|
||||
SESSION_MANAGER.with(|cell| cell.borrow().as_ref().map(|s| s as *const _));
|
||||
let password_opt = SESSION_PASSWORD.with(|pw| pw.borrow().clone());
|
||||
let session: &vault::session::SessionManager<kvstore::wasm::WasmStore> = match session_ptr {
|
||||
Some(ptr) => unsafe { &*ptr },
|
||||
None => return Err(JsValue::from_str("Session not initialized")),
|
||||
};
|
||||
let password = match password_opt {
|
||||
Some(p) => p,
|
||||
None => return Err(JsValue::from_str("Session password not set")),
|
||||
};
|
||||
match session.sign(message).await {
|
||||
Ok(sig_bytes) => {
|
||||
let hex_sig = hex::encode(&sig_bytes);
|
||||
@@ -239,3 +234,72 @@ pub async fn sign(message: &[u8]) -> Result<JsValue, JsValue> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Verify a signature with the current session's selected keypair
|
||||
#[wasm_bindgen]
|
||||
pub async fn verify(message: &[u8], signature: &str) -> Result<JsValue, JsValue> {
|
||||
{
|
||||
// SAFETY: We only use this pointer synchronously within this function, and SESSION_MANAGER outlives this scope.
|
||||
let session_ptr =
|
||||
SESSION_MANAGER.with(|cell| cell.borrow().as_ref().map(|s| s as *const _));
|
||||
let session: &vault::session::SessionManager<kvstore::wasm::WasmStore> = match session_ptr {
|
||||
Some(ptr) => unsafe { &*ptr },
|
||||
None => return Err(JsValue::from_str("Session not initialized")),
|
||||
};
|
||||
|
||||
// Convert hex signature to bytes
|
||||
let sig_bytes = match hex::decode(signature) {
|
||||
Ok(bytes) => bytes,
|
||||
Err(e) => return Err(JsValue::from_str(&format!("Invalid signature format: {e}"))),
|
||||
};
|
||||
|
||||
match session.verify(message, &sig_bytes).await {
|
||||
Ok(is_valid) => Ok(JsValue::from_bool(is_valid)),
|
||||
Err(e) => Err(JsValue::from_str(&format!("Verify error: {e}"))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Encrypt data using the current session's keyspace symmetric cipher
|
||||
#[wasm_bindgen]
|
||||
pub async fn encrypt_data(data: &[u8]) -> Result<JsValue, JsValue> {
|
||||
{
|
||||
// SAFETY: We only use this pointer synchronously within this function, and SESSION_MANAGER outlives this scope.
|
||||
let session_ptr =
|
||||
SESSION_MANAGER.with(|cell| cell.borrow().as_ref().map(|s| s as *const _));
|
||||
let session: &vault::session::SessionManager<kvstore::wasm::WasmStore> = match session_ptr {
|
||||
Some(ptr) => unsafe { &*ptr },
|
||||
None => return Err(JsValue::from_str("Session not initialized")),
|
||||
};
|
||||
|
||||
match session.encrypt(data).await {
|
||||
Ok(encrypted) => {
|
||||
// Return as Uint8Array for JavaScript
|
||||
Ok(Uint8Array::from(&encrypted[..]).into())
|
||||
}
|
||||
Err(e) => Err(JsValue::from_str(&format!("Encryption error: {e}"))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Decrypt data using the current session's keyspace symmetric cipher
|
||||
#[wasm_bindgen]
|
||||
pub async fn decrypt_data(encrypted: &[u8]) -> Result<JsValue, JsValue> {
|
||||
{
|
||||
// SAFETY: We only use this pointer synchronously within this function, and SESSION_MANAGER outlives this scope.
|
||||
let session_ptr =
|
||||
SESSION_MANAGER.with(|cell| cell.borrow().as_ref().map(|s| s as *const _));
|
||||
let session: &vault::session::SessionManager<kvstore::wasm::WasmStore> = match session_ptr {
|
||||
Some(ptr) => unsafe { &*ptr },
|
||||
None => return Err(JsValue::from_str("Session not initialized")),
|
||||
};
|
||||
|
||||
match session.decrypt(encrypted).await {
|
||||
Ok(decrypted) => {
|
||||
// Return as Uint8Array for JavaScript
|
||||
Ok(Uint8Array::from(&decrypted[..]).into())
|
||||
}
|
||||
Err(e) => Err(JsValue::from_str(&format!("Decryption error: {e}"))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user