Class: CairoUint8
Defined in: src/utils/cairoDataTypes/uint8.ts:30
A Cairo core::integer::u8 : a whole number from 0 to 255, carried in one felt252.
The value is kept as a bigint, so the shape of the input does not survive — 97, '97' and
'0x61' give the same u8. A string that reads as text rather than as a number is taken for its
UTF-8 bytes, which is why 'a' is 97 and not a rejected input.
An already built instance is not an accepted input : handed back to the constructor it is
seen as an object and refused. Inside the library it is unwrapCairoScalar that reduces an
instance to its number before an abi slot receives it.
Example
// the same value, reached four ways
new CairoUint8(97).toBigInt(); // 97n
new CairoUint8('97').toBigInt(); // 97n
new CairoUint8('0x61').toBigInt(); // 97n
new CairoUint8('a').toBigInt(); // 97n the UTF-8 code of the character
Constructors
Constructor
new CairoUint8(
data):CairoUint8
Defined in: src/utils/cairoDataTypes/uint8.ts:65
Build from a number, a string or a boolean, refusing anything out of the u8 range.
A string is read as a number when it spells one — decimal or hexadecimal — and as UTF-8 text otherwise. Text therefore only fits here while it is a single ASCII character : two characters already make a number past 255.
Parameters
data
unknown
the value to carry, within [0, 255]
Returns
CairoUint8
Throws
when the value is null, undefined, an object, a decimal number, '0x', or out of range
Example
const result = new CairoUint8('a').toApiRequest();
// result = ["97"]
Properties
abiSelector
staticabiSelector:string='core::integer::u8'
Defined in: src/utils/cairoDataTypes/uint8.ts:49
The abi type this class serializes.
Example
const result = CairoUint8.abiSelector;
// result = "core::integer::u8"
data
data:
bigint
Defined in: src/utils/cairoDataTypes/uint8.ts:39
The value, always as a bigint.
Example
const result = new CairoUint8('0x61').data;
// result = 97n
Methods
__processData()
static__processData(data):bigint
Defined in: src/utils/cairoDataTypes/uint8.ts:85
Turn an accepted input into its number, before the range is checked.
Nothing here refuses a value : validate is what reads this number and decides. So an input
far outside the u8 range comes back untouched rather than raising.
Parameters
data
unknown
the value to convert
Returns
bigint
the number the input spells, of whatever size
Example
const result = CairoUint8.__processData('a');
// result = 97n
const result2 = CairoUint8.__processData('ab');
// result2 = 24930n (past the u8 range, and returned all the same)
validate()
staticvalidate(data):void
Defined in: src/utils/cairoDataTypes/uint8.ts:167
Throw unless the value can be carried by a u8.
Five things are refused, each with its own message : a null or undefined value, an object or
an array, a number with a decimal part, '0x', and a value outside [0, 255]. A text string reaches
that last check as the number its bytes spell, so 'ab' is refused for being out of range.
Parameters
data
unknown
the value to check
Returns
void
Throws
when the value is null, undefined, an object, a decimal number, '0x', or out of range
Example
CairoUint8.validate(44); // passes
CairoUint8.validate(256);
// throws Error("Value is out of u8 range [0, 255]")
is()
staticis(data):boolean
Defined in: src/utils/cairoDataTypes/uint8.ts:198
Can this value be carried by a u8?
The non-throwing form of CairoUint8.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 u8
Example
const result = CairoUint8.is('a');
// result = true (97, the UTF-8 code of the character)
const result2 = CairoUint8.is('ab');
// result2 = false (24930, past the u8 range)
isAbiType()
staticisAbiType(abiType):boolean
Defined in: src/utils/cairoDataTypes/uint8.ts:219
Is this abi type the one this class serializes?
Parameters
abiType
string
the abi type to test
Returns
boolean
true for core::integer::u8
Example
const result = CairoUint8.isAbiType('core::integer::u8');
// result = true
const result2 = CairoUint8.isAbiType('core::integer::u16');
// result2 = false
factoryFromApiResponse()
staticfactoryFromApiResponse(responseIterator):CairoUint8
Defined in: src/utils/cairoDataTypes/uint8.ts:237
Read one u8 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 u8
Returns
CairoUint8
the u8 that was read
Example
const response = ['0x2c'];
const result = CairoUint8.factoryFromApiResponse(response.values()).toBigInt();
// result = 44n
toApiRequest()
toApiRequest():
string[]
Defined in: src/utils/cairoDataTypes/uint8.ts:102
Serialize to the single felt a contract call carries.
Returns
string[]
one decimal-string felt, flagged as compiled
Example
const result = new CairoUint8(44).toApiRequest();
// result = ["44"]
toBigInt()
toBigInt():
bigint
Defined in: src/utils/cairoDataTypes/uint8.ts:115
The value as a number.
Returns
bigint
the number this u8 holds
Example
const result = new CairoUint8(255).toBigInt();
// result = 255n
decodeUtf8()
decodeUtf8():
string
Defined in: src/utils/cairoDataTypes/uint8.ts:133
Read the value back as the one UTF-8 byte it spells.
Every u8 is a byte, so this always returns a character — including for values that were never meant as text, and a NUL for 0.
Returns
string
the byte decoded as UTF-8
Example
const result = new CairoUint8('a').decodeUtf8();
// result = "a"
const result2 = new CairoUint8(44).decodeUtf8();
// result2 = "," (44 is the code of a comma)
toHexString()
toHexString():
string
Defined in: src/utils/cairoDataTypes/uint8.ts:148
The value in hexadecimal, without padding.
Returns
string
the number as a 0x-prefixed hex string
Example
const result = new CairoUint8(44).toHexString();
// result = "0x2c"
const result2 = new CairoUint8(0).toHexString();
// result2 = "0x0" (one digit, not "0x00")