Skip to content

Commit

Permalink
Merge pull request #1063 from multiversx/rt/fix/base64utils
Browse files Browse the repository at this point in the history
Fixed base64 utils encoding check and conversion
  • Loading branch information
razvantomegea authored Mar 7, 2024
2 parents 8357749 + 3a769ea commit 9e4ada9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [Added support for nativeAuth impersonat](https://github.com/multiversx/mx-sdk-dapp/pull/1049)
- [Updated WalletConnectV2 account provider to be updated on new or existing session](https://github.com/multiversx/mx-sdk-dapp/pull/1050)

## [[v2.28.8]](https://github.com/multiversx/mx-sdk-dapp/pull/1062)] - 2024-03-07
- [Fixed base64 utils conversion](https://github.com/multiversx/mx-sdk-dapp/pull/1061)

## [[v2.28.7]](https://github.com/multiversx/mx-sdk-dapp/pull/1048)] - 2024-02-13
- [Updated AddressRow data-testids](https://github.com/multiversx/mx-sdk-dapp/pull/1047)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiversx/sdk-dapp",
"version": "2.28.7-alpha.24",
"version": "2.28.8-alpha.0",
"description": "A library to hold the main logic for a dapp on the MultiversX blockchain",
"author": "MultiversX",
"license": "GPL-3.0-or-later",
Expand Down
14 changes: 6 additions & 8 deletions src/utils/decoders/base64Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
*
* Solution:
* - if any conversion fails (atob(), btoa() or Buffer.from()), it is definitely not an encoded string
* - if atob() conversion is equal to Buffer.from() conversion
* or the string is equal to btoa() conversion of atob(), it is a regular base64 string
* - if the string is equal
*
* @see The tests for this function are in src/utils/decoders/tests/base64Utils.test.ts
* @param str
Expand All @@ -27,13 +26,12 @@ export function isStringBase64(str: string) {
const bufferFromEncoded = Buffer.from(bufferFromDecoded).toString('base64');

// If the result is equal to the initial string
const isEqualToInitialString =
str === btoaEncoded && str === bufferFromEncoded;
const isBtoaEqual = str === btoaEncoded || btoaEncoded.startsWith(str);
const isBufferFromBase64Equal =
str === bufferFromEncoded || bufferFromEncoded.startsWith(str);
const isEqualToInitialString = isBtoaEqual && isBufferFromBase64Equal;

// or the atob() conversion is equal to the Buffer.from('base64')
const isAtobEqualToBufferFrom = atobDecoded === bufferFromDecoded;

if (isEqualToInitialString || isAtobEqualToBufferFrom) {
if (isEqualToInitialString) {
// it is a regular base64 string
return true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/utils/decoders/tests/base64Utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ describe('isStringBase64', () => {
);
expect(result).toStrictEqual(false);
});

it('should return false for atob equal to base64 conversion', async () => {
const result = isStringBase64('fd');
expect(result).toStrictEqual(false);
});
});

0 comments on commit 9e4ada9

Please sign in to comment.