bin

package module
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

README

binary

Borsh
Decoding borsh
 dec := bin.NewBorshDecoder(data)
 var meta token_metadata.Metadata
 err = dec.Decode(&meta)
 if err != nil {
   panic(err)
 }
Encoding borsh
buf := new(bytes.Buffer)
enc := bin.NewBorshEncoder(buf)
err := enc.Encode(meta)
if err != nil {
  panic(err)
}
// fmt.Print(buf.Bytes())
Optional Types
type Person struct {
	Name string
	Age  uint8 `bin:"optional"`
}

Rust equivalent:

struct Person {
    name: String,
    age: Option<u8>
}
Enum Types
type MyEnum struct {
	Enum  bin.BorshEnum `borsh_enum:"true"`
	One   bin.EmptyVariant
	Two   uint32
	Three int16
}

Rust equivalent:

enum MyEnum {
    One,
    Two(u32),
    Three(i16),
}
Exported vs Unexported Fields

In this example, the two field will be skipped by the encoder/decoder because the field is not exported.

type MyStruct struct {
	One   string
	two   uint32
	Three int16
}
Skip Decoding/Encoding Attributes

Encoding/Decoding of exported fields can be skipped using the borsh_skip tag.

type MyStruct struct {
	One   string
	Two   uint32 `borsh_skip:"true"`
	Three int16
}

Documentation

Index

Constants

View Source
const ACCOUNT_DISCRIMINATOR_SIZE = 8
View Source
const SIGHASH_ACCOUNT_NAMESPACE string = "account"
View Source
const SIGHASH_GLOBAL_NAMESPACE string = "global"

Namespace for calculating instruction sighash signatures for any instruction not affecting program state.

View Source
const SIGHASH_STATE_NAMESPACE string = "state"

Namespace for calculating state instruction sighash signatures.

Variables

View Source
var ErrVarIntBufferSize = errors.New("varint: invalid buffer size")
View Source
var NoTypeIDDefaultID = TypeIDFromUint8(0)
View Source
var TypeSize = struct {
	Bool int
	Byte int

	Int8  int
	Int16 int

	Uint8   int
	Uint16  int
	Uint32  int
	Uint64  int
	Uint128 int

	Float32 int
	Float64 int

	PublicKey int
	Signature int
}{
	Byte: 1,
	Bool: 1,

	Int8:  1,
	Int16: 2,

	Uint8:   1,
	Uint16:  2,
	Uint32:  4,
	Uint64:  8,
	Uint128: 16,

	Float32: 4,
	Float64: 8,

	PublicKey: 32,
	Signature: 64,
}

Functions

func BinByteCount

func BinByteCount(v interface{}) (uint64, error)

BinByteCount computes the byte count size for the received populated structure. The reported size is the one for the populated structure received in arguments. Depending on how serialization of your fields is performed, size could vary for different structure.

func BorshByteCount

func BorshByteCount(v interface{}) (uint64, error)

BorshByteCount computes the byte count size for the received populated structure. The reported size is the one for the populated structure received in arguments. Depending on how serialization of your fields is performed, size could vary for different structure.

func CompactU16ByteCount

func CompactU16ByteCount(v interface{}) (uint64, error)

CompactU16ByteCount computes the byte count size for the received populated structure. The reported size is the one for the populated structure received in arguments. Depending on how serialization of your fields is performed, size could vary for different structure.

func DecodeCompactU16

func DecodeCompactU16(bytes []byte) (int, int, error)

func DecodeCompactU16LengthFromByteReader

func DecodeCompactU16LengthFromByteReader(reader io.ByteReader) (int, error)

DecodeCompactU16LengthFromByteReader decodes a "Compact-u16" length from the provided io.ByteReader.

func FormatByteSlice

func FormatByteSlice(buf []byte) string

FormatByteSlice formats the given byte slice into a readable format.

func FormatDiscriminator

func FormatDiscriminator(disc [8]byte) string

func IsByteSlice

func IsByteSlice(v interface{}) bool

IsByteSlice returns true if the provided element is a []byte.

func MarshalBin

func MarshalBin(v interface{}) ([]byte, error)

func MarshalBorsh

func MarshalBorsh(v interface{}) ([]byte, error)

func MarshalCompactU16

func MarshalCompactU16(v interface{}) ([]byte, error)

func MustBinByteCount

func MustBinByteCount(v interface{}) uint64

MustBinByteCount acts just like BinByteCount but panics if it encounters any encoding errors.

func MustBorshByteCount

func MustBorshByteCount(v interface{}) uint64

MustBorshByteCount acts just like BorshByteCount but panics if it encounters any encoding errors.

func MustCompactU16ByteCount

func MustCompactU16ByteCount(v interface{}) uint64

MustCompactU16ByteCount acts just like CompactU16ByteCount but panics if it encounters any encoding errors.

func QuoRem added in v0.8.2

func QuoRem(x, y *Uint128) (*Uint128, *Uint128)

func Reciprocal added in v0.8.2

func Reciprocal(m *Uint256) (mu [5]uint64)

func ReverseBytes

