Constructors

Properties

buffer: ArrayBuffer
offset: number

Methods

  • 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).

    Type Parameters

    • T

    Parameters

    • cls: Deserializable<T>

      The BCS-deserializable class to deserialize the buffered bytes into.

    Returns T

    the deserialized value of class type T

    Example

    const deserializer = new Deserializer(new Uint8Array([1, 2, 3]));
    const value = deserializer.deserialize(MyClass); // where MyClass has a `deserialize` function
    // value is now an instance of MyClass
    // equivalent to `const value = MyClass.deserialize(deserializer)`
  • Deserializes a boolean value.

    BCS layout for "boolean": One byte. "0x01" for true and "0x00" for false.

    Returns boolean

  • Deserializes an array of bytes.

    BCS layout for "bytes": bytes_length | bytes where bytes_length is a u32 integer encoded as a uleb128 integer, equal to the length of the bytes array.

    Returns Uint8Array

  • Deserializes an array of bytes. The number of bytes to read is already known.

    Parameters

    • len: number

    Returns Uint8Array

  • Deserializes a string. UTF8 string is supported. Reads the string's bytes length "l" first, and then reads "l" bytes of content. Decodes the byte array 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.

    Returns string

    Example

    const deserializer = new Deserializer(new Uint8Array([8, 49, 50, 51, 52, 97, 98, 99, 100]));
    assert(deserializer.deserializeStr() === "1234abcd");
  • Deserializes a uint128 number.

    BCS layout for "uint128": Sixteen bytes. Binary format in little-endian representation.

    Returns bigint

  • Deserializes a uint16 number.

    BCS layout for "uint16": Two bytes. Binary format in little-endian representation.

    Returns number

    Example

    const deserializer = new Deserializer(new Uint8Array([0x34, 0x12]));
    assert(deserializer.deserializeU16() === 4660);
  • Deserializes a uint256 number.

    BCS layout for "uint256": Thirty-two bytes. Binary format in little-endian representation.

    Returns bigint

  • Deserializes a uint32 number.

    BCS layout for "uint32": Four bytes. Binary format in little-endian representation.

    Returns number

    Example

    const deserializer = new Deserializer(new Uint8Array([0x78, 0x56, 0x34, 0x12]));
    assert(deserializer.deserializeU32() === 305419896);
  • Deserializes a uint64 number.

    BCS layout for "uint64": Eight bytes. Binary format in little-endian representation.

    Returns bigint

    Example

    const deserializer = new Deserializer(new Uint8Array([0x00, 0xEF, 0xCD, 0xAB, 0x78, 0x56, 0x34, 0x12]));
    assert(deserializer.deserializeU64() === 1311768467750121216);
  • Deserializes a uint8 number.

    BCS layout for "uint8": One byte. Binary format in little-endian representation.

    Returns number

  • Deserializes a uleb128 encoded uint32 number.

    BCS use uleb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values

    Returns number

  • Deserializes an array of BCS Deserializable values given an existing Deserializer instance with a loaded byte buffer.

    Type Parameters

    • T

    Parameters

    • cls: Deserializable<T>

      The BCS-deserializable class to deserialize the buffered bytes into.

    Returns T[]

    an array of deserialized values of type T

    Example

    // 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

Generated using TypeDoc