Skip to content

Commit b8783c2

Browse files
committed
refactor(crypto): Use struct destructuring instead of separate field access in some places
In several places, we access almost all the fields of a struct to create an `InboundGroupSession` from a pure data struct. When new fields are added to these data structs, it's easy to overlook updating the `InboundGroupSession` accordingly. By using struct destructuring, we ensure that newly added fields are explicitly considered, making it harder to forget one of the newly added fields.
1 parent 6110e66 commit b8783c2

File tree

2 files changed

+71
-28
lines changed

2 files changed

+71
-28
lines changed

Diff for: crates/matrix-sdk-crypto/src/olm/group_sessions/inbound.rs

+37-15
Original file line numberDiff line numberDiff line change
@@ -332,24 +332,36 @@ impl InboundGroupSession {
332332
/// * `pickle_mode` - The mode that was used to pickle the session, either
333333
/// an unencrypted mode or an encrypted using passphrase.
334334
pub fn from_pickle(pickle: PickledInboundGroupSession) -> Result<Self, PickleError> {
335-
let session: InnerSession = pickle.pickle.into();
335+
let PickledInboundGroupSession {
336+
pickle,
337+
sender_key,
338+
signing_key,
339+
sender_data,
340+
room_id,
341+
imported,
342+
backed_up,
343+
history_visibility,
344+
algorithm,
345+
} = pickle;
346+
347+
let session: InnerSession = pickle.into();
336348
let first_known_index = session.first_known_index();
337349
let session_id = session.session_id();
338350

339351
Ok(InboundGroupSession {
340352
inner: Mutex::new(session).into(),
341353
session_id: session_id.into(),
342354
creator_info: SessionCreatorInfo {
343-
curve25519_key: pickle.sender_key,
344-
signing_keys: pickle.signing_key.into(),
355+
curve25519_key: sender_key,
356+
signing_keys: signing_key.into(),
345357
},
346-
sender_data: pickle.sender_data,
347-
history_visibility: pickle.history_visibility.into(),
358+
sender_data,
359+
history_visibility: history_visibility.into(),
348360
first_known_index,
349-
room_id: (*pickle.room_id).into(),
350-
backed_up: AtomicBool::from(pickle.backed_up).into(),
351-
algorithm: pickle.algorithm.into(),
352-
imported: pickle.imported,
361+
room_id,
362+
backed_up: AtomicBool::from(backed_up).into(),
363+
algorithm: algorithm.into(),
364+
imported,
353365
})
354366
}
355367

@@ -554,25 +566,35 @@ impl TryFrom<&ExportedRoomKey> for InboundGroupSession {
554566
type Error = SessionCreationError;
555567

556568
fn try_from(key: &ExportedRoomKey) -> Result<Self, Self::Error> {
569+
let ExportedRoomKey {
570+
algorithm,
571+
room_id,
572+
sender_key,
573+
session_id,
574+
session_key,
575+
sender_claimed_keys,
576+
forwarding_curve25519_key_chain: _,
577+
} = key;
578+
557579
let config = OutboundGroupSession::session_config(&key.algorithm)?;
558-
let session = InnerSession::import(&key.session_key, config);
580+
let session = InnerSession::import(session_key, config);
559581
let first_known_index = session.first_known_index();
560582

561583
Ok(InboundGroupSession {
562584
inner: Mutex::new(session).into(),
563-
session_id: key.session_id.to_owned().into(),
585+
session_id: session_id.to_owned().into(),
564586
creator_info: SessionCreatorInfo {
565-
curve25519_key: key.sender_key,
566-
signing_keys: key.sender_claimed_keys.to_owned().into(),
587+
curve25519_key: *sender_key,
588+
signing_keys: sender_claimed_keys.to_owned().into(),
567589
},
568590
// TODO: In future, exported keys should contain sender data that we can use here.
569591
// See https://github.com/matrix-org/matrix-rust-sdk/issues/3548
570592
sender_data: SenderData::default(),
571593
history_visibility: None.into(),
572594
first_known_index,
573-
room_id: key.room_id.to_owned(),
595+
room_id: room_id.to_owned(),
574596
imported: true,
575-
algorithm: key.algorithm.to_owned().into(),
597+
algorithm: algorithm.to_owned().into(),
576598
backed_up: AtomicBool::from(false).into(),
577599
})
578600
}

Diff for: crates/matrix-sdk-crypto/src/olm/group_sessions/mod.rs

+34-13
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,32 @@ impl ExportedRoomKey {
109109
session_id: String,
110110
room_key: BackedUpRoomKey,
111111
) -> Self {
112+
let BackedUpRoomKey {
113+
algorithm,
114+
sender_key,
115+
session_key,
116+
sender_claimed_keys,
117+
forwarding_curve25519_key_chain,
118+
} = room_key;
119+
112120
Self {
113-
algorithm: room_key.algorithm,
121+
algorithm,
114122
room_id,
115-
sender_key: room_key.sender_key,
123+
sender_key,
116124
session_id,
117-
session_key: room_key.session_key,
118-
sender_claimed_keys: room_key.sender_claimed_keys,
119-
forwarding_curve25519_key_chain: room_key.forwarding_curve25519_key_chain,
125+
session_key,
126+
sender_claimed_keys,
127+
forwarding_curve25519_key_chain,
120128
}
121129
}
122130
}
123131

124-
/// A backed up version of an `InboundGroupSession`
132+
/// A backed up version of an [`InboundGroupSession`].
133+
///
134+
/// This can be used to backup the [`InboundGroupSession`] to the server using
135+
/// [server-side key backups].
125136
///
126-
/// This can be used to backup the `InboundGroupSession` to the server.
137+
/// [server-side key backups]: https://spec.matrix.org/unstable/client-server-api/#server-side-key-backups
127138
#[derive(Deserialize, Serialize)]
128139
#[allow(missing_debug_implementations)]
129140
pub struct BackedUpRoomKey {
@@ -208,13 +219,23 @@ impl TryFrom<ExportedRoomKey> for ForwardedRoomKeyContent {
208219
}
209220

210221
impl From<ExportedRoomKey> for BackedUpRoomKey {
211-
fn from(k: ExportedRoomKey) -> Self {
222+
fn from(value: ExportedRoomKey) -> Self {
223+
let ExportedRoomKey {
224+
algorithm,
225+
room_id: _,
226+
sender_key,
227+
session_id: _,
228+
session_key,
229+
sender_claimed_keys,
230+
forwarding_curve25519_key_chain,
231+
} = value;
232+
212233
Self {
213-
algorithm: k.algorithm,
214-
sender_key: k.sender_key,
215-
session_key: k.session_key,
216-
sender_claimed_keys: k.sender_claimed_keys,
217-
forwarding_curve25519_key_chain: k.forwarding_curve25519_key_chain,
234+
algorithm,
235+
sender_key,
236+
session_key,
237+
sender_claimed_keys,
238+
forwarding_curve25519_key_chain,
218239
}
219240
}
220241
}

0 commit comments

Comments
 (0)