goscale

package module
v0.0.0-...-c11d3cb Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2024 License: Apache-2.0 Imports: 10 Imported by: 37

README

SCALE codec implementation in Go compatible with a Tinygo based toolchain

codecov

The SCALE types in Go are represented by a set of custom-defined types that implement the Encodable interface. Each type also has a corresponding decode function using the convention Decode<TypeName>. Note that the type to which data should be decoded is inferred from the context and is not self-contained in the SCALE-encoded data.

One exception is the Tuple type. It doesn't have methods attached. Instead, there are EncodeTuple and DecodeTuple functions that can be invoked with any custom struct that embeds the Tuple interface.

Some quirks deserve mention. For example, the FixedSequence type, which has the same representation as the Sequence type, facilitates the encoding of arrays. As arrays are fixed-size sequences, they cannot be encoded as the Sequence type. Note that there are no type checks on the size.

The use of custom-defined types and generics reduces reliance on reflection, which isn't fully supported by TinyGo.


Boolean

SCALE/Rust Go
bool goscale.Bool

Fixed Length Integers

SCALE/Rust Go
i8 goscale.I8
u8 goscale.U8
i16 goscale.I16
u16 goscale.U16
i32 goscale.I32
u32 goscale.U32
i64 goscale.I64
u64 goscale.U64
i128 goscale.I128
u128 goscale.U128

Length and Compact (Variable Width Integers)

SCALE/Rust Go
Compact<u8> goscale.Compact
Compact<u16> goscale.Compact
Compact<u32> goscale.Compact
Compact<u64> goscale.Compact
Compact<u128> goscale.Compact

Sequence

SCALE/Rust Go
bytes goscale.Sequence[U8]
[u8; u8] goscale.FixedSequence[U8]
string goscale.Str

Dictionary

SCALE/Rust Go
goscale.Dictionary[K, V]

Empty

SCALE/Rust Go
goscale.Empty

VaryingData

SCALE/Rust Go
Enumeration(tagged-union) goscale.VaryingData

Option

SCALE/Rust Go
Option<bool> Option[goscale.Bool]
Option<i8> Option[goscale.I8]
Option<u8> Option[goscale.U8]
Option<i16> Option[goscale.I16]
Option<u16> Option[goscale.U16]
Option<i32> Option[goscale.I32]
Option<u32> Option[goscale.U32]
Option<i64> Option[goscale.I64]
Option<u64> Option[goscale.U64]
Option<i128> Option[goscale.I128]
Option<u128> Option[goscale.U128]
Option<bytes> Option[Sequence[U8]]
OptionBool OptionBool
None nil

Result

SCALE/Rust Go
Result<EncodeLike, EncodeLike> goscale.Result[goscale.Encodable]

Tuple

Go structs are encoded as SCALE Tuple, where each struct field is encoded in a sequence containing all the fields. To decode SCALE encoded structs, it is required to have prior knowledge of the destination data type.

SCALE Go
struct goscale.Tuple
Run Tests
go test -v

Documentation

Overview

Simple Concatenated Aggregate Little-Endian” (SCALE) codec

Polkadot Spec - https://spec.polkadot.network/#sect-scale-codec

Substrate Ref - https://docs.substrate.io/reference/scale-codec/

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clamp

func Clamp(value, min, max int) int

func DictionaryFieldEncode

func DictionaryFieldEncode(field reflect.Value, buffer *bytes.Buffer)

func EncodeEach

func EncodeEach(buffer *bytes.Buffer, encodables ...Encodable) error

func EncodeTuple

func EncodeTuple(t interface{}, buffer *bytes.Buffer)

func EncodedBytes

func EncodedBytes(e Encodable) []byte

func FixedSequenceU8ToBytes

func FixedSequenceU8ToBytes(bytes FixedSequence[U8]) []byte

func SequenceFieldEncode

func SequenceFieldEncode(field reflect.Value, buffer *bytes.Buffer)

func SequenceU8ToBytes

func SequenceU8ToBytes(bytes Sequence[U8]) []byte

func TrailingZeros128

func TrailingZeros128(n U128) uint

Types

type Bool

type Bool bool

func DecodeBool

func DecodeBool(buffer *bytes.Buffer) (Bool, error)

func (Bool) Bytes

func (value Bool) Bytes() []byte

func (Bool) Encode

func (value Bool) Encode(buffer *bytes.Buffer) error

type Compact

type Compact struct {
	Number Numeric
}

func DecodeCompact

func DecodeCompact[T Numeric](buffer *bytes.Buffer) (Compact, error)

func ToCompact

