From c6f491861e27ebc4e2ef051d4e6281317717191b Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 21 Apr 2023 12:14:24 +0200 Subject: [PATCH] crypto-ffi: Use proc-macros for types no longer referenced in UDL --- .../src/backup_recovery_key.rs | 5 +- bindings/matrix-sdk-crypto-ffi/src/device.rs | 1 + bindings/matrix-sdk-crypto-ffi/src/error.rs | 6 +- bindings/matrix-sdk-crypto-ffi/src/lib.rs | 16 +- bindings/matrix-sdk-crypto-ffi/src/machine.rs | 3 +- bindings/matrix-sdk-crypto-ffi/src/olm.udl | 201 ------------------ .../matrix-sdk-crypto-ffi/src/responses.rs | 6 +- bindings/matrix-sdk-crypto-ffi/src/users.rs | 1 + .../matrix-sdk-crypto-ffi/src/verification.rs | 7 + 9 files changed, 32 insertions(+), 214 deletions(-) diff --git a/bindings/matrix-sdk-crypto-ffi/src/backup_recovery_key.rs b/bindings/matrix-sdk-crypto-ffi/src/backup_recovery_key.rs index 110406fb2..5cf6e3974 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/backup_recovery_key.rs +++ b/bindings/matrix-sdk-crypto-ffi/src/backup_recovery_key.rs @@ -27,7 +27,8 @@ pub enum PkDecryptionError { } /// Error type for the decoding and storing of the backup key. -#[derive(Debug, Error)] +#[derive(Debug, Error, uniffi::Error)] +#[uniffi(flat_error)] pub enum DecodeError { /// An error happened while decoding the recovery key. #[error(transparent)] @@ -40,7 +41,7 @@ pub enum DecodeError { /// Struct containing info about the way the backup key got derived from a /// passphrase. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, uniffi::Record)] pub struct PassphraseInfo { /// The salt that was used during key derivation. pub private_key_salt: String, diff --git a/bindings/matrix-sdk-crypto-ffi/src/device.rs b/bindings/matrix-sdk-crypto-ffi/src/device.rs index ed8ae0e3a..a2a0b708b 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/device.rs +++ b/bindings/matrix-sdk-crypto-ffi/src/device.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use matrix_sdk_crypto::Device as InnerDevice; /// An E2EE capable Matrix device. +#[derive(uniffi::Record)] pub struct Device { /// The device owner. pub user_id: String, diff --git a/bindings/matrix-sdk-crypto-ffi/src/error.rs b/bindings/matrix-sdk-crypto-ffi/src/error.rs index 24b921287..04bcc9e55 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/error.rs +++ b/bindings/matrix-sdk-crypto-ffi/src/error.rs @@ -7,7 +7,8 @@ use matrix_sdk_crypto::{ use matrix_sdk_sqlite::OpenStoreError; use ruma::{IdParseError, OwnedUserId}; -#[derive(Debug, thiserror::Error)] +#[derive(Debug, thiserror::Error, uniffi::Error)] +#[uniffi(flat_error)] pub enum KeyImportError { #[error(transparent)] Export(#[from] KeyExportError), @@ -26,7 +27,8 @@ pub enum SecretImportError { Import(#[from] RustSecretImportError), } -#[derive(Debug, thiserror::Error)] +#[derive(Debug, thiserror::Error, uniffi::Error)] +#[uniffi(flat_error)] pub enum SignatureError { #[error(transparent)] Signature(#[from] InnerSignatureError), diff --git a/bindings/matrix-sdk-crypto-ffi/src/lib.rs b/bindings/matrix-sdk-crypto-ffi/src/lib.rs index 24a3cb094..d26ea8d04 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/lib.rs +++ b/bindings/matrix-sdk-crypto-ffi/src/lib.rs @@ -56,7 +56,7 @@ pub use verification::{ use vodozemac::{Curve25519PublicKey, Ed25519PublicKey}; /// Struct collecting data that is important to migrate to the rust-sdk -#[derive(Deserialize, Serialize)] +#[derive(Deserialize, Serialize, uniffi::Record)] pub struct MigrationData { /// The pickled version of the Olm Account account: PickledAccount, @@ -79,6 +79,7 @@ pub struct MigrationData { } /// Struct collecting data that is important to migrate sessions to the rust-sdk +#[derive(uniffi::Record)] pub struct SessionMigrationData { /// The user id that the data belongs to. user_id: String, @@ -100,7 +101,7 @@ pub struct SessionMigrationData { /// /// Holds all the information that needs to be stored in a database to restore /// an account. -#[derive(Debug, Deserialize, Serialize)] +#[derive(Debug, Deserialize, Serialize, uniffi::Record)] pub struct PickledAccount { /// The user id of the account owner. pub user_id: String, @@ -118,7 +119,7 @@ pub struct PickledAccount { /// /// Holds all the information that needs to be stored in a database to restore /// a Session. -#[derive(Debug, Deserialize, Serialize)] +#[derive(Debug, Deserialize, Serialize, uniffi::Record)] pub struct PickledSession { /// The pickle string holding the Olm Session. pub pickle: String, @@ -136,7 +137,7 @@ pub struct PickledSession { /// /// Holds all the information that needs to be stored in a database to restore /// an InboundGroupSession. -#[derive(Debug, Deserialize, Serialize)] +#[derive(Debug, Deserialize, Serialize, uniffi::Record)] pub struct PickledInboundGroupSession { /// The pickle string holding the InboundGroupSession. pub pickle: String, @@ -543,7 +544,7 @@ impl ProgressListener for T { } /// An encryption algorithm to be used to encrypt messages sent to a room. -#[derive(Debug, Deserialize, Serialize, PartialEq)] +#[derive(Debug, Deserialize, Serialize, PartialEq, uniffi::Enum)] pub enum EventEncryptionAlgorithm { /// Olm version 1 using Curve25519, AES-256, and SHA-256. OlmV1Curve25519AesSha2, @@ -575,6 +576,7 @@ impl TryFrom for EventEncryptionAlgorithm { } /// Who can see a room's history. +#[derive(uniffi::Enum)] pub enum HistoryVisibility { /// Previous events are accessible to newly joined members from the point /// they were invited onwards. @@ -720,7 +722,7 @@ pub struct CrossSigningStatus { /// A struct containing private cross signing keys that can be backed up or /// uploaded to the secret store. -#[derive(Deserialize, Serialize)] +#[derive(Deserialize, Serialize, uniffi::Record)] pub struct CrossSigningKeyExport { /// The seed of the master key encoded as unpadded base64. pub master_key: Option, @@ -813,7 +815,7 @@ impl From for CrossSigningStatus { } /// Room encryption settings which are modified by state events or user options -#[derive(Debug, Deserialize, Serialize, PartialEq)] +#[derive(Debug, PartialEq, Deserialize, Serialize, uniffi::Record)] pub struct RoomSettings { /// The encryption algorithm that should be used in the room. pub algorithm: EventEncryptionAlgorithm, diff --git a/bindings/matrix-sdk-crypto-ffi/src/machine.rs b/bindings/matrix-sdk-crypto-ffi/src/machine.rs index bb333529d..e9f267601 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/machine.rs +++ b/bindings/matrix-sdk-crypto-ffi/src/machine.rs @@ -79,6 +79,7 @@ impl Drop for OlmMachine { /// A pair of outgoing room key requests, both of those are sendToDevice /// requests. +#[derive(uniffi::Record)] pub struct KeyRequestPair { /// The optional cancellation, this is None if no previous key request was /// sent out for this key, thus it doesn't need to be cancelled. @@ -88,7 +89,7 @@ pub struct KeyRequestPair { } /// The result of a signature verification of a signed JSON object. -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, uniffi::Record)] pub struct SignatureVerification { /// The result of the signature verification using the public key of our own /// device. diff --git a/bindings/matrix-sdk-crypto-ffi/src/olm.udl b/bindings/matrix-sdk-crypto-ffi/src/olm.udl index 8930f37ec..b98565243 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/olm.udl +++ b/bindings/matrix-sdk-crypto-ffi/src/olm.udl @@ -8,22 +8,6 @@ callback interface ProgressListener { void on_progress(i32 progress, i32 total); }; -[Error] -enum KeyImportError { - "Export", - "CryptoStore", - "Json", -}; - -[Error] -enum SignatureError { - "Signature", - "Identifier", - "CryptoStore", - "UnknownDevice", - "UnknownUserIdentity", -}; - [Error] enum CryptoStoreError { "OpenStore", @@ -34,58 +18,12 @@ enum CryptoStoreError { "Identifier", }; -dictionary KeysImportResult { - i64 imported; - i64 total; - record>> keys; -}; - -dictionary Device { - string user_id; - string device_id; - record keys; - sequence algorithms; - string? display_name; - boolean is_blocked; - boolean locally_trusted; - boolean cross_signing_trusted; -}; - -[Enum] -interface UserIdentity { - Own( - string user_id, - boolean trusts_our_own_device, - string master_key, - string self_signing_key, - string user_signing_key - ); - Other( - string user_id, - string master_key, - string self_signing_key - ); -}; - -dictionary CrossSigningKeyExport { - string? master_key; - string? self_signing_key; - string? user_signing_key; -}; - dictionary CancelInfo { string cancel_code; string reason; boolean cancelled_by_us; }; -dictionary StartSasResult { - Sas sas; - OutgoingVerificationRequest request; -}; - -interface Sas {}; - [Enum] interface SasState { Started(); @@ -100,13 +38,6 @@ callback interface SasListener { void on_change(SasState state); }; -dictionary ScanResult { - QrCode qr; - OutgoingVerificationRequest request; -}; - -interface QrCode {}; - [Enum] interface QrCodeState { Started(); @@ -121,8 +52,6 @@ callback interface QrCodeListener { void on_change(QrCodeState state); }; -interface VerificationRequest {}; - [Enum] interface VerificationRequestState { Requested(); @@ -135,52 +64,6 @@ callback interface VerificationRequestListener { void on_change(VerificationRequestState state); }; -dictionary RequestVerificationResult { - VerificationRequest verification; - OutgoingVerificationRequest request; -}; - -dictionary ConfirmVerificationResult { - sequence requests; - SignatureUploadRequest? signature_request; -}; - -dictionary KeyRequestPair { - Request? cancellation; - Request key_request; -}; - -[Enum] -interface OutgoingVerificationRequest { - ToDevice(string request_id, string event_type, string body); - InRoom(string request_id, string room_id, string event_type, string content); -}; - -[Enum] -interface Request { - ToDevice(string request_id, string event_type, string body); - KeysUpload(string request_id, string body); - KeysQuery(string request_id, sequence users); - KeysClaim(string request_id, record> one_time_keys); - KeysBackup(string request_id, string version, string rooms); - RoomMessage(string request_id, string room_id, string event_type, string content); - SignatureUpload(string request_id, string body); -}; - -dictionary SignatureUploadRequest { - string body; -}; - -enum RequestType { - "KeysQuery", - "KeysClaim", - "KeysUpload", - "ToDevice", - "SignatureUpload", - "KeysBackup", - "RoomMessage", -}; - enum LocalTrust { "Verified", "BlackListed", @@ -188,19 +71,6 @@ enum LocalTrust { "Unset", }; - -enum EventEncryptionAlgorithm { - "OlmV1Curve25519AesSha2", - "MegolmV1AesSha2", -}; - -enum HistoryVisibility { - "Invited", - "Joined", - "Shared", - "WorldReadable", -}; - interface OlmMachine { [Throws=CryptoStoreError] constructor( @@ -211,18 +81,6 @@ interface OlmMachine { ); }; -dictionary PassphraseInfo { - string private_key_salt; - i32 private_key_iterations; -}; - -dictionary SignatureVerification { - SignatureState device_signature; - SignatureState user_identity_signature; - record other_devices_signatures; - boolean trusted; -}; - enum SignatureState { "Missing", "Invalid", @@ -230,12 +88,6 @@ enum SignatureState { "ValidAndTrusted", }; -[Error] -enum DecodeError { - "Decode", - "CryptoStore", -}; - interface BackupRecoveryKey { constructor(); [Name=from_passphrase] @@ -247,56 +99,3 @@ interface BackupRecoveryKey { [Name=from_base58, Throws=DecodeError] constructor(string key); }; - -dictionary MigrationData { - PickledAccount account; - sequence sessions; - sequence inbound_group_sessions; - string? backup_version; - string? backup_recovery_key; - sequence pickle_key; - CrossSigningKeyExport cross_signing; - sequence tracked_users; - record room_settings; -}; - -dictionary SessionMigrationData { - string user_id; - string device_id; - string curve25519_key; - string ed25519_key; - sequence sessions; - sequence inbound_group_sessions; - sequence pickle_key; -}; - -dictionary PickledAccount { - string user_id; - string device_id; - string pickle; - boolean shared; - i64 uploaded_signed_key_count; -}; - -dictionary PickledSession { - string pickle; - string sender_key; - boolean created_using_fallback_key; - string creation_time; - string last_use_time; -}; - -dictionary PickledInboundGroupSession { - string pickle; - string sender_key; - record signing_key; - string room_id; - sequence forwarding_chains; - boolean imported; - boolean backed_up; -}; - -dictionary RoomSettings { - EventEncryptionAlgorithm algorithm; - boolean only_allow_trusted_devices; -}; diff --git a/bindings/matrix-sdk-crypto-ffi/src/responses.rs b/bindings/matrix-sdk-crypto-ffi/src/responses.rs index 7132a2266..df6fbcdf2 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/responses.rs +++ b/bindings/matrix-sdk-crypto-ffi/src/responses.rs @@ -28,6 +28,7 @@ use ruma::{ }; use serde_json::json; +#[derive(uniffi::Record)] pub struct SignatureUploadRequest { pub body: String, } @@ -84,6 +85,7 @@ impl From<(RustUploadSigningKeysRequest, RustSignatureUploadRequest)> } } +#[derive(uniffi::Enum)] pub enum OutgoingVerificationRequest { ToDevice { request_id: String, event_type: String, body: String }, InRoom { request_id: String, room_id: String, event_type: String, content: String }, @@ -114,7 +116,7 @@ impl From for OutgoingVerificationRequest { } } -#[derive(Debug)] +#[derive(Debug, uniffi::Enum)] pub enum Request { ToDevice { request_id: String, event_type: String, body: String }, KeysUpload { request_id: String, body: String }, @@ -223,6 +225,7 @@ pub(crate) fn response_from_string(body: &str) -> Response> { .expect("Can't create HTTP response") } +#[derive(uniffi::Enum)] pub enum RequestType { KeysQuery, KeysClaim, @@ -256,6 +259,7 @@ impl From for RumaDeviceLists { } } +#[derive(uniffi::Record)] pub struct KeysImportResult { /// The number of room keys that were imported. pub imported: i64, diff --git a/bindings/matrix-sdk-crypto-ffi/src/users.rs b/bindings/matrix-sdk-crypto-ffi/src/users.rs index 77b59d04c..485d4be7c 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/users.rs +++ b/bindings/matrix-sdk-crypto-ffi/src/users.rs @@ -4,6 +4,7 @@ use crate::CryptoStoreError; /// Enum representing cross signing identities of our own user or some other /// user. +#[derive(uniffi::Enum)] pub enum UserIdentity { /// Our own user identity. Own { diff --git a/bindings/matrix-sdk-crypto-ffi/src/verification.rs b/bindings/matrix-sdk-crypto-ffi/src/verification.rs index ca07caad6..86561706e 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/verification.rs +++ b/bindings/matrix-sdk-crypto-ffi/src/verification.rs @@ -107,6 +107,7 @@ impl Verification { } /// The `m.sas.v1` verification flow. +#[derive(uniffi::Object)] pub struct Sas { pub(crate) inner: InnerSas, pub(crate) runtime: Handle, @@ -320,6 +321,7 @@ impl From for QrCodeState { /// The `m.qr_code.scan.v1`, `m.qr_code.show.v1`, and `m.reciprocate.v1` /// verification flow. +#[derive(uniffi::Object)] pub struct QrCode { pub(crate) inner: InnerQr, pub(crate) runtime: Handle, @@ -476,6 +478,7 @@ impl From for CancelInfo { } /// A result type for starting SAS verifications. +#[derive(uniffi::Record)] pub struct StartSasResult { /// The SAS verification object that got created. pub sas: Arc, @@ -485,6 +488,7 @@ pub struct StartSasResult { } /// A result type for scanning QR codes. +#[derive(uniffi::Record)] pub struct ScanResult { /// The QR code verification object that got created. pub qr: Arc, @@ -494,6 +498,7 @@ pub struct ScanResult { } /// A result type for requesting verifications. +#[derive(uniffi::Record)] pub struct RequestVerificationResult { /// The verification request object that got created. pub verification: Arc, @@ -503,6 +508,7 @@ pub struct RequestVerificationResult { } /// A result type for confirming verifications. +#[derive(uniffi::Record)] pub struct ConfirmVerificationResult { /// The requests that needs to be sent out to notify the other side that we /// confirmed the verification. @@ -566,6 +572,7 @@ impl From for VerificationRequestState { /// The verificatoin request object which then can transition into some concrete /// verification method +#[derive(uniffi::Object)] pub struct VerificationRequest { pub(crate) inner: InnerVerificationRequest, pub(crate) runtime: Handle,