Class: CairoClassHash
Defined in: src/utils/cairoDataTypes/classHash.ts:27
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 the RPC spec sets on it — 252 bits. That is wider than a felt252 is, so the two bounds each refuse values the other allows, and neither stands in for the other.
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:58
Build from a number or a numeric string, refusing text and anything wider than 252 bits.
Parameters
data
unknown
the hash to carry, within [0, 2^252 - 1]
Returns
CairoClassHash
Throws
when the value is text, is not a felt252 input, or is out of the ClassHash range
Example
const result = new CairoClassHash('0x1234').toApiRequest();
// result = ["4660"]
Properties
abiSelector
staticabiSelector:"core::starknet::class_hash::ClassHash"=Literal.ClassHash
Defined in: src/utils/cairoDataTypes/classHash.ts:46
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:36
The hash, always as a bigint.
Example
const result = new CairoClassHash('0x1234').data;
// result = 4660n
Methods
validate()
staticvalidate(data):void
Defined in: src/utils/cairoDataTypes/classHash.ts:122
Throw unless the value can be carried by a ClassHash.
Text is refused first, then the value is read as a felt252 — which is what refuses a null, an object or an unsupported type, and what bounds the value. There is no narrower bound to add: a class hash is a hash output, so any field element is one.
Parameters
data
unknown
the value to check
Returns
void
Throws
when the value is text, is not a felt252 input, or is outside the field
Example
CairoClassHash.validate('0x1234'); // passes
CairoClassHash.validate('abc');
// throws Error("Invalid input: a ClassHash cannot be built from text")
is()
staticis(data):boolean
Defined in: src/utils/cairoDataTypes/classHash.ts:143
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()
staticisAbiType(abiType):boolean
Defined in: src/utils/cairoDataTypes/classHash.ts:164
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()
staticfactoryFromApiResponse(responseIterator):CairoClassHash
Defined in: src/utils/cairoDataTypes/classHash.ts:179
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:72
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:85
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:103
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)