Skip to main content
Version: 11.x

Class: CairoClassHash

Defined in: src/utils/cairoDataTypes/classHash.ts:29

A Cairo core::starknet::class_hash::ClassHash : the hash identifying a declared class.

On the wire it is a field element like any other, so what this class adds over CairoFelt252 is the bound of the Cairo ClassHash type : [0, 2^251), the same range as the Cairo type of an address, and narrower than the field. The 252-bit bound the RPC spec states is looser, and never the one that binds.

A number, a bigint, a decimal string and a hexadecimal string are all read as the same number. Text is not an accepted input : a class hash spelled as words is a mistake, not a value.

Example​

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

Constructors​

Constructor​

new CairoClassHash(data): CairoClassHash

Defined in: src/utils/cairoDataTypes/classHash.ts:60

Build from a number or a numeric string, refusing text and anything outside the ClassHash range.

Parameters​

data​

unknown

the hash to carry, within [0, 2^251 - 1]

Returns​

CairoClassHash

Throws​

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

Example​

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

Properties​

abiSelector​

static abiSelector: "core::starknet::class_hash::ClassHash" = Literal.ClassHash

Defined in: src/utils/cairoDataTypes/classHash.ts:48

The abi type this class serializes.

Example​

const result = CairoClassHash.abiSelector;
// result = "core::starknet::class_hash::ClassHash"

data​

data: bigint

Defined in: src/utils/cairoDataTypes/classHash.ts:38

The hash, always as a bigint.

Example​

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

Methods​

validate()​

static validate(data): void

Defined in: src/utils/cairoDataTypes/classHash.ts:129

Throw unless the value can be carried by a ClassHash.

Text is refused first, 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. 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 range of the Cairo ClassHash type, [0, 2^251), which is narrower than the field.

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 ClassHash range

Example​

CairoClassHash.validate('0x1234'); // passes
CairoClassHash.validate('abc');
// throws Error("Invalid input: a ClassHash cannot be built from text")
CairoClassHash.validate(2n ** 251n);
// throws Error("Value is out of ClassHash range [0, 3618502788666131106986593281521497120414687020801267626233049500247285301247]")
CairoClassHash.validate(-1);
// throws Error("Value is out of ClassHash range [0, 3618502788666131106986593281521497120414687020801267626233049500247285301247]")

is()​

static is(data): boolean

Defined in: src/utils/cairoDataTypes/classHash.ts:161

Can this value be carried by a ClassHash?

The non-throwing form of CairoClassHash.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 a ClassHash

Example​

const result = CairoClassHash.is('0x1234');
// result = true
const result2 = CairoClassHash.is('abc');
// result2 = false (text, not a number)

isAbiType()​

static isAbiType(abiType): boolean

Defined in: src/utils/cairoDataTypes/classHash.ts:182

Is this abi type the one this class serializes?

Parameters​

abiType​

string

the abi type to test

Returns​

boolean

true for core::starknet::class_hash::ClassHash

Example​

const result = CairoClassHash.isAbiType('core::starknet::class_hash::ClassHash');
// result = true
const result2 = CairoClassHash.isAbiType('core::felt252');
// result2 = false

factoryFromApiResponse()​

static factoryFromApiResponse(responseIterator): CairoClassHash

Defined in: src/utils/cairoDataTypes/classHash.ts:197

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

Parameters​

responseIterator​

Iterator<string>

the response felts, positioned on this hash

Returns​

CairoClassHash

the hash that was read

Example​

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

toApiRequest()​

toApiRequest(): string[]

Defined in: src/utils/cairoDataTypes/classHash.ts:74

Serialize to the single felt a contract call carries.

Returns​

string[]

one decimal-string felt, flagged as compiled

Example​

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

toBigInt()​

toBigInt(): bigint

Defined in: src/utils/cairoDataTypes/classHash.ts:87

The hash as a number.

Returns​

bigint

the number this hash holds

Example​

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

toHexString()​

toHexString(): string

Defined in: src/utils/cairoDataTypes/classHash.ts:105

The hash in hexadecimal, without padding.

The 64 hex digits a class hash is usually written with are not restored here : leading zeros are dropped, as they are everywhere else in the library.

Returns​

string

the hash as a 0x-prefixed hex string

Example​

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