bitwarden_wasm_internal/
crypto.rs

1use bitwarden_core::{
2    client::encryption_settings::EncryptionSettingsError,
3    mobile::crypto::{
4        InitOrgCryptoRequest, InitUserCryptoRequest, MakeKeyPairResponse,
5        VerifyAsymmetricKeysRequest, VerifyAsymmetricKeysResponse,
6    },
7};
8use bitwarden_crypto::CryptoError;
9use wasm_bindgen::prelude::*;
10
11#[wasm_bindgen]
12pub struct CryptoClient(bitwarden_core::mobile::CryptoClient);
13
14impl CryptoClient {
15    pub fn new(client: bitwarden_core::mobile::CryptoClient) -> Self {
16        Self(client)
17    }
18}
19
20#[wasm_bindgen]
21impl CryptoClient {
22    /// Initialization method for the user crypto. Needs to be called before any other crypto
23    /// operations.
24    pub async fn initialize_user_crypto(
25        &self,
26        req: InitUserCryptoRequest,
27    ) -> Result<(), EncryptionSettingsError> {
28        self.0.initialize_user_crypto(req).await
29    }
30
31    /// Initialization method for the organization crypto. Needs to be called after
32    /// `initialize_user_crypto` but before any other crypto operations.
33    pub async fn initialize_org_crypto(
34        &self,
35        req: InitOrgCryptoRequest,
36    ) -> Result<(), EncryptionSettingsError> {
37        self.0.initialize_org_crypto(req).await
38    }
39
40    /// Generates a new key pair and encrypts the private key with the provided user key.
41    /// Crypto initialization not required.
42    pub fn make_key_pair(&self, user_key: String) -> Result<MakeKeyPairResponse, CryptoError> {
43        self.0.make_key_pair(user_key)
44    }
45
46    /// Verifies a user's asymmetric keys by decrypting the private key with the provided user
47    /// key. Returns if the private key is decryptable and if it is a valid matching key.
48    /// Crypto initialization not required.
49    pub fn verify_asymmetric_keys(
50        &self,
51        request: VerifyAsymmetricKeysRequest,
52    ) -> Result<VerifyAsymmetricKeysResponse, CryptoError> {
53        self.0.verify_asymmetric_keys(request)
54    }
55}