Skip to content

Address

Utility functions for working with Ethereum addresses.

Examples

Below are some examples demonstrating common usages of the Address module:

Instantiating Addresses

An Address.Address can be instantiated from a hex string using Address.from:

import { Address } from 'ox'
 
const address = Address.from('0xa0cf798816d4b9b9866b5330eea46a18382f251e')
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'

Validating Addresses

The Address.validate function will return true if the address is valid, and false otherwise:

import { Address } from 'ox'
 
const valid = Address.validate('0xA0Cf798816D4b9b9866b5330EEa46a18382f251e')
true

The Address.assert function will throw an error if the address is invalid:

import { Address } from 'ox'
 
Address.assert('0xdeadbeef')
InvalidAddressError: Address "0xdeadbeef" is invalid.

Addresses from ECDSA Public Keys

An Address.Address can be computed from an ECDSA public key using Address.fromPublicKey:

import { Address, Secp256k1 } from 'ox'
 
const privateKey = Secp256k1.randomPrivateKey()
const publicKey = Secp256k1.getPublicKey({ privateKey })
 
const address = Address.fromPublicKey(publicKey)
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'

Functions

NameDescription
Address.assertAsserts that the given value is a valid Address.Address.
Address.checksumComputes the checksum address for the given Address.Address.
Address.fromConverts a stringified address to a typed (checksummed) Address.Address.
Address.fromPublicKeyConverts an ECDSA public key to an Address.Address.
Address.isEqualChecks if two Address.Address are equal.
Address.validateChecks if the given address is a valid Address.Address.

Errors

NameDescription
Address.InvalidAddressErrorThrown when an address is invalid.
Address.InvalidChecksumErrorThrown when an address does not match its checksum counterpart.
Address.InvalidInputErrorThrown when an address is not a 20 byte (40 hexadecimal character) value.

Types

NameDescription
Address.AddressRoot type for Address.