Skip to main content

bitwarden_crypto/
uniffi_support.rs

1use std::{num::NonZeroU32, str::FromStr};
2
3use bitwarden_uniffi_error::convert_result;
4
5use crate::{
6    CryptoError, EncString, EncodingError, PublicKey, SignedPublicKey, SymmetricCryptoKey,
7    UnsignedSharedKey,
8    safe::{DataEnvelope, PasswordProtectedKeyEnvelope},
9};
10
11uniffi::custom_type!(NonZeroU32, u32, {
12    remote,
13    try_lift: |val| {
14        convert_result(NonZeroU32::new(val).ok_or(CryptoError::ZeroNumber))
15    },
16    lower: |obj| obj.get(),
17});
18
19uniffi::custom_type!(SymmetricCryptoKey, String, {
20    remote,
21    try_lift: |val| {
22        convert_result(SymmetricCryptoKey::try_from(val.as_str().to_string()))
23    },
24    lower: |obj| obj.to_base64().to_string(),
25});
26
27uniffi::custom_type!(EncString, String, {
28    try_lift: |val| {
29        convert_result(EncString::from_str(&val))
30    },
31    lower: |obj| obj.to_string(),
32});
33
34uniffi::custom_type!(UnsignedSharedKey, String, {
35    try_lift: |val| {
36        convert_result(UnsignedSharedKey::from_str(&val))
37    },
38    lower: |obj| obj.to_string(),
39});
40
41uniffi::custom_type!(SignedPublicKey, String, {
42    try_lift: |val| {
43        convert_result(SignedPublicKey::from_str(&val))
44    },
45    lower: |obj| obj.into(),
46});
47
48uniffi::custom_type!(PublicKey, String, {
49    try_lift: |val| {
50        convert_result(PublicKey::from_str(&val)
51            .map_err(|_e| EncodingError::InvalidBase64Encoding))
52    },
53    lower: |obj| obj.to_string(),
54});
55
56uniffi::custom_type!(DataEnvelope, String, {
57    try_lift: |val| convert_result(DataEnvelope::from_str(val.as_str())),
58    lower: |obj| obj.to_string(),
59});
60
61uniffi::custom_type!(PasswordProtectedKeyEnvelope, String, {
62    remote,
63    try_lift: |val| convert_result(PasswordProtectedKeyEnvelope::from_str(&val)),
64    lower: |obj| obj.into(),
65});