Addresses are a fundamental part of interacting with Ethereum. They represent the gloabal identity of Externally Owned Accounts (accounts backed by a private key) and contracts.
The Ethereum Naming Service (ENS) provides an interconnected ecosystem of contracts, standards and libraries which enable looking up an address for an ENS name.
These functions help convert between various formats, validate addresses and safely resolve ENS names.
TYPES
Anything that can be used to return or resolve an address.
FUNCTIONS
<src>getAddress(address: string)⇒ string Returns a normalized and checksumed address for address. This accepts non-checksum addresses, checksum addresses and getIcapAddress formats.
The checksum in Ethereum uses the capitalization (upper-case vs lower-case) of the characters within an address to encode its checksum, which offers, on average, a checksum of 15-bits.
If address contains both upper-case and lower-case, it is assumed to already be a checksum address and its checksum is validated, and if the address fails its expected checksum an error is thrown.
If you wish the checksum of address to be ignore, it should be converted to lower-case (i.e. .toLowercase()) before being passed in. This should be a very rare situation though, that you wish to bypass the safegaurds in place to protect against an address that has been incorrectly copied from another source.
getAddress("0x8ba1f109551bd432803012645ac136ddd64dba72")
// '0x8ba1f109551bD432803012645Ac136ddd64DBA72'
getAddress("XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK36");
// '0x8ba1f109551bD432803012645Ac136ddd64DBA72'
getAddress("0x8Ba1f109551bD432803012645Ac136ddd64DBA72")
// Error("bad address checksum", {
// code: "INVALID_ARGUMENT"
// argument: "address"
// value: "0x8Ba1f109551bD432803012645Ac136ddd64DBA72"
// shortMessage: "bad address checksum"
// })
Returns the address that would result from a CREATE2 operation with the given from, salt and initCodeHash.
To compute the initCodeHash from a contract's init code, use the keccak256 function.
For a quick overview and example of CREATE2, see Wisps: The Magical World of Create2.
from = "0x8ba1f109551bD432803012645Ac136ddd64DBA72"
salt = id("HelloWorld")
initCode = "0x6394198df16000526103ff60206004601c335afa6040516060f3";
initCodeHash = keccak256(initCode)
getCreate2Address(from, salt, initCodeHash)
// '0x533ae9d683B10C02EbDb05471642F85230071FC3'
Returns the address that would result from a CREATE for tx.
This can be used to compute the address a contract will be deployed to by an EOA when sending a deployment transaction (i.e. when the to address is null).
This can also be used to compute the address a contract will be deployed to by a contract, by using the contract's address as the to and the contract's nonce.
from = "0x8ba1f109551bD432803012645Ac136ddd64DBA72";
nonce = 5;
getCreateAddress({ from, nonce });
// '0x082B6aC9e47d7D83ea3FaBbD1eC7DAba9D687b36'
<src>getIcapAddress(address: string)⇒ string The ICAP Address format format is an early checksum format which attempts to be compatible with the banking industry IBAN format for bank accounts.
It is no longer common or a recommended format.
getIcapAddress("0x8ba1f109551bd432803012645ac136ddd64dba72");
// 'XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK36'
getIcapAddress("XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK36");
// 'XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK36'
getIcapAddress("XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK37");
// Error("bad icap checksum", {
// code: "INVALID_ARGUMENT"
// argument: "address"
// value: "XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK37"
// shortMessage: "bad icap checksum"
// })
<src>isAddress(value: any)⇒ boolean Returns true if value is a valid address.
isAddress("0x8ba1f109551bD432803012645Ac136ddd64DBA72")
// true
isAddress("XE65GB6LDNXYOFTX0NSV3FUWKOWIXAMJK36")
// true
isAddress("0x8Ba1f109551bD432803012645Ac136ddd64DBa72")
// false
isAddress("0x8Ba1f109551bD432803012645Ac136ddd64DBA72")
// false
isAddress("ricmoo.eth")
// false
<src>isAddressable(value: any)⇒ boolean Returns true if value is an object which implements the Addressable interface.
isAddressable(Wallet.createRandom())
// true
contract = new Contract("dai.tokens.ethers.eth", [ ], provider)
isAddressable(contract)
// true
Resolves to an address for the target, which may be any supported address type, an Addressable or a Promise which resolves to an address.
If an ENS name is provided, but that name has not been correctly configured a UnconfiguredNameError is thrown.
addr = "0x6B175474E89094C44Da98b954EedeAC495271d0F"
resolveAddress(addr, provider)
// '0x6B175474E89094C44Da98b954EedeAC495271d0F'
resolveAddress(Promise.resolve(addr))
// Promise<'0x6B175474E89094C44Da98b954EedeAC495271d0F'>
resolveAddress("dai.tokens.ethers.eth", provider)
// Promise<'0x6B175474E89094C44Da98b954EedeAC495271d0F'>
contract = new Contract(addr, [ ])
resolveAddress(contract, provider)
// Promise<'0x6B175474E89094C44Da98b954EedeAC495271d0F'>
resolveAddress("nothing-here.ricmoo.eth", provider)
// Promise<Error("unconfigured name", {
// code: "UNCONFIGURED_NAME"
// value: "nothing-here.ricmoo.eth"
// shortMessage: "unconfigured name"
// })>
resolveAddress("nothing-here.ricmoo.eth")
// Error("ENS resolution requires a provider", {
// code: "UNSUPPORTED_OPERATION"
// operation: "resolveName"
// shortMessage: "ENS resolution requires a provider"
// })
An interface for objects which have an address, and can resolve it asyncronously.
This allows objects such as Signer or Contract to be used most places an address can be, for example getting the balance.
METHODS
<src>addressable.getAddress()⇒ Promise< string > An interface for any object which can resolve an ENS name.
METHODS
<src>nameResolver.resolveName(name: string)⇒ Promise< null | string > Resolve to the address for the ENS name.
Resolves to null if the name is unconfigued. Use resolveAddress (passing this object as resolver) to throw for names that are unconfigured.