encoding

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package encoding provides Solana wire-format serialization primitives.

bincode.go implements the little-endian binary codec used for Solana transaction and message encoding, including Solana's compact-u16 (shortvec) length-prefix format.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidShortvec = errors.New("solana/encoding: invalid shortvec")

ErrInvalidShortvec is returned when a compact-u16 encoding is structurally invalid: either the third byte sets the continuation bit (attempting a 4-byte encoding) or its value would overflow the destination uint16. The canonical Solana shortvec is 1-3 bytes and the third byte must be in the range 0x00-0x03.

View Source
var ErrShortBuffer = errors.New("solana/encoding: short buffer")

ErrShortBuffer is returned when a Decoder runs out of bytes mid-value or when a caller asks for a negative read length.

Functions

func BorshDecodeTo

func BorshDecodeTo(data []byte, v any) error

BorshDecodeTo decodes data into v using Borsh prefix conventions.

func DecodeShortvec

func DecodeShortvec(b []byte) (uint16, int, error)

DecodeShortvec reads a compact-u16 from b and returns the decoded value, the number of bytes consumed, and an error. The error is ErrShortBuffer when b is truncated mid-value, and ErrInvalidShortvec when the encoding is structurally invalid (fourth-byte continuation or uint16 overflow).

func DecodeTo

func DecodeTo(data []byte, v any) error

DecodeTo is a one-shot reflection decoder: equivalent to NewDecoder(data).DecodeTo(v). Use this when you have a struct tagged with bin:"..." and just want to parse a buffer into it. For fixed-shape, performance-sensitive decoders prefer the Reader API.

func EncodeShortvec

func EncodeShortvec(b []byte, v uint16) int

EncodeShortvec writes v into b using Solana's compact-u16 (shortvec) encoding and returns the number of bytes written. b must have length at least 3. The encoding is 1-3 bytes: each byte holds 7 data bits in the low bits and a continuation flag in the high bit.

func RegisterDecoder

func RegisterDecoder[T any](fn func(*Decoder, *T) error)

RegisterDecoder associates a hand-written decoder with type T. When Decode encounters a value of type T — directly, as a struct field, slice element, pointer target, or inside a larger containing type — fn is called instead of walking the type reflectively.

Intended use is during package init. RegisterDecoder is safe for concurrent use, but registering a second decoder for the same type replaces the first.

The wrapper handles the unsafe.Pointer → *T conversion so callers write normal typed Go.

Types

type BorshDecoder

type BorshDecoder struct {
	// contains filtered or unexported fields
}

BorshDecoder reads Borsh-encoded data from a caller-owned byte slice.

func NewBorshDecoder

func NewBorshDecoder(b []byte) *BorshDecoder

NewBorshDecoder returns a BorshDecoder that reads from b.

func (*BorshDecoder) Pos

func (d *BorshDecoder) Pos() int

Pos returns the number of bytes consumed so far.

func (*BorshDecoder) ReadBool

func (d *BorshDecoder) ReadBool() (bool, error)

ReadBool reads a Borsh boolean.

func (*BorshDecoder) ReadBytes

func (d *BorshDecoder) ReadBytes() ([]byte, error)

ReadBytes reads a Borsh byte array: a u32 length prefix then that many bytes, returned as a copy.

func (*BorshDecoder) ReadF32

func (d *BorshDecoder) ReadF32() (float32, error)

ReadF32 reads a Borsh f32.

func (*BorshDecoder) ReadF64

func (d *BorshDecoder) ReadF64() (float64, error)

ReadF64 reads a Borsh f64.

func (*BorshDecoder) ReadFixedBytes

func (d *BorshDecoder) ReadFixedBytes(n int) ([]byte, error)

ReadFixedBytes reads exactly n bytes with no length prefix (for fixed-size arrays like public keys and hashes). Returns a copy.

func (*BorshDecoder) ReadI8

func (d *BorshDecoder) ReadI8() (int8, error)

ReadI8 reads a Borsh int8.

func (*BorshDecoder) ReadI16

func (d *BorshDecoder) ReadI16() (int16, error)

ReadI16 reads a Borsh int16 (little-endian).

func (*BorshDecoder) ReadI32

func (d *BorshDecoder) ReadI32() (int32, error)

ReadI32 reads a Borsh int32 (little-endian).

func (*BorshDecoder) ReadI64

func (d *BorshDecoder) ReadI64() (int64, error)

