From f87f388280dcaaed41f1405f1f1093bcd9b27a30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Sat, 26 Oct 2024 16:27:32 +0200 Subject: [PATCH] events: Use Signatures in more places --- crates/ruma-events/CHANGELOG.md | 1 + crates/ruma-events/src/pdu.rs | 7 ++-- crates/ruma-events/src/room/member.rs | 43 +++++++++++-------------- crates/ruma-events/tests/it/pdu.rs | 40 +++++++++++------------ crates/ruma-state-res/src/test_utils.rs | 6 ++-- 5 files changed, 46 insertions(+), 51 deletions(-) diff --git a/crates/ruma-events/CHANGELOG.md b/crates/ruma-events/CHANGELOG.md index 58054070..9c20f697 100644 --- a/crates/ruma-events/CHANGELOG.md +++ b/crates/ruma-events/CHANGELOG.md @@ -48,6 +48,7 @@ Improvements: Breaking changes: - `StickerEventContent::url` was replaced by `StickerEventContent::source` which is a `StickerMediaSource` +- Use `ServerSignatures` for the `signatures` of `RoomV1Pdu`, `RoomV3Pdu` and `SignedContent`. # 0.28.1 diff --git a/crates/ruma-events/src/pdu.rs b/crates/ruma-events/src/pdu.rs index 25b382dc..c42c93be 100644 --- a/crates/ruma-events/src/pdu.rs +++ b/crates/ruma-events/src/pdu.rs @@ -9,8 +9,7 @@ use std::collections::BTreeMap; use js_int::UInt; use ruma_common::{ - MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedServerName, - OwnedServerSigningKeyId, OwnedUserId, + MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedUserId, ServerSignatures, }; use serde::{ de::{Error as _, IgnoredAny}, @@ -87,7 +86,7 @@ pub struct RoomV1Pdu { pub hashes: EventHash, /// Signatures for the PDU. - pub signatures: BTreeMap>, + pub signatures: ServerSignatures, } /// A 'persistent data unit' (event) for room versions 3 and beyond. @@ -140,7 +139,7 @@ pub struct RoomV3Pdu { pub hashes: EventHash, /// Signatures for the PDU. - pub signatures: BTreeMap>, + pub signatures: ServerSignatures, } /// Content hashes of a PDU. diff --git a/crates/ruma-events/src/room/member.rs b/crates/ruma-events/src/room/member.rs index 0bd23808..e8d8e60c 100644 --- a/crates/ruma-events/src/room/member.rs +++ b/crates/ruma-events/src/room/member.rs @@ -2,13 +2,10 @@ //! //! [`m.room.member`]: https://spec.matrix.org/latest/client-server-api/#mroommember -use std::collections::BTreeMap; - use js_int::Int; use ruma_common::{ serde::{CanBeEmpty, Raw, StringEnum}, - OwnedMxcUri, OwnedServerName, OwnedServerSigningKeyId, OwnedTransactionId, OwnedUserId, - RoomVersionId, UserId, + OwnedMxcUri, OwnedTransactionId, OwnedUserId, RoomVersionId, ServerSignatures, UserId, }; use ruma_macros::EventContent; use serde::{Deserialize, Serialize}; @@ -369,7 +366,7 @@ pub struct SignedContent { /// A single signature from the verifying server, in the format specified by the Signing Events /// section of the server-server API. - pub signatures: BTreeMap>, + pub signatures: ServerSignatures, /// The token property of the containing `third_party_invite` object. pub token: String, @@ -377,11 +374,7 @@ pub struct SignedContent { impl SignedContent { /// Creates a new `SignedContent` with the given mxid, signature and token. - pub fn new( - signatures: BTreeMap>, - mxid: OwnedUserId, - token: String, - ) -> Self { + pub fn new(signatures: ServerSignatures, mxid: OwnedUserId, token: String) -> Self { Self { mxid, signatures, token } } } @@ -706,15 +699,16 @@ mod tests { let third_party_invite = ev.content.third_party_invite.unwrap(); assert_eq!(third_party_invite.display_name, "alice"); assert_eq!(third_party_invite.signed.mxid, "@alice:example.org"); + assert_eq!(third_party_invite.signed.signatures.len(), 1); + let server_signatures = + third_party_invite.signed.signatures.get(server_name!("magic.forest")).unwrap(); assert_eq!( - third_party_invite.signed.signatures, + *server_signatures, btreemap! { - server_name!("magic.forest").to_owned() => btreemap! { - ServerSigningKeyId::from_parts( - SigningKeyAlgorithm::Ed25519, - server_signing_key_version!("3") - ) => "foobar".to_owned() - } + ServerSigningKeyId::from_parts( + SigningKeyAlgorithm::Ed25519, + server_signing_key_version!("3") + ) => "foobar".to_owned() } ); assert_eq!(third_party_invite.signed.token, "abc123"); @@ -779,15 +773,16 @@ mod tests { let third_party_invite = prev_content.third_party_invite.unwrap(); assert_eq!(third_party_invite.display_name, "alice"); assert_eq!(third_party_invite.signed.mxid, "@alice:example.org"); + assert_eq!(third_party_invite.signed.signatures.len(), 1); + let server_signatures = + third_party_invite.signed.signatures.get(server_name!("magic.forest")).unwrap(); assert_eq!( - third_party_invite.signed.signatures, + *server_signatures, btreemap! { - server_name!("magic.forest").to_owned() => btreemap! { - ServerSigningKeyId::from_parts( - SigningKeyAlgorithm::Ed25519, - server_signing_key_version!("3") - ) => "foobar".to_owned() - } + ServerSigningKeyId::from_parts( + SigningKeyAlgorithm::Ed25519, + server_signing_key_version!("3") + ) => "foobar".to_owned() } ); assert_eq!(third_party_invite.signed.token, "abc123"); diff --git a/crates/ruma-events/tests/it/pdu.rs b/crates/ruma-events/tests/it/pdu.rs index 6945b015..b22f62f4 100644 --- a/crates/ruma-events/tests/it/pdu.rs +++ b/crates/ruma-events/tests/it/pdu.rs @@ -4,8 +4,8 @@ use std::collections::BTreeMap; use js_int::uint; use ruma_common::{ - event_id, owned_event_id, owned_room_id, owned_user_id, server_name, - server_signing_key_version, MilliSecondsSinceUnixEpoch, ServerSigningKeyId, + event_id, owned_event_id, owned_room_id, owned_server_name, owned_user_id, + server_signing_key_version, MilliSecondsSinceUnixEpoch, ServerSignatures, ServerSigningKeyId, SigningKeyAlgorithm, }; use ruma_events::{ @@ -19,16 +19,16 @@ use serde_json::{ #[test] fn serialize_pdu_as_v1() { - let mut signatures = BTreeMap::new(); - let mut inner_signature = BTreeMap::new(); - inner_signature.insert( - ServerSigningKeyId::from_parts( - SigningKeyAlgorithm::Ed25519, - server_signing_key_version!("key_version"), - ), - "86BytesOfSignatureOfTheRedactedEvent".into(), + let mut signatures = ServerSignatures::new(); + let key_id = ServerSigningKeyId::from_parts( + SigningKeyAlgorithm::Ed25519, + server_signing_key_version!("key_version"), + ); + signatures.insert_signature( + owned_server_name!("example.com"), + key_id, + "86BytesOfSignatureOfTheRedactedEvent".to_owned(), ); - signatures.insert(server_name!("example.com").to_owned(), inner_signature); let mut unsigned = BTreeMap::new(); unsigned.insert("somekey".into(), to_raw_json_value(&json!({ "a": 456 })).unwrap()); @@ -87,16 +87,16 @@ fn serialize_pdu_as_v1() { #[test] fn serialize_pdu_as_v3() { - let mut signatures = BTreeMap::new(); - let mut inner_signature = BTreeMap::new(); - inner_signature.insert( - ServerSigningKeyId::from_parts( - SigningKeyAlgorithm::Ed25519, - server_signing_key_version!("key_version"), - ), - "86BytesOfSignatureOfTheRedactedEvent".into(), + let mut signatures = ServerSignatures::new(); + let key_id = ServerSigningKeyId::from_parts( + SigningKeyAlgorithm::Ed25519, + server_signing_key_version!("key_version"), + ); + signatures.insert_signature( + owned_server_name!("example.com"), + key_id, + "86BytesOfSignatureOfTheRedactedEvent".to_owned(), ); - signatures.insert(server_name!("example.com").to_owned(), inner_signature); let mut unsigned = BTreeMap::new(); unsigned.insert("somekey".into(), to_raw_json_value(&json!({ "a": 456 })).unwrap()); diff --git a/crates/ruma-state-res/src/test_utils.rs b/crates/ruma-state-res/src/test_utils.rs index 4ae23580..8a8e00a0 100644 --- a/crates/ruma-state-res/src/test_utils.rs +++ b/crates/ruma-state-res/src/test_utils.rs @@ -11,7 +11,7 @@ use futures_util::future::ready; use js_int::{int, uint}; use ruma_common::{ event_id, room_id, user_id, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, RoomId, - RoomVersionId, UserId, + RoomVersionId, ServerSignatures, UserId, }; use ruma_events::{ pdu::{EventHash, Pdu, RoomV3Pdu}, @@ -401,7 +401,7 @@ pub(crate) fn to_init_pdu_event( prev_events: vec![], depth: uint!(0), hashes: EventHash::new("".to_owned()), - signatures: BTreeMap::new(), + signatures: ServerSignatures::default(), }), }) } @@ -439,7 +439,7 @@ where prev_events, depth: uint!(0), hashes: EventHash::new("".to_owned()), - signatures: BTreeMap::new(), + signatures: ServerSignatures::default(), }), }) }