func ToCompact(v interface{}) Compact

func (Compact) Bytes

func (c Compact) Bytes() []byte

func (Compact) Encode

func (c Compact) Encode(buffer *bytes.Buffer) error

func (Compact) ToBigInt

func (c Compact) ToBigInt() *big.Int

type Comparable

type Comparable interface {
	Encodable
	Ordered
}

type Decoder

type Decoder struct {
	Reader io.Reader
}

func (Decoder) DecodeByte

func (dec Decoder) DecodeByte() (byte, error)

func (Decoder) Read

func (dec Decoder) Read(bytes []byte) error

type Dictionary

type Dictionary[K Comparable, V Encodable] map[K]V

func DecodeDictionary

func DecodeDictionary[K Comparable, V Encodable](buffer *bytes.Buffer) (Dictionary[K, V], error)

func (Dictionary[K, V]) Bytes

func (d Dictionary[K, V]) Bytes() []byte

func (Dictionary[K, V]) Encode

func (d Dictionary[K, V]) Encode(buffer *bytes.Buffer) error

type Empty

type Empty struct{}

func DecodeEmpty

func DecodeEmpty() (Empty, error)

func (Empty) Bytes

func (e Empty) Bytes() []byte

func (Empty) Encode

func (e Empty) Encode(buffer *bytes.Buffer) error

type Encodable

type Encodable interface {
	Encode(buffer *bytes.Buffer) error
	Bytes() []byte
}

func ConvertTo

func ConvertTo[T Encodable](v reflect.Value) Encodable

func ConvertToDictionary

func ConvertToDictionary[K Comparable, V Encodable](v reflect.Value) (value Encodable)

func ConvertToSequence

func ConvertToSequence[T Encodable](v reflect.Value) (value Encodable)

type Encoder

type Encoder struct {
	Writer io.Writer
}

func (Encoder) EncodeByte

func (enc Encoder) EncodeByte(b byte) error

func (Encoder) Write

func (enc Encoder) Write(bytes []byte) error

type FixedSequence

type FixedSequence[T Encodable] []T // TODO: https://github.com/LimeChain/goscale/issues/37

func BytesToFixedSequenceU8

func BytesToFixedSequenceU8(bytes []byte) FixedSequence[U8]

func DecodeFixedSequence

func DecodeFixedSequence[T Encodable](size int, buffer *bytes.Buffer) (FixedSequence[T], error)

func NewFixedSequence

func NewFixedSequence[T Encodable](size int, values ...T) FixedSequence[T]

Initializes with the specified size and provides size checks (at least at runtime)

func (FixedSequence[T]) Bytes

func (fseq FixedSequence[T]) Bytes() []byte

func (FixedSequence[T]) Encode

func (fseq FixedSequence[T]) Encode(buffer *bytes.Buffer) error

type I128

type I128 [2]U64

little endian byte order [0] least significant bits [1] most significant bits

func DecodeI128

func DecodeI128(buffer *bytes.Buffer) (I128, error)

func MaxI128

func MaxI128() I128

ff ff ff ff ff ff ff ff | 7f ff ff ff ff ff ff ff

func MinI128

func MinI128() I128

00 00 00 00 00 00 00 00 | 80 00 00 00 00 00 00 00

func NewI128

func NewI128[N Integer](n N) I128

func NewI128FromString

func NewI128FromString(n string) (I128, error)

func (I128) Add

func (n I128) Add(other I128) I128

func (I128) Bytes

func (n I128) Bytes() []byte

func (I128) Div

func (n I128) Div(other I128) I128

func (I128) Encode

func (n I128) Encode(buffer *bytes.Buffer) error

func (I128) Eq

func (n I128) Eq(other I128) bool

func (I128) Gt

func (n I128) Gt(other I128) bool

func (I128) Gte

func (n I128) Gte(other I128) bool

func (I128) Lt

func (n I128) Lt(other I128) bool

func (I128) Lte

func (n I128) Lte(other I128) bool

func (I128) Mod

func (n I128) Mod(other I128) I128

func (I128) Mul

func (n I128) Mul(other I128) I128

func (I128) Ne

func (n I128) Ne(other I128) bool

func (I128) Sub

func (n I128) Sub(other I128) I128

func (I128) ToBigInt

func (n I128) ToBigInt() *big.Int

type I16

type I16 int16

func DecodeI16

func DecodeI16(buffer *bytes.Buffer) (I16, error)

func (I16) Bytes

func (value I16) Bytes() []byte

func (I16) Encode

func (value I16) Encode(buffer *bytes.Buffer) error

