QubicTypeScript

encodeHeader

Builds an 8-byte Qubic TCP frame header from size, message type, and dejavu nonce.

Signature

encodeHeader(size: number, messageType: number, dejavu: number): Uint8Array

Purpose

Encodes an 8-byte frame header for a Qubic TCP message. Every Qubic TCP message uses this header format:

[size: 3 bytes LE][type: 1 byte][dejavu: 4 bytes LE][payload: N bytes]
import { encodeHeader, HEADER_SIZE } from "@qubic.org/tcp"

const payload = new Uint8Array(32)
const totalSize = HEADER_SIZE + payload.length

const header = encodeHeader(totalSize, 26, crypto.getRandomValues(new Uint32Array(1))[0])
// Returns Uint8Array of length 8

Parameters

NameTypeDescription
sizenumberTotal message length (header + payload)
messageTypenumberMessage type constant (see MESSAGE_TYPE enum)
dejavunumber32-bit random nonce; echoed in responses to pair request/response

Returns

Uint8Array — exactly 8 bytes.

Frame format reference

FieldSizeDescription
size3 bytes LETotal message length including the 8-byte header
type1 byteMessage type constant
dejavu4 bytes LERandom nonce

Custom request example

import {
  createNodeConnection,
  encodeHeader,
  decodeCurrentTickInfo,
  MESSAGE_TYPE,
  HEADER_SIZE,
} from "@qubic.org/tcp"

const conn = await createNodeConnection("node1.qubic.org")

const dejavu = crypto.getRandomValues(new Uint32Array(1))[0]
const header = encodeHeader(HEADER_SIZE, MESSAGE_TYPE.REQUEST_CURRENT_TICK_INFO, dejavu)

const packets = await conn.request(MESSAGE_TYPE.REQUEST_CURRENT_TICK_INFO)
const info = decodeCurrentTickInfo(packets[0])

console.log("Tick:", info.tick)
conn.close()

On this page