ReadI64 reads a Borsh int64 (little-endian).

func (*BorshDecoder) ReadOption

func (d *BorshDecoder) ReadOption() (some bool, err error)

ReadOption reads a Borsh Option tag byte. Returns (true, nil) when the tag is 0x01 (Some), (false, nil) when it is 0x00 (None). The caller is responsible for reading the inner value when some == true.

func (*BorshDecoder) ReadString

func (d *BorshDecoder) ReadString() (string, error)

ReadString reads a Borsh string (u32 length prefix + UTF-8 bytes).

func (*BorshDecoder) ReadU8

func (d *BorshDecoder) ReadU8() (uint8, error)

ReadU8 reads a Borsh uint8.

func (*BorshDecoder) ReadU16

func (d *BorshDecoder) ReadU16() (uint16, error)

ReadU16 reads a Borsh uint16 (little-endian).

func (*BorshDecoder) ReadU32

func (d *BorshDecoder) ReadU32() (uint32, error)

ReadU32 reads a Borsh uint32 (little-endian).

func (*BorshDecoder) ReadU64

func (d *BorshDecoder) ReadU64() (uint64, error)

ReadU64 reads a Borsh uint64 (little-endian).

func (*BorshDecoder) ReadU128

func (d *BorshDecoder) ReadU128() (lo, hi uint64, err error)

ReadU128 reads a Borsh uint128 as (lo uint64, hi uint64).

func (*BorshDecoder) ReadU128Raw

func (d *BorshDecoder) ReadU128Raw() (U128, error)

ReadU128Raw reads a 16-byte little-endian Borsh u128. The name is suffixed "Raw" to coexist with the existing ReadU128 that returns two separate uint64 words.

func (*BorshDecoder) ReadU256

func (d *BorshDecoder) ReadU256() (U256, error)

ReadU256 reads a 32-byte little-endian U256.

func (*BorshDecoder) Remaining

func (d *BorshDecoder) Remaining() int

Remaining returns the number of bytes left to read.

type BorshEncoder

type BorshEncoder struct {
	// contains filtered or unexported fields
}

BorshEncoder writes Borsh-encoded data into a growable byte buffer. Borsh is a deterministic binary serialization format used by Solana programs for on-chain account state.

func NewBorshEncoder

func NewBorshEncoder(capacity int) *BorshEncoder

NewBorshEncoder returns a BorshEncoder with the given initial capacity.

func (*BorshEncoder) Bytes

func (e *BorshEncoder) Bytes() []byte

Bytes returns the accumulated encoded bytes. The slice aliases the internal buffer and is valid only until the next write.

func (*BorshEncoder) Len

func (e *BorshEncoder) Len() int

Len returns the number of bytes written so far.

func (*BorshEncoder) WriteBool

func (e *BorshEncoder) WriteBool(v bool)

WriteBool writes a Borsh boolean (0x00 or 0x01).

func (*BorshEncoder) WriteBytes

func (e *BorshEncoder) WriteBytes(b []byte)

WriteBytes writes a Borsh byte array: a u32 length prefix followed by the raw bytes.

func (*BorshEncoder) WriteF32

func (e *BorshEncoder) WriteF32(v float32)

WriteF32 writes a Borsh f32 (IEEE 754, little-endian).

func (*BorshEncoder) WriteF64

func (e *BorshEncoder) WriteF64(v float64)

WriteF64 writes a Borsh f64 (IEEE 754, little-endian).

func (*BorshEncoder) WriteFixedBytes

func (e *BorshEncoder) WriteFixedBytes(b []byte)

WriteFixedBytes writes b verbatim with no length prefix (for fixed-size arrays like public keys and hashes).

func (*BorshEncoder) WriteI8

func (e *BorshEncoder) WriteI8(v int8)

WriteI8 writes a Borsh int8.

func (*BorshEncoder) WriteI16

func (e *BorshEncoder) WriteI16(v int16)

WriteI16 writes a Borsh int16 (little-endian).

func (*BorshEncoder) WriteI32

func (e *BorshEncoder) WriteI32(v int32)

WriteI32 writes a Borsh int32 (little-endian).

func (*BorshEncoder) WriteI64

func (e *BorshEncoder) WriteI64(v int64)

WriteI64 writes a Borsh int64 (little-endian).

func (*BorshEncoder) WriteOption

func (e *BorshEncoder) WriteOption(none bool)

