From 618cec6d3d842cba0be2cdf347382efc2c4dbae3 Mon Sep 17 00:00:00 2001 From: VolodymyrBg Date: Mon, 19 May 2025 17:00:00 +0300 Subject: [PATCH 1/5] feat(types): add NumericString to TypeOutput and TypeOutputReturnType --- packages/util/src/types.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/util/src/types.ts b/packages/util/src/types.ts index 474c043e911..7c64ccfba84 100644 --- a/packages/util/src/types.ts +++ b/packages/util/src/types.ts @@ -65,6 +65,7 @@ export const TypeOutput = { BigInt: 1, Uint8Array: 2, PrefixedHexString: 3, + NumericString: 4, } as const export type TypeOutputReturnType = { @@ -72,6 +73,7 @@ export type TypeOutputReturnType = { [TypeOutput.BigInt]: bigint [TypeOutput.Uint8Array]: Uint8Array [TypeOutput.PrefixedHexString]: PrefixedHexString + [TypeOutput.NumericString]: NumericString } /** @@ -123,6 +125,8 @@ export function toType( } case TypeOutput.PrefixedHexString: return bytesToHex(output) as TypeOutputReturnType[T] + case TypeOutput.NumericString: + return bytesToBigInt(output).toString() as TypeOutputReturnType[T] default: throw EthereumJSErrorWithoutCode('unknown outputType') } From c3940399ebb00f245d2d0570a994adb6be1627e8 Mon Sep 17 00:00:00 2001 From: VolodymyrBg Date: Mon, 19 May 2025 17:01:18 +0300 Subject: [PATCH 2/5] refactor(types): update numberToHex to handle NumericString --- packages/block/src/helpers.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block/src/helpers.ts b/packages/block/src/helpers.ts index 81fb8989cc9..476117c1e03 100644 --- a/packages/block/src/helpers.ts +++ b/packages/block/src/helpers.ts @@ -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 From 77a1e0d12beef24ad861e901e7f588067d9f5890 Mon Sep 17 00:00:00 2001 From: VolodymyrBg Date: Mon, 19 May 2025 17:02:05 +0300 Subject: [PATCH 3/5] refactor(types): update SlotString to use NumericString --- packages/evm/src/journal.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/evm/src/journal.ts b/packages/evm/src/journal.ts index 1b7153845e3..05522f94939 100644 --- a/packages/evm/src/journal.ts +++ b/packages/evm/src/journal.ts @@ -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 type JournalType = Map From 86d35fef31b1841202323f6982d361ab44bcab32 Mon Sep 17 00:00:00 2001 From: VolodymyrBg Date: Mon, 19 May 2025 17:02:45 +0300 Subject: [PATCH 4/5] refactor(types): update ParamsConfig to use NumericString --- packages/common/src/types.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/common/src/types.ts b/packages/common/src/types.ts index 809a58bf331..2e3d82d8cb3 100644 --- a/packages/common/src/types.ts +++ b/packages/common/src/types.ts @@ -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' @@ -170,7 +170,7 @@ export type EIPConfig = { } export type ParamsConfig = { - [key: string]: number | string | null + [key: string]: number | NumericString | null } export type HardforkConfig = { From 3d6854b4e80ccf16896d6656c21513f3b49bda0a Mon Sep 17 00:00:00 2001 From: VolodymyrBg Date: Mon, 19 May 2025 17:03:46 +0300 Subject: [PATCH 5/5] test(types): add tests for NumericString type --- packages/util/test/types.spec.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/util/test/types.spec.ts b/packages/util/test/types.spec.ts index 71a707dce96..f2f4a2f14ab 100644 --- a/packages/util/test/types.spec.ts +++ b/packages/util/test/types.spec.ts @@ -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 @@ -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) @@ -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) @@ -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) @@ -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)