func ReverseBytes(s []byte)

func Sighash

func Sighash(namespace string, name string) []byte

Sighash creates an anchor sighash for the provided namespace and element. An anchor sighash is the first 8 bytes of the sha256 of {namespace}:{name} NOTE: you must first convert the name to snake case using `ToSnakeForSighash`.

func SighashAccount

func SighashAccount(name string) []byte

func SighashInstruction

func SighashInstruction(name string) []byte

func ToPascalCase

func ToPascalCase(s string) string

ToPascalCase converts a string to upper camel case.

func ToRustSnakeCase

func ToRustSnakeCase(s string) string

ToRustSnakeCase converts the given string to a snake_case string. Ported from https://github.com/withoutboats/heck/blob/c501fc95db91ce20eaef248a511caec7142208b4/src/lib.rs#L75 as used by Anchor.

func ToSnakeForSighash

func ToSnakeForSighash(s string) string

func Uint32FromTypeID

func Uint32FromTypeID(vid TypeID) (out uint32)

Uint32FromTypeID parses a TypeID bytes to a uint32.

func Uint8FromTypeID

func Uint8FromTypeID(vid TypeID) (out uint8)

Uint32FromTypeID parses a TypeID bytes to a uint8.

func UnmarshalBin

func UnmarshalBin(v interface{}, b []byte) error

func UnmarshalBorsh

func UnmarshalBorsh(v interface{}, b []byte) error

func UnmarshalCompactU16

func UnmarshalCompactU16(v interface{}, b []byte) error

func Uvarint32FromTypeID

func Uvarint32FromTypeID(vid TypeID) (out uint32)

Uvarint32FromTypeID parses a TypeID bytes to a uvarint 32.

Types

type BaseVariant

type BaseVariant struct {
	TypeID TypeID
	Impl   interface{}
}

func (*BaseVariant) Assign

func (a *BaseVariant) Assign(typeID TypeID, impl interface{})

func (*BaseVariant) Obtain

func (a *BaseVariant) Obtain(def *VariantDefinition) (typeID TypeID, typeName string, impl interface{})

func (*BaseVariant) UnmarshalBinaryVariant

func (a *BaseVariant) UnmarshalBinaryVariant(decoder *Decoder, def *VariantDefinition) (err error)

type BinaryMarshaler

type BinaryMarshaler interface {
	MarshalWithEncoder(encoder *Encoder) error
}

type BinaryUnmarshaler

type BinaryUnmarshaler interface {
	UnmarshalWithDecoder(decoder *Decoder) error
}

type Bool

type Bool bool

func (Bool) MarshalWithEncoder

func (b Bool) MarshalWithEncoder(encoder *Encoder) error

func (*Bool) UnmarshalJSON

func (b *Bool) UnmarshalJSON(data []byte) error

func (*Bool) UnmarshalWithDecoder

func (b *Bool) UnmarshalWithDecoder(decoder *Decoder) error

type BorshEnum

type BorshEnum uint8

type Decoder

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

func NewBinDecoder

func NewBinDecoder(data []byte) *Decoder

func NewBorshDecoder

func NewBorshDecoder(data []byte) *Decoder

func NewCompactU16Decoder

func NewCompactU16Decoder(data []byte) *Decoder

func NewDecoderWithEncoding

func NewDecoderWithEncoding(data []byte, enc Encoding) *Decoder

func (*Decoder) Decode

func (dec *Decoder) Decode(v interface{}) (err error)

func (*Decoder) Discard

func (dec *Decoder) Discard(n int) (err error)

func (*Decoder) HasRemaining

func (dec *Decoder) HasRemaining() bool

func (*Decoder) IsBin

func (dec *Decoder) IsBin() bool

func (*Decoder) IsBorsh

func (dec *Decoder) IsBorsh() bool

func (*Decoder) IsCompactU16

func (dec *Decoder) IsCompactU16() bool

func (*Decoder) Len

func (dec *Decoder) Len() int

func (*Decoder) Peek

func (dec *Decoder) Peek(n int) (out []byte, err error)

func (*Decoder) PeekDiscriminator

func (dec *Decoder) PeekDiscriminator() (out TypeID, err error)

func (*Decoder) Position

func (dec *Decoder) Position() uint

func (*Decoder) Read

func (d *Decoder) Read(buf []byte) (int, error)

func (*Decoder) ReadBool

func (dec *Decoder) ReadBool() (out bool, err error)

func (*Decoder) ReadByte

func (dec *Decoder) ReadByte() (out byte, err error)

func (*Decoder) ReadByteSlice

func (dec *Decoder) ReadByteSlice() (out []byte, err error)

func (*Decoder) ReadBytes

func (dec *Decoder) ReadBytes(n int) (out []byte, err error)

ReadBytes reads a byte slice of length n.

func (*Decoder) ReadCOption

func (dec *Decoder) ReadCOption() (out bool, err error)

func (*Decoder) ReadCompactU16

func (dec *Decoder) ReadCompactU16() (out int, err error)

ReadCompactU16 reads a compact u16 from the decoder.

func (*Decoder) ReadCompactU16Length

