diff --git a/js/util_worker_tasks.js b/js/util_worker_tasks.js index 5aae7348e..2d9b98e32 100644 --- a/js/util_worker_tasks.js +++ b/js/util_worker_tasks.js @@ -3,6 +3,7 @@ const functions = { arrayBufferToStringBase64, + fromBase64ToArrayBuffer, }; onmessage = async e => { @@ -36,3 +37,7 @@ function prepareErrorForPostMessage(error) { function arrayBufferToStringBase64(arrayBuffer) { return dcodeIO.ByteBuffer.wrap(arrayBuffer).toString('base64'); } + +function fromBase64ToArrayBuffer(value) { + return dcodeIO.ByteBuffer.wrap(value, 'base64').toArrayBuffer(); +} diff --git a/preload.js b/preload.js index 5023abca0..e6875aa8b 100644 --- a/preload.js +++ b/preload.js @@ -296,7 +296,6 @@ const utilWorkerPath = path.join(app.getAppPath(), 'js', 'util_worker.js'); const utilWorker = new WorkerInterface(utilWorkerPath, 3 * 60 * 1000); window.callWorker = (fnName, ...args) => utilWorker.callWorker(fnName, ...args); - // Linux seems to periodically let the event loop stop, so this is a global workaround setInterval(() => { window.nodeSetImmediate(() => {}); diff --git a/ts/fileserver/FileServerApiV2.ts b/ts/fileserver/FileServerApiV2.ts index 326f72a1e..d8c921cc7 100644 --- a/ts/fileserver/FileServerApiV2.ts +++ b/ts/fileserver/FileServerApiV2.ts @@ -1,7 +1,6 @@ import { OpenGroupV2Request } from '../opengroup/opengroupV2/ApiUtil'; import { sendApiV2Request } from '../opengroup/opengroupV2/OpenGroupAPIV2'; import { parseStatusCodeFromOnionRequest } from '../opengroup/opengroupV2/OpenGroupAPIV2Parser'; -import { fromArrayBufferToBase64, fromBase64ToArrayBuffer } from '../session/utils/String'; // tslint:disable-next-line: no-http-string export const oldFileServerV2URL = 'http://88.99.175.227'; @@ -41,7 +40,7 @@ export const uploadFileToFsV2 = async ( return null; } const queryParams = { - file: fromArrayBufferToBase64(fileContent), + file: await window.callWorker('arrayBufferToStringBase64', fileContent), }; const request: FileServerV2Request = { @@ -110,7 +109,7 @@ export const downloadFileFromFSv2 = async ( if (!base64Data) { return null; } - return fromBase64ToArrayBuffer(base64Data); + return window.callWorker('fromBase64ToArrayBuffer', base64Data); }; /** diff --git a/ts/opengroup/opengroupV2/ApiAuth.ts b/ts/opengroup/opengroupV2/ApiAuth.ts index 77a21c774..cf1f94f40 100644 --- a/ts/opengroup/opengroupV2/ApiAuth.ts +++ b/ts/opengroup/opengroupV2/ApiAuth.ts @@ -153,8 +153,11 @@ export async function requestNewAuthToken({ window?.log?.warn('Parsing failed'); return null; } - const ciphertext = fromBase64ToArrayBuffer(base64EncodedCiphertext); - const ephemeralPublicKey = fromBase64ToArrayBuffer(base64EncodedEphemeralPublicKey); + const ciphertext = await window.callWorker('fromBase64ToArrayBuffer', base64EncodedCiphertext); + const ephemeralPublicKey = await window.callWorker( + 'fromBase64ToArrayBuffer', + base64EncodedEphemeralPublicKey + ); try { const symmetricKey = await window.libloki.crypto.deriveSymmetricKey( ephemeralPublicKey, diff --git a/ts/opengroup/opengroupV2/ApiUtil.ts b/ts/opengroup/opengroupV2/ApiUtil.ts index 4672447fe..c1613deae 100644 --- a/ts/opengroup/opengroupV2/ApiUtil.ts +++ b/ts/opengroup/opengroupV2/ApiUtil.ts @@ -47,7 +47,7 @@ export const parseMessages = async ( window?.log?.info('no new messages'); return []; } - const chunks = _.chunk(rawMessages, 10); + const chunks = _.chunk(rawMessages, 1000); const handleChunk = async (chunk: Array>) => { return Promise.all( @@ -65,8 +65,14 @@ export const parseMessages = async ( } // Validate the message signature const senderPubKey = PubKey.cast(opengroupv2Message.sender).withoutPrefix(); - const signature = fromBase64ToArrayBuffer(opengroupv2Message.base64EncodedSignature); - const messageData = fromBase64ToArrayBuffer(opengroupv2Message.base64EncodedData); + const signature = await window.callWorker( + 'fromBase64ToArrayBuffer', + opengroupv2Message.base64EncodedSignature + ); + const messageData = await window.callWorker( + 'fromBase64ToArrayBuffer', + opengroupv2Message.base64EncodedData + ); // throws if signature failed await window.libsignal.Curve.async.verifySignature( fromHex(senderPubKey), diff --git a/ts/opengroup/opengroupV2/OpenGroupAPIV2.ts b/ts/opengroup/opengroupV2/OpenGroupAPIV2.ts index 944e8a009..4760808da 100644 --- a/ts/opengroup/opengroupV2/OpenGroupAPIV2.ts +++ b/ts/opengroup/opengroupV2/OpenGroupAPIV2.ts @@ -7,7 +7,6 @@ import { import { FSv2 } from '../../fileserver/'; import { sendViaOnion } from '../../session/onions/onionSend'; import { PubKey } from '../../session/types'; -import { fromArrayBufferToBase64, fromBase64ToArrayBuffer } from '../../session/utils/String'; import { OpenGroupRequestCommonType, OpenGroupV2Info, OpenGroupV2Request } from './ApiUtil'; import { parseMemberCount, @@ -396,7 +395,7 @@ export const downloadFileOpenGroupV2 = async ( if (!base64Data) { return null; } - return new Uint8Array(fromBase64ToArrayBuffer(base64Data)); + return new Uint8Array(await window.callWorker('fromBase64ToArrayBuffer', base64Data)); }; export const downloadFileOpenGroupV2ByUrl = async ( @@ -423,7 +422,7 @@ export const downloadFileOpenGroupV2ByUrl = async ( if (!base64Data) { return null; } - return new Uint8Array(fromBase64ToArrayBuffer(base64Data)); + return new Uint8Array(await window.callWorker('fromBase64ToArrayBuffer', base64Data)); }; /** @@ -469,7 +468,7 @@ export const uploadFileOpenGroupV2 = async ( return null; } const queryParams = { - file: fromArrayBufferToBase64(fileContent), + file: await window.callWorker('arrayBufferToStringBase64', fileContent), }; const filesEndpoint = 'files'; @@ -509,7 +508,7 @@ export const uploadImageForRoomOpenGroupV2 = async ( } const queryParams = { - file: fromArrayBufferToBase64(fileContent), + file: await window.callWorker('arrayBufferToStringBase64', fileContent), }; const imageEndpoint = `rooms/${roomInfos.roomId}/image`; diff --git a/ts/opengroup/opengroupV2/OpenGroupMessageV2.ts b/ts/opengroup/opengroupV2/OpenGroupMessageV2.ts index fd4481f90..96bb53bc7 100644 --- a/ts/opengroup/opengroupV2/OpenGroupMessageV2.ts +++ b/ts/opengroup/opengroupV2/OpenGroupMessageV2.ts @@ -1,12 +1,5 @@ -import { getSodium } from '../../session/crypto'; import { UserUtils } from '../../session/utils'; -import { - fromArrayBufferToBase64, - fromBase64ToArray, - fromHex, - fromHexToArray, - toHex, -} from '../../session/utils/String'; +import { fromBase64ToArray } from '../../session/utils/String'; export class OpenGroupMessageV2 { public serverId?: number; @@ -77,7 +70,7 @@ export class OpenGroupMessageV2 { return new OpenGroupMessageV2({ base64EncodedData: this.base64EncodedData, sentTimestamp: this.sentTimestamp, - base64EncodedSignature: fromArrayBufferToBase64(signature), + base64EncodedSignature: await window.callWorker('arrayBufferToStringBase64', signature), sender: this.sender, serverId: this.serverId, }); diff --git a/ts/opengroup/opengroupV2/OpenGroupServerPoller.ts b/ts/opengroup/opengroupV2/OpenGroupServerPoller.ts index 8dc67d5d3..44357bf75 100644 --- a/ts/opengroup/opengroupV2/OpenGroupServerPoller.ts +++ b/ts/opengroup/opengroupV2/OpenGroupServerPoller.ts @@ -480,7 +480,7 @@ const handleBase64AvatarUpdate = async ( const upgradedAttachment = await processNewAttachment({ isRaw: true, - data: fromBase64ToArrayBuffer(res.base64), + data: await window.callWorker('fromBase64ToArrayBuffer', res.base64), url: `${serverUrl}/${res.roomId}`, }); // update the hash on the conversationModel diff --git a/ts/opengroup/opengroupV2/OpenGroupUpdate.ts b/ts/opengroup/opengroupV2/OpenGroupUpdate.ts index 2596a6d8f..3d47cb7c4 100644 --- a/ts/opengroup/opengroupV2/OpenGroupUpdate.ts +++ b/ts/opengroup/opengroupV2/OpenGroupUpdate.ts @@ -70,8 +70,6 @@ export async function updateOpenGroupV2(convo: ConversationModel, groupName: str isRaw: true, url: pathname, }); - // FIXME audric update of roomname on the server? - window?.log?.warn('TODO update of roomName'); const newHash = sha256(fromArrayBufferToBase64(downloaded.buffer)); await convo.setLokiProfile({ displayName: groupName || convo.get('name') || 'Unknown', diff --git a/ts/pushnotification/PnServer.ts b/ts/pushnotification/PnServer.ts index 3652e8168..7df502da9 100644 --- a/ts/pushnotification/PnServer.ts +++ b/ts/pushnotification/PnServer.ts @@ -1,5 +1,4 @@ import { serverRequest } from '../session/onions/onionSend'; -import { fromArrayBufferToBase64 } from '../session/utils/String'; const pnServerPubkeyHex = '642a6585919742e5a2d4dc51244964fbcd8bcab2b75612407de58b810740d049'; @@ -11,7 +10,7 @@ export async function notify(plainTextBuffer: ArrayBuffer, sentTo: string) { const options = { method: 'post', objBody: { - data: fromArrayBufferToBase64(plainTextBuffer), + data: await window.callWorker('arrayBufferToStringBase64', plainTextBuffer), send_to: sentTo, }, }; diff --git a/ts/receiver/attachments.ts b/ts/receiver/attachments.ts index 7be8d68ab..b1a7950b6 100644 --- a/ts/receiver/attachments.ts +++ b/ts/receiver/attachments.ts @@ -2,7 +2,6 @@ import _ from 'lodash'; import { MessageModel } from '../models/message'; import { saveMessage } from '../../ts/data/data'; -import { fromBase64ToArrayBuffer } from '../session/utils/String'; import { AttachmentDownloads } from '../session/utils'; import { ConversationModel } from '../models/conversation'; import { @@ -54,8 +53,8 @@ export async function downloadAttachment(attachment: any) { data = await window.textsecure.crypto.decryptAttachment( data, - fromBase64ToArrayBuffer(key), - fromBase64ToArrayBuffer(digest) + await window.callWorker('fromBase64ToArrayBuffer', key), + await window.callWorker('fromBase64ToArrayBuffer', digest) ); if (!size || size !== data.byteLength) { diff --git a/ts/session/snode_api/onions.ts b/ts/session/snode_api/onions.ts index a4e81ed25..4bc691e2f 100644 --- a/ts/session/snode_api/onions.ts +++ b/ts/session/snode_api/onions.ts @@ -4,7 +4,7 @@ import https from 'https'; import { dropSnodeFromSnodePool, dropSnodeFromSwarmIfNeeded, updateSwarmFor } from './snodePool'; import ByteBuffer from 'bytebuffer'; import { OnionPaths } from '../onions'; -import { fromBase64ToArrayBuffer, toHex } from '../utils/String'; +import { toHex } from '../utils/String'; import pRetry from 'p-retry'; import { incrementBadPathCountOrDrop } from '../onions/onionPath'; import _ from 'lodash'; @@ -395,7 +395,7 @@ export async function decodeOnionResult(symmetricKey: ArrayBuffer, ciphertext: s } catch (e) { // just try to get a json object from what is inside (for PN requests), if it fails, continue () } - const ciphertextBuffer = fromBase64ToArrayBuffer(parsedCiphertext); + const ciphertextBuffer = await window.callWorker('fromBase64ToArrayBuffer', parsedCiphertext); const plaintextBuffer = await window.libloki.crypto.DecryptAESGCM(symmetricKey, ciphertextBuffer); diff --git a/ts/session/utils/syncUtils.ts b/ts/session/utils/syncUtils.ts index 33bff516a..a40be7046 100644 --- a/ts/session/utils/syncUtils.ts +++ b/ts/session/utils/syncUtils.ts @@ -14,14 +14,11 @@ import { ConfigurationMessageContact, } from '../messages/outgoing/controlMessage/ConfigurationMessage'; import { ConversationModel } from '../../models/conversation'; -import { fromBase64ToArray, fromBase64ToArrayBuffer, fromHexToArray } from './String'; -import { fromBase64 } from 'bytebuffer'; +import { fromBase64ToArray } from './String'; import { SignalService } from '../../protobuf'; import _ from 'lodash'; import { - AttachmentPointer, AttachmentPointerWithUrl, - Preview, PreviewWithAttachmentUrl, Quote, VisibleMessage, diff --git a/ts/window.d.ts b/ts/window.d.ts index d8a86a540..50297c1ed 100644 --- a/ts/window.d.ts +++ b/ts/window.d.ts @@ -84,5 +84,9 @@ declare global { LokiPushNotificationServer: any; globalOnlineStatus: boolean; confirmationDialog: any; + callWorker: ( + fnName: 'arrayBufferToStringBase64' | 'fromBase64ToArrayBuffer', + ...args + ) => Promise; } }