1
1
import { getRandomBytesSync } from 'ethereum-cryptography/random.js'
2
2
// eslint-disable-next-line no-restricted-imports
3
- import { bytesToHex as _bytesToUnprefixedHex } from 'ethereum-cryptography/utils.js'
3
+ import {
4
+ bytesToHex as _bytesToUnprefixedHex ,
5
+ hexToBytes as nobleH2B ,
6
+ } from 'ethereum-cryptography/utils.js'
4
7
5
8
import { assertIsArray , assertIsBytes , assertIsHexString } from './helpers.js'
6
9
import { isHexString , padToEven , stripHexPrefix } from './internal.js'
@@ -14,48 +17,26 @@ const BIGINT_0 = BigInt(0)
14
17
*/
15
18
export const bytesToUnprefixedHex = _bytesToUnprefixedHex
16
19
17
- // hexToBytes cache
18
- const hexToBytesMapFirstKey : { [ key : string ] : number } = { }
19
- const hexToBytesMapSecondKey : { [ key : string ] : number } = { }
20
-
21
- for ( let i = 0 ; i < 16 ; i ++ ) {
22
- const vSecondKey = i
23
- const vFirstKey = i * 16
24
- const key = i . toString ( 16 ) . toLowerCase ( )
25
- hexToBytesMapSecondKey [ key ] = vSecondKey
26
- hexToBytesMapSecondKey [ key . toUpperCase ( ) ] = vSecondKey
27
- hexToBytesMapFirstKey [ key ] = vFirstKey
28
- hexToBytesMapFirstKey [ key . toUpperCase ( ) ] = vFirstKey
29
- }
30
-
31
20
/**
32
- * @deprecated
21
+ * Converts a {@link PrefixedHexString} to a {@link Uint8Array}
22
+ * @param {PrefixedHexString } hex The 0x-prefixed hex string to convert
23
+ * @returns {Uint8Array } The converted bytes
24
+ * @throws If the input is not a valid 0x-prefixed hex string
33
25
*/
34
- export const unprefixedHexToBytes = ( inp : string ) => {
35
- if ( inp . slice ( 0 , 2 ) === '0x' ) {
36
- throw new Error ( 'hex string is prefixed with 0x, should be unprefixed' )
37
- } else {
38
- inp = padToEven ( inp )
39
- const byteLen = inp . length
40
- const bytes = new Uint8Array ( byteLen / 2 )
41
- for ( let i = 0 ; i < byteLen ; i += 2 ) {
42
- bytes [ i / 2 ] = hexToBytesMapFirstKey [ inp [ i ] ] + hexToBytesMapSecondKey [ inp [ i + 1 ] ]
43
- }
44
- return bytes
45
- }
26
+ export const hexToBytes = ( hex : string ) => {
27
+ if ( ! hex . startsWith ( '0x' ) ) throw new Error ( 'input string must be 0x prefixed' )
28
+ return nobleH2B ( padToEven ( stripHexPrefix ( hex ) ) )
46
29
}
47
30
48
- /**************** Borrowed from @chainsafe/ssz */
49
- // Caching this info costs about ~1000 bytes and speeds up toHexString() by x6
50
- const hexByByte = Array . from ( { length : 256 } , ( v , i ) => i . toString ( 16 ) . padStart ( 2 , '0' ) )
31
+ export const unprefixedHexToBytes = ( hex : string ) => {
32
+ if ( hex . startsWith ( '0x' ) ) throw new Error ( 'input string cannot be 0x prefixed' )
33
+ return nobleH2B ( padToEven ( hex ) )
34
+ }
51
35
52
36
export const bytesToHex = ( bytes : Uint8Array ) : PrefixedHexString => {
53
- let hex : PrefixedHexString = `0x`
54
- if ( bytes === undefined || bytes . length === 0 ) return hex
55
- for ( const byte of bytes ) {
56
- hex = `${ hex } ${ hexByByte [ byte ] } `
57
- }
58
- return hex
37
+ if ( bytes === undefined || bytes . length === 0 ) return '0x'
38
+ const unprefixedHex = bytesToUnprefixedHex ( bytes )
39
+ return ( '0x' + unprefixedHex ) as PrefixedHexString
59
40
}
60
41
61
42
// BigInt cache for the numbers 0 - 256*256-1 (two-byte bytes)
@@ -99,26 +80,6 @@ export const bytesToInt = (bytes: Uint8Array): number => {
99
80
return res
100
81
}
101
82
102
- /**
103
- * Converts a {@link PrefixedHexString} to a {@link Uint8Array}
104
- * @param {PrefixedHexString } hex The 0x-prefixed hex string to convert
105
- * @returns {Uint8Array } The converted bytes
106
- * @throws If the input is not a valid 0x-prefixed hex string
107
- */
108
- export const hexToBytes = ( hex : PrefixedHexString ) : Uint8Array => {
109
- if ( typeof hex !== 'string' ) {
110
- throw new Error ( `hex argument type ${ typeof hex } must be of type string` )
111
- }
112
-
113
- if ( ! / ^ 0 x [ 0 - 9 a - f A - F ] * $ / . test ( hex ) ) {
114
- throw new Error ( `Input must be a 0x-prefixed hexadecimal string, got ${ hex } ` )
115
- }
116
-
117
- const unprefixedHex = hex . slice ( 2 )
118
-
119
- return unprefixedHexToBytes ( unprefixedHex )
120
- }
121
-
122
83
/******************************************/
123
84
124
85
/**
@@ -130,7 +91,7 @@ export const intToHex = (i: number): PrefixedHexString => {
130
91
if ( ! Number . isSafeInteger ( i ) || i < 0 ) {
131
92
throw new Error ( `Received an invalid integer type: ${ i } ` )
132
93
}
133
- return `0x ${ i . toString ( 16 ) } `
94
+ return ( '0x' + i . toString ( 16 ) ) as PrefixedHexString
134
95
}
135
96
136
97
/**
0 commit comments