Skip to content

Address

A 20-byte account address. An Address is a branded string that is always lowercase, so two addresses compare with ===. Every address ethect writes is in its EIP-55 form, and an address is read in any case.

import { Address } from "@ethect/core";
const A = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const B = Address.makeUnsafe("0xF39FD6E51AAD88F6F4CE6AB8827279CFFFB92266");
const address = A; // "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"
const same = A === B; // true
const display = Address.checksum(A); // "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"

Every method that takes an account takes an Address.Address, and every address in a result is one. A plain string is a type error: validate it once with make, where it enters your program. An address written in your source, such as the address of a token, is a constant: build it with makeUnsafe.

Export Kind Description
make function Validates a string and returns an Address, as an Effect.
makeResult function The same, as a Result.
makeUnsafe function The same for a constant written in the source. It throws on what is no address.
isAddress function Whether a value is a valid address string.
checksum function The EIP-55 form of an address, for display.
equals function Whether two addresses are the same account.
zero constant The zero address.
schema schema Decodes an address a person wrote to an Address.
wireSchema schema Decodes an address a node or a wallet sent to an Address.
checksumSchema schema Decodes any valid address to a ChecksumAddress.
Address type A validated, lowercase address.
ChecksumAddress type An EIP-55 checksummed address.
InvalidAddressError error What make fails with.
Where Form Why
in memory: every Address lowercase two addresses compare with ===, and key a Map or a Set
written out: every request, to a node or a wallet, and Tx.toJson, Connection.toJson and Connection.toCookie EIP-55 the one form every wallet takes
read in: every result, and what was stored any case, made lowercase a node writes either; the case is not read as a checksum

A checksum is a keccak, about 3 µs. An address read in is lowercased, which costs no hash: a page of logs, receipts or ABI results holds many addresses. An address written out is checksummed once, and the last 8,192 checksums are kept, so writing it again costs a Map lookup. Some wallets refuse any other form: Ambire keeps an account under its checksum, and an account sent in lowercase “does not exist”. Nodes read hex in any case. In ABI data an address is bytes, with no case.

make, makeResult, makeUnsafe, isAddress, schema and checksumSchema apply the same rule: 20 bytes of hex after 0x, and a valid EIP-55 checksum when the letters are in mixed case.

Input Accepted
0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266, all lowercase yes
0xF39FD6E51AAD88F6F4CE6AB8827279CFFFB92266, all uppercase yes
0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266, mixed case with a valid checksum yes
0xF39Fd6e51aad88F6F4ce6aB8827279cffFb92266, mixed case with a wrong checksum no
0x1234, or 40 hex digits without 0x no

An all-lowercase or all-uppercase string carries no checksum, so there is nothing to check. A mixed-case string claims one: a wrong checksum is a mistyped address, and it is refused.

wireSchema is the exception: it reads what a node sent, in any case, and checks no checksum.

Validates a string and returns it as an Address, in lowercase. It is for an address your program is given: what a user typed, what a request carries, what a file holds.

import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
// On a server: the account is whatever the request says.
const balanceOf = (request: Request) =>
Effect.gen(function* () {
const address = yield* Address.make(new URL(request.url).searchParams.get("account") ?? "");
return yield* Eth.getBalance(address, "latest");
});
  • Parameters: value (string).
  • Returns: Effect<Address.Address, Address.InvalidAddressError>.

A string that is not an address is a failure in the error channel, not an exception. Handle it with Effect.catchTag.

import { Effect } from "effect";
import { Address } from "@ethect/core";
const parse = (input: string) =>
Address.make(input).pipe(
Effect.catchTag("InvalidAddressError", (error) =>
Effect.succeed(`not an address: ${error.value}`),
),
);
const result = parse("0x1234"); // succeeds with "not an address: 0x1234"

The same validation as a Result, for code that is not inside an Effect.

import { Result } from "effect";
import { Address } from "@ethect/core";
/** What a form field holds, in a handler that runs no effect. */
const recipient = (typed: string) => {
const result = Address.makeResult(typed);
return Result.isSuccess(result) ? result.success : undefined;
};
  • Parameters: value (string).
  • Returns: Result<Address.Address, Address.InvalidAddressError>. make is this function lifted into an Effect.

The address of a constant written in the source, with no effect to run. It validates as make does and returns the address in lowercase. It throws when the text is no address: a typo stops the program when the file loads, before anything is sent to it.

