Skip to content

@tevm/address

The @tevm/address package provides utilities for handling Ethereum addresses. It wraps the functionality from @ethereumjs/utils address with a Tevm-style API.

Installation

npm install @tevm/address

Overview

This package provides a set of utilities for working with Ethereum addresses, including:

  • Creating and validating addresses
  • Generating contract addresses (both regular and CREATE2)
  • Converting between different address formats
  • Handling checksummed addresses

API Reference

Classes

Address

A utility class for Ethereum addresses that extends EthjsAddress. It provides a more user-friendly API and ensures checksummed address output.

import { createAddress } from '@tevm/address'
 
// Create from hex string
const address = createAddress(`0x${'00'.repeat(20)}`)
 
// Create from number or bigint
const address2 = createAddress(0n)
 
// Create from bytes
const address3 = createAddress(new Uint8Array(20))
 
// Create from non-hex string
const address4 = createAddress('55'.repeat(20))
Methods:
  • toString(): Returns the checksummed address as a string
  • toBytes(): Returns the address as a Uint8Array
  • equals(address: Address): Checks if two addresses are equal
  • isZero(): Checks if the address is zero
  • isPrecompileOrSystemAddress(): Checks if the address is a precompile or system address

Functions

createAddress(address: string | number | bigint | Address | Uint8Array): Address

Creates an Address instance from various input types.

import { createAddress } from '@tevm/address'
 
// From hex string
const address = createAddress('0x1234...')
 
// From number
const address2 = createAddress(123)
 
// From bytes
const address3 = createAddress(new Uint8Array(20))
Parameters:
  • address: The input to create an address from (hex string, number, bigint, Address instance, or Uint8Array)
Returns:
  • An Address instance
Throws:
  • InvalidAddressError if the input is not a valid address

createContractAddress(from: Address, nonce: bigint): Address

Generates an address for a newly created contract using the standard CREATE operation.

import { createAddress, createContractAddress } from '@tevm/address'
 
const from = createAddress('0x1234...')
const nonce = 0n
const contractAddress = createContractAddress(from, nonce)
Parameters:
  • from: The address of the account creating the contract
  • nonce: The nonce of the account creating the contract
Returns:
  • The generated contract address
Throws:
  • InvalidAddressError if the 'from' parameter is not a valid address

create2ContractAddress(from: Address, salt: string, code: string): Address

Generates an address for a contract created using CREATE2 (EIP-1014).

import { createAddress, create2ContractAddress } from '@tevm/address'
 
const from = createAddress('0x1234...')
const salt = `0x${'00'.repeat(32)}`
const code = '0x...' // Contract creation code
const contractAddress = create2ContractAddress(from, salt, code)
Parameters:
  • from: The address creating the contract
  • salt: A 32-byte salt value as a hex string
  • code: The contract creation code
Returns:
  • The generated contract address
Throws:
  • InvalidSaltError if the salt is not 32 bytes
  • InvalidAddressError if inputs are invalid

Error Handling

The package provides specific error types for different validation scenarios:

  • InvalidAddressError: Thrown when an invalid address is provided
  • InvalidSaltError: Thrown when an invalid salt is provided for CREATE2
  • Create2ContractAddressError: Union type of both error types above

Examples

Basic Address Creation and Validation

import { createAddress } from '@tevm/address'
 
// Create an address
const address = createAddress('0x742d35Cc6634C0532925a3b844Bc454e4438f44e')
 
// Get checksummed string representation
console.log(address.toString())
// '0x742d35Cc6634C0532925a3b844Bc454e4438f44e'
 
// Check if it's a zero address
console.log(address.isZero())
// false
 
// Compare addresses
const address2 = createAddress('0x742d35Cc6634C0532925a3b844Bc454e4438f44e')
console.log(address.equals(address2))
// true

Contract Address Generation

import { createAddress, createContractAddress, create2ContractAddress } from '@tevm/address'
 
// Generate regular contract address
const from = createAddress('0x742d35Cc6634C0532925a3b844Bc454e4438f44e')
const nonce = 1n
const contractAddress = createContractAddress(from, nonce)
 
// Generate CREATE2 contract address
const salt = `0x${'00'.repeat(32)}`
const code = '0x608060405234801561001057600080fd5b506101...' // Contract bytecode
const create2Address = create2ContractAddress(from, salt, code)

See Also