A class for serializing various data types into a binary format. It provides methods to serialize strings, bytes, numbers, and other serializable objects using the Binary Coded Serialization (BCS) layout. The serialized data can be retrieved as a Uint8Array.

Constructors

  • Constructs a serializer with a buffer of size length bytes, 64 bytes by default. The length must be greater than 0.

    Parameters

    • length: number = 64

      The size of the buffer in bytes.

    Returns Serializer

Methods

  • Serializes a Serializable value, facilitating composable serialization.

    Type Parameters

    Parameters

    • value: T

      The Serializable value to serialize.

    Returns void

    the serializer instance

  • Serializes a boolean value into a byte representation.

    The BCS layout for a boolean uses one byte, where "0x01" represents true and "0x00" represents false.

    Parameters

    • value: boolean

      The boolean value to serialize.

    Returns void

  • Serializes an array of bytes.

    This function encodes the length of the byte array as a u32 integer in uleb128 format, followed by the byte array itself. 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.

    Parameters

    • value: Uint8Array

      The byte array to serialize.

    Returns void

  • Serializes an array of bytes with a known length, allowing for efficient deserialization without needing to serialize the length itself. When deserializing, the number of bytes to deserialize needs to be passed in.

    Parameters

    • value: Uint8Array

      The Uint8Array to be serialized.

    Returns void

  • Serializes a BCS Serializable value into a serializer instance or handles the case when the value is undefined. This function allows you to efficiently add serialized data to the serializer's byte buffer.

    Type Parameters

    Parameters

    • Optionalvalue: T

      The BCS Serializable value to serialize, or undefined if there is no value.

    Returns void

    const serializer = new Serializer();
    serializer.serializeOption(new AccountAddress(...));
    const serializedBytes = serializer.toUint8Array();
    // serializedBytes is now the BCS-serialized byte representation of AccountAddress

    const serializer = new Serializer();
    serializer.serializeOption(undefined);
    assert(serializer.toUint8Array() === new Uint8Array([0x00]));
  • Serializes an optional string, supporting UTF8 encoding. The function encodes the existence of the string first, followed by the length and content if it exists.

    BCS layout for optional "string": 1 | 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. BCS layout for undefined: 0

    Parameters

    • Optionalvalue: string

      The optional string to serialize. If undefined, it will serialize as 0.

    Returns void

  • Serializes a string. UTF8 string is supported. The number of bytes in the string content is serialized first, as a uleb128-encoded u32 integer. Then the string content is serialized as UTF8 encoded bytes.

    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.

    Parameters

    • value: string

      The string to serialize.

    Returns void

    const serializer = new Serializer();
    serializer.serializeStr("1234abcd");
    assert(serializer.toUint8Array() === new Uint8Array([8, 49, 50, 51, 52, 97, 98, 99, 100]));
  • Serializes a U128 value into a format suitable for storage or transmission.

    Parameters

    • value: AnyNumber

      The U128 value to serialize, represented as a number.

    Returns void

  • Serializes a 16-bit unsigned integer value into a binary format. BCS layout for "uint16": Two bytes. Binary format in little-endian representation.

    Parameters

    • value: number

      The 16-bit unsigned integer value to serialize.

    Returns void

    const serializer = new Serializer();
    serializer.serializeU16(4660);
    assert(serializer.toUint8Array() === new Uint8Array([0x34, 0x12]));
  • Serializes a U256 value into a byte representation. This function is essential for encoding large numbers in a compact format suitable for transmission or storage.

    Parameters

    • value: AnyNumber

      The U256 value to serialize, represented as an AnyNumber.

    Returns void

  • Serializes a 32-bit unsigned integer value into a binary format. This function is useful for encoding data that needs to be stored or transmitted in a compact form.

    Parameters

    • value: number

      The 32-bit unsigned integer value to serialize.

    Returns void

    const serializer = new Serializer();
    serializer.serializeU32(305419896);
    assert(serializer.toUint8Array() === new Uint8Array([0x78, 0x56, 0x34, 0x12]));
  • Serializes a 32-bit unsigned integer as a variable-length ULEB128 encoded byte array. BCS uses uleb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values

    Parameters

    • val: number

      The 32-bit unsigned integer value to be serialized.

    Returns void

  • Serializes a 64-bit unsigned integer into a format suitable for storage or transmission. This function breaks down the value into two 32-bit components and writes them in little-endian order.

    Parameters

    • value: AnyNumber

      The 64-bit unsigned integer to serialize, represented as a number.

    Returns void

    const serializer = new Serializer();
    serializer.serializeU64(1311768467750121216);
    assert(serializer.toUint8Array() === new Uint8Array([0x00, 0xEF, 0xCD, 0xAB, 0x78, 0x56, 0x34, 0x12]));
  • Serializes a Uint8 value and appends it to the buffer. BCS layout for "uint8": One byte. Binary format in little-endian representation.

    Parameters

    • value: number

      The Uint8 value to serialize.

    Returns void

  • Serializes an array of BCS Serializable values to a serializer instance. The bytes are added to the serializer instance's byte buffer.

    Type Parameters

    Parameters

    • values: T[]

      The array of BCS Serializable values

    Returns void

    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();
    // serializedBytes is now the BCS-serialized bytes
    // The equivalent value in Move would be:
    // `bcs::to_bytes(&vector<address> [@0x1, @0x2, @0xa, @0xb])`;
  • Returns the buffered bytes as a Uint8Array.

    This function allows you to retrieve the byte representation of the buffer up to the current offset.

    Returns Uint8Array

    Uint8Array - The byte array representation of the buffer.