func (dec *Decoder) ReadCompactU16Length() (int, error)

func (*Decoder) ReadDiscriminator

func (dec *Decoder) ReadDiscriminator() (out TypeID, err error)

func (*Decoder) ReadFloat32

func (dec *Decoder) ReadFloat32() (out float32, err error)

func (*Decoder) ReadFloat64

func (dec *Decoder) ReadFloat64() (out float64, err error)

func (*Decoder) ReadInt16

func (dec *Decoder) ReadInt16() (out int16, err error)

func (*Decoder) ReadInt32

func (dec *Decoder) ReadInt32() (out int32, err error)

func (*Decoder) ReadInt64

func (dec *Decoder) ReadInt64() (out int64, err error)

func (*Decoder) ReadInt8

func (dec *Decoder) ReadInt8() (out int8, err error)

func (*Decoder) ReadLength

func (dec *Decoder) ReadLength() (length int, err error)

func (*Decoder) ReadNBytes

func (dec *Decoder) ReadNBytes(n int) (out []byte, err error)

func (*Decoder) ReadOption

func (dec *Decoder) ReadOption() (out bool, err error)

func (*Decoder) ReadRustString

func (dec *Decoder) ReadRustString() (out string, err error)

func (*Decoder) ReadString

func (dec *Decoder) ReadString() (out string, err error)

func (*Decoder) ReadTypeID

func (dec *Decoder) ReadTypeID() (out TypeID, err error)

func (*Decoder) ReadUint128

func (dec *Decoder) ReadUint128() (out Uint128, err error)

func (*Decoder) ReadUint16

func (dec *Decoder) ReadUint16() (out uint16, err error)

func (*Decoder) ReadUint32

func (dec *Decoder) ReadUint32() (out uint32, err error)

func (*Decoder) ReadUint64

func (dec *Decoder) ReadUint64() (out uint64, err error)

func (*Decoder) ReadUint8

func (dec *Decoder) ReadUint8() (out uint8, err error)

func (*Decoder) ReadUvarint16

func (dec *Decoder) ReadUvarint16() (out uint16, err error)

func (*Decoder) ReadUvarint32

func (dec *Decoder) ReadUvarint32() (out uint32, err error)

func (*Decoder) ReadUvarint64

func (dec *Decoder) ReadUvarint64() (uint64, error)

func (*Decoder) ReadVarint16

func (dec *Decoder) ReadVarint16() (out int16, err error)

func (*Decoder) ReadVarint32

func (dec *Decoder) ReadVarint32() (out int32, err error)

func (*Decoder) ReadVarint64

func (d *Decoder) ReadVarint64() (out int64, err error)

func (*Decoder) Remaining

func (dec *Decoder) Remaining() int

func (*Decoder) Reset

func (dec *Decoder) Reset(data []byte)

Reset resets the decoder to decode a new message.

func (*Decoder) SafeReadUTF8String

func (dec *Decoder) SafeReadUTF8String() (out string, err error)

func (*Decoder) SetEncoding

func (dec *Decoder) SetEncoding(enc Encoding)

SetEncoding sets the encoding scheme to use for decoding.

func (*Decoder) SetPosition

func (dec *Decoder) SetPosition(idx uint) error

func (*Decoder) SkipBytes

func (dec *Decoder) SkipBytes(count uint) error

type EmptyVariant

type EmptyVariant struct{}

EmptyVariant is an empty borsh enum variant.

func (*EmptyVariant) MarshalWithEncoder

func (*EmptyVariant) MarshalWithEncoder(_ *Encoder) error

func (*EmptyVariant) UnmarshalWithDecoder

func (*EmptyVariant) UnmarshalWithDecoder(_ *Decoder) error

type Encoder

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

func NewBinEncoder

func NewBinEncoder(writer io.Writer) *Encoder

func NewBorshEncoder

func NewBorshEncoder(writer io.Writer) *Encoder

func NewCompactU16Encoder

func NewCompactU16Encoder(writer io.Writer) *Encoder

func NewEncoderWithEncoding

func NewEncoderWithEncoding(writer io.Writer, enc Encoding) *Encoder

func (*Encoder) Encode

func (e *Encoder) Encode(v interface{}) (err error)

func (*Encoder) IsBin

func (enc *Encoder) IsBin() bool

func (*Encoder) IsBorsh

func (enc *Encoder) IsBorsh() bool

func (*Encoder) IsCompactU16

func (enc *Encoder) IsCompactU16() bool

func (*Encoder) Write

func (e *Encoder) Write(b []byte) (n int, err error)

func (*Encoder) WriteBool

func (e *Encoder) WriteBool(b bool) (err error)

func (*Encoder) WriteByte

func (e *Encoder) WriteByte(b byte) (err error)

func (*Encoder) WriteBytes

func (e *Encoder) WriteBytes(b []byte, writeLength bool) error

func (*Encoder) WriteCOption

func (e *Encoder) WriteCOption(b bool) (err error)

func (*Encoder) WriteCompactU16

func (e *Encoder) WriteCompactU16(ln int) (err error)

