solana_kit_codecs_core library

Core codec interfaces and composition helpers for the Solana Kit Dart SDK.

Use this library when you need foundational Encoder, Decoder, and Codec abstractions plus size, padding, transform, and sentinel helpers.

Compose core codecs

Use solana_kit_codecs_core when you need to adapt, wrap, or combine lower- level encoders and decoders.

import 'dart:typed_data';

import 'package:solana_kit_codecs_core/solana_kit_codecs_core.dart';
import 'package:solana_kit_codecs_numbers/solana_kit_codecs_numbers.dart';

void main() {
  final codec = addCodecSentinel(getU8Codec(), Uint8List.fromList([255]));

  final encoded = codec.encode(42);
  final decoded = codec.decode(encoded);

  print(encoded);
  print(decoded);
}

These helpers are the glue layer between simple primitive codecs and the more specialized Solana-facing structures built on top of them.

Classes

Codec<TFrom, TTo>
An object that can both encode and decode values.
Decoder<T>
An object that can decode a Uint8List into a value of type T.
Encoder<T>
An object that can encode a value of type T into a Uint8List.
FixedSizeCodec<TFrom, TTo>
A fixed-size codec where encoding always produces fixedSize bytes and decoding always consumes fixedSize bytes.
FixedSizeDecoder<T>
A fixed-size decoder that always reads exactly fixedSize bytes.
FixedSizeEncoder<T>
A fixed-size encoder where every encoded value occupies exactly fixedSize bytes.
OffsetConfig
Configuration for modifying the offset of an encoder, decoder, or codec.
PostOffsetScope
Extended scope for post-offset functions, which also includes the new pre-offset and the original post-offset.
PreOffsetScope
Scope provided to pre-offset and post-offset functions.
VariableSizeCodec<TFrom, TTo>
A variable-size codec whose byte size depends on the value being encoded/decoded.
VariableSizeDecoder<T>
A variable-size decoder whose byte consumption depends on the data.
VariableSizeEncoder<T>
A variable-size encoder whose byte length depends on the value being encoded.

Functions

