Skip to content

Commit bc648e7

Browse files
util: revert some PrefixedHexString breaking changes (#3427)
* util: remove redundant isHexPrefixed since we have isHexString * util: re-add string type support * util: misc fixes * docs: undo readme docs change * Update packages/util/README.md Co-authored-by: Scorbajio <indigophi@protonmail.com> * Update packages/util/src/internal.ts Co-authored-by: Scorbajio <indigophi@protonmail.com> --------- Co-authored-by: Scorbajio <indigophi@protonmail.com>
1 parent 8cae3bb commit bc648e7

File tree

18 files changed

+68
-94
lines changed

18 files changed

+68
-94
lines changed

packages/block/src/block.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
getProvider,
2020
hexToBytes,
2121
intToHex,
22-
isHexPrefixed,
22+
isHexString,
2323
} from '@ethereumjs/util'
2424
import { keccak256 } from 'ethereum-cryptography/keccak.js'
2525

@@ -370,7 +370,7 @@ export class Block {
370370
params: [bigIntToHex(blockTag), true],
371371
})
372372
} else if (
373-
isHexPrefixed(blockTag) ||
373+
isHexString(blockTag) ||
374374
blockTag === 'latest' ||
375375
blockTag === 'earliest' ||
376376
blockTag === 'pending' ||

packages/block/test/block.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ describe('[Block]: block functions', () => {
154154

155155
it('should test block validation on pow chain', async () => {
156156
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Istanbul })
157-
const blockRlp = toBytes(testDataPreLondon.blocks[0].rlp as PrefixedHexString)
157+
const blockRlp = hexToBytes(testDataPreLondon.blocks[0].rlp as PrefixedHexString)
158158
try {
159159
Block.fromRLPSerializedBlock(blockRlp, { common })
160160
assert.ok(true, 'should pass')
@@ -180,7 +180,7 @@ describe('[Block]: block functions', () => {
180180
}
181181

182182
it('should test transaction validation - invalid tx trie', async () => {
183-
const blockRlp = toBytes(testDataPreLondon.blocks[0].rlp as PrefixedHexString)
183+
const blockRlp = hexToBytes(testDataPreLondon.blocks[0].rlp as PrefixedHexString)
184184
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
185185
const block = Block.fromRLPSerializedBlock(blockRlp, { common, freeze: false })
186186
await testTransactionValidation(block)
@@ -221,7 +221,7 @@ describe('[Block]: block functions', () => {
221221

222222
it('should test transaction validation with legacy tx in london', async () => {
223223
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
224-
const blockRlp = toBytes(testDataPreLondon.blocks[0].rlp as PrefixedHexString)
224+
const blockRlp = hexToBytes(testDataPreLondon.blocks[0].rlp as PrefixedHexString)
225225
const block = Block.fromRLPSerializedBlock(blockRlp, { common, freeze: false })
226226
await testTransactionValidation(block)
227227
;(block.transactions[0] as any).gasPrice = BigInt(0)
@@ -234,7 +234,7 @@ describe('[Block]: block functions', () => {
234234

235235
it('should test uncles hash validation', async () => {
236236
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Istanbul })
237-
const blockRlp = toBytes(testDataPreLondon2.blocks[2].rlp as PrefixedHexString)
237+
const blockRlp = hexToBytes(testDataPreLondon2.blocks[2].rlp as PrefixedHexString)
238238
const block = Block.fromRLPSerializedBlock(blockRlp, { common, freeze: false })
239239
assert.equal(block.uncleHashIsValid(), true)
240240
;(block.header as any).uncleHash = new Uint8Array(32)

packages/client/src/rpc/modules/engine/engine.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,8 @@ export class Engine {
927927
const { headBlockHash, finalizedBlockHash, safeBlockHash } = params[0]
928928
const payloadAttributes = params[1]
929929

930-
const safe = toBytes(safeBlockHash)
931-
const finalized = toBytes(finalizedBlockHash)
930+
const safe = hexToBytes(safeBlockHash)
931+
const finalized = hexToBytes(finalizedBlockHash)
932932

933933
if (!equalsBytes(finalized, zeroBlockHash) && equalsBytes(safe, zeroBlockHash)) {
934934
throw {

packages/common/src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { intToHex, isHexPrefixed, stripHexPrefix } from '@ethereumjs/util'
1+
import { intToHex, isHexString, stripHexPrefix } from '@ethereumjs/util'
22

33
import { Hardfork } from './enums.js'
44

@@ -16,7 +16,7 @@ function formatNonce(nonce: string): PrefixedHexString {
1616
if (!nonce || nonce === '0x0') {
1717
return '0x0000000000000000'
1818
}
19-
if (isHexPrefixed(nonce)) {
19+
if (isHexString(nonce)) {
2020
return `0x${stripHexPrefix(nonce).padStart(16, '0')}`
2121
}
2222
return `0x${nonce.padStart(16, '0')}`
@@ -67,7 +67,7 @@ function parseGethParams(json: any, mergeForkIdPostMerge: boolean = true) {
6767
unparsedExtraData === '' ? '0x' : (unparsedExtraData as PrefixedHexString)
6868

6969
// geth may use number for timestamp
70-
const timestamp: PrefixedHexString = isHexPrefixed(unparsedTimestamp)
70+
const timestamp: PrefixedHexString = isHexString(unparsedTimestamp)
7171
? unparsedTimestamp
7272
: intToHex(parseInt(unparsedTimestamp))
7373

packages/devp2p/src/protocol/eth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
bytesToUnprefixedHex,
99
hexToBytes,
1010
intToBytes,
11-
isHexPrefixed,
11+
isHexString,
1212
} from '@ethereumjs/util'
1313
import * as snappy from 'snappyjs'
1414

@@ -286,7 +286,7 @@ export class ETH extends Protocol {
286286
this._latestBlock = latestBlock
287287
}
288288
const forkHashB = hexToBytes(
289-
isHexPrefixed(this._forkHash) ? this._forkHash : `0x${this._forkHash}`
289+
isHexString(this._forkHash) ? this._forkHash : `0x${this._forkHash}`
290290
)
291291

292292
const nextForkB =

packages/rlp/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ function padToEven(a: string): string {
253253
}
254254

255255
/** Check if a string is prefixed by 0x */
256-
function isHexPrefixed(str: string): boolean {
256+
function isHexString(str: string): boolean {
257257
return str.length >= 2 && str[0] === '0' && str[1] === 'x'
258258
}
259259

@@ -262,7 +262,7 @@ function stripHexPrefix(str: string): string {
262262
if (typeof str !== 'string') {
263263
return str
264264
}
265-
return isHexPrefixed(str) ? str.slice(2) : str
265+
return isHexString(str) ? str.slice(2) : str
266266
}
267267

268268
/** Transform anything into a Uint8Array */
@@ -271,7 +271,7 @@ function toBytes(v: Input): Uint8Array {
271271
return v
272272
}
273273
if (typeof v === 'string') {
274-
if (isHexPrefixed(v)) {
274+
if (isHexString(v)) {
275275
return hexToBytes(padToEven(stripHexPrefix(v)))
276276
}
277277
return utf8ToBytes(v)

packages/trie/src/util/genesisState.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { RLP } from '@ethereumjs/rlp'
22
import {
33
Account,
44
hexToBytes,
5-
isHexPrefixed,
6-
toBytes,
5+
isHexString,
76
unpadBytes,
87
unprefixedHexToBytes,
98
} from '@ethereumjs/util'
@@ -19,7 +18,7 @@ import type { AccountState, GenesisState } from '@ethereumjs/util'
1918
export async function genesisStateRoot(genesisState: GenesisState) {
2019
const trie = new Trie({ useKeyHashing: true })
2120
for (const [key, value] of Object.entries(genesisState)) {
22-
const address = isHexPrefixed(key) ? hexToBytes(key) : unprefixedHexToBytes(key)
21+
const address = isHexString(key) ? hexToBytes(key) : unprefixedHexToBytes(key)
2322
const account = new Account()
2423
if (typeof value === 'string') {
2524
account.balance = BigInt(value)
@@ -29,15 +28,15 @@ export async function genesisStateRoot(genesisState: GenesisState) {
2928
account.balance = BigInt(balance)
3029
}
3130
if (code !== undefined) {
32-
const codeBytes = isHexPrefixed(code) ? toBytes(code) : unprefixedHexToBytes(code)
31+
const codeBytes = isHexString(code) ? hexToBytes(code) : unprefixedHexToBytes(code)
3332
account.codeHash = keccak256(codeBytes)
3433
}
3534
if (storage !== undefined) {
3635
const storageTrie = new Trie({ useKeyHashing: true })
3736
for (const [k, val] of storage) {
38-
const storageKey = isHexPrefixed(k) ? toBytes(k) : unprefixedHexToBytes(k)
37+
const storageKey = isHexString(k) ? hexToBytes(k) : unprefixedHexToBytes(k)
3938
const storageVal = RLP.encode(
40-
unpadBytes(isHexPrefixed(val) ? toBytes(val) : unprefixedHexToBytes(val))
39+
unpadBytes(isHexString(val) ? hexToBytes(val) : unprefixedHexToBytes(val))
4140
)
4241
await storageTrie.put(storageKey, storageVal)
4342
}

packages/trie/test/official.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { bytesToHex, hexToBytes, isHexPrefixed, utf8ToBytes } from '@ethereumjs/util'
1+
import { bytesToHex, hexToBytes, isHexString, utf8ToBytes } from '@ethereumjs/util'
22
import { assert, describe, it } from 'vitest'
33

44
import { Trie } from '../src/index.js'
@@ -41,13 +41,13 @@ describe('official tests any order', async () => {
4141
for (key of keys) {
4242
let val = test.in[key]
4343

44-
if (typeof key === 'string' && isHexPrefixed(key)) {
44+
if (typeof key === 'string' && isHexString(key)) {
4545
key = hexToBytes(key)
4646
} else if (typeof key === 'string') {
4747
key = utf8ToBytes(key)
4848
}
4949

50-
if (typeof val === 'string' && isHexPrefixed(val)) {
50+
if (typeof val === 'string' && isHexString(val)) {
5151
val = hexToBytes(val)
5252
} else if (typeof val === 'string') {
5353
val = utf8ToBytes(val)

packages/util/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Genesis related interfaces and helpers.
117117

118118
### Module: [internal](src/internal.ts)
119119

120-
Internalized simple helper methods like `isHexPrefixed`. Note that methods from this module might get deprectared in the future.
120+
Internalized simple helper methods like `isHexString`. Note that methods from this module might get deprecated in the future.
121121

122122
### Module: [kzg](src/kzg.ts)
123123

@@ -257,7 +257,7 @@ The following methods are available by an internalized version of the [ethjs-uti
257257
- arrayContainsArray
258258
- getBinarySize
259259
- stripHexPrefix
260-
- isHexPrefixed
260+
- isHexString
261261
- isHexString
262262
- padToEven
263263
- fromAscii

packages/util/src/bytes.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getRandomBytesSync } from 'ethereum-cryptography/random.js'
33
import { bytesToHex as _bytesToUnprefixedHex } from 'ethereum-cryptography/utils.js'
44

55
import { assertIsArray, assertIsBytes, assertIsHexString } from './helpers.js'
6-
import { isHexPrefixed, isHexString, padToEven, stripHexPrefix } from './internal.js'
6+
import { isHexString, padToEven, stripHexPrefix } from './internal.js'
77

88
import type { PrefixedHexString, TransformabletoBytes } from './types.js'
99

@@ -107,13 +107,14 @@ export const bytesToInt = (bytes: Uint8Array): number => {
107107
return res
108108
}
109109

110+
// TODO: Restrict the input type to only PrefixedHexString
110111
/**
111112
* Converts a {@link PrefixedHexString} to a {@link Uint8Array}
112-
* @param {PrefixedHexString} hex The 0x-prefixed hex string to convert
113+
* @param {PrefixedHexString | string} hex The 0x-prefixed hex string to convert
113114
* @returns {Uint8Array} The converted bytes
114115
* @throws If the input is not a valid 0x-prefixed hex string
115116
*/
116-
export const hexToBytes = (hex: PrefixedHexString): Uint8Array => {
117+
export const hexToBytes = (hex: PrefixedHexString | string): Uint8Array => {
117118
if (typeof hex !== 'string') {
118119
throw new Error(`hex argument type ${typeof hex} must be of type string`)
119120
}
@@ -256,12 +257,13 @@ export const unpadArray = (a: number[]): number[] => {
256257
return stripZeros(a)
257258
}
258259

260+
// TODO: Restrict the input type to only PrefixedHexString
259261
/**
260262
* Trims leading zeros from a `PrefixedHexString`.
261-
* @param {PrefixedHexString} a
263+
* @param {PrefixedHexString | string} a
262264
* @return {PrefixedHexString}
263265
*/
264-
export const unpadHex = (a: PrefixedHexString): PrefixedHexString => {
266+
export const unpadHex = (a: PrefixedHexString | string): PrefixedHexString => {
265267
assertIsHexString(a)
266268
return `0x${stripZeros(stripHexPrefix(a))}`
267269
}
@@ -353,7 +355,7 @@ export const addHexPrefix = (str: string): PrefixedHexString => {
353355
return str
354356
}
355357

356-
return isHexPrefixed(str) ? str : `0x${str}`
358+
return isHexString(str) ? str : `0x${str}`
357359
}
358360

359361
/**
@@ -540,6 +542,7 @@ export function bigInt64ToBytes(value: bigint, littleEndian: boolean = false): U
540542
// eslint-disable-next-line no-restricted-imports
541543
export { bytesToUtf8, equalsBytes, utf8ToBytes } from 'ethereum-cryptography/utils.js'
542544

543-
export function hexToBigInt(input: PrefixedHexString): bigint {
544-
return bytesToBigInt(hexToBytes(input))
545+
// TODO: Restrict the input type to only PrefixedHexString
546+
export function hexToBigInt(input: PrefixedHexString | string): bigint {
547+
return bytesToBigInt(hexToBytes(isHexString(input) ? input : `0x${input}`))
545548
}

0 commit comments

Comments
 (0)