func (*Encoder) WriteCompactU16Length

func (e *Encoder) WriteCompactU16Length(ln int) (err error)

func (*Encoder) WriteFloat32

func (e *Encoder) WriteFloat32(f float32) (err error)

func (*Encoder) WriteFloat64

func (e *Encoder) WriteFloat64(f float64) (err error)

func (*Encoder) WriteInt16

func (e *Encoder) WriteInt16(i int16) (err error)

func (*Encoder) WriteInt32

func (e *Encoder) WriteInt32(i int32) (err error)

func (*Encoder) WriteInt64

func (e *Encoder) WriteInt64(i int64) (err error)

func (*Encoder) WriteInt8

func (e *Encoder) WriteInt8(i int8) (err error)

func (*Encoder) WriteLength

func (e *Encoder) WriteLength(length int) error

func (*Encoder) WriteOption

func (e *Encoder) WriteOption(b bool) (err error)

func (*Encoder) WriteRustString

func (e *Encoder) WriteRustString(s string) (err error)

func (*Encoder) WriteString

func (e *Encoder) WriteString(s string) (err error)

func (*Encoder) WriteUVarInt

func (e *Encoder) WriteUVarInt(v int) (err error)

func (*Encoder) WriteUint128

func (e *Encoder) WriteUint128(z Uint128) (err error)

func (*Encoder) WriteUint16

func (e *Encoder) WriteUint16(i uint16) (err error)

func (*Encoder) WriteUint32

func (e *Encoder) WriteUint32(i uint32) (err error)

func (*Encoder) WriteUint64

func (e *Encoder) WriteUint64(i uint64) (err error)

func (*Encoder) WriteUint8

func (e *Encoder) WriteUint8(i uint8) (err error)

func (*Encoder) WriteVarInt

func (e *Encoder) WriteVarInt(v int) (err error)

func (*Encoder) Written

func (e *Encoder) Written() int

Written returns the count of bytes written.

type EncoderDecoder

type EncoderDecoder interface {
	BinaryMarshaler
	BinaryUnmarshaler
}

type Encoding

type Encoding int
const (
	EncodingBin Encoding = iota
	EncodingCompactU16
	EncodingBorsh
)

func (Encoding) IsBin

func (en Encoding) IsBin() bool

func (Encoding) IsBorsh

func (en Encoding) IsBorsh() bool

func (Encoding) IsCompactU16

func (en Encoding) IsCompactU16() bool

func (Encoding) String

func (enc Encoding) String() string

type HexBytes

type HexBytes []byte

func (HexBytes) MarshalJSON

func (t HexBytes) MarshalJSON() ([]byte, error)

func (HexBytes) MarshalWithEncoder

func (o HexBytes) MarshalWithEncoder(encoder *Encoder) error

func (HexBytes) String

func (t HexBytes) String() string

func (*HexBytes) UnmarshalJSON

func (t *HexBytes) UnmarshalJSON(data []byte) (err error)

func (*HexBytes) UnmarshalWithDecoder

func (o *HexBytes) UnmarshalWithDecoder(decoder *Decoder) error

type Int64

type Int64 int64

func (Int64) MarshalJSON

func (i Int64) MarshalJSON() (data []byte, err error)

func (Int64) MarshalWithEncoder

func (i Int64) MarshalWithEncoder(enc *Encoder) error

func (*Int64) UnmarshalJSON

func (i *Int64) UnmarshalJSON(data []byte) error

func (*Int64) UnmarshalWithDecoder

func (i *Int64) UnmarshalWithDecoder(dec *Decoder) error

type InvalidDecoderError

type InvalidDecoderError struct {
	Type reflect.Type
}

An InvalidDecoderError describes an invalid argument passed to Decoder. (The argument to Decoder must be a non-nil pointer.)

func (*InvalidDecoderError) Error

func (e *InvalidDecoderError) Error() string

type JSONFloat64

type JSONFloat64 float64

func (JSONFloat64) MarshalWithEncoder

func (f JSONFloat64) MarshalWithEncoder(enc *Encoder) error

func (*JSONFloat64) UnmarshalJSON

func (f *JSONFloat64) UnmarshalJSON(data []byte) error

func (*JSONFloat64) UnmarshalWithDecoder

func (f *JSONFloat64) UnmarshalWithDecoder(dec *Decoder) error

type OnVariant

type OnVariant = func(impl interface{}) error

type SafeString

type SafeString string

func (SafeString) MarshalWithEncoder

func (ss SafeString) MarshalWithEncoder(encoder *Encoder) error

func (*SafeString) UnmarshalWithDecoder

func (ss *SafeString) UnmarshalWithDecoder(d *Decoder) error

type TypeID

type TypeID [8]byte

TypeID defines the internal representation of an instruction type ID (or account type, etc. in anchor programs) and it's used to associate instructions to decoders in the variant tracker.

func SighashTypeID

func SighashTypeID(namespace string, name string) TypeID

NOTE: no casing conversion is done here, it's up to the caller to provide the correct casing.

func TypeIDFromBytes

func TypeIDFromBytes(slice []byte) (id TypeID)