WriteOption writes a Borsh Option<T>: 0x00 if none is true, or 0x01 followed by the caller-written payload otherwise. The caller is responsible for writing the inner value after WriteOption(false).

func (*BorshEncoder) WriteString

func (e *BorshEncoder) WriteString(s string)

WriteString writes a Borsh string (UTF-8 bytes with a u32 length prefix).

func (*BorshEncoder) WriteU8

func (e *BorshEncoder) WriteU8(v uint8)

WriteU8 writes a Borsh uint8.

func (*BorshEncoder) WriteU16

func (e *BorshEncoder) WriteU16(v uint16)

WriteU16 writes a Borsh uint16 (little-endian).

func (*BorshEncoder) WriteU32

func (e *BorshEncoder) WriteU32(v uint32)

WriteU32 writes a Borsh uint32 (little-endian).

func (*BorshEncoder) WriteU64

func (e *BorshEncoder) WriteU64(v uint64)

WriteU64 writes a Borsh uint64 (little-endian).

func (*BorshEncoder) WriteU128

func (e *BorshEncoder) WriteU128(lo, hi uint64)

WriteU128 writes a Borsh uint128 as two little-endian uint64 words (lo then hi).

func (*BorshEncoder) WriteU128Raw

func (e *BorshEncoder) WriteU128Raw(u U128)

WriteU128 appends u verbatim to the Borsh stream (Borsh u128 is the same on-wire format as bincode: 16 little-endian bytes).

func (*BorshEncoder) WriteU256

func (e *BorshEncoder) WriteU256(u U256)

WriteU256 appends u verbatim to the Borsh stream. Borsh does not specify a u256 type but this byte layout matches every Solana program in the wild that serialises a 32-byte number (oracle prices, curve params, etc.).

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder is a zero-copy reader for Solana's little-endian wire format. It holds a cursor into a caller-owned byte slice; ReadBytes returns slices aliasing that buffer.

func NewBinDecoder

func NewBinDecoder(data []byte) *Decoder

NewBinDecoder returns a Decoder reading from data. It is an alias for NewDecoder and exists so the gagliardetto/binary idiom

d := encoding.NewBinDecoder(data)
d.DecodeTo(&v)

compiles unchanged against this package.

func NewDecoder

func NewDecoder(b []byte) *Decoder

NewDecoder returns a Decoder that reads from b. The caller retains ownership of b and must not mutate it for the lifetime of the Decoder or of any slice returned by ReadBytes.

func (*Decoder) DecodeFast

func (d *Decoder) DecodeFast(v any) error

DecodeFast is Decode using a cached, compiled per-type plan.

func (*Decoder) DecodeTo

func (d *Decoder) DecodeTo(v any) error

DecodeTo decodes the next value from the stream into v, which must be a non-nil pointer. It is the entry point for reflection-based decoding and is a drop-in replacement for the gagliardetto/binary NewBinDecoder+Decode pattern.

Before reflecting, DecodeTo consults two escape hatches in order:

  1. A hand-written decoder registered via RegisterDecoder for the target type — library-provided fast paths for PublicKey, Transaction, etc.
  2. The Unmarshaler interface — types that implement it are dispatched directly without reflecting into their fields.

For reflected values, the supported kinds are: uint8/16/32/64, int8/16/32/64, bool, [N]byte and [N]T fixed arrays, []byte and []T slices (length-prefixed), string (length-prefixed), pointer (bincode Option: 1-byte tag + optional payload), and struct (recursively, respecting `bin:"..."` field tags).

The default length prefix is Rust's bincode u64. Override on a per-field basis with `bin:"sizePrefix=shortvec"` etc. Top-level slice decoding uses the default; to read a shortvec-prefixed slice at the top level, either wrap it in a struct or call ReadShortvec + a loop manually.

func (*Decoder) PeekUint8

func (d *Decoder) PeekUint8() (uint8, error)

PeekUint8 returns the next byte without advancing the cursor.

func (*Decoder) Pos

func (d *Decoder) Pos() int

Pos returns the number of bytes consumed so far.

func (*Decoder) Position

func (d *Decoder) Position() int

Position returns the number of bytes consumed so far. It is an alias for Pos, using the gagliardetto/binary spelling.

func (*Decoder) ReadBytes

func (d *Decoder) ReadBytes(n int) ([]byte, error)

