refactor: Offload AES encryption/decryption to Web Worker #3377
+861
−168
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Offload CPU-intensive AES-256-CTR encryption/decryption to a Web Worker using Comlink, improving main thread responsiveness during stream message processing. This PR also fixes several
BuffervsUint8Arrayserialization issues that caused test failures on Linux CI due to inconsistent binary type handling across the worker boundary.Changes
@streamr/sdkEncryptionServicesingleton with lazy worker initializationEncryptionWorkerwith handlers for encrypt, decrypt, and group key operationsaesUtils.tswithconcatBytes()helper to avoidBuffer.concat()createEncryptionWorker.tsfor browser/Node.js/Jest/Karma environmentsMessageFactoryanddecrypt.tsto useEncryptionServiceEncryptionUtil.tsto delegate to sharedaesUtils.tsStreamMessageTranslator.tsto convert protobuf binary fields (signature,content,newGroupKey.data) to plainUint8Arrayafter deserializationcreateMockEncryptionService()test utility for synchronous test executionEncryptionService.decryptWithAESmethod@streamr/utilsEcdsaSecp256k1Evm.createSignature()to return plainUint8Arrayinstead ofBufferhexToBinary()to return plainUint8Arraywith upfront hex validationWatch Out
Uint8Arrayas declared. Code that relied onBuffer-specific methods (like.toString('hex')) may need updates to usebinaryToHex()or similar utilities.Note
Medium Risk
Touches core message publish/decrypt paths and introduces worker-based crypto plus binary type conversions; failures could break encryption/decryption or cross-env serialization, though changes are mostly additive with good test coverage.
Overview
Moves AES-256-CTR stream message encryption/decryption (including group key wrapping/unwrapping) off the main thread by introducing
EncryptionService+ a Comlink-exposedEncryptionWorker, and rewiring publish (MessageFactory) and subscribe (decrypt,messagePipeline) flows to use it.Extracts shared AES primitives into
aesUtils.tsand introducesencryptionUtils.tsrequest/response shapes for worker communication, while removing AES/group-key helpers fromEncryptionUtil/GroupKeyand narrowingEncryptionUtilto asymmetric key-exchange + ML-KEM wrapping.Hardens cross-environment binary handling by normalizing protobuf
content/signature/newGroupKey.datato plainUint8Array, returningUint8ArrayfromcreateSignature, and reimplementinghexToBinaryto validate input and avoidBuffer.Updates build/test plumbing to bundle/alias the new worker across Node/browser/Jest/Karma, adds a synchronous
createMockEncryptionService()for tests, and adjusts a key-request timeout to account for worker startup latency.Written by Cursor Bugbot for commit 3208dca. This will update automatically on new commits. Configure here.