import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
// At the top of a file. The literal is checked when the module loads, checksum included.
const USDC = Address.makeUnsafe("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
// "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", an `Address.Address`
const code = Eth.getCode(USDC, "latest");

It is for that one case, and for no other. It is the only function of ethect that throws.

The address is Use What is no address is
written in your source: a token, a contract of yours Address.makeUnsafe an InvalidAddressError thrown when the file loads: a bug to fix
typed by a user, sent by a server, read from a file Address.make a failure in the error channel: an answer to give
import { Effect } from "effect";
import { Address } from "@ethect/core";
// Yours: a wrong character is a mistake in the code, and the program should not start.
const VAULT = Address.makeUnsafe("0xBEEF01735c132Ada46AA9aA4c54623cAA92A64CB");
// Theirs: a wrong character is something to tell them.
const recipient = (typed: string) =>
Address.make(typed).pipe(
Effect.catchTag("InvalidAddressError", (error) => Effect.succeed(error.message)),
);
  • Parameters: value (string).
  • Returns: Address.Address.
  • Throws: Address.InvalidAddressError, the error make fails with, with the same value and message.

Copy an address as an explorer shows it, in mixed case: the case is its checksum, so a character that is wrong is caught. In lowercase there is nothing to check it against.

The name ends in Unsafe as Effect’s own functions that throw do: DateTime.makeUnsafe, BigDecimal.fromStringUnsafe.

Tells whether a value is a valid address string.

import { Address } from "@ethect/core";
const valid = Address.isAddress("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"); // true
const typo = Address.isAddress("0xF39Fd6e51aad88F6F4ce6aB8827279cffFb92266"); // false
  • Parameters: u (unknown).
  • Returns: boolean. As a type guard it narrows to Hex.Hex, not to Address: the string can still be in mixed case. Use make to get an Address.

Returns the EIP-55 form of an address: the mixed-case form to show to a user.

import { Address } from "@ethect/core";
const ACCOUNT = Address.makeUnsafe("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266");
const display = Address.checksum(ACCOUNT); // "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
  • Parameters: address (Address.Address).
  • Returns: Address.ChecksumAddress. It cannot fail: the address is already validated.
  • Kept. The last 8,192 checksums are kept, the oldest dropped first: a checksum asked again costs a Map lookup, not a keccak. Every address written to a node, a wallet or JSON text is checksummed with it.

Tells whether two addresses are the same account.

import { Address } from "@ethect/core";
const A = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const B = Address.makeUnsafe("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266");
const same = Address.equals(A, B); // true
  • Parameters: self (Address.Address), then that (Address.Address).
  • Returns: boolean. It is self === that, as a function.

The zero address, 0x0000000000000000000000000000000000000000, as an Address.

import { Address } from "@ethect/core";
const isUnset = (owner: Address.Address) => owner === Address.zero;

The schema of an address a person wrote: a form field, a config file, an argument. It decodes any valid address to a lowercase Address, and encodes an Address to its EIP-55 form.

import { Schema } from "effect";
import { Address } from "@ethect/core";
const Approval = Schema.Struct({
owner: Address.schema,
spender: Address.schema,
});
const decode = Schema.decodeUnknownEffect(Approval);
  • Type: Address.Address. Encoded: a string.
  • A mixed-case string with a wrong EIP-55 checksum is refused, as by make.
  • A value it refuses fails with a SchemaError, not with an InvalidAddressError.

The schema of an address on the wire, to and from a node or a wallet. It decodes 20 bytes of hex in any case to a lowercase Address, and the case is not read as a checksum. It encodes an Address to its EIP-55 form.

import { Schema } from "effect";
import { Address } from "@ethect/core";
import { BlockId, Method } from "@ethect/core/rpc";
/** Whether an address has code. An Otterscan method, which anvil serves. */
const hasCode = Method.make<[address: unknown, block: unknown]>()(
"ots_hasCode",
[Address.wireSchema, BlockId.numberOrTagSchema],
Schema.Boolean,
);
  • Type: Address.Address. Encoded: a string.
  • Every method of the library uses it for its account params and results. Use it in your own methods.
  • Encoding takes a lowercase Address and sends its EIP-55 form, the one every wallet takes.
  • A value it refuses, inside a method call, fails with a CodecError.
schema wireSchema
decoding all lowercase, all uppercase, or a valid checksum lowercase Address lowercase Address
decoding mixed case with a wrong checksum refused lowercase Address
decoding what is not 20 bytes of hex refused refused
encoding an Address its EIP-55 form its EIP-55 form

The schema of an address in its EIP-55 form. It decodes any valid address to a ChecksumAddress, and encodes one as it is.

import { Schema } from "effect";
import { Address } from "@ethect/core";
const Row = Schema.Struct({ owner: Address.checksumSchema });
const row = Schema.decodeUnknownEffect(Row)({
// decodes to "0x70997970C51812dc3A010C7d01b50e0d17dc79C8"
owner: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",
});
  • Type: Address.ChecksumAddress. Encoded: a string.

The type of a validated address: Hex.Hex & Brand<"Address">.

import type { Address } from "@ethect/core";
const isOwner = (owner: Address.Address, caller: Address.Address) => owner === caller;
  • Branded. The brand is what stops a plain string from being passed as an account. You get an Address from make, makeResult, schema, wireSchema, or the result of a method.
  • Lowercase. An address has one form in memory. Two addresses that name the same account are the same string, so ===, Set and the keys of a Map work with no helper. It is written in its EIP-55 form.

The type of an address in its EIP-55 form: Hex.Hex & Brand<"ChecksumAddress">.

import { Address } from "@ethect/core";
const label = (address: Address.Address): Address.ChecksumAddress =>
Address.checksum(address);

It is for display. It is not an Address: keep the Address for calls and comparisons, and take the checksum when you show it.

The error make and makeResult fail with: a string that is not an address, or whose mixed case is not a valid EIP-55 checksum.

Field Type Notes
_tag "InvalidAddressError" the tag Effect.catchTag matches
value string the input
message string which of the two rules failed
import { Effect } from "effect";
import { Address } from "@ethect/core";
// one letter of the checksum is in the wrong case
const typo = Address.make("0xF39Fd6e51aad88F6F4ce6aB8827279cffFb92266").pipe(
Effect.catchTag("InvalidAddressError", (error) =>
Effect.succeed(error.message),
),
);

The two messages, for the input above and for 0x1234:

Invalid EIP-55 checksum for address 0xF39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Expected a 0x-prefixed 20-byte hex address, got 0x1234
  • Lowercase in results. A method returns every address in lowercase, whatever case the node used. Call checksum to display one.
  • No checksum check on results. An address a node sends in mixed case is not checked against EIP-55: a chain that checksums its own way still decodes.
  • EIP-55 on the wire. A method sends every address in its EIP-55 form, whatever case you wrote it in: in its params, in the keys of a state override, in a log filter, in typed data. What is stored as text, by Tx.toJson and Connection.toJson, is in that form too.
  • Read in any case. What was stored in lowercase before, a cookie or a pending transaction, reads as it did.
  • The prefix is 0x. An uppercase 0X is refused.