Skip to content

Codecs

Solace::Utils::Codecs is the encoding toolbox behind the serializers and instruction builders: base58/base64 conversions and the little-endian and compact-u16 integer encodings Solana's binary format uses. All methods are module functions.

Base58

Solana addresses and signatures are base58.

MethodSignatureDescription
bytes_to_base58(Array<Integer>) → StringEncode a byte array to base58.
base58_to_bytes(String) → Array<Integer>Decode base58 to a byte array.
binary_to_base58(String) → StringEncode a binary string to base58.
base58_to_binary(String) → StringDecode base58 to a binary string.
valid_base58?(String) → BooleanWhether a string is valid base58.
ruby
bytes = Solace::Utils::Codecs.base58_to_bytes("11111111111111111111111111111111")
Solace::Utils::Codecs.bytes_to_base58(bytes) # round-trips

Compact-u16 (ShortVec)

Solana encodes array lengths as a variable-length "compact-u16" (ShortVec). Decoders read from an IO and report how many bytes they consumed.

MethodSignatureDescription
encode_compact_u16(Integer) → StringEncode a length as packed bytes.
decode_compact_u16(IO) → [Integer, Integer]Decode [value, bytes_read] from a stream.
ruby
packed = Solace::Utils::Codecs.encode_compact_u16(300)
value, read = Solace::Utils::Codecs.decode_compact_u16(StringIO.new(packed))

Little-endian u64

Lamport amounts and most numeric instruction arguments are little-endian u64.

MethodSignatureDescription
encode_le_u64(Integer) → StringEncode an integer as 8 little-endian bytes.
decode_le_u64(IO) → IntegerDecode a u64 from a stream.
ruby
data = Solace::Utils::Codecs.encode_le_u64(1_000_000)

Base64

RPC returns account and transaction data as base64; turn it into a stream the deserializers can read:

MethodSignatureDescription
base64_to_bytestream(String) → StringIODecode base64 into a StringIO.
ruby
io = Solace::Utils::Codecs.base64_to_bytestream(account_info['data'][0])

These primitives are what you compose when writing your own instruction builder.

A Ruby SDK for Solana