use std::collections::BTreeMap;
use ruma_identifiers::{DeviceIdBox, DeviceKeyId, EventEncryptionAlgorithm, UserId};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct DeviceKeys {
pub user_id: UserId,
pub device_id: DeviceIdBox,
pub algorithms: Vec<EventEncryptionAlgorithm>,
pub keys: BTreeMap<DeviceKeyId, String>,
pub signatures: BTreeMap<UserId, BTreeMap<DeviceKeyId, String>>,
#[serde(default, skip_serializing_if = "UnsignedDeviceInfo::is_empty")]
pub unsigned: UnsignedDeviceInfo,
}
impl DeviceKeys {
pub fn new(
user_id: UserId,
device_id: DeviceIdBox,
algorithms: Vec<EventEncryptionAlgorithm>,
keys: BTreeMap<DeviceKeyId, String>,
signatures: BTreeMap<UserId, BTreeMap<DeviceKeyId, String>>,
) -> Self {
Self { user_id, device_id, algorithms, keys, signatures, unsigned: Default::default() }
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct UnsignedDeviceInfo {
#[serde(skip_serializing_if = "Option::is_none")]
pub device_display_name: Option<String>,
}
impl UnsignedDeviceInfo {
pub fn new() -> Self {
Default::default()
}
pub fn is_empty(&self) -> bool {
self.device_display_name.is_none()
}
}
pub type SignedKeySignatures = BTreeMap<UserId, BTreeMap<DeviceKeyId, String>>;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct SignedKey {
pub key: String,
pub signatures: SignedKeySignatures,
}
impl SignedKey {
pub fn new(key: String, signatures: SignedKeySignatures) -> Self {
Self { key, signatures }
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[serde(untagged)]
pub enum OneTimeKey {
SignedKey(SignedKey),
Key(String),
}
pub type CrossSigningKeySignatures = BTreeMap<UserId, BTreeMap<String, String>>;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct CrossSigningKey {
pub user_id: UserId,
pub usage: Vec<KeyUsage>,
pub keys: BTreeMap<String, String>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub signatures: CrossSigningKeySignatures,
}
impl CrossSigningKey {
pub fn new(
user_id: UserId,
usage: Vec<KeyUsage>,
keys: BTreeMap<String, String>,
signatures: CrossSigningKeySignatures,
) -> Self {
Self { user_id, usage, keys, signatures }
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[serde(rename_all = "snake_case")]
pub enum KeyUsage {
Master,
SelfSigning,
UserSigning,
}