@ethereumjs/util
v10.1.2
Published
A collection of utility functions for Ethereum
Readme
@ethereumjs/util v10
| A collection of utility functions for Ethereum. | | ----------------------------------------------- |
- 🧰 Shared primitives for the whole monorepo (bytes, accounts, addresses, signatures)
- 📋 EIP-7928 Block Level Access Lists — parse, validate, hash (Amsterdam, experimental)
- 📲 EIP-7702 authorization signing helpers
- 🔮 EIP-4844 / EIP-7594 blob and cell proof utilities
- 📨 EIP-7685 consensus-layer request types
- 💸 EIP-4895 withdrawal helpers
- 🌴 Tree-shakeable root imports (
import { … } from '@ethereumjs/util') - 👷🏼 Controlled dependency set (
@noblecrypto + minimal externals) - 🏄🏾♂️ WASM-free default + fully browser ready
Table of Contents
Installation
To obtain the latest version, simply require the project using npm:
npm install @ethereumjs/utilGetting Started
@ethereumjs/util bundles small, focused helpers used across the monorepo — from byte conversion and accounts to fork-specific types (BAL, blobs, CL requests). Everything is re-exported from the package root; deep imports are not necessary:
import { hexToBytes, isValidChecksumAddress } from '@ethereumjs/util'See At a glance for common entry points, or browse the module guide below grouped by role.
At a glance
| If you need… | Module | Details |
| --- | --- | --- |
| BAL JSON/RLP, validation, header hash | bal | Offline tooling; execution in @ethereumjs/vm |
| Blob / cell proofs (4844, PeerDAS) | blobs | KZG commitments, versioned hashes |
| Hex ↔ bytes ↔ bigint conversion | bytes | Most-used helpers |
| Accounts (full or partial) | account | State trie account objects |
| Ethereum addresses | address | Creation, validation, conversion |
| Sign / recover secp256k1 | signature | Thin wrappers over @noble |
| EIP-7702 auth list signing | authorization | Prague+ |
| CL requests (7685) | request | Deposits, exits, consolidations |
| Beacon withdrawals (4895) | withdrawal | Block withdrawal objects |
| Trie / blockchain storage | db / mapDB | Pluggable key-value API |
Module guide
Modules are grouped by role. Each section keeps the ## Module: [name] anchors used elsewhere in the docs.
Core primitives
Everyday building blocks — bytes, accounts, addresses, signatures, constants, and shared types.
Module: bytes
Byte-related helper and conversion functions.
// ./examples/bytes.ts
import { bytesToBigInt } from '@ethereumjs/util'
const bytesValue = new Uint8Array([97])
const bigIntValue = bytesToBigInt(bytesValue)
console.log(`Converted value: ${bigIntValue}`)Module: account
Class representing an Account and providing private/public key and address-related functionality (creation, validation, conversion). It is not recommended to use this constructor directly. Instead use the static factory methods to assist in creating an Account from varying data types.
// ./examples/account.ts
import { createAccount } from '@ethereumjs/util'
const account = createAccount({
nonce: '0x02',
balance: '0x0384',
storageRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
codeHash: '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470',
})
console.log(`Account with nonce=${account.nonce} and balance=${account.balance} created`)For Verkle or other contexts it can be useful to create partial accounts not containing all the account parameters. This is supported starting with v9.1.0:
// ./examples/accountPartial.ts
import { createPartialAccount } from '@ethereumjs/util'
const account = createPartialAccount({
nonce: '0x02',
balance: '0x0384',
})
console.log(`Partial account with nonce=${account.nonce} and balance=${account.balance} created`)Module: address
Class representing an Ethereum Address with instantiation helpers and validation methods.
// ./examples/address.ts
import { createAddressFromString } from '@ethereumjs/util'
const address = createAddressFromString('0x2f015c60e0be116b1f0cd534704db9c92118fb6a')
console.log(`Ethereum address ${address.toString()} created`)Module: signature
Small helpers around signature validation, conversion, recovery as well as selected convenience wrappers for calls to the underlying crypo libraries, using the cryptographic primitive implementations from the Noble crypto library set. If possible for your use case it is recommended to use the underlying crypto libraries directly for robustness.
// ./examples/signature.ts
import { bytesToHex, ecrecover, hexToBytes } from '@ethereumjs/util'
const chainId = BigInt(3) // Ropsten
const ecHash = hexToBytes('0x82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28')
const r = hexToBytes('0x99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9')
const s = hexToBytes('0x129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66')
const v = BigInt(41)
const pubkey = ecrecover(ecHash, v, r, s, chainId)
console.log(`Recovered public key ${bytesToHex(pubkey)} from valid signature values`)Module: constants
Exposed constants (e.g. KECCAK256_NULL_S for string representation of Keccak-256 hash of null)
// ./examples/constants.ts
import { BIGINT_2EXP96, KECCAK256_NULL_S } from '@ethereumjs/util'
console.log(`The keccak-256 hash of null: ${KECCAK256_NULL_S}`)
console.log(`BigInt constants (performance), e.g. BIGINT_2EXP96: ${BIGINT_2EXP96}`)Module: types
Various TypeScript types. Direct usage is not recommended, type structure might change in the future.
Fork & protocol helpers
Fork-specific types and helpers — BAL, blobs, authorization lists, CL requests, withdrawals.
Module: bal
Helpers for EIP-7928 Block Level Access Lists (BAL): the BlockLevelAccessList class, JSON/RLP conversion, hashing, and validation utilities. Use this module for offline fixture checks or tooling; block execution and BAL accumulation live in @ethereumjs/vm. See the canonical Amsterdam overview for release ↔ spec tracking.
// ./examples/bal.ts
import {
bytesToHex,
createBlockLevelAccessListFromJSON,
validateBlockAccessListHashFromJSON,
validateBlockAccessListStructure,
} from '@ethereumjs/util'
const main = () => {
const balJson = [
{
address: '0x0000000000000000000000000000000000000001',
storageChanges: [],
storageReads: [],
balanceChanges: [{ blockAccessIndex: '0x01', postBalance: '0x03e8' }],
nonceChanges: [],
codeChanges: [],
},
]
const bal = createBlockLevelAccessListFromJSON(balJson)
validateBlockAccessListStructure(bal)
validateBlockAccessListHashFromJSON(balJson, bal.hash())
console.log(`BAL account count: ${bal.toJSON().length}`)
console.log(`BAL hash: ${bytesToHex(bal.hash())}`)
}
void main()
Module: blobs
Module providing helpers around EIP-4844 blobs for creating blobs, associated KZG commitments and proofs as well as versioned hashes. It also provides helpers for EIP-7594 conformant blobs for creating extended cells and corresponding proofs.
// ./examples/blobs.ts
//import * as fs from 'fs'
import {
type PrefixedHexString,
blobsToCellProofs,
blobsToProofs,
computeVersionedHash,
hexToBytes,
} from '@ethereumjs/util'
import { trustedSetup } from '@paulmillr/trusted-setups/fast-peerdas.js'
import { KZG as microEthKZG } from 'micro-eth-signer/kzg.js'
const kzg = new microEthKZG(trustedSetup)
/**
* Uncomment for a more realistic example using a real blob, e.g. from https://blobscan.com/
* Use with node ./examples/blobs.ts <file path>
*/
// const filePath = process.argv[2]
//const blob: PrefixedHexString = `0x${fs.readFileSync(filePath, 'ascii')}`
const blob: PrefixedHexString = `0x${'11'.repeat(131072)}` // 128 KiB
console.log(blob)
const commitment = kzg.blobToKzgCommitment(blob)
const blobCommitmentVersion = 0x01
const versionedHash = computeVersionedHash(commitment as PrefixedHexString, blobCommitmentVersion)
// EIP-4844 only
const blobProof = blobsToProofs(kzg, [blob], [commitment as PrefixedHexString])
const cellProofs = blobsToCellProofs(kzg, [blob])
console.log(`Blob size : ${hexToBytes(blob).length / 1024}KiB`)
console.log(`Commitment : ${commitment}`)
console.log(`Versioned hash : ${versionedHash}`)
console.log(`Blob proof (EIP-4844) : ${blobProof}`)
console.log(`First cell proof (EIP-7594) : ${cellProofs[0]}`)
console.log(`Num cell proofs (EIP-7594) : ${cellProofs.length}`)
Module: authorization
Module with EIP-7702 authorization list signing utilities.
Module: request
Module with a compact generic request class for EIP-7685 general purpose execution layer requests to the CL (Prague hardfork) with the possibility to set data and a type conforming to the following request types:
- EIP-6110:
DepositRequest(Prague Hardfork) - EIP-7002:
WithdrawalRequest(Prague Hardfork) - EIP-7251:
ConsolidationRequest(Prague Hardfork)
These request types are mainly used within the @ethereumjs/block library where applied usage instructions are provided in the README.
Module: withdrawal
Class representing an EIP-4895 Withdrawal with different constructors as well as conversion and output helpers.
// ./examples/withdrawal.ts
import { createWithdrawal } from '@ethereumjs/util'
const withdrawal = createWithdrawal({
index: 0n,
validatorIndex: 65535n,
address: '0x0000000000000000000000000000000000000000',
amount: 0n,
})
console.log('Withdrawal object created:')
console.log(withdrawal.toJSON())Storage & integration
Database abstractions, KZG typing, genesis helpers, and legacy internal utilities.
Module: db
DB interface for database abstraction (Blockchain, Trie), see e.g. @ethereumjs/trie recipes) for usage.
Module: mapDB
Simple map DB implementation using the DB interface (see above).
Module: kzg
KZG interface (used for 4844 blob txs), see @ethereumjs/tx README for main usage instructions.
Module: genesis
Genesis related interfaces and helpers.
Module: internal
Internalized simple helper methods like isHexString. Note that methods from this module might get deprecated in the future. Prefer the documented helpers in API → ethjs-util methods when available.
Browser
We provide hybrid ESM/CJS builds for all our libraries. With the v10 breaking release round from Spring 2025, all libraries are "pure-JS" by default and we have eliminated all hard-wired WASM code. Additionally we have substantially lowered the bundle sizes, reduced the number of dependencies, and cut out all usages of Node.js-specific primitives (like the Node.js event emitter).
It is easily possible to run a browser build of one of the EthereumJS libraries within a modern browser using the provided ESM build. For a setup example see ./examples/browser.html.
API
Documentation
Read the API docs.
Hybrid CJS/ESM Builds
With the breaking releases from Summer 2023 we have started to ship our libraries with both CommonJS (cjs folder) and ESM builds (esm folder), see package.json for the detailed setup.
If you use an ES6-style import in your code files from the ESM build will be used:
import { EthereumJSClass } from '@ethereumjs/[PACKAGE_NAME]'If you use Node.js specific require, the CJS build will be used:
const { EthereumJSClass } = require('@ethereumjs/[PACKAGE_NAME]')Using ESM will give you additional advantages over CJS beyond browser usage like static code analysis / Tree Shaking which CJS can not provide.
ethjs-util methods
The following methods are available by an internalized version of the ethjs-util package (MIT license), see internal.ts. The original package is not maintained any more and the original functionality will be replaced by own implementations over time (starting with the v7.1.3 release, October 2021).
- arrayContainsArray
- getBinarySize
- stripHexPrefix
- isHexString
- isHexString
- padToEven
- fromAscii
- fromUtf8
- toUtf8
- toAscii
- getKeys
They can be imported by name:
import { stripHexPrefix } from '@ethereumjs/util'EthereumJS
The EthereumJS GitHub organization and its repositories are managed by members of the former Ethereum Foundation JavaScript team and the broader Ethereum community. If you want to join for work or carry out improvements on the libraries see the developer docs for an overview of current standards and tools and review our code of conduct.
