EthereumJS Utilities
EthereumJS Util (currently at stable version 10.1.1, with version 7.1.5 referenced in the prompt as a specific historical point) is a foundational utility library within the larger EthereumJS ecosystem. It provides a comprehensive collection of low-level, high-performance functions essential for interacting with the Ethereum blockchain, including cryptographic hashing (e.g., Keccak256), RLP encoding/decoding, address validation (like checksum addresses), byte array manipulation, and elliptic curve operations. It is actively maintained as part of the EthereumJS monorepo, with regular minor and patch releases, and major versions typically aligning with significant Ethereum protocol updates or fundamental architectural shifts (e.g., ESM-only in v7). Its key differentiators include its tight integration and alignment with official Ethereum specifications, extensive TypeScript support, and being a reference implementation for various Ethereum primitives. It re-exports critical libraries like `BN.js` for arbitrary-precision arithmetic and `rlp` for Recursive Length Prefix encoding, making it a central point for many Ethereum-related development tasks.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use CommonJS `require()` syntax in an ES module context or with an ES module library.fixChange `const { Name } = require('ethereumjs-util')` to `import { Name } from 'ethereumjs-util'`. Ensure your project's `package.json` has `"type": "module"` or that files using `import` have a `.mjs` extension. -
TypeError: Invalid hex string (0x<length 0>)
cause Providing an empty string or incorrectly formatted string where a hex-prefixed hex string is expected by a utility function (e.g., `toBuffer`, `bufferToHex`).fixEnsure that hex string inputs are properly formatted, typically with a '0x' prefix and valid hexadecimal characters. If an empty value is intended, pass an empty Buffer or Uint8Array instead of an empty string for byte-related functions. -
Error: Invalid checksum address
cause The provided Ethereum address does not pass the EIP-55 checksum validation.fixDouble-check the Ethereum address for typos or incorrect casing. Ensure it matches the EIP-55 checksum standard. If you intend to work with non-checksummed addresses, use functions that do not perform checksum validation or convert to lowercase before validation if appropriate for your use case.
Warnings
- breaking Starting with version 7.0.0, `ethereumjs-util` is an ESM-only package. Direct `require()` statements will result in runtime errors. Projects must use ES module syntax (`import`) and configure their build tools accordingly.
- breaking Node.js 18 support has been deprecated across the EthereumJS monorepo, including `@ethereumjs/util`. The minimum required Node.js version is now 20.
- deprecated The `createBinaryObject` helper function within the `object` module (src/object.ts) has been marked as deprecated.
- gotcha Functions originally from the `ethjs-util` package have been internalized into `ethereumjs-util` since v7.1.3. While their API remains largely the same, this internalization means that subtle behavioral changes or future deprecations might occur independently of the original `ethjs-util` library.
- breaking Recent updates in version 10.1.1 include dependency bumps for `@noble/curves` and `@noble/hashes` to v2. While these are indirect dependencies for most users, direct interaction with these libraries or reliance on their previous versions' specific behaviors could lead to breaking changes.
Install
-
npm install ethereumjs-util -
yarn add ethereumjs-util -
pnpm add ethereumjs-util
Imports
- isValidChecksumAddress
const { isValidChecksumAddress } = require('ethereumjs-util')import { isValidChecksumAddress } from 'ethereumjs-util' - BN
import BN from 'bn.js'
import { BN } from 'ethereumjs-util' - stripHexPrefix
import { stripHexPrefix } from 'ethjs-util'import { stripHexPrefix } from 'ethereumjs-util' - rlp
import { rlp } from 'ethereumjs-util'
Quickstart
import assert from 'assert';
import { isValidChecksumAddress, unpadBuffer, BN } from 'ethereumjs-util';
// Example 1: Validate an Ethereum checksum address
const address = '0x2F015C60E0be116B1f0CD534704Db9c92118FB6A';
const isValid = isValidChecksumAddress(address);
console.log(`Address ${address} is valid: ${isValid}`);
assert.ok(isValid, 'Checksum address validation failed');
// Example 2: Unpad a Buffer
const paddedBuffer = Buffer.from('000000006600', 'hex');
const unpaddedBuffer = unpadBuffer(paddedBuffer);
console.log(`Unpadded buffer: ${unpaddedBuffer.toString('hex')}`);
assert.ok(unpaddedBuffer.equals(Buffer.from('6600', 'hex')), 'Buffer unpadding failed');
// Example 3: Perform arithmetic with BN.js
const bn1 = new BN('dead', 16); // Hexadecimal 'dead'
const bn2 = new BN('101010', 2); // Binary '101010'
const sum = bn1.add(bn2);
const expectedValue = new BN(57047); // Decimal equivalent of 'dead' (57005) + '101010' (42) = 57047
console.log(`BN sum: ${sum.toString()}`);
assert.ok(sum.eqn(expectedValue), 'BN arithmetic failed');
console.log('All quickstart assertions passed!');