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

Add base64 encoding and decoding functions #145

Merged
merged 1 commit into from
Oct 16, 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"dependencies": {
"@ethereumjs/tx": "^4.1.2",
"@noble/hashes": "^1.3.1",
"@scure/base": "^1.1.3",
"@types/debug": "^4.1.7",
"debug": "^4.3.4",
"semver": "^7.5.4",
Expand Down
15 changes: 15 additions & 0 deletions src/__fixtures__/bytes.ts

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions src/bytes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
} from './__fixtures__';
import {
assertIsBytes,
base64ToBytes,
bigIntToBytes,
bytesToBase64,
bytesToBigInt,
bytesToHex,
bytesToNumber,
Expand Down Expand Up @@ -170,6 +172,30 @@ describe('bytesToString', () => {
);
});

describe('bytesToBase64', () => {
it.each(BYTES_FIXTURES)(
'returns a base64 string from a byte array',
({ bytes, base64 }) => {
expect(bytesToBase64(bytes)).toBe(base64);
},
);

it.each(LARGE_BYTES_FIXTURES)(
'returns a base64 string from a large byte array',
({ bytes, base64 }) => {
expect(bytesToBase64(bytes)).toBe(base64);
},
);

it.each(INVALID_BYTES_FIXTURES)(
'throws an error for invalid byte arrays',
(value) => {
// @ts-expect-error Invalid type.
expect(() => bytesToBase64(value)).toThrow('Value must be a Uint8Array.');
},
);
});

describe('hexToBytes', () => {
it.each(BYTES_FIXTURES)(
'returns a byte array from a hex string',
Expand Down Expand Up @@ -364,6 +390,22 @@ describe('stringToBytes', () => {
);
});

describe('base64ToBytes', () => {
it.each(BYTES_FIXTURES)(
'returns a byte array from a base64 string',
({ bytes, base64 }) => {
expect(base64ToBytes(base64)).toStrictEqual(bytes);
},
);

it.each(LARGE_BYTES_FIXTURES)(
'returns a byte array from a large base64 string',
({ bytes, base64 }) => {
expect(base64ToBytes(base64)).toStrictEqual(bytes);
},
);
});

describe('valueToBytes', () => {
it.each(BYTES_FIXTURES)(
'returns a byte array from a value',
Expand Down
26 changes: 26 additions & 0 deletions src/bytes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { base64 } from '@scure/base';

import { assert } from './assert';
import type { Hex } from './hex';
import { add0x, assertIsHexString, remove0x } from './hex';
Expand Down Expand Up @@ -167,6 +169,18 @@ export function bytesToString(bytes: Uint8Array): string {
return new TextDecoder().decode(bytes);
}

/**
* Convert a `Uint8Array` to a base64 encoded string.
*
* @param bytes - The bytes to convert to a base64 encoded string.
* @returns The base64 encoded string.
*/
export function bytesToBase64(bytes: Uint8Array): string {
assertIsBytes(bytes);

return base64.encode(bytes);
}

/**
* Convert a hexadecimal string to a `Uint8Array`. The string can optionally be
* prefixed with `0x`. It accepts even and odd length strings.
Expand Down Expand Up @@ -318,6 +332,18 @@ export function stringToBytes(value: string): Uint8Array {
return new TextEncoder().encode(value);
}

/**
* Convert a base64 encoded string to a `Uint8Array`.
*
* @param value - The base64 encoded string to convert to bytes.
* @returns The bytes as `Uint8Array`.
*/
export function base64ToBytes(value: string): Uint8Array {
assert(typeof value === 'string', 'Value must be a string.');

return base64.decode(value);
}

/**
* Convert a byte-like value to a `Uint8Array`. The value can be a `Uint8Array`,
* a `bigint`, a `number`, or a `string`.
Expand Down
9 changes: 5 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,7 @@ __metadata:
"@metamask/eslint-config-nodejs": ^12.0.0
"@metamask/eslint-config-typescript": ^12.0.0
"@noble/hashes": ^1.3.1
"@scure/base": ^1.1.3
"@swc/cli": ^0.1.62
"@swc/core": ^1.3.66
"@types/debug": ^4.1.7
Expand Down Expand Up @@ -1273,10 +1274,10 @@ __metadata:
languageName: node
linkType: hard

"@scure/base@npm:~1.1.0":
version: 1.1.1
resolution: "@scure/base@npm:1.1.1"
checksum: b4fc810b492693e7e8d0107313ac74c3646970c198bbe26d7332820886fa4f09441991023ec9aa3a2a51246b74409ab5ebae2e8ef148bbc253da79ac49130309
"@scure/base@npm:^1.1.3, @scure/base@npm:~1.1.0":
version: 1.1.3
resolution: "@scure/base@npm:1.1.3"
checksum: 1606ab8a4db898cb3a1ada16c15437c3bce4e25854fadc8eb03ae93cbbbac1ed90655af4b0be3da37e12056fef11c0374499f69b9e658c9e5b7b3e06353c630c
languageName: node
linkType: hard

Expand Down