TypeIDFromBytes converts a []byte to a TypeID. The provided slice must be 8 bytes long or less.

func TypeIDFromSighash

func TypeIDFromSighash(sh []byte) TypeID

TypeIDFromSighash converts a sighash bytes to a TypeID.

func TypeIDFromUint32

func TypeIDFromUint32(v uint32) TypeID

TypeIDFromUint32 converts a uint32 to a TypeID.

func TypeIDFromUint8

func TypeIDFromUint8(v uint8) TypeID

TypeIDFromUint32 converts a uint8 to a TypeID.

func TypeIDFromUvarint32

func TypeIDFromUvarint32(v uint32) TypeID

TypeIDFromUvarint32 converts a Uvarint to a TypeID.

func (TypeID) Bytes

func (vid TypeID) Bytes() []byte

func (TypeID) Equal

func (vid TypeID) Equal(b []byte) bool

Equal returns true if the provided bytes are equal to the bytes of the TypeID.

func (TypeID) Uint32

func (vid TypeID) Uint32() uint32

Uint32 parses the TypeID to a uint32.

func (TypeID) Uint8

func (vid TypeID) Uint8() uint8

Uint8 parses the TypeID to a Uint8.

func (TypeID) Uvarint32

func (vid TypeID) Uvarint32() uint32

Uvarint32 parses the TypeID to a uint32.

type TypeIDEncoding

type TypeIDEncoding uint32
const (
	Uvarint32TypeIDEncoding TypeIDEncoding = iota
	Uint32TypeIDEncoding
	Uint8TypeIDEncoding
	// AnchorTypeIDEncoding is the instruction ID encoding used by programs
	// written using the anchor SDK.
	// The typeID is the sighash of the instruction.
	AnchorTypeIDEncoding
	// No type ID; ONLY ONE VARIANT PER PROGRAM.
	NoTypeIDEncoding
)

type Uint128

type Uint128 [2]uint64

Uint128

func NewUint128 added in v0.8.2

func NewUint128() *Uint128

func NewUint128From64

func NewUint128From64(x uint64) *Uint128

func NewUint128FromBig added in v0.8.3

func NewUint128FromBig(x *big.Int) *Uint128

func QuoRem64 added in v0.8.2

func QuoRem64(x *Uint128, y uint64) (*Uint128, uint64)

func (*Uint128) Add

func (z *Uint128) Add(x, y *Uint128) *Uint128

func (*Uint128) Add64

func (z *Uint128) Add64(x *Uint128, y uint64) *Uint128

func (*Uint128) And

func (z *Uint128) And(x, y *Uint128) *Uint128

func (*Uint128) And64

func (z *Uint128) And64(x *Uint128, y uint64) *Uint128

func (*Uint128) BigInt

func (z *Uint128) BigInt() *big.Int

func (*Uint128) Bytes

func (z *Uint128) Bytes() []byte

func (*Uint128) Clone added in v0.8.2

func (z *Uint128) Clone() *Uint128

func (*Uint128) Cmp

func (z *Uint128) Cmp(x *Uint128) int

func (*Uint128) Cmp64

func (z *Uint128) Cmp64(x uint64) int

func (*Uint128) DecimalString

func (z *Uint128) DecimalString() string

func (*Uint128) Div

func (z *Uint128) Div(x, v *Uint128) *Uint128

Div returns u/v.

func (*Uint128) Div64

func (z *Uint128) Div64(x *Uint128, v uint64) *Uint128

Div64 returns u/v.

func (*Uint128) Equals

func (z *Uint128) Equals(x *Uint128) bool

func (*Uint128) Equals64

func (z *Uint128) Equals64(x uint64) bool

func (*Uint128) HexString

func (z *Uint128) HexString() string

func (*Uint128) IsZero

func (z *Uint128) IsZero() bool

func (*Uint128) LeadingZeros

func (z *Uint128) LeadingZeros() int

func (*Uint128) Lsh

func (z *Uint128) Lsh(x *Uint128, n uint) *Uint128

func (Uint128) MarshalJSON

func (z Uint128) MarshalJSON() (data []byte, err error)

func (Uint128) MarshalWithEncoder

func (z Uint128) MarshalWithEncoder(enc *Encoder) error

func (*Uint128) Mod

func (z *Uint128) Mod(x, y *Uint128) *Uint128

func (*Uint128) Mod64

func (z *Uint128) Mod64(x *Uint128, y uint64) *Uint128

func (*Uint128) Mul

func (z *Uint128) Mul(x, y *Uint128) *Uint128

func (*Uint128) Mul64

func (z *Uint128) Mul64(x *Uint128, y uint64) *Uint128

func (*Uint128) Or

func (z *Uint128) Or(x, y *Uint128) *Uint128

func (*Uint128) Or64

func (z *Uint128) Or64(x *Uint128, y uint64) *Uint128

func (*Uint128) Rsh

func (z *Uint128) Rsh(x *Uint128, n uint) *Uint128

func (*Uint128) String

func (z *Uint128) String() string

func (*Uint128) Sub

