Skip to content

Commit 8732f00

Browse files
authored
Add base64 encoding and decoding functions (#145)
1 parent b8cf222 commit 8732f00

File tree

5 files changed

+89
-4
lines changed

5 files changed

+89
-4
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"dependencies": {
5656
"@ethereumjs/tx": "^4.1.2",
5757
"@noble/hashes": "^1.3.1",
58+
"@scure/base": "^1.1.3",
5859
"@types/debug": "^4.1.7",
5960
"debug": "^4.3.4",
6061
"semver": "^7.5.4",

src/__fixtures__/bytes.ts

Lines changed: 15 additions & 0 deletions
Large diffs are not rendered by default.

src/bytes.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import {
88
} from './__fixtures__';
99
import {
1010
assertIsBytes,
11+
base64ToBytes,
1112
bigIntToBytes,
13+
bytesToBase64,
1214
bytesToBigInt,
1315
bytesToHex,
1416
bytesToNumber,
@@ -170,6 +172,30 @@ describe('bytesToString', () => {
170172
);
171173
});
172174

175+
describe('bytesToBase64', () => {
176+
it.each(BYTES_FIXTURES)(
177+
'returns a base64 string from a byte array',
178+
({ bytes, base64 }) => {
179+
expect(bytesToBase64(bytes)).toBe(base64);
180+
},
181+
);
182+
183+
it.each(LARGE_BYTES_FIXTURES)(
184+
'returns a base64 string from a large byte array',
185+
({ bytes, base64 }) => {
186+
expect(bytesToBase64(bytes)).toBe(base64);
187+
},
188+
);
189+
190+
it.each(INVALID_BYTES_FIXTURES)(
191+
'throws an error for invalid byte arrays',
192+
(value) => {
193+
// @ts-expect-error Invalid type.
194+
expect(() => bytesToBase64(value)).toThrow('Value must be a Uint8Array.');
195+
},
196+
);
197+
});
198+
173199
describe('hexToBytes', () => {
174200
it.each(BYTES_FIXTURES)(
175201
'returns a byte array from a hex string',
@@ -364,6 +390,22 @@ describe('stringToBytes', () => {
364390
);
365391
});
366392

393+
describe('base64ToBytes', () => {
394+
it.each(BYTES_FIXTURES)(
395+
'returns a byte array from a base64 string',
396+
({ bytes, base64 }) => {
397+
expect(base64ToBytes(base64)).toStrictEqual(bytes);
398+
},
399+
);
400+
401+
it.each(LARGE_BYTES_FIXTURES)(
402+
'returns a byte array from a large base64 string',
403+
({ bytes, base64 }) => {
404+
expect(base64ToBytes(base64)).toStrictEqual(bytes);
405+
},
406+
);
407+
});
408+
367409
describe('valueToBytes', () => {
368410
it.each(BYTES_FIXTURES)(
369411
'returns a byte array from a value',

src/bytes.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { base64 } from '@scure/base';
2+
13
import { assert } from './assert';
24
import type { Hex } from './hex';
35
import { add0x, assertIsHexString, remove0x } from './hex';
@@ -167,6 +169,18 @@ export function bytesToString(bytes: Uint8Array): string {
167169
return new TextDecoder().decode(bytes);
168170
}
169171

172+
/**
173+
* Convert a `Uint8Array` to a base64 encoded string.
174+
*
175+
* @param bytes - The bytes to convert to a base64 encoded string.
176+
* @returns The base64 encoded string.
177+
*/
178+
export function bytesToBase64(bytes: Uint8Array): string {
179+
assertIsBytes(bytes);
180+
181+
return base64.encode(bytes);
182+
}
183+
170184
/**
171185
* Convert a hexadecimal string to a `Uint8Array`. The string can optionally be
172186
* prefixed with `0x`. It accepts even and odd length strings.
@@ -318,6 +332,18 @@ export function stringToBytes(value: string): Uint8Array {
318332
return new TextEncoder().encode(value);
319333
}
320334

335+
/**
336+
* Convert a base64 encoded string to a `Uint8Array`.
337+
*
338+
* @param value - The base64 encoded string to convert to bytes.
339+
* @returns The bytes as `Uint8Array`.
340+
*/
341+
export function base64ToBytes(value: string): Uint8Array {
342+
assert(typeof value === 'string', 'Value must be a string.');
343+
344+
return base64.decode(value);
345+
}
346+
321347
/**
322348
* Convert a byte-like value to a `Uint8Array`. The value can be a `Uint8Array`,
323349
* a `bigint`, a `number`, or a `string`.

yarn.lock

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,7 @@ __metadata:
11021102
"@metamask/eslint-config-nodejs": ^12.0.0
11031103
"@metamask/eslint-config-typescript": ^12.0.0
11041104
"@noble/hashes": ^1.3.1
1105+
"@scure/base": ^1.1.3
11051106
"@swc/cli": ^0.1.62
11061107
"@swc/core": ^1.3.66
11071108
"@types/debug": ^4.1.7
@@ -1273,10 +1274,10 @@ __metadata:
12731274
languageName: node
12741275
linkType: hard
12751276

1276-
"@scure/base@npm:~1.1.0":
1277-
version: 1.1.1
1278-
resolution: "@scure/base@npm:1.1.1"
1279-
checksum: b4fc810b492693e7e8d0107313ac74c3646970c198bbe26d7332820886fa4f09441991023ec9aa3a2a51246b74409ab5ebae2e8ef148bbc253da79ac49130309
1277+
"@scure/base@npm:^1.1.3, @scure/base@npm:~1.1.0":
1278+
version: 1.1.3
1279+
resolution: "@scure/base@npm:1.1.3"
1280+
checksum: 1606ab8a4db898cb3a1ada16c15437c3bce4e25854fadc8eb03ae93cbbbac1ed90655af4b0be3da37e12056fef11c0374499f69b9e658c9e5b7b3e06353c630c
12801281
languageName: node
12811282
linkType: hard
12821283

0 commit comments

Comments
 (0)