Creates a new instance of the class with a copy of the provided data buffer. This prevents outside mutation of the buffer.
The data to be copied into the internal buffer as a Uint8Array.
Helper function that primarily exists to support alternative syntax for deserialization.
That is, if we have a const deserializer: new Deserializer(...)
, instead of having to use
MyClass.deserialize(deserializer)
, we can call deserializer.deserialize(MyClass)
.
The BCS-deserializable class to deserialize the buffered bytes into.
the deserialized value of class type T
Deserializes a boolean value from a byte stream.
The BCS layout for a boolean uses one byte, where "0x01" represents true and "0x00" represents false. An error is thrown if the byte value is not valid.
The deserialized boolean value.
Deserializes an array of bytes.
The BCS layout for "bytes" consists of a bytes_length followed by the bytes themselves, where bytes_length is a u32 integer encoded as a uleb128 integer, indicating the length of the bytes array.
The deserialized array of bytes.
Deserializes an array of bytes of a specified length.
The number of bytes to read from the source.
Deserializes an optional value from the buffer.
The BCS layout for Optional
Either a Deserializable class or one of the string literals: "string", "bytes", or "fixedBytes"
The deserialized value if present, undefined otherwise
// Deserialize an optional string
const deserializer = new Deserializer(new Uint8Array([1, 3, 97, 98, 99]));
const optStr = deserializer.deserializeOption("string");
// optStr === "abc"
// Deserialize an optional custom type
const deserializer = new Deserializer(new Uint8Array([0]));
const optValue = deserializer.deserializeOption(MyClass);
// optValue === undefined
// Deserialize optional bytes
const deserializer = new Deserializer(new Uint8Array([1, 3, 1, 2, 3]));
const optBytes = deserializer.deserializeOption("bytes");
// optBytes === Uint8Array[1, 2, 3]
// Deserialize optional fixed bytes
const deserializer = new Deserializer(new Uint8Array([1, 1, 2, 3, 4]));
const optBytes = deserializer.deserializeOption("fixedBytes", 4);
// optBytes === Uint8Array[1, 2, 3, 4]
Deserializes an optional value from the buffer.
The BCS layout for Optional
Either a Deserializable class or one of the string literals: "string", "bytes", or "fixedBytes"
The deserialized value if present, undefined otherwise
// Deserialize an optional string
const deserializer = new Deserializer(new Uint8Array([1, 3, 97, 98, 99]));
const optStr = deserializer.deserializeOption("string");
// optStr === "abc"
// Deserialize an optional custom type
const deserializer = new Deserializer(new Uint8Array([0]));
const optValue = deserializer.deserializeOption(MyClass);
// optValue === undefined
// Deserialize optional bytes
const deserializer = new Deserializer(new Uint8Array([1, 3, 1, 2, 3]));
const optBytes = deserializer.deserializeOption("bytes");
// optBytes === Uint8Array[1, 2, 3]
// Deserialize optional fixed bytes
const deserializer = new Deserializer(new Uint8Array([1, 1, 2, 3, 4]));
const optBytes = deserializer.deserializeOption("fixedBytes", 4);
// optBytes === Uint8Array[1, 2, 3, 4]
Deserializes an optional value from the buffer.
The BCS layout for Optional
Either a Deserializable class or one of the string literals: "string", "bytes", or "fixedBytes"
Required length when type is "fixedBytes", ignored otherwise
The deserialized value if present, undefined otherwise
// Deserialize an optional string
const deserializer = new Deserializer(new Uint8Array([1, 3, 97, 98, 99]));
const optStr = deserializer.deserializeOption("string");
// optStr === "abc"
// Deserialize an optional custom type
const deserializer = new Deserializer(new Uint8Array([0]));
const optValue = deserializer.deserializeOption(MyClass);
// optValue === undefined
// Deserialize optional bytes
const deserializer = new Deserializer(new Uint8Array([1, 3, 1, 2, 3]));
const optBytes = deserializer.deserializeOption("bytes");
// optBytes === Uint8Array[1, 2, 3]
// Deserialize optional fixed bytes
const deserializer = new Deserializer(new Uint8Array([1, 1, 2, 3, 4]));
const optBytes = deserializer.deserializeOption("fixedBytes", 4);
// optBytes === Uint8Array[1, 2, 3, 4]
Deserializes an optional value from the buffer.
The BCS layout for Optional
Either a Deserializable class or one of the string literals: "string", "bytes", or "fixedBytes"
The deserialized value if present, undefined otherwise
// Deserialize an optional string
const deserializer = new Deserializer(new Uint8Array([1, 3, 97, 98, 99]));
const optStr = deserializer.deserializeOption("string");
// optStr === "abc"
// Deserialize an optional custom type
const deserializer = new Deserializer(new Uint8Array([0]));
const optValue = deserializer.deserializeOption(MyClass);
// optValue === undefined
// Deserialize optional bytes
const deserializer = new Deserializer(new Uint8Array([1, 3, 1, 2, 3]));
const optBytes = deserializer.deserializeOption("bytes");
// optBytes === Uint8Array[1, 2, 3]
// Deserialize optional fixed bytes
const deserializer = new Deserializer(new Uint8Array([1, 1, 2, 3, 4]));
const optBytes = deserializer.deserializeOption("fixedBytes", 4);
// optBytes === Uint8Array[1, 2, 3, 4]
Deserializes a UTF-8 encoded string from a byte array. It first reads the length of the string in bytes, followed by the actual byte content, and decodes it into a string.
BCS layout for "string": string_length | string_content where string_length is a u32 integer encoded as a uleb128 integer, equal to the number of bytes in string_content.
Deserializes a uint128 number from its binary representation. This function combines two 64-bit values to return a single uint128 value in little-endian format.
The deserialized uint128 number.
Deserializes a uint256 number from its binary representation.
The BCS layout for "uint256" consists of thirty-two bytes in little-endian format.
The deserialized uint256 number.
Deserializes a uint64 number.
This function combines two 32-bit values to return a 64-bit unsigned integer in little-endian representation.
Deserializes a uint8 number from the binary data.
BCS layout for "uint8": One byte. Binary format in little-endian representation.
The deserialized uint8 number.
Deserializes an array of BCS Deserializable values given an existing Deserializer instance with a loaded byte buffer.
The BCS-deserializable class to deserialize the buffered bytes into.
An array of deserialized values of type T.
// serialize a vector of addresses
const addresses = new Array<AccountAddress>(
AccountAddress.from("0x1"),
AccountAddress.from("0x2"),
AccountAddress.from("0xa"),
AccountAddress.from("0xb"),
);
const serializer = new Serializer();
serializer.serializeVector(addresses);
const serializedBytes = serializer.toUint8Array();
// deserialize the bytes into an array of addresses
const deserializer = new Deserializer(serializedBytes);
const deserializedAddresses = deserializer.deserializeVector(AccountAddress);
// deserializedAddresses is now an array of AccountAddress instances
Returns the number of bytes remaining in the buffer.
This information is useful to determine if there's more data to be read.
The number of bytes remaining in the buffer.
Static
from
A class that provides methods for deserializing various data types from a byte buffer. It supports deserialization of primitive types, strings, and complex objects using a BCS (Binary Common Serialization) layout.