func (z *Uint128) Sub(x, y *Uint128) *Uint128

func (*Uint128) Sub64

func (z *Uint128) Sub64(x *Uint128, y uint64) *Uint128

func (*Uint128) Uint64 added in v0.8.2

func (z *Uint128) Uint64() uint64

func (*Uint128) UnmarshalJSON

func (z *Uint128) UnmarshalJSON(data []byte) error

func (*Uint128) UnmarshalWithDecoder

func (z *Uint128) UnmarshalWithDecoder(dec *Decoder) error

func (*Uint128) Xor

func (z *Uint128) Xor(x, y *Uint128) *Uint128

Xor returns u^v.

func (*Uint128) Xor64

func (z *Uint128) Xor64(x *Uint128, y uint64) *Uint128

Xor64 returns u^v.

type Uint256 added in v0.8.2

type Uint256 [4]uint64

func NewUint256From128 added in v0.8.2

func NewUint256From128(x *Uint128) *Uint256

func NewUint256From64 added in v0.8.2

func NewUint256From64(x uint64) *Uint256

func (*Uint256) Abs added in v0.8.2

func (z *Uint256) Abs(x *Uint256) *Uint256

Abs interprets x as a two's complement signed number, and sets z to the absolute value

Abs(0)        = 0
Abs(1)        = 1
Abs(2**255)   = -2**255
Abs(2**256-1) = -1

func (*Uint256) Add added in v0.8.2

func (z *Uint256) Add(x, y *Uint256) *Uint256

Add sets z to the sum x+y

func (*Uint256) Add64 added in v0.8.2

func (z *Uint256) Add64(x *Uint256, y uint64) *Uint256

AddUint64 sets z to x + y, where y is a uint64, and returns z

func (*Uint256) AddMod added in v0.8.2

func (z *Uint256) AddMod(x, y, m *Uint256) *Uint256

AddMod sets z to the sum ( x+y ) mod m, and returns z. If m == 0, z is set to 0 (OBS: differs from the big.Int)

func (*Uint256) AddOverflow added in v0.8.2

func (z *Uint256) AddOverflow(x, y *Uint256) (*Uint256, bool)

AddOverflow sets z to the sum x+y, and returns z and whether overflow occurred

func (*Uint256) And added in v0.8.2

func (z *Uint256) And(x, y *Uint256) *Uint256

And sets z = x & y and returns z.

func (*Uint256) BitLen added in v0.8.2

func (z *Uint256) BitLen() int

BitLen returns the number of bits required to represent z

func (*Uint256) Byte added in v0.8.2

func (z *Uint256) Byte(n *Uint256) *Uint256

Byte sets z to the value of the byte at position n, with z considered as a big-endian 32-byte integer. if n >= 32, z is set to 0 Example: z=5, n=31 => 5

func (*Uint256) ByteLen added in v0.8.2

func (z *Uint256) ByteLen() int

ByteLen returns the number of bytes required to represent z

func (*Uint256) Clear added in v0.8.2

func (z *Uint256) Clear() *Uint256

Clear sets z to 0

func (*Uint256) Clone added in v0.8.2

func (z *Uint256) Clone() *Uint256

Clone creates a new Uint256 identical to z

func (*Uint256) Cmp added in v0.8.2

func (z *Uint256) Cmp(x *Uint256) (r int)

Cmp compares z and x and returns:

-1 if z <  x
 0 if z == x
+1 if z >  x

func (*Uint256) Cmp64 added in v0.8.2

func (z *Uint256) Cmp64(x uint64) int

CmpUint64 compares z and x and returns:

-1 if z <  x
 0 if z == x
+1 if z >  x

func (*Uint256) Div added in v0.8.2

func (z *Uint256) Div(x, y *Uint256) *Uint256

Div sets z to the quotient x/y for returns z. If y == 0, z is set to 0

func (*Uint256) DivMod added in v0.8.2

func (z *Uint256) DivMod(x, y, m *Uint256) (*Uint256, *Uint256)

DivMod sets z to the quotient x div y and m to the modulus x mod y and returns the pair (z, m) for y != 0. If y == 0, both z and m are set to 0 (OBS: differs from the big.Int)

func (*Uint256) Eq added in v0.8.2

func (z *Uint256) Eq(x *Uint256) bool

Eq returns true if z == x

func (*Uint256) Exp added in v0.8.2

func (z *Uint256) Exp(base, exponent *Uint256) *Uint256

Exp sets z = base**exponent mod 2**256, and returns z.

func (*Uint256) ExtendSign added in v0.8.2

func (z *Uint256) ExtendSign(x, byteNum *Uint256) *Uint256

ExtendSign extends length of two’s complement signed integer, sets z to

  • x if byteNum > 30
  • x interpreted as a signed number with sign-bit at (byteNum*8+7), extended to the full 256 bits

and returns z.

func (*Uint256) Gt added in v0.8.2

func (z *Uint256) Gt(x *Uint256) bool

Gt returns true if z > x

func (*Uint256) Gt64 added in v0.8.2

func (z *Uint256) Gt64(n uint64) bool

