From 46e9463a6001b5608a62645ee481ca45c6c518fa Mon Sep 17 00:00:00 2001 From: Andreas Coroiu Date: Wed, 25 Sep 2024 15:04:23 +0200 Subject: [PATCH] feat: hacky implemenation of tsify-next --- Cargo.lock | 29 ++++++++++++++++++ crates/bitwarden-vault/Cargo.toml | 6 ++++ crates/bitwarden-vault/src/totp.rs | 5 ++++ crates/bitwarden-wasm/Cargo.toml | 2 ++ crates/bitwarden-wasm/src/vault/mod.rs | 41 +++++++++++++++----------- 5 files changed, 65 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b79f195f8..7111d184e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -657,14 +657,17 @@ dependencies = [ "reqwest", "schemars", "serde", + "serde-wasm-bindgen", "serde_json", "serde_repr", "sha1", "sha2", "thiserror", "tokio", + "tsify-next", "uniffi", "uuid", + "wasm-bindgen", ] [[package]] @@ -674,6 +677,7 @@ dependencies = [ "argon2", "bitwarden", "bitwarden-json", + "bitwarden-vault", "chrono", "console_error_panic_hook", "console_log", @@ -681,6 +685,7 @@ dependencies = [ "log", "serde", "serde-wasm-bindgen", + "tsify-next", "wasm-bindgen", "wasm-bindgen-futures", "wasm-bindgen-test", @@ -4208,6 +4213,30 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "tsify-next" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f4a645dca4ee0800f5ab60ce166deba2db6a0315de795a2691e138a3d55d756" +dependencies = [ + "serde", + "serde-wasm-bindgen", + "tsify-next-macros", + "wasm-bindgen", +] + +[[package]] +name = "tsify-next-macros" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d5c06f8a51d759bb58129e30b2631739e7e1e4579fad1f30ac09a6c88e488a6" +dependencies = [ + "proc-macro2", + "quote", + "serde_derive_internals", + "syn 2.0.77", +] + [[package]] name = "typenum" version = "1.17.0" diff --git a/crates/bitwarden-vault/Cargo.toml b/crates/bitwarden-vault/Cargo.toml index 04a64078f..3dfc9cb7d 100644 --- a/crates/bitwarden-vault/Cargo.toml +++ b/crates/bitwarden-vault/Cargo.toml @@ -19,6 +19,7 @@ uniffi = [ "bitwarden-crypto/uniffi", "dep:uniffi", ] # Uniffi bindings +wasm = [] # Typescript and WASM bindings [dependencies] base64 = ">=0.22.1, <0.23" @@ -42,6 +43,11 @@ thiserror = ">=1.0.40, <2.0" uniffi = { version = "=0.28.1", optional = true } uuid = { version = ">=1.3.3, <2.0", features = ["serde"] } +# [target.'cfg(feature = "wasm")'.dependencies] +serde-wasm-bindgen = "0.6.5" +tsify-next = { version = "0.5.4", features = ["js"], default-features = false } +wasm-bindgen = { version = "0.2.91", features = ["serde-serialize"] } + [dev-dependencies] tokio = { version = "1.36.0", features = ["rt", "macros"] } diff --git a/crates/bitwarden-vault/src/totp.rs b/crates/bitwarden-vault/src/totp.rs index 8aee3e694..5fb8b84ea 100644 --- a/crates/bitwarden-vault/src/totp.rs +++ b/crates/bitwarden-vault/src/totp.rs @@ -8,6 +8,10 @@ use reqwest::Url; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use thiserror::Error; +// #[cfg(feature = "wasm")] +use tsify_next::Tsify; +// #[cfg(feature = "wasm")] +use wasm_bindgen::prelude::*; use crate::CipherListView; @@ -38,6 +42,7 @@ pub enum TotpError { #[derive(Serialize, Deserialize, Debug, JsonSchema)] #[serde(rename_all = "camelCase", deny_unknown_fields)] #[cfg_attr(feature = "uniffi", derive(uniffi::Record))] +#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))] pub struct TotpResponse { /// Generated TOTP code pub code: String, diff --git a/crates/bitwarden-wasm/Cargo.toml b/crates/bitwarden-wasm/Cargo.toml index dc0c21372..12e18df2b 100644 --- a/crates/bitwarden-wasm/Cargo.toml +++ b/crates/bitwarden-wasm/Cargo.toml @@ -20,6 +20,7 @@ argon2 = { version = ">=0.5.0, <0.6", features = [ "zeroize", ], default-features = false } bitwarden = { workspace = true } +bitwarden-vault = { workspace = true, features = ["wasm"] } bitwarden-json = { path = "../bitwarden-json", features = [ "secrets", "internal", @@ -33,6 +34,7 @@ js-sys = "0.3.68" log = "0.4.20" serde = { version = "1.0.196", features = ["derive"] } serde-wasm-bindgen = "0.6.5" +tsify-next = { version = "0.5.4", features = ["js"], default-features = false } wasm-bindgen = { version = "0.2.91", features = ["serde-serialize"] } wasm-bindgen-futures = "0.4.41" diff --git a/crates/bitwarden-wasm/src/vault/mod.rs b/crates/bitwarden-wasm/src/vault/mod.rs index c8eabf049..e68b861f4 100644 --- a/crates/bitwarden-wasm/src/vault/mod.rs +++ b/crates/bitwarden-wasm/src/vault/mod.rs @@ -1,5 +1,7 @@ +use serde::{Deserialize, Serialize}; use serde_wasm_bindgen::to_value; use std::sync::Arc; +use tsify_next::Tsify; use wasm_bindgen::prelude::*; use bitwarden::{ @@ -10,23 +12,24 @@ use chrono::prelude::*; use crate::error::Result; -#[wasm_bindgen(typescript_custom_section)] -const TOTP_RESPONSE: &'static str = r#" -export interface TotpResponse { - code: string; - period_foo: number; -} +// #[wasm_bindgen(typescript_custom_section)] +// const TOTP_RESPONSE: &'static str = r#" +// export interface TotpResponse { +// code: string; +// period_foo: number; +// } -export interface ClientVault { - generate_totp(key: string, time?: string): Promise; -} -"#; +// export interface ClientVault { +// generate_totp(key: string, time?: string): Promise; +// } +// "#; -#[wasm_bindgen] -pub struct JsTotpResult { - code: String, - period: u64, -} +// #[derive(Tsify, Serialize, Deserialize)] +// #[tsify(into_wasm_abi, from_wasm_abi)] +// pub struct JsTotpResult { +// code: String, +// period: u64, +// } // #[derive(uniffi::Object)] #[wasm_bindgen] @@ -66,8 +69,8 @@ impl ClientVault { /// - A base32 encoded string /// - OTP Auth URI /// - Steam URI - #[wasm_bindgen(skip_typescript)] - pub async fn generate_totp(&self, key: String, time: Option) -> JsValue { + #[wasm_bindgen] + pub async fn generate_totp(&self, key: String, time: Option) -> TotpResponse { // TODO: Fix time // let time = time.map(|time| { // // TODO: fix error @@ -80,7 +83,9 @@ impl ClientVault { .map_err(Error::Totp) .unwrap(); - to_value(&result).unwrap() + result + + // to_value(&result).unwrap() // JsTotpResult { // code: result.code,