encodeHeader
Builds an 8-byte Qubic TCP frame header from size, message type, and dejavu nonce.
Signature
encodeHeader(size: number, messageType: number, dejavu: number): Uint8ArrayPurpose
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 8Parameters
| Name | Type | Description |
|---|---|---|
size | number | Total message length (header + payload) |
messageType | number | Message type constant (see MESSAGE_TYPE enum) |
dejavu | number | 32-bit random nonce; echoed in responses to pair request/response |
Returns
Uint8Array — exactly 8 bytes.
Frame format reference
| Field | Size | Description |
|---|---|---|
size | 3 bytes LE | Total message length including the 8-byte header |
type | 1 byte | Message type constant |
dejavu | 4 bytes LE | Random 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()