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:0encodes as an empty buffer, and a0x00byte is appended when the top byte's high bit is set.nof 2^39 (549,755,813,888) or more makes the contract returnERR_INVALID_UNLOCK_HEIGHT (u52). The SDK then throws anErrorwhose message starts withserialize-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
Errorif the node returns a non-2xx status (the message starts withError calling read-only function.) or answersokay: false(the message is the node'scause).
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?