binarycodec

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2026 License: MIT Imports: 12 Imported by: 3

README

Binary Codec

This package contains functions to encode/decode to/from the ripple binary serialization format.

Overview

XRPL nodes communicate using a compact binary format rather than JSON. Before a transaction can be signed or submitted, it must be serialized into this format. The binarycodec package handles that transformation in both directions.

Encoding converts a JSON transaction object into a canonical binary blob:

  • Fields are sorted by a protocol-defined ordinal (type code + field code).
  • Each field is prefixed with a compact header identifying its type and position.
  • Variable-length fields (blobs, account IDs) are preceded by a length prefix.
  • The resulting hex string is what gets signed and submitted to the network.

Decoding reverses the process, turning a hex-encoded binary blob back into a JSON object.

There are several encoding variants depending on the use case:

Function Use case
Encode Produce the full transaction blob for submission
Decode Parse a binary blob back to JSON
EncodeForSigning Produce the payload that the private key signs (excludes TxnSignature)
EncodeForMultisigning Like EncodeForSigning but appends the signing account ID
EncodeForSigningClaim For payment channel claims
EncodeQuality / DecodeQuality Encode offer quality (exchange rate) values
DecodeLedgerData Parse raw ledger state data

Package Structure

The codec is split into three sub-packages, each with a distinct responsibility:

definitions/

The schema registry for the entire codec. At startup it embeds and parses definitions.json — the authoritative XRPL protocol document — into a singleton Definitions struct. Everything the serializer and parser need to know about a field lives here:

  • Types — maps type names (e.g. "UInt32", "Amount") to their numeric type codes.
  • Fields — maps field names (e.g. "Fee", "Destination") to a FieldInstance, which contains:
    • FieldHeader (TypeCode + FieldCode): the binary identity of the field written into the encoded stream.
    • Ordinal (TypeCode<<16 | FieldCode): used to sort fields into canonical order before encoding.
    • IsVLEncoded: whether the field value is preceded by a variable-length prefix.
    • IsSerialized: whether the field is included in the encoded blob at all.
    • IsSigningField: whether the field is included when computing the signing payload (EncodeForSigning). Fields like TxnSignature are excluded here.
  • TransactionTypes, TransactionResults, LedgerEntryTypes — numeric code mappings for each category.
  • DelegatablePermissions / GranularPermissions — permission value mappings used for account delegation features.
serdes/

Contains the low-level binary read/write primitives:

  • BinarySerializer — accumulates bytes into a sink. For each field it writes the field header (via FieldIDCodec), an optional variable-length prefix (for VL-encoded fields), and then the raw value bytes. Appends 0xE1 as the STObject end marker when needed.
  • BinaryParser — the inverse: reads a hex-encoded stream, decodes field headers back to field names, and hands off to the appropriate type deserializer.
  • FieldIDCodec — encodes/decodes the compact field header bytes that prefix every field in the binary format.
types/

One file per XRPL serialization type. Each type implements the SerializedType interface:

type SerializedType interface {
    FromJSON(json any) ([]byte, error)
    ToJSON(parser BinaryParser, opts ...int) (any, error)
}
Type Description
UInt8, UInt16, UInt32, UInt64 Fixed-width unsigned integers
Int32 Fixed-width signed integer
Hash128, Hash160, Hash192, Hash256 Fixed-length byte arrays for hashes and addresses
AccountID 20-byte account address (Base58Check encoded in JSON, raw bytes in binary)
Amount XRP drops (64-bit) or issued currency amounts (special 64-bit float encoding)
Blob Variable-length byte array (VL-encoded)
Currency 160-bit currency representation
Issue Currency + issuer pair
STObject Nested object; fields sorted by ordinal, terminated with 0xE1
STArray Array of STObject entries, terminated with 0xF1
PathSet Payment path data
Vector256 Array of Hash256 values
XChainBridge Cross-chain bridge descriptor
Number Arbitrary-precision number (STNumber)

GetSerializedType(typeName string) acts as the factory, returning the right implementation for a given type name from the definitions.

API

Encode
encoded, err := binarycodec.Encode(jsonObject)
Decode
json, err := binarycodec.Decode(hexEncodedString)
EncodeForMultisigning
encoded, err := binarycodec.EncodeForMultisigning(jsonObject, xrpAccountID)
EncodeForSigning
encoded, err := binarycodec.EncodeForSigning(jsonObject)
EncodeForSigningClaim
encoded, err := binarycodec.EncodeForSigningClaim(jsonObject)
EncodeQuality
encoded, err := binarycodec.EncodeQuality(amountString)
DecodeQuality
decoded, err := binarycodec.DecodeQuality(encoded)
DecodeLedgerData
ledgerData, err := binarycodec.DecodeLedgerData(hexEncodedString)