type I32

type I32 int32

func DecodeI32

func DecodeI32(buffer *bytes.Buffer) (I32, error)

func (I32) Bytes

func (value I32) Bytes() []byte

func (I32) Encode

func (value I32) Encode(buffer *bytes.Buffer) error

type I64

type I64 int64

func DecodeI64

func DecodeI64(buffer *bytes.Buffer) (I64, error)

func (I64) Bytes

func (value I64) Bytes() []byte

func (I64) Encode

func (value I64) Encode(buffer *bytes.Buffer) error

type I8

type I8 int8

func DecodeI8

func DecodeI8(buffer *bytes.Buffer) (I8, error)

func (I8) Bytes

func (value I8) Bytes() []byte

func (I8) Encode

func (value I8) Encode(buffer *bytes.Buffer) error

type Integer

Signed/Unsigned integer constraint, for type safety checks

type Integer128

type Integer128 interface {
	U128 | I128
}

type Numeric

type Numeric interface {
	ToBigInt() *big.Int
}

type Option

type Option[T Encodable] struct {
	HasValue Bool
	Value    T
}

func DecodeOption

func DecodeOption[T Encodable](buffer *bytes.Buffer) (Option[T], error)

func DecodeOptionWith

func DecodeOptionWith[T Encodable](buffer *bytes.Buffer, decodeFunc func(buffer *bytes.Buffer) (T, error)) (Option[T], error)

func NewOption

func NewOption[T Encodable](value Encodable) Option[T]

func (Option[T]) Bytes

func (o Option[T]) Bytes() []byte

func (Option[T]) Encode

func (o Option[T]) Encode(buffer *bytes.Buffer) error

type OptionBool

type OptionBool Option[Bool]

func DecodeOptionBool

func DecodeOptionBool(buffer *bytes.Buffer) (OptionBool, error)

func (OptionBool) Bytes

func (o OptionBool) Bytes() []byte

func (OptionBool) Encode

func (o OptionBool) Encode(buffer *bytes.Buffer) error

type Ordered

type Ordered interface {
	I8 | I16 | I32 | I64 | U8 | U16 | U32 | U64 | Str
}

type Result

type Result[T Encodable] struct {
	HasError Bool
	Value    T
}

func DecodeResult

func DecodeResult[T, E Encodable](buffer *bytes.Buffer, decodeValid func(*bytes.Buffer) (T, error), decodeErr func(*bytes.Buffer) (E, error)) (Result[Encodable], error)

func (Result[T]) Bytes

func (r Result[T]) Bytes() []byte

func (Result[T]) Encode

func (r Result[T]) Encode(buffer *bytes.Buffer) error

type Sequence

type Sequence[T Encodable] []T

func BytesToSequenceU8

func BytesToSequenceU8(bytes []byte) Sequence[U8]

func DecodeSequence

func DecodeSequence[T Encodable](buffer *bytes.Buffer) (Sequence[T], error)

func DecodeSequenceWith

func DecodeSequenceWith[T Encodable](buffer *bytes.Buffer, decodeFunc func(buffer *bytes.Buffer) (T, error)) (Sequence[T], error)

func (Sequence[T]) Bytes

func (seq Sequence[T]) Bytes() []byte

func (Sequence[T]) Encode

func (seq Sequence[T]) Encode(buffer *bytes.Buffer) error

type SignedPrimitiveInteger

type SignedPrimitiveInteger interface {
	int | int8 | int16 | int32 | int64 | I8 | I16 | I32 | I64
}

Signed integer constraint, for type safety checks

type Str

type Str string

additional helper type

func DecodeStr

func DecodeStr(buffer *bytes.Buffer) (Str, error)

func SliceU8ToStr

func SliceU8ToStr(values []U8) Str

func (Str) Bytes

func (value Str) Bytes() []byte

func (Str) Encode

func (value Str) Encode(buffer *bytes.Buffer) error

type Tuple

type Tuple struct {
	Encodable
}

func (Tuple) Bytes

func (t Tuple) Bytes() []byte

func (Tuple) Encode

func (t Tuple) Encode(buffer *bytes.Buffer) error

type U128

type U128 [2]U64

little endian byte order [0] least significant bits [1] most significant bits

func CheckedAddU128

func CheckedAddU128(a, b U128) (U128, error)

func CheckedSubU128

func CheckedSubU128(a, b U128) (U128, error)

func DecodeU128

func DecodeU128(buffer *bytes.Buffer) (U128, error)

func Max128

func Max128(a, b U128) U128

func MaxU128

func MaxU128() U128

