For the complete documentation index, see llms.txt. This page is also available as Markdown.

fetchSerializeCScriptNum

Calls the pox-5 serialize-c-script-num read-only and returns the minimal little-endian Bitcoin Script number encoding (CScriptNum) of n, without a push opcode.


Usage

import { bytesToHex } from '@stacks/common';
import { fetchSerializeCScriptNum } from '@stacks/bitcoin-staking';

const encoded = await fetchSerializeCScriptNum({ n: 1000, network: 'mainnet' });

bytesToHex(encoded); // 'e803'

Notes

  • Wraps serialize-c-script-num. The result is 0 to 5 bytes: 0 encodes as an empty buffer, and a 0x00 byte is appended when the top byte's high bit is set.

  • n of 2^39 (549,755,813,888) or more makes the contract return ERR_INVALID_UNLOCK_HEIGHT (u52). The SDK then throws an Error whose message starts with serialize-c-script-num returned (err u52), followed by the error's name and description.

  • For the encoding with its push opcode, as it appears in a lockup script, use fetchPushCScriptNum.

  • The SDK applies the same encoding locally when it builds a lockup script. Call this to cross-check that encoding against the deployed contract.

  • Throws an Error if the node returns a non-2xx status (the message starts with Error calling read-only function.) or answers okay: false (the message is the node's cause).


Signature

opts is a NetworkClientParam plus n.


Returns

Promise<Uint8Array>

The CScriptNum bytes, 0 to 5 bytes long.


Parameters

opts.n (required)

  • Type: number | bigint

Non-negative integer to encode, below 2^39. Sent as a Clarity uint.

opts.network (optional)

  • Type: StacksNetworkName | StacksNetwork

The network to query: a name such as 'mainnet' or 'testnet', or a StacksNetwork object. Defaults to 'mainnet'.

opts.client (optional)

  • Type: { baseUrl?: string; fetch?: FetchFn }

Overrides the API base URL or the fetch function, for example to query your own node. Fields you set replace the network's defaults.

Was this helpful?