GtUint64 returns true if z is larger than n

func (*Uint256) Is64 added in v0.8.2

func (z *Uint256) Is64() bool

IsUint64 reports whether z can be represented as a uint64.

func (*Uint256) IsZero added in v0.8.2

func (z *Uint256) IsZero() bool

IsZero returns true if z == 0

func (*Uint256) Log10 added in v0.8.2

func (z *Uint256) Log10() uint

Log10 returns the log in base 10, floored to nearest integer. **OBS** This method returns '0' for '0', not `-Inf`.

func (*Uint256) Lsh added in v0.8.2

func (z *Uint256) Lsh(x *Uint256, n uint) *Uint256

Lsh sets z = x << n and returns z.

func (*Uint256) Lt added in v0.8.2

func (z *Uint256) Lt(x *Uint256) bool

Lt returns true if z < x

func (*Uint256) Lt64 added in v0.8.2

func (z *Uint256) Lt64(n uint64) bool

LtUint64 returns true if z is smaller than n

func (*Uint256) Mod added in v0.8.2

func (z *Uint256) Mod(x, y *Uint256) *Uint256

Mod sets z to the modulus x%y for y != 0 and returns z. If y == 0, z is set to 0 (OBS: differs from the big.Int)

func (*Uint256) Mul added in v0.8.2

func (z *Uint256) Mul(x, y *Uint256) *Uint256

Mul sets z to the product x*y

func (*Uint256) MulDivOverflow added in v0.8.2

func (z *Uint256) MulDivOverflow(x, y, d *Uint256) (*Uint256, bool)

MulDivOverflow calculates (x*y)/d with full precision, returns z and whether overflow occurred in multiply process (result does not fit to 256-bit). computes 512-bit multiplication and 512 by 256 division.

func (*Uint256) MulMod added in v0.8.2

func (z *Uint256) MulMod(x, y, m *Uint256) *Uint256

MulMod calculates the modulo-m multiplication of x and y and returns z. If m == 0, z is set to 0 (OBS: differs from the big.Int)

func (*Uint256) MulModWithReciprocal added in v0.8.2

func (z *Uint256) MulModWithReciprocal(x, y, m *Uint256, mu *[5]uint64) *Uint256

MulModWithReciprocal calculates the modulo-m multiplication of x and y and returns z, using the reciprocal of m provided as the mu parameter. Use uint256.Reciprocal to calculate mu from m. If m == 0, z is set to 0 (OBS: differs from the big.Int)

func (*Uint256) MulOverflow added in v0.8.2

func (z *Uint256) MulOverflow(x, y *Uint256) (*Uint256, bool)

MulOverflow sets z to the product x*y, and returns z and whether overflow occurred

func (*Uint256) Neg added in v0.8.2

func (z *Uint256) Neg(x *Uint256) *Uint256

Neg returns -x mod 2**256.

func (*Uint256) Not added in v0.8.2

func (z *Uint256) Not(x *Uint256) *Uint256

Not sets z = ^x and returns z.

func (*Uint256) Or added in v0.8.2

func (z *Uint256) Or(x, y *Uint256) *Uint256

Or sets z = x | y and returns z.

func (*Uint256) PaddedBytes added in v0.8.2

func (z *Uint256) PaddedBytes(n int) []byte

PaddedBytes encodes a Uint256 as a 0-padded byte slice. The length of the slice is at least n bytes. Example, z =1, n = 20 => [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1]

func (*Uint256) Rsh added in v0.8.2

func (z *Uint256) Rsh(x *Uint256, n uint) *Uint256

Rsh sets z = x >> n and returns z.

func (*Uint256) SDiv added in v0.8.2

func (z *Uint256) SDiv(n, d *Uint256) *Uint256

SDiv interprets n and d as two's complement signed integers, does a signed division on the two operands and sets z to the result. If d == 0, z is set to 0

func (*Uint256) SMod added in v0.8.2

func (z *Uint256) SMod(x, y *Uint256) *Uint256

SMod interprets x and y as two's complement signed integers, sets z to (sign x) * { abs(x) modulus abs(y) } If y == 0, z is set to 0 (OBS: differs from the big.Int)

func (*Uint256) SRsh added in v0.8.2

func (z *Uint256) SRsh(x *Uint256, n uint) *Uint256

SRsh (Signed/Arithmetic right shift) considers z to be a signed integer, during right-shift and sets z = x >> n and returns z.

func (*Uint256) Set added in v0.8.2

func (z *Uint256) Set(x *Uint256) *Uint256

Set sets z to x and returns z.

func (*Uint256) Set64 added in v0.8.2

func (z *Uint256) Set64(x uint64) *Uint256

SetUint64 sets z to the value x

func (*Uint256) SetAllOne added in v0.8.2

func (z *Uint256) SetAllOne() *Uint256

SetAllOne sets all the bits of z to 1

func (*Uint256) SetOne added in v0.8.2

func (z *Uint256) SetOne() *Uint256

SetOne sets z to 1

func (*Uint256) Sgt added in v0.8.2

func (z *Uint256) Sgt(x *Uint256) bool

