Class CborWriter

A class for encoding data into the Concise Binary Object Representation (CBOR) format.

This class provides a high-level interface for constructing CBOR-encoded data items. It manages the underlying CBOR writer instance and ensures proper memory management.

Constructors

  • Creates a new instance of the CborWriter class.

    This constructor initializes a new native CBOR writer instance and ensures it is ready for use. Throws an error if the writer instance cannot be created.

    Returns CborWriter

Properties

ptr: number

The internal pointer to the native CBOR writer instance. This pointer is managed internally and should not be modified directly.

Methods

  • Finalizes the CBOR encoding process and retrieves the encoded data.

    This method computes the final CBOR representation of all the data items written to the writer. It allocates and returns a Uint8Array containing the serialized CBOR data.

    Returns Uint8Array

    A Uint8Array containing the CBOR-encoded data.

    Throws an error if the encoding process fails.

  • Finalizes the CBOR encoding process and retrieves the encoded data as a hexadecimal string.

    This method computes the final CBOR representation of all the data items written to the writer and returns the serialized CBOR data as a hexadecimal string.

    Returns string

    A string containing the CBOR-encoded data in hexadecimal format.

    Throws an error if the encoding process fails.

  • Ends the encoding of an indefinite-length CBOR array.

    This method writes the stop code to indicate the end of an indefinite-length CBOR array (major type 4). It must only be called after startArray has been called with -1 as the size.

    Returns CborWriter

    The current CborWriter instance.

    Throws an error if the operation fails.

  • Ends the encoding of an indefinite-length CBOR map.

    This method writes the stop code to indicate the end of an indefinite-length CBOR map (major type 5). It must only be called after startMap has been called with -1 as the size.

    Returns CborWriter

    The current CborWriter instance.

    Throws an error if the operation fails.

  • Retrieves the last error message recorded by the CBOR writer.

    This method provides a human-readable description of the last error encountered during CBOR encoding operations. If no error has occurred, it returns an empty string.

    Returns string

    A string describing the last error message, or empty if no message is available.

  • The reference count retrieved by this method reflects the number of references maintained internally by libcardano-c for this native instance of the CBOR writer object. This is unrelated to the reference counting mechanism in JavaScript, which is managed by the JavaScript engine's garbage collector.

    This method is primarily intended for diagnostic purposes.

    Returns number

    The current reference count of the CBOR writer in the WASM context.

  • Resets the CBOR writer, clearing all written data.

    This method resets the internal state of the CBOR writer, effectively removing any data that has been written. It allows the writer to be reused for a new CBOR encoding session.

    Returns CborWriter

    The current CborWriter instance.

    Throws an error if the reset operation fails.

  • Begins the encoding of a CBOR array.

    This method writes the start of a CBOR array into the CBOR stream. CBOR arrays are encoded as major type 4. The size parameter specifies the number of elements in the array for definite-length arrays. For indefinite-length arrays, pass -1 as the size.

    Parameters

    • Optionalsize: number

      The number of elements in the array. Use -1 for an indefinite-length array.

    Returns CborWriter

    The current CborWriter instance.

    Throws an error if the operation fails.

  • Begins the encoding of a CBOR map.

    This method writes the start of a CBOR map into the CBOR stream. CBOR maps are encoded as major type 5. The size parameter specifies the number of key-value pairs in the map for definite-length maps. For indefinite-length maps, pass -1 as the size.

    Parameters

    • Optionalsize: number

      The number of key-value pairs in the map. Use -1 for an indefinite-length map.

    Returns CborWriter

    The current CborWriter instance.

    Throws an error if the operation fails.

  • Encodes and writes a BigInt value as a CBOR bignum.

    This method writes a big integer value into the CBOR stream using the appropriate CBOR tag for bignum encoding. It supports both positive and negative bignums.

    Parameters

    • value: BigInt

      The BigInt value to encode and write.

    Returns CborWriter

    The current CborWriter instance.

    Throws an error if the encoding fails.

  • Encodes and writes a boolean value as a CBOR simple value.

    This method writes a boolean value (true or false) into the CBOR stream. CBOR encodes boolean values as simple values (major type 7).

    Parameters

    • value: boolean

      The boolean value to encode and write.

    Returns CborWriter

    The current CborWriter instance.

    Throws an error if the encoding operation fails.

  • Encodes and writes a byte string as a CBOR data item.

    This method writes a byte string (Uint8Array) into the CBOR stream. CBOR encodes byte strings as major type 2.

    Parameters

    • data: Uint8Array

      The byte string to encode and write as a Uint8Array.

    Returns CborWriter

    The current CborWriter instance.

    Throws an error if the encoding operation fails.

  • Writes a pre-encoded CBOR data item into the CBOR stream.

    This method directly writes a byte array containing a pre-encoded CBOR data item into the CBOR stream. This is useful for embedding CBOR data items that have already been encoded elsewhere.

    Parameters

    • data: Uint8Array

      The byte array containing the pre-encoded CBOR data item.

    Returns CborWriter

    The current CborWriter instance.

    Throws an error if the operation fails.

  • Encodes and writes an integer as a CBOR data item.

    This method writes a integer value into the CBOR stream. CBOR encodes integers as either major type 0 (positive integers) or major type 1 (negative integers).

    Parameters

    • value: number | bigint

      The integer value to encode and write. This can be a number or bigint.

    Returns CborWriter

    The current CborWriter instance.

    Throws an error if the encoding operation fails or if the value exceeds the range of a 64-bit signed integer.

  • Encodes and writes a null value as a CBOR data item.

    This method writes a null value into the CBOR stream. CBOR encodes null values as a simple value with major type 7 and additional information 22.

    Returns CborWriter

    The current CborWriter instance.

    Throws an error if the encoding operation fails.

  • Encodes and writes a signed integer as a CBOR data item.

    This method writes a signed integer value into the CBOR stream. CBOR encodes signed integers as either major type 0 (for positive integers) or major type 1 (for negative integers).

    Parameters

    • value: number | bigint

      The signed integer value to encode and write. This can be a number or bigint.

    Returns CborWriter

    The current CborWriter instance.

    Throws an error if the encoding operation fails or if the value exceeds the range of a 64-bit signed integer.

  • Encodes and writes a CBOR semantic tag.

    This method writes a semantic tag into the CBOR stream. CBOR tags are encoded as major type 6 and provide additional context or meaning for the data that follows. For example, a tag may indicate a date, a base64-encoded string, or another semantic type.

    Parameters

    • tag: number

      The semantic tag to encode and write, represented as a value of the CborTag enum or a number.

    Returns CborWriter

    The current CborWriter instance.

    Throws an error if the encoding operation fails.

  • Encodes and writes a UTF-8 encoded text string as a CBOR data item.

    This method writes a text string into the CBOR stream. CBOR encodes text strings as major type 3.

    Parameters

    • text: string

      The text string to encode and write.

    Returns CborWriter

    The current CborWriter instance.

    Throws an error if the encoding operation fails.

  • Encodes and writes an undefined value as a CBOR data item.

    This method writes an undefined value into the CBOR stream. CBOR encodes undefined values as a simple value with major type 7 and additional information 23.

    Returns CborWriter

    The current CborWriter instance.

    Throws an error if the encoding operation fails.

  • Encodes and writes an unsigned integer as a CBOR data item.

    This method writes an unsigned integer value into the CBOR stream. CBOR encodes unsigned integers as major type 0.

    Parameters

    • value: number | bigint

      The unsigned integer value to encode and write. This can be a number or bigint.

    Returns CborWriter

    The current CborWriter instance.

    Throws an error if the encoding operation fails or if the value exceeds the range of a 64-bit unsigned integer.