ff ff ff ff ff ff ff ff | ff ff ff ff ff ff ff ff

func Min128

func Min128(a, b U128) U128

func MinU128

func MinU128() U128

00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00

func NewU128

func NewU128[N Integer](n N) U128

func NewU128FromString

func NewU128FromString(n string) (U128, error)

func SaturatingAddU128

func SaturatingAddU128(a, b U128) U128

func SaturatingSubU128

func SaturatingSubU128(a, b U128) U128

func (U128) Add

func (n U128) Add(other U128) U128

func (U128) Bytes

func (n U128) Bytes() []byte

func (U128) Div

func (n U128) Div(other U128) U128

func (U128) Encode

func (n U128) Encode(buffer *bytes.Buffer) error

func (U128) Eq

func (n U128) Eq(other U128) bool

func (U128) Gt

func (n U128) Gt(other U128) bool

func (U128) Gte

func (n U128) Gte(other U128) bool

func (U128) Lt

func (n U128) Lt(other U128) bool

func (U128) Lte

func (n U128) Lte(other U128) bool

func (U128) Mod

func (n U128) Mod(other U128) U128

func (U128) Mul

func (n U128) Mul(other U128) U128

func (U128) Ne

func (n U128) Ne(other U128) bool

func (U128) Sub

func (n U128) Sub(other U128) U128

func (U128) ToBigInt

func (n U128) ToBigInt() *big.Int

type U16

type U16 uint16

func DecodeU16

func DecodeU16(buffer *bytes.Buffer) (U16, error)

func NewU16

func NewU16(n uint16) U16

func (U16) Bytes

func (value U16) Bytes() []byte

func (U16) Encode

func (value U16) Encode(buffer *bytes.Buffer) error

func (U16) ToBigInt

func (value U16) ToBigInt() *big.Int

type U32

type U32 uint32

func CheckedAddU32

func CheckedAddU32(a, b U32) (U32, error)

func DecodeU32

func DecodeU32(buffer *bytes.Buffer) (U32, error)

func Min32

func Min32(a, b U32) U32

func NewU32

func NewU32(n uint32) U32

func SaturatingAddU32

func SaturatingAddU32(a, b U32) U32

func (U32) Bytes

func (value U32) Bytes() []byte

func (U32) Encode

func (value U32) Encode(buffer *bytes.Buffer) error

func (U32) ToBigInt

func (value U32) ToBigInt() *big.Int

type U64

type U64 uint64

func CheckedAddU64

func CheckedAddU64(a, b U64) (U64, error)

func DecodeU64

func DecodeU64(buffer *bytes.Buffer) (U64, error)

func Max64

func Max64(a, b U64) U64

func Min64

func Min64(a, b U64) U64

func NewU64

func NewU64(n uint64) U64

func SaturatingAddU64

func SaturatingAddU64(a, b U64) U64

func SaturatingMulU64

func SaturatingMulU64(a, b U64) U64

func SaturatingSubU64

func SaturatingSubU64(a, b U64) U64

func (U64) Bytes

func (value U64) Bytes() []byte

func (U64) Encode

func (value U64) Encode(buffer *bytes.Buffer) error

func (U64) ToBigInt

func (value U64) ToBigInt() *big.Int

type U8

type U8 uint8

func DecodeSliceU8

func DecodeSliceU8(buffer *bytes.Buffer) ([]U8, error)

func DecodeU8

func DecodeU8(buffer *bytes.Buffer) (U8, error)

func NewU8

func NewU8(n uint8) U8

func StrToSliceU8

func StrToSliceU8(s Str) []U8

func (U8) Bytes

func (value U8) Bytes() []byte

func (U8) Encode

func (value U8) Encode(buffer *bytes.Buffer) error

func (U8) ToBigInt

func (value U8) ToBigInt() *big.Int

type UnsignedPrimitiveInteger

type UnsignedPrimitiveInteger interface {
	uint | uint8 | uint16 | uint32 | uint64 | U8 | U16 | U32 | U64
}

Unsigned integer constraint, for type safety checks

type VaryingData

type VaryingData []Encodable

func DecodeVaryingData

func DecodeVaryingData(decodeFuncs []func(buffer *bytes.Buffer) []Encodable, buffer *bytes.Buffer) (VaryingData, error)

func NewVaryingData

func NewVaryingData(values ...Encodable) VaryingData

func (VaryingData) Bytes

func (vd VaryingData) Bytes() []byte

func (VaryingData) Encode

func (vd VaryingData) Encode(buffer *bytes.Buffer) error

Jump to

Keyboard shortcuts

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