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
Name | Description |
---|---|
Address.assert | Asserts that the given value is a valid Address.Address . |
Address.checksum | Computes the checksum address for the given Address.Address . |
Address.from | Converts a stringified address to a typed (checksummed) Address.Address . |
Address.fromPublicKey | Converts an ECDSA public key to an Address.Address . |
Address.isEqual | Checks if two Address.Address are equal. |
Address.validate | Checks if the given address is a valid Address.Address . |
Errors
Name | Description |
---|---|
Address.InvalidAddressError | Thrown when an address is invalid. |
Address.InvalidChecksumError | Thrown when an address does not match its checksum counterpart. |
Address.InvalidInputError | Thrown when an address is not a 20 byte (40 hexadecimal character) value. |
Types
Name | Description |
---|---|
Address.Address | Root type for Address. |