@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))
toString()
: Returns the checksummed address as a stringtoBytes()
: Returns the address as a Uint8Arrayequals(address: Address)
: Checks if two addresses are equalisZero()
: Checks if the address is zeroisPrecompileOrSystemAddress()
: 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))
address
: The input to create an address from (hex string, number, bigint, Address instance, or Uint8Array)
- An
Address
instance
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)
from
: The address of the account creating the contractnonce
: The nonce of the account creating the contract
- The generated contract address
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)
from
: The address creating the contractsalt
: A 32-byte salt value as a hex stringcode
: The contract creation code
- The generated contract address
InvalidSaltError
if the salt is not 32 bytesInvalidAddressError
if inputs are invalid
Error Handling
The package provides specific error types for different validation scenarios:
InvalidAddressError
: Thrown when an invalid address is providedInvalidSaltError
: Thrown when an invalid salt is provided for CREATE2Create2ContractAddressError
: 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)