add a sql function to check if a keypair is already saved in db

pull/1498/head
Audric Ackermann 4 years ago
parent a31c457c08
commit df3ca5d38a
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -171,6 +171,7 @@ module.exports = {
getAllEncryptionKeyPairsForGroup,
getLatestClosedGroupEncryptionKeyPair,
addClosedGroupEncryptionKeyPair,
isKeyPairAlreadySaved,
removeAllClosedGroupEncryptionKeyPairs,
};
@ -3267,6 +3268,18 @@ async function addClosedGroupEncryptionKeyPair(
);
}
async function isKeyPairAlreadySaved(
groupPublicKey,
newKeyPairInHex // : HexKeyPair
) {
const allKeyPairs = await getAllEncryptionKeyPairsForGroup(groupPublicKey);
return (allKeyPairs || []).some(
k =>
newKeyPairInHex.publicHex === k.publicHex &&
newKeyPairInHex.privateHex === k.privateHex
);
}
async function removeAllClosedGroupEncryptionKeyPairs(groupPublicKey) {
await db.run(
`DELETE FROM ${CLOSED_GROUP_V2_KEY_PAIRS_TABLE} WHERE groupPublicKey = $groupPublicKey`,

@ -1,5 +1,6 @@
import { KeyPair } from '../../libtextsecure/libsignal-protocol';
import { HexKeyPair } from '../../ts/receiver/closedGroups';
import { ECKeyPair } from '../../ts/receiver/keypairs';
import { PubKey } from '../../ts/session/types';
import { ConversationType } from '../../ts/state/ducks/conversations';
import { Message } from '../../ts/types/Message';
@ -401,6 +402,10 @@ export function removeAllClosedGroupRatchets(groupId: string): Promise<void>;
export function getAllEncryptionKeyPairsForGroup(
groupPublicKey: string | PubKey
): Promise<Array<HexKeyPair> | undefined>;
export function isKeyPairAlreadySaved(
groupPublicKey: string,
keypair: HexKeyPair
): Promise<boolean>;
export function getLatestClosedGroupEncryptionKeyPair(
groupPublicKey: string
): Promise<HexKeyPair | undefined>;

@ -196,6 +196,7 @@ module.exports = {
getAllEncryptionKeyPairsForGroup,
getLatestClosedGroupEncryptionKeyPair,
addClosedGroupEncryptionKeyPair,
isKeyPairAlreadySaved,
removeAllClosedGroupEncryptionKeyPairs,
};
@ -723,6 +724,10 @@ async function addClosedGroupEncryptionKeyPair(groupPublicKey, keypair) {
return channels.addClosedGroupEncryptionKeyPair(groupPublicKey, keypair);
}
async function isKeyPairAlreadySaved(groupPublicKey, keypair) {
return channels.isKeyPairAlreadySaved(groupPublicKey, keypair);
}
async function removeAllClosedGroupEncryptionKeyPairs(groupPublicKey) {
return channels.removeAllClosedGroupEncryptionKeyPairs(groupPublicKey);
}

@ -16,6 +16,7 @@ import {
addClosedGroupEncryptionKeyPair,
getAllEncryptionKeyPairsForGroup,
getLatestClosedGroupEncryptionKeyPair,
isKeyPairAlreadySaved,
removeAllClosedGroupEncryptionKeyPairs,
} from '../../js/modules/data';
import {
@ -449,16 +450,12 @@ async function handleClosedGroupEncryptionKeyPair(
// Store it if needed
const newKeyPairInHex = keyPair.toHexKeyPair();
const keyPairsAlreadySaved = await getAllEncryptionKeyPairsForGroup(
groupPublicKey
);
const isKeyPairAlreadySaved = (keyPairsAlreadySaved || []).some(
k =>
newKeyPairInHex.publicHex === k.publicHex &&
newKeyPairInHex.privateHex === k.privateHex
const isKeyPairAlreadyHere = await isKeyPairAlreadySaved(
groupPublicKey,
newKeyPairInHex
);
if (isKeyPairAlreadySaved) {
if (isKeyPairAlreadyHere) {
window.log.info('Dropping already saved keypair for group', groupPublicKey);
await removeFromCache(envelope);
return;

Loading…
Cancel
Save