|
|
@ -44,7 +44,7 @@ export type OpenGroupV2InfoJoinable = OpenGroupV2Info & {
|
|
|
|
export const parseMessages = async (
|
|
|
|
export const parseMessages = async (
|
|
|
|
rawMessages: Array<Record<string, any>>
|
|
|
|
rawMessages: Array<Record<string, any>>
|
|
|
|
): Promise<Array<OpenGroupMessageV2>> => {
|
|
|
|
): Promise<Array<OpenGroupMessageV2>> => {
|
|
|
|
if (!rawMessages) {
|
|
|
|
if (!rawMessages || rawMessages.length === 0) {
|
|
|
|
window?.log?.info('no new messages');
|
|
|
|
window?.log?.info('no new messages');
|
|
|
|
return [];
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -64,50 +64,19 @@ export const parseMessages = async (
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Validate the message signature
|
|
|
|
// Validate the message signature
|
|
|
|
console.time(`worker1-${opengroupv2Message?.serverId}`);
|
|
|
|
|
|
|
|
const senderPubKey = PubKey.cast(opengroupv2Message.sender).withoutPrefix();
|
|
|
|
const senderPubKey = PubKey.cast(opengroupv2Message.sender).withoutPrefix();
|
|
|
|
const signature = (await window.callWorker(
|
|
|
|
|
|
|
|
'fromBase64ToArrayBuffer',
|
|
|
|
|
|
|
|
opengroupv2Message.base64EncodedSignature
|
|
|
|
|
|
|
|
)) as ArrayBuffer;
|
|
|
|
|
|
|
|
console.timeEnd(`worker1-${opengroupv2Message?.serverId}`);
|
|
|
|
|
|
|
|
console.time(`worker2-${opengroupv2Message?.serverId}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const messageData = (await window.callWorker(
|
|
|
|
|
|
|
|
'fromBase64ToArrayBuffer',
|
|
|
|
|
|
|
|
opengroupv2Message.base64EncodedData
|
|
|
|
|
|
|
|
)) as ArrayBuffer;
|
|
|
|
|
|
|
|
console.timeEnd(`worker2-${opengroupv2Message?.serverId}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// throws if signature failed
|
|
|
|
|
|
|
|
console.time(`verifySignature-${opengroupv2Message?.serverId}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const senderEd = (await getSodium()).crypto_sign_ed25519_sk_to_curve25519(
|
|
|
|
const signatureValid = (await window.callWorker(
|
|
|
|
// fromHexToArray(senderPubKey),
|
|
|
|
'verifySignature',
|
|
|
|
// 'uint8array'
|
|
|
|
fromHexToArray(senderPubKey),
|
|
|
|
// );
|
|
|
|
opengroupv2Message.base64EncodedData,
|
|
|
|
|
|
|
|
opengroupv2Message.base64EncodedSignature
|
|
|
|
const valid = (await getSodium()).crypto_sign_verify_detached(
|
|
|
|
)) as boolean;
|
|
|
|
new Uint8Array(signature),
|
|
|
|
if (!signatureValid) {
|
|
|
|
new Uint8Array(messageData),
|
|
|
|
|
|
|
|
fromHexToArray(senderPubKey)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const signatureValid = (await window.callWorker(
|
|
|
|
|
|
|
|
// 'verifySignature',
|
|
|
|
|
|
|
|
// fromHexToArray(senderPubKey),
|
|
|
|
|
|
|
|
// new Uint8Array(messageData),
|
|
|
|
|
|
|
|
// new Uint8Array(signature)
|
|
|
|
|
|
|
|
// )) as boolean;
|
|
|
|
|
|
|
|
if (!valid) {
|
|
|
|
|
|
|
|
console.timeEnd(`verifySignature-${opengroupv2Message?.serverId}`);
|
|
|
|
|
|
|
|
throw new Error('opengroup message signature invalisd');
|
|
|
|
throw new Error('opengroup message signature invalisd');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.timeEnd(`verifySignature-${opengroupv2Message?.serverId}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parsedMessages.push(opengroupv2Message);
|
|
|
|
parsedMessages.push(opengroupv2Message);
|
|
|
|
// as we are not running in a worker, just give some time for UI events
|
|
|
|
|
|
|
|
await sleepFor(5);
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
window?.log?.error('An error happened while fetching getMessages output:', e);
|
|
|
|
window?.log?.error('An error happened while fetching getMessages output:', e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|