Store contact PGP publicKey in browser storage

pgp
Alex Gleason 2022-06-14 18:43:01 -05:00
rodzic ba7a5a80b4
commit 195edae41f
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
2 zmienionych plików z 21 dodań i 1 usunięć

Wyświetl plik

@ -11,6 +11,7 @@ import {
} from 'soapbox/actions/chats';
import { STREAMING_CHAT_UPDATE } from 'soapbox/actions/streaming';
import { normalizeChatMessage } from 'soapbox/normalizers';
import { isPgpPublicKeyMessage, savePgpKey } from 'soapbox/utils/pgp';
import type { AnyAction } from 'redux';
@ -21,7 +22,14 @@ type APIEntities = Array<APIEntity>;
type State = ImmutableMap<string, ChatMessageRecord>;
const importMessage = (state: State, message: APIEntity) => {
return state.set(message.id, normalizeChatMessage(message));
const normalMessage = normalizeChatMessage(message);
if (isPgpPublicKeyMessage(normalMessage.content)) {
// FIXME: use account fqn instead of account_id
savePgpKey(normalMessage.account_id, normalMessage.content);
}
return state.set(normalMessage.id, normalMessage);
};
const importMessages = (state: State, messages: APIEntities) =>

Wyświetl plik

@ -10,6 +10,11 @@ const isPgpMessage = (message: string): boolean => {
return /^-----BEGIN PGP [A-Z ]+-----/.test(message);
};
/** Check whether a message contains a PGP public key. */
const isPgpPublicKeyMessage = (message: string): boolean => {
return /^-----BEGIN PGP PUBLIC KEY BLOCK-----/.test(message);
};
/** Generate a key and store it in the browser, if one doesn't already exist. */
const initPgpKey = async(fqn: string) => {
const item = await KVStore.getItem(`pgp:${fqn}`);
@ -22,7 +27,14 @@ const initPgpKey = async(fqn: string) => {
}
};
/** Store the public key of another user. */
const savePgpKey = async(fqn: string, publicKey: string) => {
return await KVStore.setItem(`pgp:${fqn}`, { publicKey });
};
export {
isPgpMessage,
isPgpPublicKeyMessage,
initPgpKey,
savePgpKey,
};