Introduce kyber pre key triple table

This commit is contained in:
Fedor Indutny
2025-09-29 16:23:41 -07:00
committed by GitHub
parent af55cf4682
commit 658a63cfe6
8 changed files with 178 additions and 15 deletions

View File

@@ -140,6 +140,7 @@ import type {
GetUnreadByConversationAndMarkReadResultType,
IdentityKeyIdType,
ItemKeyType,
KyberPreKeyTripleType,
MediaItemDBType,
MessageAttachmentsCursorType,
MessageCursorType,
@@ -531,6 +532,7 @@ export const DataWriter: ServerWritableInterface = {
bulkAddKyberPreKeys,
removeKyberPreKeyById,
removeKyberPreKeysByServiceId,
markKyberTripleSeenOrFail,
removeAllKyberPreKeys,
createOrUpdatePreKey,
@@ -1075,6 +1077,34 @@ function removeKyberPreKeysByServiceId(
serviceId,
});
}
function markKyberTripleSeenOrFail(
db: WritableDB,
{ id, signedPreKeyId, baseKey }: KyberPreKeyTripleType
): 'seen' | 'fail' {
// Notes that `kyberPreKey_triples` has
// - Unique constraint on id, signedPreKeyId, baseKey so that we can't insert
// two identical rows
// - `ON DELETE CASCADE` trigger linked to `kyberPreKeys` table so that we
// cleanup the triples whenever we remove the key
const [query, parameters] = sql`
INSERT OR FAIL INTO kyberPreKey_triples
(id, signedPreKeyId, baseKey)
VALUES
(${id}, ${signedPreKeyId}, ${baseKey});
`;
try {
db.prepare(query).run(parameters);
return 'seen';
} catch (error) {
if (error.code === 'SQLITE_CONSTRAINT_UNIQUE') {
return 'fail';
}
// Unexpected error
throw error;
}
}
function removeAllKyberPreKeys(db: WritableDB): number {
return removeAllFromTable(db, KYBER_PRE_KEYS_TABLE);
}