Skip to main content
Version: 11.x

Class: CairoEthAddress

Defined in: src/utils/cairoDataTypes/ethAddress.ts:31

A Cairo core::starknet::eth_address::EthAddress : an Ethereum address, carried in one felt252 but only 160 bits wide.

On the wire it is a field element like any other, so what this class adds over CairoFelt252 is the narrower bound : an address must fit in 160 bits, and a value past that is refused before any calldata leaves.

A number, a bigint, a decimal string and a hexadecimal string are all read as the same number, so the shape of the input does not survive. Text is not an accepted input : unlike the other Cairo classes, which take a string that spells no number for its UTF-8 bytes, an address has no meaning as text and refuses it rather than encoding it into a number nobody meant.

Example​

// the same address, reached three ways
new CairoEthAddress('0x1234').toBigInt(); // 4660n
new CairoEthAddress(4660).toBigInt(); // 4660n
new CairoEthAddress('4660').toBigInt(); // 4660n

Constructors​

Constructor​

new CairoEthAddress(data): CairoEthAddress

Defined in: src/utils/cairoDataTypes/ethAddress.ts:62

Build from a number or a numeric string, refusing text and anything wider than 160 bits.

Parameters​

data​

unknown

the address to carry, within [0, 2^160 - 1]

Returns​

CairoEthAddress

Throws​

when the value is text, a decimal number, not a felt252 input, or out of the EthAddress range

Example​

const result = new CairoEthAddress('0x1234').toApiRequest();
// result = ["4660"]

Properties​

abiSelector​

static abiSelector: string = ETH_ADDRESS

Defined in: src/utils/cairoDataTypes/ethAddress.ts:50

The abi type this class serializes.

Example​

const result = CairoEthAddress.abiSelector;
// result = "core::starknet::eth_address::EthAddress"

data​

data: bigint

Defined in: src/utils/cairoDataTypes/ethAddress.ts:40

The address, always as a bigint.

Example​

const result = new CairoEthAddress('0x1234').data;
// result = 4660n

Methods​

validate()​

static validate(data): void

Defined in: src/utils/cairoDataTypes/ethAddress.ts:132

Throw unless the value can be carried by an EthAddress.

Text is refused first, since an address spelled as words is a mistake rather than a value to encode, then a number with a decimal part. A number or a bigint is read as it is, so that a negative one reaches the range check below. Anything else is read as a felt252 — which is what refuses a null, an object or an unsupported type. The value is finally checked against the 160 bits an Ethereum address occupies.

Parameters​

data​

unknown

the value to check

Returns​

void

Throws​

when the value is text, a decimal number, not a felt252 input, or out of the EthAddress range

Example​

CairoEthAddress.validate('0x1234'); // passes
CairoEthAddress.validate('abc');
// throws Error("Invalid input: an EthAddress cannot be built from text")
CairoEthAddress.validate(2n ** 160n);
// throws Error("Value is out of EthAddress range [0, 1461501637330902918203684832716283019655932542975]")
CairoEthAddress.validate(-1);
// throws Error("Value is out of EthAddress range [0, 1461501637330902918203684832716283019655932542975]")

is()​

static is(data): boolean

Defined in: src/utils/cairoDataTypes/ethAddress.ts:166

Can this value be carried by an EthAddress?

The non-throwing form of CairoEthAddress.validate, so it answers false for every input that one refuses, whatever the reason.

Parameters​

data​

unknown

the value to test

Returns​

boolean

true when the value fits in an EthAddress

Example​

const result = CairoEthAddress.is('0x1234');
// result = true
const result2 = CairoEthAddress.is('abc');
// result2 = false (text, not a number)
const result3 = CairoEthAddress.is(2n ** 160n);
// result3 = false (one bit too wide)

isAbiType()​

static isAbiType(abiType): boolean

Defined in: src/utils/cairoDataTypes/ethAddress.ts:187

Is this abi type the one this class serializes?

Parameters​

abiType​

string

the abi type to test

Returns​

boolean

true for core::starknet::eth_address::EthAddress

Example​

const result = CairoEthAddress.isAbiType('core::starknet::eth_address::EthAddress');
// result = true
const result2 = CairoEthAddress.isAbiType('core::felt252');
// result2 = false

factoryFromApiResponse()​

static factoryFromApiResponse(responseIterator): CairoEthAddress

Defined in: src/utils/cairoDataTypes/ethAddress.ts:205

Read one EthAddress off a contract response, advancing the iterator past it.

The felts a node returns are hex strings, and one is consumed per call, so successive calls read successive return values.

Parameters​

responseIterator​

Iterator<string>

the response felts, positioned on this address

Returns​

CairoEthAddress

the address that was read

Example​

const response = ['0x1234'];
const result = CairoEthAddress.factoryFromApiResponse(response.values()).toBigInt();
// result = 4660n

toApiRequest()​

toApiRequest(): string[]

Defined in: src/utils/cairoDataTypes/ethAddress.ts:76

Serialize to the single felt a contract call carries.

Returns​

string[]

one decimal-string felt, flagged as compiled

Example​

const result = new CairoEthAddress('0x1234').toApiRequest();
// result = ["4660"]

toBigInt()​

toBigInt(): bigint

Defined in: src/utils/cairoDataTypes/ethAddress.ts:89

The address as a number.

Returns​

bigint

the number this address holds

Example​

const result = new CairoEthAddress('0x1234').toBigInt();
// result = 4660n

toHexString()​

toHexString(): string

Defined in: src/utils/cairoDataTypes/ethAddress.ts:107

The address in hexadecimal, without padding.

The 40 hex digits an Ethereum address is usually written with are not restored here : leading zeros are dropped, as they are everywhere else in the library.

Returns​

string

the address as a 0x-prefixed hex string

Example​

const result = new CairoEthAddress(4660).toHexString();
// result = "0x1234"
const result2 = new CairoEthAddress('0x0034').toHexString();
// result2 = "0x34" (four digits in, two out)