Documentation

Overview

Package binarycodec provides encoding and decoding functionality for XRPL binary format.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrSigningClaimFieldNotFound is returned when the 'Channel' & 'Amount' fields are both required, but were not found.
	ErrSigningClaimFieldNotFound = errors.New("'Channel' & 'Amount' fields are both required, but were not found")
	// ErrBatchFlagsFieldNotFound is returned when the 'flags' field is missing.
	ErrBatchFlagsFieldNotFound = errors.New("no field `flags`")
	// ErrBatchTxIDsFieldNotFound is returned when the 'txIDs' field is missing.
	ErrBatchTxIDsFieldNotFound = errors.New("no field `txIDs`")
	// ErrBatchTxIDsNotArray is returned when the 'txIDs' field is not an array.
	ErrBatchTxIDsNotArray = errors.New("txIDs field must be an array")
	// ErrBatchTxIDNotString is returned when a txID is not a string.
	ErrBatchTxIDNotString = errors.New("each txID must be a string")
	// ErrBatchFlagsNotUInt32 is returned when the 'flags' field is not a uint32.
	ErrBatchFlagsNotUInt32 = errors.New("flags field must be a uint32")
	// ErrBatchTxIDsLengthTooLong is returned when the 'txIDs' field is too long.
	ErrBatchTxIDsLengthTooLong = errors.New("txIDs length exceeds maximum uint32 value")
)
View Source
var ErrInvalidQuality = errors.New("invalid quality")

ErrInvalidQuality is returned when the quality is invalid.

Functions

func Decode

func Decode(hexEncoded string) (map[string]any, error)

Decode decodes a hex string in the canonical binary format into a JSON transaction object.

func DecodeQuality

func DecodeQuality(quality string) (string, error)

DecodeQuality decodes a quality amount from a hex string to a string.

func Encode

func Encode(json map[string]any) (string, error)

Encode converts a JSON transaction object to a hex string in the canonical binary format. The binary format is defined in XRPL's core codebase.

func EncodeForMultisigning

func EncodeForMultisigning(json map[string]any, xrpAccountID string) (string, error)

EncodeForMultisigning encodes a transaction into binary format in preparation for providing one signature towards a multi-signed transaction. Only encodes fields that are intended to be signed. NOTE: The caller is responsible for setting SigningPubKey to "" for regular multisigning. For counterparty signing (e.g. LoanSet), SigningPubKey must remain set to the first signer's public key, so this function must not overwrite it.

func EncodeForSigning

func EncodeForSigning(json map[string]any) (string, error)

EncodeForSigning encodes a transaction into binary format in preparation for signing.

func EncodeForSigningBatch added in v0.1.11

func EncodeForSigningBatch(json map[string]any) (string, error)

EncodeForSigningBatch encodes a batch transaction into binary format in preparation for signing.

func EncodeForSigningClaim

func EncodeForSigningClaim(json map[string]any) (string, error)

EncodeForSigningClaim encodes a payment channel claim into binary format in preparation for signing.

func EncodeQuality

func EncodeQuality(quality string) (string, error)

EncodeQuality encodes a quality amount to a hex string.

Types

type LedgerData

type LedgerData struct {
	LedgerIndex         uint32
	TotalCoins          string
	ParentHash          string
	TransactionHash     string
	AccountHash         string
	ParentCloseTime     uint32
	CloseTime           uint32
	CloseTimeResolution uint8
	CloseFlags          uint8
}

LedgerData represents the data of a ledger.

func DecodeLedgerData

func DecodeLedgerData(data string) (LedgerData, error)

DecodeLedgerData decodes a hex string in the canonical binary format into a LedgerData object. The hex string should represent a ledger data object.

Directories

Path Synopsis
Package definitions contains XRPL binary codec field and type definitions.
Package definitions contains XRPL binary codec field and type definitions.
Package serdes provides utilities to parse and serialize XRPL binary data fields.
Package serdes provides utilities to parse and serialize XRPL binary data fields.
interfaces
Package interfaces defines interfaces for binary serialization and deserialization of XRPL fields.
Package interfaces defines interfaces for binary serialization and deserialization of XRPL fields.
testutil
Package testutil is a generated GoMock package.
Package testutil is a generated GoMock package.
Package types contains data structures for binary codec operations.
Package types contains data structures for binary codec operations.
interfaces
Package interfaces defines the BinaryParser interface for binary codec parsing operations.
Package interfaces defines the BinaryParser interface for binary codec parsing operations.
testutil
Package testutil is a generated GoMock package.
Package testutil is a generated GoMock package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL