Skip to content

Commit

Permalink
refactor(bindings): Start using #[uniffi::export] for matrix-sdk-cryp…
Browse files Browse the repository at this point in the history
…to-ffi
  • Loading branch information
jplatte committed Oct 6, 2022
1 parent a75d717 commit a3113bd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 35 deletions.
23 changes: 13 additions & 10 deletions bindings/matrix-sdk-crypto-ffi/src/backup_recovery_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ pub struct MegolmV1BackupKey {
pub backup_algorithm: String,
}

#[uniffi::export]
impl BackupRecoveryKey {
/// Convert the recovery key to a base 58 encoded string.
pub fn to_base58(&self) -> String {
self.inner.to_base58()
}

/// Convert the recovery key to a base 64 encoded string.
pub fn to_base64(&self) -> String {
self.inner.to_base64()
}
}

impl BackupRecoveryKey {
const KEY_SIZE: usize = 32;
const SALT_SIZE: usize = 32;
Expand Down Expand Up @@ -134,16 +147,6 @@ impl BackupRecoveryKey {
}
}

/// Convert the recovery key to a base 58 encoded string.
pub fn to_base58(&self) -> String {
self.inner.to_base58()
}

/// Convert the recovery key to a base 64 encoded string.
pub fn to_base64(&self) -> String {
self.inner.to_base64()
}

/// Try to decrypt a message that was encrypted using the public part of the
/// backup key.
pub fn decrypt_v1(
Expand Down
4 changes: 4 additions & 0 deletions bindings/matrix-sdk-crypto-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ fn parse_user_id(user_id: &str) -> Result<OwnedUserId, CryptoStoreError> {
ruma::UserId::parse(user_id).map_err(|e| CryptoStoreError::InvalidUserId(user_id.to_owned(), e))
}

mod uniffi_types {
pub use crate::{backup_recovery_key::BackupRecoveryKey, machine::OlmMachine};
}

#[allow(warnings)]
mod generated {
use super::*;
Expand Down
41 changes: 22 additions & 19 deletions bindings/matrix-sdk-crypto-ffi/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@ pub struct KeyRequestPair {
pub key_request: Request,
}

#[uniffi::export]
impl OlmMachine {
/// Get the user ID of the owner of this `OlmMachine`.
pub fn user_id(&self) -> String {
self.inner.user_id().to_string()
}

/// Get the device ID of the device of this `OlmMachine`.
pub fn device_id(&self) -> String {
self.inner.device_id().to_string()
}

/// Get our own identity keys.
pub fn identity_keys(&self) -> HashMap<String, String> {
let identity_keys = self.inner.identity_keys();
let curve_key = identity_keys.curve25519.to_base64();
let ed25519_key = identity_keys.ed25519.to_base64();

HashMap::from([("ed25519".to_owned(), ed25519_key), ("curve25519".to_owned(), curve_key)])
}
}

impl OlmMachine {
/// Create a new `OlmMachine`
///
Expand Down Expand Up @@ -116,16 +138,6 @@ impl OlmMachine {
})
}

/// Get the user ID of the owner of this `OlmMachine`.
pub fn user_id(&self) -> String {
self.inner.user_id().to_string()
}

/// Get the device ID of the device of this `OlmMachine`.
pub fn device_id(&self) -> String {
self.inner.device_id().to_string()
}

/// Get the display name of our own device.
pub fn display_name(&self) -> Result<Option<String>, CryptoStoreError> {
Ok(self.runtime.block_on(self.inner.display_name())?)
Expand Down Expand Up @@ -313,15 +325,6 @@ impl OlmMachine {
.collect())
}

/// Get our own identity keys.
pub fn identity_keys(&self) -> HashMap<String, String> {
let identity_keys = self.inner.identity_keys();
let curve_key = identity_keys.curve25519.to_base64();
let ed25519_key = identity_keys.ed25519.to_base64();

HashMap::from([("ed25519".to_owned(), ed25519_key), ("curve25519".to_owned(), curve_key)])
}

/// Get the list of outgoing requests that need to be sent to the
/// homeserver.
///
Expand Down
6 changes: 0 additions & 6 deletions bindings/matrix-sdk-crypto-ffi/src/olm.udl
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,6 @@ interface OlmMachine {
string? passphrase
);

record<DOMString, string> identity_keys();
string user_id();
string device_id();

[Throws=CryptoStoreError]
string receive_sync_changes([ByRef] string events,
DeviceLists device_changes,
Expand Down Expand Up @@ -438,8 +434,6 @@ interface BackupRecoveryKey {
constructor(string key);
[Name=from_base58, Throws=DecodeError]
constructor(string key);
string to_base58();
string to_base64();
MegolmV1BackupKey megolm_v1_public_key();
[Throws=PkDecryptionError]
string decrypt_v1(string ephemeral_key, string mac, string ciphertext);
Expand Down

0 comments on commit a3113bd

Please sign in to comment.