Skip to content
Closed
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
4 changes: 2 additions & 2 deletions packages/block/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import {

import type { Common } from '@ethereumjs/common'
import type { TypedTransaction } from '@ethereumjs/tx'
import type { CLRequest, CLRequestType, PrefixedHexString, Withdrawal } from '@ethereumjs/util'
import type { CLRequest, CLRequestType, PrefixedHexString, Withdrawal, NumericString } from '@ethereumjs/util'
import type { BlockHeaderBytes, HeaderData } from './types.ts'
/**
* Returns a 0x-prefixed hex number string from a hex string or string integer.
* @param {string} input string to check, convert, and return
*/
export const numberToHex = function (input?: string): PrefixedHexString | undefined {
export const numberToHex = function (input?: string | NumericString): PrefixedHexString | undefined {
if (input === undefined) return undefined
if (!isHexString(input)) {
const regex = new RegExp(/^\d+$/) // test to make sure input contains only digits
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BigIntLike, KZG, PrefixedHexString, VerkleCrypto } from '@ethereumjs/util'
import type { BigIntLike, KZG, PrefixedHexString, VerkleCrypto, NumericString } from '@ethereumjs/util'
import type { secp256k1 } from 'ethereum-cryptography/secp256k1.js'
import type { ConsensusAlgorithm, ConsensusType, Hardfork } from './enums.ts'

Expand Down Expand Up @@ -170,7 +170,7 @@ export type EIPConfig = {
}

export type ParamsConfig = {
[key: string]: number | string | null
[key: string]: number | NumericString | null
}

export type HardforkConfig = {
Expand Down
4 changes: 2 additions & 2 deletions packages/evm/src/journal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import {
import debugDefault from 'debug'

import type { Common, StateManagerInterface } from '@ethereumjs/common'
import type { Account, PrefixedHexString } from '@ethereumjs/util'
import type { Account, PrefixedHexString, NumericString } from '@ethereumjs/util'
import type { Debugger } from 'debug'

type AddressString = string
type SlotString = string
type SlotString = NumericString
type WarmSlots = Set<SlotString>

type JournalType = Map<AddressString, WarmSlots>
Expand Down
4 changes: 4 additions & 0 deletions packages/util/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ export const TypeOutput = {
BigInt: 1,
Uint8Array: 2,
PrefixedHexString: 3,
NumericString: 4,
} as const

export type TypeOutputReturnType = {
[TypeOutput.Number]: number
[TypeOutput.BigInt]: bigint
[TypeOutput.Uint8Array]: Uint8Array
[TypeOutput.PrefixedHexString]: PrefixedHexString
[TypeOutput.NumericString]: NumericString
}

/**
Expand Down Expand Up @@ -123,6 +125,8 @@ export function toType<T extends TypeOutput>(
}
case TypeOutput.PrefixedHexString:
return bytesToHex(output) as TypeOutputReturnType[T]
case TypeOutput.NumericString:
return bytesToBigInt(output).toString() as TypeOutputReturnType[T]
default:
throw EthereumJSErrorWithoutCode('unknown outputType')
}
Expand Down
14 changes: 14 additions & 0 deletions packages/util/test/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ describe('toType', () => {
assert.strictEqual(toType(null, TypeOutput.BigInt), null)
assert.strictEqual(toType(null, TypeOutput.Uint8Array), null)
assert.strictEqual(toType(null, TypeOutput.PrefixedHexString), null)
assert.strictEqual(toType(null, TypeOutput.NumericString), null)
assert.strictEqual(toType(undefined, TypeOutput.Number), undefined)
assert.strictEqual(toType(undefined, TypeOutput.BigInt), undefined)
assert.strictEqual(toType(undefined, TypeOutput.Uint8Array), undefined)
assert.strictEqual(toType(undefined, TypeOutput.PrefixedHexString), undefined)
assert.strictEqual(toType(undefined, TypeOutput.NumericString), undefined)
})
it('from Number', () => {
const num = 1000
Expand All @@ -38,6 +40,9 @@ describe('toType', () => {
result = toType(num, TypeOutput.PrefixedHexString)
assert.strictEqual(result, bytesToHex(bigIntToBytes(BigInt(num))))

result = toType(num, TypeOutput.NumericString)
assert.strictEqual(result, num.toString())

assert.throws(() => {
const num = Number.MAX_SAFE_INTEGER + 1
toType(num, TypeOutput.BigInt)
Expand All @@ -59,6 +64,9 @@ describe('toType', () => {
result = toType(num, TypeOutput.PrefixedHexString)
assert.strictEqual(result, bytesToHex(bigIntToBytes(num)))

result = toType(num, TypeOutput.NumericString)
assert.strictEqual(result, num.toString())

const num2 = BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1)
assert.throws(() => {
toType(num2, TypeOutput.Number)
Expand All @@ -79,6 +87,9 @@ describe('toType', () => {

result = toType(num, TypeOutput.PrefixedHexString)
assert.strictEqual(result, bytesToHex(num))

result = toType(num, TypeOutput.NumericString)
assert.strictEqual(result, bytesToBigInt(num).toString())
})
it('from PrefixedHexString', () => {
const num = intToHex(1000)
Expand All @@ -93,6 +104,9 @@ describe('toType', () => {
result = toType(num, TypeOutput.Uint8Array)
assert.deepEqual(result, hexToBytes(num))

result = toType(num, TypeOutput.NumericString)
assert.strictEqual(result, bytesToBigInt(hexToBytes(num)).toString())

assert.throws(() => {
//@ts-expect-error -- Testing invalid input
toType('1', TypeOutput.Number)
Expand Down
Loading