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 ¶
- Variables
- func BorshDecodeTo(data []byte, v any) error
- func DecodeShortvec(b []byte) (uint16, int, error)
- func DecodeTo(data []byte, v any) error
- func EncodeShortvec(b []byte, v uint16) int
- func RegisterDecoder[T any](fn func(*Decoder, *T) error)
- func ReleaseDecoder(d *Decoder)
- func ReleaseEncoder(e *Encoder)
- type BorshDecoder
- func (d *BorshDecoder) Pos() int
- func (d *BorshDecoder) ReadBool() (bool, error)
- func (d *BorshDecoder) ReadBytes() ([]byte, error)
- func (d *BorshDecoder) ReadF32() (float32, error)
- func (d *BorshDecoder) ReadF64() (float64, error)
- func (d *BorshDecoder) ReadFixedBytes(n int) ([]byte, error)
- func (d *BorshDecoder) ReadI8() (int8, error)
- func (d *BorshDecoder) ReadI16() (int16, error)
- func (d *BorshDecoder) ReadI32() (int32, error)
- func (d *BorshDecoder) ReadI64() (int64, error)
- func (d *BorshDecoder) ReadOption() (some bool, err error)
- func (d *BorshDecoder) ReadString() (string, error)
- func (d *BorshDecoder) ReadU8() (uint8, error)
- func (d *BorshDecoder) ReadU16() (uint16, error)
- func (d *BorshDecoder) ReadU32() (uint32, error)
- func (d *BorshDecoder) ReadU64() (uint64, error)
- func (d *BorshDecoder) ReadU128() (lo, hi uint64, err error)
- func (d *BorshDecoder) ReadU128Raw() (U128, error)
- func (d *BorshDecoder) ReadU256() (U256, error)
- func (d *BorshDecoder) Remaining() int
- type BorshEncoder
- func (e *BorshEncoder) Bytes() []byte
- func (e *BorshEncoder) Len() int
- func (e *BorshEncoder) WriteBool(v bool)
- func (e *BorshEncoder) WriteBytes(b []byte)
- func (e *BorshEncoder) WriteF32(v float32)
- func (e *BorshEncoder) WriteF64(v float64)
- func (e *BorshEncoder) WriteFixedBytes(b []byte)
- func (e *BorshEncoder) WriteI8(v int8)
- func (e *BorshEncoder) WriteI16(v int16)
- func (e *BorshEncoder) WriteI32(v int32)
- func (e *BorshEncoder) WriteI64(v int64)
- func (e *BorshEncoder) WriteOption(none bool)
- func (e *BorshEncoder) WriteString(s string)
- func (e *BorshEncoder) WriteU8(v uint8)
- func (e *BorshEncoder) WriteU16(v uint16)
- func (e *BorshEncoder) WriteU32(v uint32)
- func (e *BorshEncoder) WriteU64(v uint64)
- func (e *BorshEncoder) WriteU128(lo, hi uint64)
- func (e *BorshEncoder) WriteU128Raw(u U128)
- func (e *BorshEncoder) WriteU256(u U256)
- type Decoder
- func (d *Decoder) DecodeFast(v any) error
- func (d *Decoder) DecodeTo(v any) error
- func (d *Decoder) PeekUint8() (uint8, error)
- func (d *Decoder) Pos() int
- func (d *Decoder) Position() int
- func (d *Decoder) ReadBytes(n int) ([]byte, error)
- func (d *Decoder) ReadInt8() (int8, error)
- func (d *Decoder) ReadInt16() (int16, error)
- func (d *Decoder) ReadInt32() (int32, error)
- func (d *Decoder) ReadInt64() (int64, error)
- func (d *Decoder) ReadShortvec() (uint16, error)
- func (d *Decoder) ReadU128() (U128, error)
- func (d *Decoder) ReadU256() (U256, error)
- func (d *Decoder) ReadUint8() (uint8, error)
- func (d *Decoder) ReadUint16() (uint16, error)
- func (d *Decoder) ReadUint32() (uint32, error)
- func (d *Decoder) ReadUint64() (uint64, error)
- func (d *Decoder) Remaining() int
- func (d *Decoder) UseBorsh() *Decoder
- type Encoder
- func (e *Encoder) Bool(v bool) *Encoder
- func (e *Encoder) Bytes() []byte
- func (e *Encoder) Discriminator(d [8]byte) *Encoder
- func (e *Encoder) I8(v int8) *Encoder
- func (e *Encoder) I16(v int16) *Encoder
- func (e *Encoder) I32(v int32) *Encoder
- func (e *Encoder) I64(v int64) *Encoder
- func (e *Encoder) Len() int
- func (e *Encoder) OptBool(v *bool) *Encoder
- func (e *Encoder) OptI64(v *int64) *Encoder
- func (e *Encoder) OptRaw(b []byte) *Encoder
- func (e *Encoder) OptU8(v *uint8) *Encoder
- func (e *Encoder) OptU32(v *uint32) *Encoder
- func (e *Encoder) OptU64(v *uint64) *Encoder
- func (e *Encoder) OptU128(v *U128) *Encoder
- func (e *Encoder) Raw(b []byte) *Encoder
- func (e *Encoder) Reset()
- func (e *Encoder) Shortvec(v uint16) *Encoder
- func (e *Encoder) Str(s string) *Encoder
- func (e *Encoder) U8(v uint8) *Encoder
- func (e *Encoder) U16(v uint16) *Encoder
- func (e *Encoder) U32(v uint32) *Encoder
- func (e *Encoder) U64(v uint64) *Encoder
- func (e *Encoder) U128c(v U128) *Encoder
- func (e *Encoder) U256c(v U256) *Encoder
- func (e *Encoder) WriteBytes(b []byte)
- func (e *Encoder) WriteInt8(v int8)
- func (e *Encoder) WriteInt16(v int16)
- func (e *Encoder) WriteInt32(v int32)
- func (e *Encoder) WriteInt64(v int64)
- func (e *Encoder) WriteShortvec(v uint16)
- func (e *Encoder) WriteU128(u U128)
- func (e *Encoder) WriteU256(u U256)
- func (e *Encoder) WriteUint8(v uint8)
- func (e *Encoder) WriteUint16(v uint16)
- func (e *Encoder) WriteUint32(v uint32)
- func (e *Encoder) WriteUint64(v uint64)
- type Reader
- func (r *Reader) Bool() bool
- func (r *Reader) Bytes(n int) []byte
- func (r *Reader) Bytes32() [32]byte
- func (r *Reader) Bytes64() [64]byte
- func (r *Reader) Decoder() *Decoder
- func (r *Reader) Done() error
- func (r *Reader) Err() error
- func (r *Reader) I8() int8
- func (r *Reader) I16() int16
- func (r *Reader) I32() int32
- func (r *Reader) I64() int64
- func (r *Reader) Pos() int
- func (r *Reader) Read(out []byte)
- func (r *Reader) Remaining() int
- func (r *Reader) Shortvec() uint16
- func (r *Reader) Skip(n int)
- func (r *Reader) Str() string
- func (r *Reader) U8() uint8
- func (r *Reader) U16() uint16
- func (r *Reader) U32() uint32
- func (r *Reader) U64() uint64
- func (r *Reader) U128() U128
- func (r *Reader) U256() U256
- type TrailingBytesError
- type U128
- func (u U128) BigInt() *big.Int
- func (u U128) Hi() uint64
- func (u U128) IsZero() bool
- func (u U128) Lo() uint64
- func (u U128) MarshalJSON() ([]byte, error)
- func (u U128) MarshalText() ([]byte, error)
- func (u *U128) SetBigInt(x *big.Int) error
- func (u *U128) SetLoHi(lo, hi uint64)
- func (u *U128) SetUint64(v uint64)
- func (u U128) String() string
- func (u *U128) UnmarshalJSON(b []byte) error
- func (u *U128) UnmarshalText(b []byte) error
- type U256
- func (u U256) BigInt() *big.Int
- func (u U256) IsZero() bool
- func (u U256) MarshalJSON() ([]byte, error)
- func (u U256) MarshalText() ([]byte, error)
- func (u *U256) SetBigInt(x *big.Int) error
- func (u *U256) SetWord(i int, v uint64)
- func (u U256) String() string
- func (u *U256) UnmarshalJSON(b []byte) error
- func (u *U256) UnmarshalText(b []byte) error
- func (u U256) Word(i int) uint64
- type Unmarshaler
Constants ¶
This section is empty.
Variables ¶
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.
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 ¶
BorshDecodeTo is the Borsh-flavoured one-shot reflection decoder: equivalent to NewDecoder(data).UseBorsh().DecodeTo(v). It flips the default Vec/string length prefix from bincode's u64 to Borsh's u32. Use this for Anchor-generated account state and other Borsh-encoded structs.
Anchor's 8-byte account discriminator must be stripped (or modeled as the first [8]byte field) before calling. Borsh enums are 1-byte — model them as a Go uint8 with one explicit case per variant.
func DecodeShortvec ¶
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 ¶
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 ¶
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 ¶
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.
func ReleaseDecoder ¶
func ReleaseDecoder(d *Decoder)
ReleaseDecoder returns d to the pool. The caller must not use d after this.
func ReleaseEncoder ¶
func ReleaseEncoder(e *Encoder)
ReleaseEncoder returns e to the pool. The caller must not use e after this.
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) 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 AcquireDecoder ¶
AcquireDecoder returns a pooled Decoder reset to read from b. The caller must call ReleaseDecoder when done — do not use the Decoder after that call.
Note: ReadBytes returns zero-copy slices into b. Those slices remain valid after ReleaseDecoder because the pool only reuses the Decoder struct, not the underlying data buffer.
func NewBinDecoder ¶
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 ¶
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 ¶
DecodeFast is the high-performance drop-in for Decode. It compiles a plan for v's element type on first use, caches it globally, and executes the plan with pointer arithmetic so the per-value cost is effectively one opcode dispatch per leaf field.
Behaviour is equivalent to Decode for all supported types; the only visible difference is that error strings may reach a leaf with a different path formatter. For types that decode through a registered hand-written decoder or an Unmarshaler implementation, the two entry points are byte-for-byte identical because they dispatch to the same function.
Decode itself now delegates here, so calling either name is equivalent; DecodeFast is retained as the explicit name for the plan-cached path and for benchmarks that want to bypass the dispatch shim.
func (*Decoder) DecodeTo ¶
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:
- A hand-written decoder registered via RegisterDecoder for the target type — library-provided fast paths for PublicKey, Transaction, etc.
- 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) Position ¶
Position returns the number of bytes consumed so far. It is an alias for Pos, using the gagliardetto/binary spelling.
func (*Decoder) ReadBytes ¶
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) ReadShortvec ¶
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 ¶
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) ReadUint16 ¶
ReadUint16 reads a little-endian uint16.
func (*Decoder) ReadUint32 ¶
ReadUint32 reads a little-endian uint32.
func (*Decoder) ReadUint64 ¶
ReadUint64 reads a little-endian uint64.
func (*Decoder) UseBorsh ¶
UseBorsh switches the decoder into Borsh mode: untagged Vec<T> / string fields use a u32 length prefix instead of the bincode u64 default. Returns d for chaining.
Per-field `bin:"sizePrefix=..."` tags still take precedence; UseBorsh only changes the implied default. Other Borsh / bincode differences (1-byte vs 4-byte enum discriminator) are not auto-translated — model enums as uint8 (Borsh) or uint32 (bincode) on the Go side.
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 AcquireEncoder ¶
AcquireEncoder returns a pooled Encoder reset to zero length. initialCap is a capacity hint; the pooled buffer may already be larger. The caller must call ReleaseEncoder when done — do not use the Encoder after calling ReleaseEncoder.
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 ¶
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 ¶
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) Bytes ¶
Bytes returns the accumulated bytes. The returned slice aliases the internal buffer and is valid only until the next write.
func (*Encoder) Discriminator ¶
Discriminator is an 8-byte Anchor instruction discriminator. Identical to Raw(d[:]) but the named method documents intent at the call site.
func (*Encoder) OptRaw ¶
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) Raw ¶
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 ¶
Shortvec writes a Solana compact-u16 (1-3 bytes). Used inside transaction and message structures, not in instruction data.
func (*Encoder) WriteBytes ¶
WriteBytes appends b verbatim, without any length prefix.
func (*Encoder) WriteInt16 ¶
WriteInt16 appends a little-endian int16.
func (*Encoder) WriteInt32 ¶
WriteInt32 appends a little-endian int32.
func (*Encoder) WriteInt64 ¶
WriteInt64 appends a little-endian int64.
func (*Encoder) WriteShortvec ¶
WriteShortvec appends a Solana compact-u16 length prefix (1-3 bytes), encoding v using EncodeShortvec.
func (*Encoder) WriteUint8 ¶
WriteUint8 appends a single byte.
func (*Encoder) WriteUint16 ¶
WriteUint16 appends a little-endian uint16.
func (*Encoder) WriteUint32 ¶
WriteUint32 appends a little-endian uint32.
func (*Encoder) WriteUint64 ¶
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 ¶
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 (*Reader) Bytes ¶
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 ¶
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 ¶
Bytes64 reads exactly 64 bytes and returns them by value. Convenient for fields backed by solana.Signature, which is [64]byte.
func (*Reader) Decoder ¶
Decoder returns the underlying Decoder. Mutations through it are visible to subsequent Reader calls.
func (*Reader) Done ¶
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 ¶
Err returns the first error encountered, or nil. Once non-nil, all subsequent reads are no-ops and return zero values.
func (*Reader) Read ¶
Read fills out from the stream. The slice length determines how many bytes are consumed; passing pubkey[:] reads exactly 32 bytes.
func (*Reader) Shortvec ¶
Shortvec reads a Solana compact-u16. Used for transaction and message length prefixes, not in account state or instruction data.
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) MarshalJSON ¶
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 ¶
MarshalText implements encoding.TextMarshaler (decimal string).
func (*U128) SetBigInt ¶
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) UnmarshalJSON ¶
UnmarshalJSON accepts either a JSON string or a JSON number encoding a non-negative decimal U128.
func (*U128) UnmarshalText ¶
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) MarshalJSON ¶
MarshalJSON emits u as a JSON string decimal. See U128.MarshalJSON.
func (U256) MarshalText ¶
MarshalText implements encoding.TextMarshaler (decimal string).
func (*U256) SetBigInt ¶
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) UnmarshalJSON ¶
UnmarshalJSON accepts either a JSON string or a JSON number encoding a non-negative decimal U256.
func (*U256) UnmarshalText ¶
UnmarshalText parses a decimal U256 produced by MarshalText.
type Unmarshaler ¶
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.