Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Rust SDK to latest version #134

Merged
merged 7 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 49 additions & 4 deletions src/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub struct EncryptionSettings {

/// Should untrusted devices receive the room key, or should they be
/// excluded from the conversation.
#[wasm_bindgen(js_name = "onlyAllowTrustedDevices")]
pub only_allow_trusted_devices: bool,
#[wasm_bindgen(js_name = "sharingStrategy")]
pub sharing_strategy: CollectStrategy,
richvdh marked this conversation as resolved.
Show resolved Hide resolved
}

impl Default for EncryptionSettings {
Expand All @@ -46,7 +46,7 @@ impl Default for EncryptionSettings {
rotation_period: default.rotation_period.as_micros().try_into().unwrap(),
rotation_period_messages: default.rotation_period_msgs,
history_visibility: default.history_visibility.into(),
only_allow_trusted_devices: default.only_allow_trusted_devices,
sharing_strategy: default.sharing_strategy.into(),
}
}
}
Expand All @@ -69,7 +69,7 @@ impl From<&EncryptionSettings> for matrix_sdk_crypto::olm::EncryptionSettings {
rotation_period: Duration::from_micros(value.rotation_period),
rotation_period_msgs: value.rotation_period_messages,
history_visibility: value.history_visibility.clone().into(),
only_allow_trusted_devices: value.only_allow_trusted_devices,
sharing_strategy: value.sharing_strategy.clone().into(),
}
}
}
Expand Down Expand Up @@ -116,6 +116,51 @@ impl From<matrix_sdk_crypto::types::EventEncryptionAlgorithm> for EncryptionAlgo
}
}

#[wasm_bindgen()]
#[derive(Debug, Clone, PartialEq, Eq)]
/// Strategy to collect the devices that should receive room keys for the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put the #[…] after the documentation. It's more Rust idiomatic to do so.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

/// current discussion.
///
/// See matrix_sdk_crypto::CollectStrategy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make these docs stand in their own right, so that the online doc (https://matrix-org.github.io/matrix-rust-sdk-crypto-wasm/classes/OlmMachine.html) is useful. (The fact that there are existing docs that just point to a Rust class is definitely bad.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

pub enum CollectStrategy {
/// Device based sharing strategy, excluding devices that are not trusted.
DeviceBasedStrategyOnlyTrustedDevices,
/// Device based sharing strategy, including all devices.
DeviceBasedStrategyAllDevices,
/// Only distribute to devices signed by their owner.
IdentityBasedStrategy,
}

impl From<CollectStrategy> for matrix_sdk_crypto::CollectStrategy {
fn from(value: CollectStrategy) -> Self {
match value {
CollectStrategy::DeviceBasedStrategyOnlyTrustedDevices => {
Self::DeviceBasedStrategy { only_allow_trusted_devices: true }
}
CollectStrategy::DeviceBasedStrategyAllDevices => {
Self::DeviceBasedStrategy { only_allow_trusted_devices: false }
}
CollectStrategy::IdentityBasedStrategy => Self::IdentityBasedStrategy,
}
}
}

impl From<matrix_sdk_crypto::CollectStrategy> for CollectStrategy {
fn from(value: matrix_sdk_crypto::CollectStrategy) -> Self {
match value {
matrix_sdk_crypto::CollectStrategy::DeviceBasedStrategy {
only_allow_trusted_devices: true,
} => Self::DeviceBasedStrategyOnlyTrustedDevices,
matrix_sdk_crypto::CollectStrategy::DeviceBasedStrategy {
only_allow_trusted_devices: false,
} => Self::DeviceBasedStrategyAllDevices,
matrix_sdk_crypto::CollectStrategy::IdentityBasedStrategy => {
Self::IdentityBasedStrategy
}
}
}
}

/// Take a look at [`matrix_sdk_common::deserialized_responses::ShieldState`]
/// for more info.
#[wasm_bindgen]
Expand Down
17 changes: 7 additions & 10 deletions src/libolm_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,19 +300,15 @@ async fn import_olm_sessions_to_store(
.await?
.context("Base data must be imported before calling `migrateOlmSessions`")?;

let user_id = account.user_id();
let device_id = account.device_id();
let identity_keys = &account.identity_keys;

let sessions = pickled_sessions
.into_iter()
.map(|pickled_session| {
Session::from_pickle(
user_id.to_owned(),
device_id.to_owned(),
identity_keys.clone(),
pickled_session,
)
// Session::from_pickle normally needs the device keys from storage
// (which will include cross-signing signatures), for embedding the
// key in outgoing messages. But in this case, it is just getting
// stored, so we can use the device keys generated by the account.
Session::from_pickle(account.device_keys(), pickled_session)
.expect("The account is invalid")
})
.collect();

Expand Down Expand Up @@ -441,6 +437,7 @@ fn libolm_pickled_megolm_session_to_rust_pickled_session(
pickle,
sender_key,
signing_key: sender_signing_keys,
sender_data: Default::default(),
room_id: libolm_session
.room_id
.clone()
Expand Down
Loading