Skip to main content

Module varint

Module varint 

Source
Expand description

Bitcoin VarInt encoding/decoding

VarInt (Variable Integer) is a compact encoding for integers used throughout Bitcoin’s wire format. It uses 1-9 bytes depending on the value.

Encoding rules:

  • If value < 0xfd: single byte
  • If value <= 0xffff: 0xfd prefix + 2 bytes (little-endian)
  • If value <= 0xffffffff: 0xfe prefix + 4 bytes (little-endian)
  • Otherwise: 0xff prefix + 8 bytes (little-endian)

This must match consensus’s CVarInt implementation exactly.

Enums§

VarIntError
Error type for VarInt encoding/decoding failures

Functions§

decode_varint
Decode a Bitcoin VarInt from bytes
encode_varint
Encode a u64 value as a Bitcoin VarInt