Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keysend Fix #1689

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions utils/Base64Utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,37 @@ describe('Base64Utils', () => {
'4465636f64696e672074686973204261736536343f2051756974652073706f6f6b792e'
);
expect(Base64Utils.base64ToHex('WkVVUw==')).toEqual('5a455553');
expect(
Base64Utils.base64ToHex(
'AoJXmNvi6wDqzNnB9rrptOrK1T+20beEtJ/HCtFt4gWE'
)
).toEqual(
'02825798dbe2eb00eaccd9c1f6bae9b4eacad53fb6d1b784b49fc70ad16de20584'
);
});
});

describe('hexToBase64', () => {
it('Converts hexadecimal string to base64 string', () => {
expect(
Base64Utils.hexToBase64(
'54686973206973207468652066697273742074657374'
)
).toEqual('VGhpcyBpcyB0aGUgZmlyc3QgdGVzdA==');
expect(Base64Utils.hexToBase64('456e642074686520466564')).toEqual(
'RW5kIHRoZSBGZWQ='
);
expect(
Base64Utils.hexToBase64(
'4465636f64696e672074686973204261736536343f2051756974652073706f6f6b792e'
)
).toEqual('RGVjb2RpbmcgdGhpcyBCYXNlNjQ/IFF1aXRlIHNwb29reS4=');
expect(Base64Utils.hexToBase64('5a455553')).toEqual('WkVVUw==');
expect(
Base64Utils.hexToBase64(
'02825798dbe2eb00eaccd9c1f6bae9b4eacad53fb6d1b784b49fc70ad16de20584'
)
).toEqual('AoJXmNvi6wDqzNnB9rrptOrK1T+20beEtJ/HCtFt4gWE');
});
});
});
62 changes: 4 additions & 58 deletions utils/Base64Utils.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,11 @@
const chars =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';

class Base64Utils {
encodeStringToBase64 = (input = '') =>
Buffer.from(input).toString('base64');

decodeBase64ToString = (input = '') =>
Buffer.from(input, 'base64').toString('utf8');
btoa = (input = '') => {
const str = input;
let output = '';

for (
let block = 0, charCode, i = 0, map = chars;
str.charAt(i | 0) || ((map = '='), i % 1);
output += map.charAt(63 & (block >> (8 - (i % 1) * 8)))
) {
charCode = str.charCodeAt((i += 3 / 4));

if (charCode > 0xff) {
throw new Error(
"'btoa' failed: The string to be encoded contains characters outside of the Latin1 range."
);
}

block = (block << 8) | charCode;
}

return output;
};

hexStringToByte = (str = '') => {
if (!str) {
return new Uint8Array();
}

const a = [];
for (let i = 0, len = str.length; i < len; i += 2) {
a.push(parseInt(str.substring(i, i + 2), 16));
}

return new Uint8Array(a);
};

byteToBase64 = (buffer: Uint8Array) => {
let binary = '';
const bytes = new Uint8Array(buffer);
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return this.btoa(binary);
};

hexToBase64 = (str = '') => this.byteToBase64(this.hexStringToByte(str));
hexToBase64 = (str = '') => Buffer.from(str, 'hex').toString('base64');

stringToUint8Array = (str: string) =>
Uint8Array.from(str, (x) => x.charCodeAt(0));
Expand All @@ -71,15 +24,8 @@ class Base64Utils {
utf8ToHexString = (hexString: string) =>
Buffer.from(hexString, 'utf8').toString('hex');

base64ToHex = (base64String: string) => {
const raw = this.decodeBase64ToString(base64String);
let result = '';
for (let i = 0; i < raw.length; i++) {
const hex = raw.charCodeAt(i).toString(16);
result += hex.length === 2 ? hex : '0' + hex;
}
return result;
};
base64ToHex = (base64String: string) =>
Buffer.from(base64String, 'base64').toString('hex');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function is a duplicate of decodeBase64ToString

Copy link
Contributor Author

@myxmaster myxmaster Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the toString parameters differ. Here we convert to hex, in decodeBase64ToString to utf8.


// from https://coolaj86.com/articles/unicode-string-to-a-utf-8-typed-array-buffer-in-javascript/
unicodeStringToUint8Array = (s: string) => {
Expand Down