addCodecSentinel<TFrom, TTo>(Codec<TFrom, TTo> codec, Uint8List sentinel) → Codec<TFrom, TTo>
Creates a codec that writes a sentinel byte sequence after the encoded value and, when decoding, reads until the sentinel is found.
addCodecSizePrefix<TFrom, TTo>(Codec<TFrom, TTo> codec, Codec<num, num> prefix) → Codec<TFrom, TTo>
Stores the byte size of codec as an encoded number prefix.
addDecoderSentinel<TTo>(Decoder<TTo> decoder, Uint8List sentinel) → Decoder<TTo>
Creates a decoder that continues reading until a given sentinel byte sequence is found.
addDecoderSizePrefix<TTo>(Decoder<TTo> decoder, Decoder<num> prefix) → Decoder<TTo>
Bounds the size of the nested decoder by reading its encoded prefix.
addEncoderSentinel<TFrom>(Encoder<TFrom> encoder, Uint8List sentinel) → Encoder<TFrom>
Creates an encoder that writes a sentinel byte sequence after the encoded value.
addEncoderSizePrefix<TFrom>(Encoder<TFrom> encoder, Encoder<num> prefix) → Encoder<TFrom>
Stores the size of the encoder in bytes as a prefix using the prefix encoder.
assertByteArrayHasEnoughBytesForCodec(String codecDescription, int expected, Uint8List bytes, [int offset = 0]) → void
Asserts that the byte array has at least expected bytes remaining after offset.
assertByteArrayIsNotEmptyForCodec(String codecDescription, Uint8List bytes, [int offset = 0]) → void
Asserts that the byte array is not empty after the optional offset.
assertByteArrayOffsetIsNotOutOfRange(String codecDescription, int offset, int bytesLength) → void
Asserts that offset is within the valid range [0, bytesLength].
assertIsFixedSize(Object? object) → void
Asserts that the given object is fixed-size.
assertIsVariableSize(Object? object) → void
Asserts that the given object is variable-size.
bytesEqual(Uint8List bytes1, Uint8List bytes2) → bool
Returns true if bytes1 and bytes2 are element-wise equal.
combineCodec<TFrom, TTo>(Encoder<TFrom> encoder, Decoder<TTo> decoder) → Codec<TFrom, TTo>
Combines an Encoder and a Decoder into a Codec.
containsBytes(Uint8List data, Uint8List bytes, int offset) → bool
Returns true if data contains bytes at the given offset.
createDecoderThatConsumesEntireByteArray<T>(Decoder<T> decoder) → Decoder<T>
Creates a Decoder that asserts the bytes provided to decode or read are fully consumed by the inner decoder.
decoderFromCodec<T>(Codec<Object?, T> codec) → Decoder<T>
Extracts a Decoder view from a Codec.
encoderFromCodec<T>(Codec<T, Object?> codec) → Encoder<T>
Extracts an Encoder view from a Codec.
fixBytes(Uint8List bytes, int length) → Uint8List
Fixes bytes to exactly length bytes.
fixCodecSize<TFrom, TTo>(Codec<TFrom, TTo> codec, int fixedBytes, {bool allowTruncation = true}) → FixedSizeCodec<TFrom, TTo>
Creates a fixed-size codec from a given codec.
fixDecoderSize<TTo>(Decoder<TTo> decoder, int fixedBytes) → FixedSizeDecoder<TTo>
Creates a fixed-size decoder from a given decoder.
fixEncoderSize<TFrom>(Encoder<TFrom> encoder, int fixedBytes, {bool allowTruncation = true}) → FixedSizeEncoder<TFrom>
Creates a fixed-size encoder from a given encoder.
getEncodedSize<T>(T value, Encoder<T> encoder) → int
Gets the encoded size of value using the provided encoder.
isFixedSize(Object? object) → bool
Returns true if the given encoder, decoder, or codec is fixed-size.
isVariableSize(Object? object) → bool
Returns true if the given encoder, decoder, or codec is variable-size.
keccak256(Uint8List input) → Uint8List
Computes the Keccak-256 hash of input.
mergeBytes(List<Uint8List> byteArrays) → Uint8List
Concatenates a list of Uint8Lists into a single Uint8List.
offsetCodec<TFrom, TTo>(Codec<TFrom, TTo> codec, OffsetConfig config) → Codec<TFrom, TTo>
Moves the offset of a given codec before and/or after encoding and decoding.
offsetDecoder<T>(Decoder<T> decoder, OffsetConfig config) → Decoder<T>
Moves the offset of a given decoder before and/or after decoding.
offsetEncoder<T>(Encoder<T> encoder, OffsetConfig config) → Encoder<T>
Moves the offset of a given encoder before and/or after encoding.
padBytes(Uint8List bytes, int length) → Uint8List
Pads bytes with trailing zeroes to reach length.
padLeftCodec<TFrom, TTo>(Codec<TFrom, TTo> codec, int offset) → Codec<TFrom, TTo>
Adds left padding to codec, shifting encoding and decoding positions forward by offset bytes.
padLeftDecoder<T>(Decoder<T> decoder, int offset) → Decoder<T>
Adds left padding to decoder, shifting the decoding position forward by offset bytes and increasing the decoder size accordingly.
padLeftEncoder<T>(Encoder<T> encoder, int offset) → Encoder<T>
Adds left padding to encoder, shifting the encoded value forward by offset bytes and increasing the encoder size accordingly.
padRightCodec<TFrom, TTo>(Codec<TFrom, TTo> codec, int offset) → Codec<TFrom, TTo>
Adds right padding to codec, extending the encoded/decoded value by offset bytes.
padRightDecoder<T>(Decoder<T> decoder, int offset) → Decoder<T>
Adds right padding to decoder, extending the post-offset by offset bytes and increasing the decoder size accordingly.
padRightEncoder<T>(Encoder<T> encoder, int offset) → Encoder<T>
Adds right padding to encoder, extending the encoded value by offset bytes and increasing the encoder size accordingly.
resizeCodec<TFrom, TTo>(Codec<TFrom, TTo> codec, int resize(int size)) → Codec<TFrom, TTo>
Updates the size of a given codec using a resize function.
resizeDecoder<T>(Decoder<T> decoder, int resize(int size)) → Decoder<T>
Updates the size of a given decoder using a resize function.
resizeEncoder<T>(Encoder<T> encoder, int resize(int size)) → Encoder<T>
Updates the size of a given encoder using a resize function.
reverseCodec<TFrom, TTo>(FixedSizeCodec<TFrom, TTo> codec) → FixedSizeCodec<TFrom, TTo>
Reverses the bytes of a fixed-size codec.
reverseDecoder<TTo>(FixedSizeDecoder<TTo> decoder) → FixedSizeDecoder<TTo>
Reverses the bytes of a fixed-size decoder.
reverseEncoder<TFrom>(FixedSizeEncoder<TFrom> encoder) → FixedSizeEncoder<TFrom>
Reverses the bytes of a fixed-size encoder.
tapCodec<TFrom, TTo>(Codec<TFrom, TTo> codec, void encodeTap(TFrom value), [void decodeTap(TTo value)?]) → Codec<TFrom, TTo>
Observes both encoded and decoded values without modifying them.
tapCodecBytes<TFrom, TTo>(Codec<TFrom, TTo> codec, void encodeTap(Uint8List bytes, int preOffset, int postOffset), [void decodeTap(Uint8List bytes, int offset)?]) → Codec<TFrom, TTo>
Observes the bytes an encoder writes and a decoder reads.
tapDecoder<T>(Decoder<T> decoder, void tap(T value)) → Decoder<T>
Observes decoded values without modifying them.
tapDecoderBytes<T>(Decoder<T> decoder, void tap(Uint8List bytes, int offset)) → Decoder<T>
Observes the bytes a decoder is about to read.
tapEncoder<T>(Encoder<T> encoder, void tap(T value)) → Encoder<T>
Observes encoded values without modifying them.
tapEncoderBytes<T>(Encoder<T> encoder, void tap(Uint8List bytes, int preOffset, int postOffset)) → Encoder<T>
Observes encoded bytes without modifying them.
transformCodec<TOldFrom, TNewFrom, TOldTo, TNewTo>(Codec<TOldFrom, TOldTo> codec, TOldFrom unmap(TNewFrom value), [TNewTo map(TOldTo value, Uint8List bytes, int offset)?]) → Codec<TNewFrom, TNewTo>
Transforms a codec by mapping its input and output values.
transformDecoder<TOldTo, TNewTo>(Decoder<TOldTo> decoder, TNewTo map(TOldTo value, Uint8List bytes, int offset)) → Decoder<TNewTo>
Transforms a decoder by mapping its output values.
transformEncoder<TOldFrom, TNewFrom>(Encoder<TOldFrom> encoder, TOldFrom unmap(TNewFrom value)) → Encoder<TNewFrom>
Transforms an encoder by mapping its input values.

Typedefs

PostOffsetFunction = int Function(PostOffsetScope scope)
A function that modifies the post-offset after encoding or decoding.
PreOffsetFunction = int Function(PreOffsetScope scope)
A function that modifies the pre-offset before encoding or decoding.