EthereumJS Utilities

7.1.5 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic usage, including Ethereum checksum address validation, byte buffer manipulation, and arbitrary-precision arithmetic using the re-exported BN.js library.

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!');

view raw JSON →