refactor: Offload AES encryption/decryption to Web Worker#3377
Open
mondoreale wants to merge 9 commits intoworker-based-signingfrom
Open
refactor: Offload AES encryption/decryption to Web Worker#3377mondoreale wants to merge 9 commits intoworker-based-signingfrom
mondoreale wants to merge 9 commits intoworker-based-signingfrom
Conversation
49a0bc6 to
be2c78a
Compare
…r boundary Fix comlink serialization by returning Uint8Array from aesUtils aesUtils now returns plain Uint8Array instead of Buffer, fixing 'Unserializable return value' errors on Linux CI where Buffer objects don't serialize correctly through comlink's structured clone.
be2c78a to
cf7ac0c
Compare
Matches the declared return type and ensures proper serialization across environments (e.g., through comlink workers).
Ensures signature, content, and newGroupKey.data are plain Uint8Array after fromBinary(), fixing Buffer serialization issues in tests.
Validates hex characters upfront with regex before parsing.
Now that both `createSignature` and `hexToBinary` return plain `Uint8Array`, we can use strict equality comparison again.
juslesan
approved these changes
Feb 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.