ReadBytes returns a zero-copy subslice of n bytes aliasing the underlying buffer. The returned slice is valid only as long as the buffer passed to NewDecoder is valid, and its capacity is clamped to n so that a rogue append cannot overwrite neighbouring bytes.

func (*Decoder) ReadInt8

func (d *Decoder) ReadInt8() (int8, error)

ReadInt8 reads a signed int8.

func (*Decoder) ReadInt16

func (d *Decoder) ReadInt16() (int16, error)

ReadInt16 reads a little-endian int16.

func (*Decoder) ReadInt32

func (d *Decoder) ReadInt32() (int32, error)

ReadInt32 reads a little-endian int32.

func (*Decoder) ReadInt64

func (d *Decoder) ReadInt64() (int64, error)

ReadInt64 reads a little-endian int64.

func (*Decoder) ReadShortvec

func (d *Decoder) ReadShortvec() (uint16, error)

ReadShortvec decodes a Solana compact-u16 length prefix and advances the cursor by the number of bytes consumed. On error the cursor is left unchanged.

func (*Decoder) ReadU128

func (d *Decoder) ReadU128() (U128, error)

ReadU128 reads a 16-byte little-endian U128. The bytes are copied, so the returned value is independent of the Decoder's underlying buffer.

func (*Decoder) ReadU256

func (d *Decoder) ReadU256() (U256, error)

ReadU256 reads a 32-byte little-endian U256. See ReadU128 for ownership.

func (*Decoder) ReadUint8

func (d *Decoder) ReadUint8() (uint8, error)

ReadUint8 reads a single byte.

func (*Decoder) ReadUint16

func (d *Decoder) ReadUint16() (uint16, error)

ReadUint16 reads a little-endian uint16.

func (*Decoder) ReadUint32

func (d *Decoder) ReadUint32() (uint32, error)

ReadUint32 reads a little-endian uint32.

func (*Decoder) ReadUint64

func (d *Decoder) ReadUint64() (uint64, error)

ReadUint64 reads a little-endian uint64.

func (*Decoder) Remaining

func (d *Decoder) Remaining() int

Remaining returns the number of bytes left to read.

func (*Decoder) UseBorsh

func (d *Decoder) UseBorsh() *Decoder

UseBorsh switches the bincode Decoder into Borsh mode (u32 length prefix).

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

Encoder is an append-based writer for Solana's little-endian wire format. It holds a growable byte buffer and never reflects on its arguments.

func New

func New() *Encoder

New returns an Encoder with a 128-byte default capacity — large enough that essentially every Solana instruction encodes without a single buffer re-allocation. Use this when you don't care to compute the exact instruction-data size yourself; reach for NewEncoder(N) only when N is much larger than 128 (e.g. a variable-length address list).

func NewEncoder

func NewEncoder(capacity int) *Encoder

NewEncoder returns an Encoder whose internal buffer starts with the given initial capacity. Passing 0 is legal — the buffer grows on first write — but providing an estimate avoids the small re-alloc cost.

func Wrap

func Wrap(b []byte) *Encoder

Wrap returns an Encoder whose buffer is the slice b truncated to zero length. The underlying array is reused, so callers who want to preserve b's contents must not write through the returned Encoder.

func (*Encoder) Bool

func (e *Encoder) Bool(v bool) *Encoder

Bool writes 1 byte: 1 for true, 0 for false (Rust bincode bool encoding).

func (*Encoder) Bytes

func (e *Encoder) Bytes() []byte

Bytes returns the accumulated bytes. The returned slice aliases the internal buffer and is valid only until the next write.

func (*Encoder) Discriminator

func (e *Encoder) Discriminator(d [8]byte) *Encoder

Discriminator is an 8-byte Anchor instruction discriminator. Identical to Raw(d[:]) but the named method documents intent at the call site.

func (*Encoder) I8

func (e *Encoder) I8(v int8) *Encoder

I8 appends a signed int8 and returns e for chaining.

func (*Encoder) I16

func (e *Encoder) I16(v int16) *Encoder

I16 appends a little-endian int16 and returns e for chaining.

func (*Encoder) I32

func (e *Encoder) I32(v int32) *Encoder

I32 appends a little-endian int32 and returns e for chaining.

func (*Encoder) I64

func (e *Encoder) I64(v int64) *Encoder

I64 appends a little-endian int64 and returns e for chaining.

func (*Encoder) Len

func (e *Encoder) Len() int

Len returns the number of bytes written so far.

func (*Encoder) OptBool

