Utilities & Addresses
Lightweight utility functions and classes built on tevm/utils, @evmts/zevm, and viem.
createAddress
Create an Ethereum address from various inputs.
import { createAddress } from 'tevm/address'
createAddress(`0x${'00'.repeat(20)}`)
createAddress(420n)
createAddress(new Uint8Array(20))Signature
declare function createAddress(
address: number | bigint | string | Uint8Array | EthjsAddress
): AddressAccepts 0x-prefixed hex, unprefixed hex, numbers, bigints, Uint8Arrays, or EthjsAddress. Throws InvalidAddressError on invalid input.
Address Class
Thin wrapper around EthjsAddress.
import { Address } from 'tevm/address'
const a = new Address(Uint8Array.from([ /* 20 bytes */ ]))
a.bytes // raw 20-byte address
a.toString() // '0x...'createContractAddress
CREATE address per EIP-1014:
import { createContractAddress } from 'tevm/address'
const contractAddr = createContractAddress(createAddress('0x1111...1111'), 1n)Computes keccak256(rlp([senderAddress, nonce]))[-20..]. Throws InvalidAddressError if from is invalid.
Common Errors
InvalidAddressError- bytes/string can't parse as a 20-byte address.UnreachableCodeError- unexpected code path; usually wrapped into a more descriptive error.
Other Re-exports
Lower-level helpers from tevm/utils, ZEVM, or viem:
hexToBytes(hex)- hex to raw bytes (with optional size checks).keccak256(data, 'bytes' | 'hex')- keccak256 hasher.encodeFunctionData(...),toRlp(...)- ABI/RLP encoders.- EIP-7702 helpers:
eoaCode7702SignAuthorization,eoaCode7702RecoverAuthority, and authorization-list encoders.
@tevm/utils
Full API: packages/utils/docs. Combines ZEVM primitives, viem utilities, and Tevm helpers.
Installation
npm install @tevm/utilsMain Components
Hex and Bytes Conversion
import {
bytesToHex,
hexToBytes,
bytesToBigInt,
bytesToNumber,
hexToBigInt,
hexToNumber,
numberToHex,
stringToHex,
hexToString,
} from '@tevm/utils'
bytesToHex(new Uint8Array([1, 164])) // '0x01a4'
hexToBytes('0x01a4') // Uint8Array([1, 164])
hexToNumber('0x01a4') // 420
numberToHex(420) // '0x1a4'
hexToString('0x48656c6c6f') // 'Hello'
stringToHex('Hello') // '0x48656c6c6f'Type Checking
import { isHex, isBytes, isAddress } from '@tevm/utils'
isHex('0x123')
isBytes(new Uint8Array())
isAddress('0x123...')Unit Conversion
import { formatEther, parseEther, formatGwei, parseGwei } from '@tevm/utils'
formatEther(1000000000000000000n) // '1'
parseEther('1.0') // 1000000000000000000n
formatGwei(1000000000n) // '1'
parseGwei('1.0') // 1000000000nCryptography
import { keccak256, ecrecover, ecsign, randomBytes } from '@tevm/utils'
const hash = keccak256('0x1234')
const signature = ecsign(messageHash, privateKey)
const address = ecrecover(messageHash, v, r, s)
const random = randomBytes(32)ABI Encoding/Decoding
import {
encodeAbiParameters,
decodeAbiParameters,
encodeFunctionData,
decodeFunctionData,
encodeEventTopics,
decodeEventLog,
} from '@tevm/utils'
const data = encodeFunctionData({ abi: [...], functionName: 'transfer', args: [address, amount] })
const result = decodeFunctionData({ abi: [...], data: '0x...' })
const topics = encodeEventTopics({ abi: [...], eventName: 'Transfer', args: [from, to, null] })RLP
import { toRlp, fromRlp } from '@tevm/utils'
const rlp = toRlp(['0x123', '0x456'])
const decoded = fromRlp(rlp)Memory Database
import { createMemoryDb } from '@tevm/utils'
const db = createMemoryDb()
const db2 = createMemoryDb(new Map())Event Emitter
import { AsyncEventEmitter } from '@tevm/utils'
const emitter = new AsyncEventEmitter()
emitter.on('event', async (data) => { /* ... */ })
await emitter.emit('event', data)Types
Basic
import type { Address, Hex, BlockTag, BlockNumber, BytesLike, BigIntLike } from '@tevm/utils'
const address: Address = '0x...'
const hex: Hex = '0x...'
const blockTag: BlockTag = 'latest'ABI
import type { Abi, AbiFunction, AbiEvent, AbiConstructor, ParseAbi, FormatAbi } from '@tevm/utils'
type ParsedAbi = ParseAbi<typeof abiString>
type FormattedAbi = FormatAbi<typeof parsedAbi>Contract
import type {
ContractFunctionName,
ContractConstructorArgs,
ExtractAbiFunction,
ExtractAbiEvent,
} from '@tevm/utils'
type TransferFunction = ExtractAbiFunction<typeof abi, 'transfer'>
type TransferEvent = ExtractAbiEvent<typeof abi, 'Transfer'>Constants
import { GWEI_TO_WEI, KECCAK256_RLP, KECCAK256_RLP_ARRAY } from '@tevm/utils'
GWEI_TO_WEI // 1000000000nError Handling
import { invariant } from '@tevm/utils'
invariant(condition, 'Error message')
