Skip to content

Commit 78e7e54

Browse files
committed
chore: remove buffer impl
1 parent 42279c9 commit 78e7e54

File tree

7 files changed

+49
-209
lines changed

7 files changed

+49
-209
lines changed

Diff for: src/utils/encoding/toBytes.test.ts

+24-55
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { describe, expect, test } from 'vitest'
33
import {
44
boolToBytes,
55
hexToBytes,
6-
hexToBytes_native,
76
numberToBytes,
87
stringToBytes,
98
toBytes,
@@ -831,6 +830,30 @@ describe('converts hex to bytes', () => {
831830
33,
832831
]
833832
`)
833+
expect(
834+
hexToBytes('0x48656c6c620576f726c6421ABCDEFabcdef'),
835+
).toMatchInlineSnapshot(`
836+
Uint8Array [
837+
4,
838+
134,
839+
86,
840+
198,
841+
198,
842+
32,
843+
87,
844+
111,
845+
114,
846+
108,
847+
100,
848+
33,
849+
171,
850+
205,
851+
239,
852+
171,
853+
205,
854+
239,
855+
]
856+
`)
834857
})
835858

836859
test('args: size', () => {
@@ -979,60 +1002,6 @@ describe('converts hex to bytes', () => {
9791002

9801003
test('error: invalid hex', () => {
9811004
expect(() => hexToBytes('0xabcdefgh')).toThrowErrorMatchingInlineSnapshot(`
982-
"0xabcdefgh is not a valid hex value.
983-
984-
985-
`)
986-
})
987-
})
988-
989-
describe('converts hex to bytes (native)', () => {
990-
test('default', () => {
991-
expect(hexToBytes_native('0x')).toMatchInlineSnapshot('Uint8Array []')
992-
expect(hexToBytes_native('0x61')).toMatchInlineSnapshot(`
993-
Uint8Array [
994-
97,
995-
]
996-
`)
997-
expect(hexToBytes_native('0x613')).toMatchInlineSnapshot(`
998-
Uint8Array [
999-
6,
1000-
19,
1001-
]
1002-
`)
1003-
expect(hexToBytes_native('0x616263')).toMatchInlineSnapshot(`
1004-
Uint8Array [
1005-
97,
1006-
98,
1007-
99,
1008-
]
1009-
`)
1010-
expect(
1011-
hexToBytes_native('0x48656c6c6f20576f726c6421'),
1012-
).toMatchInlineSnapshot(
1013-
`
1014-
Uint8Array [
1015-
72,
1016-
101,
1017-
108,
1018-
108,
1019-
111,
1020-
32,
1021-
87,
1022-
111,
1023-
114,
1024-
108,
1025-
100,
1026-
33,
1027-
]
1028-
`,
1029-
)
1030-
})
1031-
1032-
test('error: invalid hex', () => {
1033-
expect(() =>
1034-
hexToBytes_native('0xabcdefgh'),
1035-
).toThrowErrorMatchingInlineSnapshot(`
10361005
"Invalid byte sequence (\\"gh\\" in \\"abcdefgh\\").
10371006
10381007

Diff for: src/utils/encoding/toBytes.ts

+20-39
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import { type NumberToHexOpts, numberToHex } from './toHex.js'
88

99
const encoder = /*#__PURE__*/ new TextEncoder()
1010

11-
const hasBuffer = 'Buffer' in globalThis && typeof Buffer.from === 'function'
12-
1311
export type ToBytesParameters = {
1412
/** Size of the output bytes. */
1513
size?: number
@@ -85,6 +83,26 @@ export function boolToBytes(value: boolean, opts: BoolToHexOpts = {}) {
8583
return bytes
8684
}
8785

86+
// We use very optimized technique to convert hex string to byte array
87+
const charCodeMap = {
88+
zero: 48,
89+
nine: 57,
90+
A: 65,
91+
F: 70,
92+
a: 97,
93+
f: 102,
94+
} as const
95+
96+
function charCodeToBase16(char: number) {
97+
if (char >= charCodeMap.zero && char <= charCodeMap.nine)
98+
return char - charCodeMap.zero
99+
else if (char >= charCodeMap.A && char <= charCodeMap.F)
100+
return char - (charCodeMap.A - 10)
101+
else if (char >= charCodeMap.a && char <= charCodeMap.f)
102+
return char - (charCodeMap.a - 10)
103+
return undefined
104+
}
105+
88106
export type HexToBytesOpts = {
89107
/** Size of the output bytes. */
90108
size?: number
@@ -116,43 +134,6 @@ export function hexToBytes(hex_: Hex, opts: HexToBytesOpts = {}): ByteArray {
116134
hex = pad(hex, { dir: 'right', size: opts.size })
117135
}
118136

119-
return hasBuffer ? hexToBytes_buffer(hex) : hexToBytes_native(hex)
120-
}
121-
122-
/** @internal */
123-
export function hexToBytes_buffer(hex: Hex): ByteArray {
124-
if (!hasBuffer) throw new Error('`Buffer` not implemented.')
125-
126-
let hexString = hex.slice(2) as string
127-
if (hexString.length % 2) hexString = `0${hexString}`
128-
129-
if (hexString === '') return Uint8Array.from([])
130-
if (!isHex(hex)) throw new BaseError(`${hex} is not a valid hex value.`)
131-
return Uint8Array.from(Buffer.from(hexString, 'hex'))
132-
}
133-
134-
// We use very optimized technique to convert hex string to byte array
135-
const charCodeMap = {
136-
zero: 48,
137-
nine: 57,
138-
A: 65,
139-
F: 70,
140-
a: 97,
141-
f: 102,
142-
} as const
143-
144-
function charCodeToBase16(char: number) {
145-
if (char >= charCodeMap.zero && char <= charCodeMap.nine)
146-
return char - charCodeMap.zero
147-
else if (char >= charCodeMap.A && char <= charCodeMap.F)
148-
return char - (charCodeMap.A - 10)
149-
else if (char >= charCodeMap.a && char <= charCodeMap.f)
150-
return char - (charCodeMap.a - 10)
151-
return undefined
152-
}
153-
154-
/** @internal */
155-
export function hexToBytes_native(hex: Hex): ByteArray {
156137
let hexString = hex.slice(2) as string
157138
if (hexString.length % 2) hexString = `0${hexString}`
158139

Diff for: src/utils/encoding/toHex.test.ts

-20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { describe, expect, test } from 'vitest'
33
import {
44
boolToHex,
55
bytesToHex,
6-
bytesToHex_native,
76
numberToHex,
87
stringToHex,
98
toHex,
@@ -431,22 +430,3 @@ describe('converts bytes to hex', () => {
431430
`)
432431
})
433432
})
434-
435-
describe('converts bytes to hex (native)', () => {
436-
test('default', () => {
437-
expect(bytesToHex_native(new Uint8Array([]))).toMatchInlineSnapshot('"0x"')
438-
expect(bytesToHex_native(new Uint8Array([97]))).toMatchInlineSnapshot(
439-
'"0x61"',
440-
)
441-
expect(
442-
bytesToHex_native(new Uint8Array([97, 98, 99])),
443-
).toMatchInlineSnapshot('"0x616263"')
444-
expect(
445-
bytesToHex_native(
446-
new Uint8Array([
447-
72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33,
448-
]),
449-
),
450-
).toMatchInlineSnapshot('"0x48656c6c6f20576f726c6421"')
451-
})
452-
})

Diff for: src/utils/encoding/toHex.ts

+5-23
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { pad } from '../data/pad.js'
44

55
import { assertSize } from './fromHex.js'
66

7-
const hasBuffer = 'Buffer' in globalThis && typeof Buffer.from === 'function'
8-
97
const hexes = /*#__PURE__*/ Array.from({ length: 256 }, (_v, i) =>
108
i.toString(16).padStart(2, '0'),
119
)
@@ -116,7 +114,11 @@ export type BytesToHexOpts = {
116114
* // '0x48656c6c6f20576f726c64210000000000000000000000000000000000000000'
117115
*/
118116
export function bytesToHex(value: ByteArray, opts: BytesToHexOpts = {}): Hex {
119-
const hex = hasBuffer ? bytesToHex_buffer(value) : bytesToHex_native(value)
117+
let string = ''
118+
for (let i = 0; i < value.length; i++) {
119+
string += hexes[value[i]]
120+
}
121+
const hex = `0x${string}` as const
120122

121123
if (typeof opts.size === 'number') {
122124
assertSize(hex, { size: opts.size })
@@ -125,26 +127,6 @@ export function bytesToHex(value: ByteArray, opts: BytesToHexOpts = {}): Hex {
125127
return hex
126128
}
127129

128-
/** @internal */
129-
export function bytesToHex_buffer(value: ByteArray): Hex {
130-
if (!hasBuffer) throw new Error('`Buffer` not implemented.')
131-
if (value.length === 0) return '0x'
132-
return `0x${Buffer.from(
133-
value.buffer,
134-
value.byteOffset,
135-
value.byteLength,
136-
).toString('hex')}`
137-
}
138-
139-
/** @internal */
140-
export function bytesToHex_native(value: ByteArray): Hex {
141-
let string = ''
142-
for (let i = 0; i < value.length; i++) {
143-
string += hexes[value[i]]
144-
}
145-
return `0x${string}`
146-
}
147-
148130
export type NumberToHexOpts =
149131
| {
150132
/** Whether or not the number of a signed representation. */

Diff for: vectors/src/bytes.json.gz

-81 MB
Binary file not shown.

Diff for: vectors/src/bytes.ts

-40
This file was deleted.

Diff for: vectors/src/bytes.vectors.test.ts

-32
This file was deleted.

0 commit comments

Comments
 (0)