Sgt interprets z and x as signed integers, and returns true if z > x

func (*Uint256) Sign added in v0.8.2

func (z *Uint256) Sign() int

Sign returns:

-1 if z <  0
 0 if z == 0
+1 if z >  0

Where z is interpreted as a two's complement signed number

func (*Uint256) Slt added in v0.8.2

func (z *Uint256) Slt(x *Uint256) bool

Slt interprets z and x as signed integers, and returns true if z < x

func (*Uint256) Sqrt added in v0.8.2

func (z *Uint256) Sqrt(x *Uint256) *Uint256

Sqrt sets z to ⌊√x⌋, the largest integer such that z² ≤ x, and returns z.

func (*Uint256) Sub added in v0.8.2

func (z *Uint256) Sub(x, y *Uint256) *Uint256

Sub sets z to the difference x-y

func (*Uint256) Sub64 added in v0.8.2

func (z *Uint256) Sub64(x *Uint256, y uint64) *Uint256

SubUint64 set z to the difference x - y, where y is a uint64, and returns z

func (*Uint256) SubOverflow added in v0.8.2

func (z *Uint256) SubOverflow(x, y *Uint256) (*Uint256, bool)

SubOverflow sets z to the difference x-y and returns z and true if the operation underflowed

func (*Uint256) Uint128 added in v0.8.2

func (z *Uint256) Uint128() *Uint128

func (*Uint256) Uint64 added in v0.8.2

func (z *Uint256) Uint64() uint64

Uint64 returns the lower 64-bits of z

func (*Uint256) Uint64WithOverflow added in v0.8.2

func (z *Uint256) Uint64WithOverflow() (uint64, bool)

Uint64WithOverflow returns the lower 64-bits of z and bool whether overflow occurred

func (*Uint256) Xor added in v0.8.2

func (z *Uint256) Xor(x, y *Uint256) *Uint256

Xor sets z = x ^ y and returns z.

type Uint64

type Uint64 uint64

func (Uint64) MarshalJSON

func (i Uint64) MarshalJSON() (data []byte, err error)

func (Uint64) MarshalWithEncoder

func (i Uint64) MarshalWithEncoder(enc *Encoder) error

func (*Uint64) UnmarshalJSON

func (i *Uint64) UnmarshalJSON(data []byte) error

func (*Uint64) UnmarshalWithDecoder

func (i *Uint64) UnmarshalWithDecoder(dec *Decoder) error

type Variant

type Variant interface {
	Assign(typeID TypeID, impl interface{})
	Obtain(*VariantDefinition) (typeID TypeID, typeName string, impl interface{})
}

type VariantDefinition

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

func NewVariantDefinition

func NewVariantDefinition(typeIDEncoding TypeIDEncoding, types []VariantType) (out *VariantDefinition)

NewVariantDefinition creates a variant definition based on the *ordered* provided types.

  • For anchor instructions, it's the name that defines the binary variant value.
  • For all other types, it's the ordering that defines the binary variant value just like in native `nodeos` C++ and in Smart Contract via the `std::variant` type. It's important to pass the entries in the right order!

This variant definition can now be passed to functions of `BaseVariant` to implement marshal/unmarshaling functionalities for binary & JSON.

func (*VariantDefinition) TypeID

func (d *VariantDefinition) TypeID(name string) TypeID

type VariantImplFactory

type VariantImplFactory = func() interface{}

type VariantType

type VariantType struct {
	Name string
	Type interface{}
}

type Varint16

type Varint16 int16

func (Varint16) MarshalWithEncoder

func (o Varint16) MarshalWithEncoder(encoder *Encoder) error

func (*Varint16) UnmarshalWithDecoder

func (o *Varint16) UnmarshalWithDecoder(decoder *Decoder) error

type Varint32

type Varint32 int32

func (Varint32) MarshalWithEncoder

func (o Varint32) MarshalWithEncoder(encoder *Encoder) error

func (*Varint32) UnmarshalWithDecoder

func (o *Varint32) UnmarshalWithDecoder(decoder *Decoder) error

type Varuint16

type Varuint16 uint16

func (Varuint16) MarshalWithEncoder

func (o Varuint16) MarshalWithEncoder(encoder *Encoder) error

func (*Varuint16) UnmarshalWithDecoder

func (o *Varuint16) UnmarshalWithDecoder(decoder *Decoder) error

type Varuint32

type Varuint32 uint32

func (Varuint32) MarshalWithEncoder

func (o Varuint32) MarshalWithEncoder(encoder *Encoder) error

func (*Varuint32) UnmarshalWithDecoder

func (o *Varuint32) UnmarshalWithDecoder(decoder *Decoder) error

type WriteByWrite

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

func NewWriteByWrite

func NewWriteByWrite(name string) *WriteByWrite

func (*WriteByWrite) Bytes

func (rec *WriteByWrite) Bytes() []byte

func (WriteByWrite) String

func (rec WriteByWrite) String() string

func (*WriteByWrite) Write

func (rec *WriteByWrite) Write(b []byte) (int, error)

Jump to

Keyboard shortcuts

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