Fix Incorrect Usage of bn.js API in browser.js and Ensure Backward Compatibility for Decryption #96
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.
This PR addresses an incorrect usage of the bn.js library within browser.js on the master branch, specifically in how BN objects are converted to buffers. The existing code uses
Px.toArray()
method, which can lead to variable length byte arrays, potentially causing inconsistent behavior:To ensure consistent byte array lengths, we should leverage
Buffer.from(Px.toArray(undefined, 32))
method, specifying a fixed output length. The proposed change is as follows:This modification guarantees that the conversion yields a buffer of a fixed size, mitigating issues encountered with variable length arrays.
Additionally, this PR introduces a backward compatibility layer for decrypting messages encrypted with previously derived incorrect keys. The solution attempts decryption using the corrected key length; should this fail, it then falls back to the previously incorrect key length by trimming leading zeros. This dual-path approach ensures that both newly encrypted messages and those encrypted under the prior flawed logic remain accessible.
Related Issues: This fix is in response to issues encountered by users, documented in #90, #81, #63, and #52 providing a comprehensive solution to the encryption key length inconsistencies observed.