arminius_lib_model/user_keys.rs
1// SPDX-FileCopyrightText: 2025 BWI GmbH
2//
3// SPDX-License-Identifier: Apache-2.0
4
5//! Data types related to cross-signing keys.
6use std::collections::HashMap;
7
8use ruma::{OwnedDeviceId, OwnedOneTimeKeyId, OwnedUserId, encryption::OneTimeKey};
9
10/// Enum representing all our key types stored in the database
11///
12/// This enum is also defined inside the database. It corresponds to the
13/// Device Keys (See <https://spec.matrix.org/v1.16/client-server-api/#device-keys>) or the
14/// 3 cross-signing key types (See <https://spec.matrix.org/v1.15/client-server-api/#cross-signing>).
15#[derive(Debug, Clone, sqlx::Type, PartialEq, Eq, PartialOrd, Ord, Hash)]
16#[sqlx(type_name = "KEY_TYPE", rename_all = "snake_case")]
17pub enum DbKeyType {
18 /// Device keys
19 Device,
20 /// Master Key (MSK)
21 Master,
22 /// Self-Signing Key (SSK)
23 SelfSigning,
24 /// User-Signing Key (USK)
25 UserSigning,
26}
27
28/// This is a map from user IDs to a map from device IDs to a map from key
29/// ID to [`OneTimeKeyRow`].
30///
31/// This complex is used by the database layer to fetch all the requested
32/// one-time and fallback keys at once.
33pub type OneTimeKeyMap =
34 HashMap<OwnedUserId, HashMap<OwnedDeviceId, HashMap<OwnedOneTimeKeyId, OneTimeKeyRow>>>;
35
36/// Represents a one-time key in the database (See
37/// <https://spec.matrix.org/v1.15/client-server-api/#one-time-and-fallback-keys>).
38#[derive(Debug)]
39pub struct OneTimeKeyRow {
40 /// The ID of the one-time key.
41 pub nid: i32,
42 /// The actual key object containing the one-time key data.
43 pub key: OneTimeKey,
44 /// Whether or not the key has ever been claimed. This is `Some` only if
45 /// the underlying `OneTimeKey` is a fallback key.
46 pub used: Option<bool>,
47}