Skip to content

Commit

Permalink
Deduplicate SignedPreKeyEntity and SignedPreKey
Browse files Browse the repository at this point in the history
  • Loading branch information
rubdos committed Jan 14, 2024
1 parent 3519ddd commit 0faee1b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
12 changes: 7 additions & 5 deletions libsignal-service/src/account_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl<Service: PushService> AccountManager<Service> {
) -> Result<
(
Vec<PreKeyEntity>,
SignedPreKeyRecord,
SignedPreKeyEntity,
Vec<KyberPreKeyEntity>,
Option<KyberPreKeyEntity>,
),
Expand Down Expand Up @@ -184,6 +184,8 @@ impl<Service: PushService> AccountManager<Service> {
&signed_prekey_record,
)
.instrument(tracing::trace_span!(parent: &span, "save signed pre key", signed_pre_key_id = ?next_signed_pre_key_id)).await?;
let signed_prekey_entity =
SignedPreKeyEntity::try_from(signed_prekey_record)?;

let pq_last_resort_key = if use_last_resort_key {
tracing::warn!("Last resort Kyber key unimplemented");
Expand All @@ -204,7 +206,7 @@ impl<Service: PushService> AccountManager<Service> {

Ok((
pre_key_entities,
signed_prekey_record,
signed_prekey_entity,
pq_pre_key_entities,
pq_last_resort_key,
))
Expand Down Expand Up @@ -260,8 +262,8 @@ impl<Service: PushService> AccountManager<Service> {
return Ok(());
}

let (pre_keys, signed_pre_key_record, pq_pre_keys, pq_last_resort_key) =
self.generate_pre_keys(
let (pre_keys, signed_pre_key, pq_pre_keys, pq_last_resort_key) = self
.generate_pre_keys(
protocol_store,
service_id_type,
csprng,
Expand All @@ -281,7 +283,7 @@ impl<Service: PushService> AccountManager<Service> {

let pre_key_state = PreKeyState {
pre_keys,
signed_pre_key: signed_pre_key_record.try_into()?,
signed_pre_key,
identity_key,
pq_pre_keys,
pq_last_resort_key,
Expand Down
20 changes: 5 additions & 15 deletions libsignal-service/src/pre_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,14 @@ pub struct SignedPreKeyEntity {
pub signature: Vec<u8>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SignedPreKey {
key_id: u32,
#[serde(with = "serde_public_key")]
public_key: PublicKey,
#[serde(with = "serde_base64")]
signature: Vec<u8>,
}

impl TryFrom<SignedPreKeyRecord> for SignedPreKey {
impl TryFrom<SignedPreKeyRecord> for SignedPreKeyEntity {
type Error = SignalProtocolError;

fn try_from(key: SignedPreKeyRecord) -> Result<Self, Self::Error> {
Ok(SignedPreKey {
Ok(SignedPreKeyEntity {
key_id: key.id()?.into(),
public_key: key.key_pair()?.public_key,
signature: key.signature()?,
public_key: key.key_pair()?.public_key.serialize().to_vec(),
signature: key.signature()?.to_vec(),
})
}
}
Expand Down Expand Up @@ -122,7 +112,7 @@ impl TryFrom<KyberPreKeyRecord> for KyberPreKeyEntity {
#[serde(rename_all = "camelCase")]
pub struct PreKeyState {
pub pre_keys: Vec<PreKeyEntity>,
pub signed_pre_key: SignedPreKey,
pub signed_pre_key: SignedPreKeyEntity,
#[serde(with = "serde_public_key")]
pub identity_key: PublicKey,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
7 changes: 3 additions & 4 deletions libsignal-service/src/push_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use crate::{
envelope::*,
groups_v2::GroupDecodingError,
pre_keys::{
KyberPreKeyEntity, PreKeyEntity, PreKeyState, SignedPreKey,
SignedPreKeyEntity,
KyberPreKeyEntity, PreKeyEntity, PreKeyState, SignedPreKeyEntity,
},
profile_cipher::ProfileCipherError,
proto::{attachment_pointer::AttachmentIdentifier, AttachmentPointer},
Expand Down Expand Up @@ -407,8 +406,8 @@ pub struct StaleDevices {
pub struct LinkRequest {
pub verification_code: String,
pub account_attributes: LinkAccountAttributes,
pub aci_signed_pre_key: SignedPreKey,
pub pni_signed_pre_key: SignedPreKey,
pub aci_signed_pre_key: SignedPreKeyEntity,
pub pni_signed_pre_key: SignedPreKeyEntity,
pub aci_pq_last_resort_pre_key: KyberPreKeyEntity,
pub pni_pq_last_resort_pre_key: KyberPreKeyEntity,
}
Expand Down

0 comments on commit 0faee1b

Please sign in to comment.