From e5507bf84d8d13cf946f27dab9fec1a835b45df4 Mon Sep 17 00:00:00 2001 From: Ruben De Smet Date: Sat, 12 Oct 2024 14:24:17 +0200 Subject: [PATCH 1/3] Allow PNI in PendingMember --- src/groups_v2/model.rs | 10 ++++++---- src/groups_v2/operations.rs | 17 +++++++++++++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/groups_v2/model.rs b/src/groups_v2/model.rs index 3fa3e6417..92be11d0d 100644 --- a/src/groups_v2/model.rs +++ b/src/groups_v2/model.rs @@ -5,6 +5,8 @@ use serde::{Deserialize, Serialize}; use uuid::Uuid; use zkgroup::profiles::ProfileKey; +use crate::ServiceAddress; + use super::GroupDecodingError; #[derive(Copy, Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] @@ -30,15 +32,15 @@ impl PartialEq for Member { } } -#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +#[derive(Clone, Debug, PartialEq, Eq)] pub struct PendingMember { - pub uuid: Uuid, + pub address: ServiceAddress, pub role: Role, pub added_by_uuid: Uuid, pub timestamp: u64, } -#[derive(Derivative, Clone, Deserialize, Serialize)] +#[derive(Derivative, Clone)] #[derivative(Debug)] pub struct RequestingMember { pub uuid: Uuid, @@ -69,7 +71,7 @@ pub struct AccessControl { pub add_from_invite_link: AccessRequired, } -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq)] pub struct Group { pub title: String, pub avatar: String, diff --git a/src/groups_v2/operations.rs b/src/groups_v2/operations.rs index 9c3279ab1..6b766631a 100644 --- a/src/groups_v2/operations.rs +++ b/src/groups_v2/operations.rs @@ -58,6 +58,19 @@ impl From for GroupDecodingError { } impl GroupOperations { + fn decrypt_service_id( + &self, + ciphertext: &[u8], + ) -> Result { + match self + .group_secret_params + .decrypt_service_id(bincode::deserialize(ciphertext)?)? + { + ServiceId::Aci(aci) => Ok(ServiceId::from(aci)), + ServiceId::Pni(pni) => Ok(ServiceId::from(pni)), + } + } + fn decrypt_aci( &self, ciphertext: &[u8], @@ -131,11 +144,11 @@ impl GroupOperations { ) -> Result { let inner_member = member.member.ok_or(GroupDecodingError::WrongBlob)?; - let aci = self.decrypt_aci(&inner_member.user_id)?; + let service_id = self.decrypt_service_id(&inner_member.user_id)?; let added_by_uuid = self.decrypt_aci(&member.added_by_user_id)?; Ok(PendingMember { - uuid: aci.into(), + address: service_id.into(), role: inner_member.role.try_into()?, added_by_uuid: added_by_uuid.into(), timestamp: member.timestamp, From b5db2895cf24d8c65d3d7b56cc5854a655fdbea2 Mon Sep 17 00:00:00 2001 From: Ruben De Smet Date: Sat, 12 Oct 2024 14:58:02 +0200 Subject: [PATCH 2/3] impl From for ServiceAddress --- src/service_address.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/service_address.rs b/src/service_address.rs index de3c74267..d0e4f2469 100644 --- a/src/service_address.rs +++ b/src/service_address.rs @@ -87,6 +87,19 @@ impl ServiceAddress { } } +impl From for ServiceAddress { + fn from(service_id: ServiceId) -> Self { + match service_id { + ServiceId::Aci(service_id) => { + ServiceAddress::from_aci(service_id.into()) + }, + ServiceId::Pni(service_id) => { + ServiceAddress::from_pni(service_id.into()) + }, + } + } +} + impl TryFrom<&ProtocolAddress> for ServiceAddress { type Error = ParseServiceAddressError; From b80922a7e6d38e7f4f78acc21a8a5ad53745e677 Mon Sep 17 00:00:00 2001 From: Ruben De Smet Date: Sat, 12 Oct 2024 15:00:19 +0200 Subject: [PATCH 3/3] Rename new_{aci,pni} to from_{aci,pni} This is more according to Rust convention regarding constructors --- src/service_address.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/service_address.rs b/src/service_address.rs index d0e4f2469..de3680879 100644 --- a/src/service_address.rs +++ b/src/service_address.rs @@ -1,6 +1,6 @@ use std::convert::TryFrom; -use libsignal_protocol::{DeviceId, ProtocolAddress}; +use libsignal_protocol::{DeviceId, ProtocolAddress, ServiceId}; use uuid::Uuid; pub use crate::push_service::ServiceIdType; @@ -48,14 +48,24 @@ impl ServiceAddress { } } + #[deprecated] pub fn new_aci(uuid: Uuid) -> Self { + Self::from_aci(uuid) + } + + pub fn from_aci(uuid: Uuid) -> Self { Self { uuid, identity: ServiceIdType::AccountIdentity, } } + #[deprecated] pub fn new_pni(uuid: Uuid) -> Self { + Self::from_pni(uuid) + } + + pub fn from_pni(uuid: Uuid) -> Self { Self { uuid, identity: ServiceIdType::PhoneNumberIdentity, @@ -106,9 +116,9 @@ impl TryFrom<&ProtocolAddress> for ServiceAddress { fn try_from(addr: &ProtocolAddress) -> Result { let value = addr.name(); if let Some(pni) = value.strip_prefix("PNI:") { - Ok(ServiceAddress::new_pni(Uuid::parse_str(pni)?)) + Ok(ServiceAddress::from_pni(Uuid::parse_str(pni)?)) } else { - Ok(ServiceAddress::new_aci(Uuid::parse_str(value)?)) + Ok(ServiceAddress::from_aci(Uuid::parse_str(value)?)) } .map_err(|e| { tracing::error!("Parsing ServiceAddress from {:?}", addr); @@ -122,9 +132,9 @@ impl TryFrom<&str> for ServiceAddress { fn try_from(value: &str) -> Result { if let Some(pni) = value.strip_prefix("PNI:") { - Ok(ServiceAddress::new_pni(Uuid::parse_str(pni)?)) + Ok(ServiceAddress::from_pni(Uuid::parse_str(pni)?)) } else { - Ok(ServiceAddress::new_aci(Uuid::parse_str(value)?)) + Ok(ServiceAddress::from_aci(Uuid::parse_str(value)?)) } .map_err(|e| { tracing::error!("Parsing ServiceAddress from '{}'", value); @@ -138,9 +148,9 @@ impl TryFrom<&[u8]> for ServiceAddress { fn try_from(value: &[u8]) -> Result { if let Some(pni) = value.strip_prefix(b"PNI:") { - Ok(ServiceAddress::new_pni(Uuid::from_slice(pni)?)) + Ok(ServiceAddress::from_pni(Uuid::from_slice(pni)?)) } else { - Ok(ServiceAddress::new_aci(Uuid::from_slice(value)?)) + Ok(ServiceAddress::from_aci(Uuid::from_slice(value)?)) } .map_err(|e| { tracing::error!("Parsing ServiceAddress from {:?}", value);