Skip to content

CompactSize

Utility functions for Bitcoin's CompactSize variable-length integer encoding.

Examples

Below are some examples demonstrating common usages of the CompactSize module:

Encoding

import { CompactSize } from 'ox'
 
const bytes = CompactSize.toBytes(65535)

Decoding

import { CompactSize } from 'ox'
 
const { value, size } = CompactSize.fromBytes(new Uint8Array([0xfd, 0xff, 0xff]))

Functions

NameDescription
CompactSize.fromBytesDecodes a CompactSize-encoded value from Bytes.Bytes.
CompactSize.fromHexDecodes a CompactSize-encoded value from Hex.Hex.
CompactSize.toBytesEncodes an integer using Bitcoin's CompactSize variable-length encoding.
CompactSize.toHexEncodes an integer using Bitcoin's CompactSize variable-length encoding and returns it as Hex.Hex.

Errors

NameDescription
CompactSize.InsufficientBytesErrorThrown when there are insufficient bytes to decode a CompactSize value.
CompactSize.NegativeValueErrorThrown when a CompactSize value is negative.