func (e *Encoder) OptBool(v *bool) *Encoder

OptBool writes Option<bool>.

func (*Encoder) OptI64

func (e *Encoder) OptI64(v *int64) *Encoder

OptI64 writes Option<i64>.

func (*Encoder) OptRaw

func (e *Encoder) OptRaw(b []byte) *Encoder

OptRaw writes a Rust Option<[N]byte>: 1-byte tag, then b verbatim when non-nil. Pass nil to encode None. The slice length is not length-prefixed: this is intended for fixed-size optional fields such as Option<Pubkey> via OptRaw(pk[:]).

func (*Encoder) OptU8

func (e *Encoder) OptU8(v *uint8) *Encoder

OptU8 writes Option<u8>.

func (*Encoder) OptU32

func (e *Encoder) OptU32(v *uint32) *Encoder

OptU32 writes Option<u32>.

func (*Encoder) OptU64

func (e *Encoder) OptU64(v *uint64) *Encoder

OptU64 writes Option<u64>.

func (*Encoder) OptU128

func (e *Encoder) OptU128(v *U128) *Encoder

OptU128 writes Option<u128>.

func (*Encoder) Raw

func (e *Encoder) Raw(b []byte) *Encoder

Raw appends b verbatim (no length prefix). Use for fixed-size byte arrays such as pubkeys (caller passes pk[:]) or pre-encoded discriminators.

func (*Encoder) Reset

func (e *Encoder) Reset()

Reset truncates the buffer length to zero, keeping the allocated capacity so the Encoder can be reused for a new encoding run.

func (*Encoder) Shortvec

func (e *Encoder) Shortvec(v uint16) *Encoder

Shortvec writes a Solana compact-u16 (1-3 bytes). Used inside transaction and message structures, not in instruction data.

func (*Encoder) Str

func (e *Encoder) Str(s string) *Encoder

Str writes a bincode string: u64 little-endian length, then UTF-8 bytes.

func (*Encoder) U8

func (e *Encoder) U8(v uint8) *Encoder

U8 appends a single byte and returns e for chaining.

func (*Encoder) U16

func (e *Encoder) U16(v uint16) *Encoder

U16 appends a little-endian uint16 and returns e for chaining.

func (*Encoder) U32

func (e *Encoder) U32(v uint32) *Encoder

U32 appends a little-endian uint32 and returns e for chaining.

func (*Encoder) U64

func (e *Encoder) U64(v uint64) *Encoder

U64 appends a little-endian uint64 and returns e for chaining.

func (*Encoder) U128c

func (e *Encoder) U128c(v U128) *Encoder

U128c is the chainable form of WriteU128. (U128 is taken by the type name.)

func (*Encoder) U256c

func (e *Encoder) U256c(v U256) *Encoder

U256c is the chainable form of WriteU256.

func (*Encoder) WriteBytes

func (e *Encoder) WriteBytes(b []byte)

WriteBytes appends b verbatim, without any length prefix.

func (*Encoder) WriteInt8

func (e *Encoder) WriteInt8(v int8)

WriteInt8 appends a signed int8.

func (*Encoder) WriteInt16

func (e *Encoder) WriteInt16(v int16)

WriteInt16 appends a little-endian int16.

func (*Encoder) WriteInt32

func (e *Encoder) WriteInt32(v int32)

WriteInt32 appends a little-endian int32.

func (*Encoder) WriteInt64

func (e *Encoder) WriteInt64(v int64)

WriteInt64 appends a little-endian int64.

func (*Encoder) WriteShortvec

func (e *Encoder) WriteShortvec(v uint16)

WriteShortvec appends a Solana compact-u16 length prefix (1-3 bytes), encoding v using EncodeShortvec.

func (*Encoder) WriteU128

func (e *Encoder) WriteU128(u U128)

WriteU128 appends u verbatim in 16 little-endian bytes.

func (*Encoder) WriteU256

func (e *Encoder) WriteU256(u U256)

WriteU256 appends u verbatim in 32 little-endian bytes.

func (*Encoder) WriteUint8

func (e *Encoder) WriteUint8(v uint8)

WriteUint8 appends a single byte.

func (*Encoder) WriteUint16

func (e *Encoder) WriteUint16(v uint16)

WriteUint16 appends a little-endian uint16.

func (*Encoder) WriteUint32

func (e *Encoder) WriteUint32(v uint32)

WriteUint32 appends a little-endian uint32.

