BSV.VarInt (bsv_sdk v1.5.0)

Copy Markdown View Source

Bitcoin variable-length integer encoding/decoding.

Summary

Functions

Decode a Bitcoin VarInt from binary, returning value and remaining bytes.

Encode an integer as a Bitcoin VarInt.

Functions

decode(data, max \\ nil)

@spec decode(binary(), non_neg_integer() | nil) ::
  {:ok, {non_neg_integer(), binary()}} | {:error, String.t()}

Decode a Bitcoin VarInt from binary, returning value and remaining bytes.

Optionally pass max to reject values above a threshold (prevents resource exhaustion when VarInt controls allocation sizes).

encode(n)

@spec encode(non_neg_integer()) :: binary()

Encode an integer as a Bitcoin VarInt.