Skip to main content

river_core/
chat_delegate.rs

1use serde::{Deserialize, Serialize};
2
3/// Room key identifier (owner's verifying key bytes)
4pub type RoomKey = [u8; 32];
5
6/// Unique identifier for a signing request (for request/response correlation)
7pub type RequestId = u64;
8
9/// Messages sent from the App to the Chat Delegate
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub enum ChatDelegateRequestMsg {
12    // Key-value storage operations
13    StoreRequest {
14        key: ChatDelegateKey,
15        value: Vec<u8>,
16    },
17    GetRequest {
18        key: ChatDelegateKey,
19    },
20    DeleteRequest {
21        key: ChatDelegateKey,
22    },
23    ListRequest,
24
25    // Signing key management
26    /// Store a signing key for a room (room_key = owner's verifying key bytes)
27    StoreSigningKey {
28        room_key: RoomKey,
29        signing_key_bytes: [u8; 32],
30    },
31    /// Get the public key for a stored signing key
32    GetPublicKey {
33        room_key: RoomKey,
34    },
35
36    // Signing operations - pass serialized data, get signature back
37    // All signing ops include request_id for response correlation
38    /// Sign a message (MessageV1 serialized)
39    SignMessage {
40        room_key: RoomKey,
41        request_id: RequestId,
42        message_bytes: Vec<u8>,
43    },
44    /// Sign a member invitation (Member serialized)
45    SignMember {
46        room_key: RoomKey,
47        request_id: RequestId,
48        member_bytes: Vec<u8>,
49    },
50    /// Sign a ban (BanV1 serialized)
51    SignBan {
52        room_key: RoomKey,
53        request_id: RequestId,
54        ban_bytes: Vec<u8>,
55    },
56    /// Sign a room configuration (Configuration serialized)
57    SignConfig {
58        room_key: RoomKey,
59        request_id: RequestId,
60        config_bytes: Vec<u8>,
61    },
62    /// Sign member info (MemberInfo serialized)
63    SignMemberInfo {
64        room_key: RoomKey,
65        request_id: RequestId,
66        member_info_bytes: Vec<u8>,
67    },
68    /// Sign a secret version record (SecretVersionRecordV1 serialized)
69    SignSecretVersion {
70        room_key: RoomKey,
71        request_id: RequestId,
72        record_bytes: Vec<u8>,
73    },
74    /// Sign an encrypted secret for member (EncryptedSecretForMemberV1 serialized)
75    SignEncryptedSecret {
76        room_key: RoomKey,
77        request_id: RequestId,
78        secret_bytes: Vec<u8>,
79    },
80    /// Sign a room upgrade (RoomUpgrade serialized)
81    SignUpgrade {
82        room_key: RoomKey,
83        request_id: RequestId,
84        upgrade_bytes: Vec<u8>,
85    },
86}
87
88#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
89pub struct ChatDelegateKey(pub Vec<u8>);
90
91impl ChatDelegateKey {
92    pub fn new(key: Vec<u8>) -> Self {
93        Self(key)
94    }
95
96    pub fn as_bytes(&self) -> &[u8] {
97        &self.0
98    }
99}
100
101/// Responses sent from the Chat Delegate to the App
102#[derive(Debug, Clone, Serialize, Deserialize)]
103pub enum ChatDelegateResponseMsg {
104    // Key-value storage responses
105    GetResponse {
106        key: ChatDelegateKey,
107        value: Option<Vec<u8>>,
108    },
109    ListResponse {
110        keys: Vec<ChatDelegateKey>,
111    },
112    StoreResponse {
113        key: ChatDelegateKey,
114        value_size: usize,
115        result: Result<(), String>,
116    },
117    DeleteResponse {
118        key: ChatDelegateKey,
119        result: Result<(), String>,
120    },
121
122    // Signing key management responses
123    /// Response to StoreSigningKey
124    StoreSigningKeyResponse {
125        room_key: RoomKey,
126        result: Result<(), String>,
127    },
128    /// Response to GetPublicKey
129    GetPublicKeyResponse {
130        room_key: RoomKey,
131        /// The public key bytes if the signing key exists
132        public_key: Option<[u8; 32]>,
133    },
134
135    // Signing response (used for all signing operations)
136    /// Response to any signing operation
137    SignResponse {
138        room_key: RoomKey,
139        /// The request ID for correlation
140        request_id: RequestId,
141        /// The signature bytes (64 bytes for Ed25519, as Vec for serde compatibility)
142        signature: Result<Vec<u8>, String>,
143    },
144}