func (*Encoder) WriteUint64

func (e *Encoder) WriteUint64(v uint64)

WriteUint64 appends a little-endian uint64.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader is a sticky-error wrapper around Decoder for chained, fluent decoding of fixed-shape Solana wire data. Each accessor reads one field; the first short-buffer (or invalid-shortvec) error is stored on the Reader and silences subsequent reads — so callers can sequence the happy path without per-call err checks and check Err() once at the end.

r := encoding.NewReader(data)
flag := r.U32()
var pk [32]byte
r.Read(pk[:])
supply := r.U64()
decimals := r.U8()
if err := r.Err(); err != nil {
    return nil, err
}

The underlying Decoder is exposed via Decoder() for callers who need shortvec, position, or the reflective Decode path mid-stream.

func FromDecoder

func FromDecoder(d *Decoder) *Reader

FromDecoder returns a Reader sharing d's position. Useful when you've already advanced d (e.g. read a discriminator) and want to switch to the chained style for the rest.

func NewReader

func NewReader(b []byte) *Reader

NewReader wraps b in a Reader. The bytes are not copied.

func (*Reader) Bool

func (r *Reader) Bool() bool

Bool reads 1 byte and reports whether it is non-zero.

func (*Reader) Bytes

func (r *Reader) Bytes(n int) []byte

Bytes reads exactly n bytes and returns them as a fresh, independent slice (the data is copied out of the underlying buffer). Use Read when you already have a destination slice.

func (*Reader) Bytes32

func (r *Reader) Bytes32() [32]byte

Bytes32 reads exactly 32 bytes and returns them by value. Convenient for fields backed by solana.PublicKey or solana.Hash, which are both [32]byte.

func (*Reader) Bytes64

func (r *Reader) Bytes64() [64]byte

Bytes64 reads exactly 64 bytes and returns them by value. Convenient for fields backed by solana.Signature, which is [64]byte.

func (*Reader) Decoder

func (r *Reader) Decoder() *Decoder

Decoder returns the underlying Decoder. Mutations through it are visible to subsequent Reader calls.

func (*Reader) Done

func (r *Reader) Done() error

Done returns nil if all bytes were consumed exactly. If a sticky read error is pending it returns that. Otherwise, if any unread bytes remain, it returns a *TrailingBytesError reporting how many.

Use this to enforce "no extra data" at the end of a decode. Callers that want to allow trailing data should check Err() instead.

func (*Reader) Err

func (r *Reader) Err() error

Err returns the first error encountered, or nil. Once non-nil, all subsequent reads are no-ops and return zero values.

func (*Reader) I8

func (r *Reader) I8() int8

func (*Reader) I16

func (r *Reader) I16() int16

func (*Reader) I32

func (r *Reader) I32() int32

func (*Reader) I64

func (r *Reader) I64() int64

func (*Reader) Pos

func (r *Reader) Pos() int

Pos reports the current read offset.

func (*Reader) Read

func (r *Reader) Read(out []byte)

Read fills out from the stream. The slice length determines how many bytes are consumed; passing pubkey[:] reads exactly 32 bytes.

func (*Reader) Remaining

func (r *Reader) Remaining() int

Remaining reports the number of unread bytes.

func (*Reader) Shortvec

func (r *Reader) Shortvec() uint16

Shortvec reads a Solana compact-u16. Used for transaction and message length prefixes, not in account state or instruction data.

func (*Reader) Skip

func (r *Reader) Skip(n int)

Skip advances the position by n bytes without copying.

func (*Reader) Str

func (r *Reader) Str() string

Str reads a bincode string: u64 little-endian length, then UTF-8 bytes.

func (*Reader) U8

func (r *Reader) U8() uint8

U8 reads a single byte. Returns 0 if the Reader is in an error state.

func (*Reader) U16

func (r *Reader) U16() uint16

U16 reads a little-endian uint16.

func (*Reader) U32

func (r *Reader) U32() uint32

U32 reads a little-endian uint32.

func (*Reader) U64

func (r *Reader) U64() uint64

U64 reads a little-endian uint64.

func (*Reader) U128

func (r *Reader) U128() U128

U128 reads a 16-byte little-endian unsigned 128-bit integer.

func (*Reader) U256

func (r *Reader) U256() U256

U256 reads a 32-byte little-endian unsigned 256-bit integer.

type TrailingBytesError

type TrailingBytesError struct {
	// Remaining is the number of unread bytes at the end of the buffer.
	Remaining int
}

