1- use zeroize:: { Zeroize , ZeroizeOnDrop } ;
1+ use zeroize:: Zeroize ;
22
33use crate :: {
44 CryptoKeystoreResult ,
5- traits:: { EntityBase , EntityGetBorrowed as _, KeyType , OwnedKeyType , PrimaryKey , SearchableEntity as _} ,
5+ traits:: { EntityBase , EntityGetBorrowed as _, KeyType , PrimaryKey , SearchableEntity as _} ,
66} ;
77
88/// This type exists so that we can efficiently search for the children of a given group.
@@ -88,77 +88,6 @@ impl<'a> KeyType for ConversationId<'a> {
8888 }
8989}
9090
91- /// [`MlsPendingMessage`]s have no distinct primary key;
92- /// they must always be accessed via [`MlsPendingMessage::find_all_by_conversation_id`] and
93- /// cleaned up with [`MlsPendingMessage::delete_by_conversation_id`]
94- ///
95- /// However, we have to fake a primary key type in order to support
96- /// `KeystoreTransaction::remove_pending_messages_by_conversation_id`. Additionally we need the same one in WASM, where
97- /// it's necessary for item-level encryption.
98- ///
99- /// This implementation is fairly inefficient and hopefully temporary. But it at least implements the correct semantics.
100- #[ derive( ZeroizeOnDrop ) ]
101- pub struct MlsPendingMessagePrimaryKey {
102- pub ( crate ) foreign_id : Vec < u8 > ,
103- message : Vec < u8 > ,
104- }
105-
106- impl MlsPendingMessagePrimaryKey {
107- /// Construct a partial mls pending message primary key from only the conversation id.
108- ///
109- /// This does not in fact uniquely identify a single pending message--it should always uniquely
110- /// identify exactly 0 pending messages--but we have to have it so that we can search and delete
111- /// by conversation id within transactions.
112- pub ( crate ) fn from_conversation_id ( conversation_id : impl AsRef < [ u8 ] > ) -> Self {
113- Self {
114- foreign_id : conversation_id. as_ref ( ) . to_owned ( ) ,
115- message : Vec :: new ( ) ,
116- }
117- }
118- }
119-
120- impl From < & MlsPendingMessage > for MlsPendingMessagePrimaryKey {
121- fn from ( value : & MlsPendingMessage ) -> Self {
122- Self {
123- foreign_id : value. foreign_id . clone ( ) ,
124- message : value. message . clone ( ) ,
125- }
126- }
127- }
128-
129- impl KeyType for MlsPendingMessagePrimaryKey {
130- fn bytes ( & self ) -> std:: borrow:: Cow < ' _ , [ u8 ] > {
131- // run-length encoding: 32 bits of size for each field, followed by the field
132- let fields = [ & self . foreign_id , & self . message ] ;
133- let mut key = Vec :: with_capacity (
134- ( ( u32:: BITS / u8:: BITS ) as usize * fields. len ( ) ) + self . foreign_id . len ( ) + self . message . len ( ) ,
135- ) ;
136- for field in fields {
137- key. extend ( ( field. len ( ) as u32 ) . to_le_bytes ( ) ) ;
138- key. extend ( field. as_slice ( ) ) ;
139- }
140- key. into ( )
141- }
142- }
143-
144- impl OwnedKeyType for MlsPendingMessagePrimaryKey {
145- fn from_bytes ( bytes : & [ u8 ] ) -> Option < Self > {
146- // run-length decoding: 32 bits of size for each field, followed by the field
147- let ( len, bytes) = bytes. split_at_checked ( 4 ) ?;
148- let len = u32:: from_le_bytes ( len. try_into ( ) . ok ( ) ?) ;
149- let ( foreign_id, bytes) = bytes. split_at_checked ( len as _ ) ?;
150-
151- let ( len, bytes) = bytes. split_at_checked ( 4 ) ?;
152- let len = u32:: from_le_bytes ( len. try_into ( ) . ok ( ) ?) ;
153- let ( message, bytes) = bytes. split_at_checked ( len as _ ) ?;
154-
155- bytes. is_empty ( ) . then ( || Self {
156- foreign_id : foreign_id. to_owned ( ) ,
157- message : message. to_owned ( ) ,
158- } )
159- }
160- }
161-
16291/// Entity representing a buffered message
16392#[ derive( core_crypto_macros:: Debug , Clone , PartialEq , Eq , Zeroize , serde:: Serialize , serde:: Deserialize ) ]
16493#[ zeroize( drop) ]
@@ -168,11 +97,11 @@ pub struct MlsPendingMessage {
16897 pub message : Vec < u8 > ,
16998}
17099
100+ /// [`MlsPendingMessage`] has no real primary key, but we have to fake it for the trait hierarchy,
101+ /// so here we are.
171102impl PrimaryKey for MlsPendingMessage {
172- type PrimaryKey = MlsPendingMessagePrimaryKey ;
173- fn primary_key ( & self ) -> Self :: PrimaryKey {
174- self . into ( )
175- }
103+ type PrimaryKey = ( ) ;
104+ fn primary_key ( & self ) -> Self :: PrimaryKey { }
176105}
177106
178107/// Entity representing a buffered commit.
0 commit comments