TrailingBytesError is returned by Reader.Done when the buffer was not fully consumed. Match it via errors.As:

var te *encoding.TrailingBytesError
if errors.As(err, &te) {
    // log te.Remaining, recover, etc.
}

func (*TrailingBytesError) Error

func (e *TrailingBytesError) Error() string

type U128

type U128 [16]byte

U128 is a 128-bit unsigned integer stored in Rust / Solana little-endian byte order: u[0] is the least-significant byte, u[15] the most-significant. This matches the wire representation, so ReadU128 / WriteU128 are a single 16-byte memcpy on little-endian hosts.

func (U128) BigInt

func (u U128) BigInt() *big.Int

BigInt returns u as a new non-negative *big.Int.

func (U128) Hi

func (u U128) Hi() uint64

Hi returns the high 64 bits of u.

func (U128) IsZero

func (u U128) IsZero() bool

IsZero reports whether u == 0. Branch-free on amd64/arm64.

func (U128) Lo

func (u U128) Lo() uint64

Lo returns the low 64 bits of u.

func (U128) MarshalJSON

func (u U128) MarshalJSON() ([]byte, error)

MarshalJSON emits u as a JSON *string* decimal, so JavaScript consumers that lose precision above 2^53 still receive the exact value.

func (U128) MarshalText

func (u U128) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler (decimal string).

func (*U128) SetBigInt

func (u *U128) SetBigInt(x *big.Int) error

SetBigInt sets u from x, which must be non-negative and fit in 128 bits. Returns an error otherwise; u is left unchanged on error.

func (*U128) SetLoHi

func (u *U128) SetLoHi(lo, hi uint64)

SetLoHi sets u from two 64-bit words.

func (*U128) SetUint64

func (u *U128) SetUint64(v uint64)

SetUint64 sets u to v (high 64 bits zero).

func (U128) String

func (u U128) String() string

String renders u as a decimal string, like %d for built-in uints.

func (*U128) UnmarshalJSON

func (u *U128) UnmarshalJSON(b []byte) error

UnmarshalJSON accepts either a JSON string or a JSON number encoding a non-negative decimal U128.

func (*U128) UnmarshalText

func (u *U128) UnmarshalText(b []byte) error

UnmarshalText parses a decimal U128 produced by MarshalText.

type U256

type U256 [32]byte

U256 is a 256-bit unsigned integer in Rust / Solana little-endian byte order (u[0] least-significant, u[31] most-significant). Used by SPL programs that expose u256 fields (oracle prices, curve parameters, some concentrated-liquidity math) and by Ethereum-style bridged state.

func (U256) BigInt

func (u U256) BigInt() *big.Int

BigInt returns u as a new non-negative *big.Int.

func (U256) IsZero

func (u U256) IsZero() bool

IsZero reports whether u == 0.

func (U256) MarshalJSON

func (u U256) MarshalJSON() ([]byte, error)

MarshalJSON emits u as a JSON string decimal. See U128.MarshalJSON.

func (U256) MarshalText

func (u U256) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler (decimal string).

func (*U256) SetBigInt

func (u *U256) SetBigInt(x *big.Int) error

SetBigInt sets u from x, which must be non-negative and fit in 256 bits. Returns an error otherwise; u is left unchanged on error.

func (*U256) SetWord

func (u *U256) SetWord(i int, v uint64)

SetWord sets the i-th 64-bit word of u. Panics if i is out of range.

func (U256) String

func (u U256) String() string

String renders u as a decimal string.

func (*U256) UnmarshalJSON

func (u *U256) UnmarshalJSON(b []byte) error

UnmarshalJSON accepts either a JSON string or a JSON number encoding a non-negative decimal U256.

func (*U256) UnmarshalText

func (u *U256) UnmarshalText(b []byte) error

UnmarshalText parses a decimal U256 produced by MarshalText.

func (U256) Word

func (u U256) Word(i int) uint64

Word returns the i-th 64-bit word of u (i = 0 is least significant, i = 3 most significant). Panics if i is out of range.

type Unmarshaler

type Unmarshaler interface {
	UnmarshalFromDecoder(d *Decoder) error
}

Unmarshaler lets a type plug in hand-written decoding logic and bypass reflection entirely. Types whose addressable value satisfies this interface are dispatched directly by Decode; reflection never descends into them.

Jump to

Keyboard shortcuts

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