common

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2022 License: MIT Imports: 16 Imported by: 396

Documentation

Index

Constants

View Source
const (
	UINT168SIZE = 21
	// Address types
	STANDARD   = 0xAC
	DID        = 0xAD
	MULTISIG   = 0xAE
	CROSSCHAIN = 0xAF
)
View Source
const (
	// MaxVarStringLength is the maximum bytes a var string.
	MaxVarStringLength = 1024 * 1024 * 16 // 16MB

)
View Source
const UINT160SIZE int = 20
View Source
const UINT256SIZE = 32

Variables

View Source
var EmptyHash = Uint256{}
View Source
var EmptyProgramHash = Uint168{}

Functions

func BytesReverse

func BytesReverse(u []byte) []byte

func BytesToHexString

func BytesToHexString(data []byte) string

func BytesToInt16 added in v0.2.2

func BytesToInt16(b []byte) int16

func ClearBytes added in v0.2.2

func ClearBytes(arr []byte)

func FromReversedString added in v0.7.0

func FromReversedString(reversed string) ([]byte, error)

func FuncError added in v0.2.2

func FuncError(f string, desc string) error

FuncError creates an error for the given function and description.

func GetIpFromAddr added in v0.8.0

func GetIpFromAddr(addr string) string

get ip string from addr string if addr is not the format x.x.x.x:port return empt string ""

func Goid added in v0.3.0

func Goid() string

Goid returns the current goroutine id.

func HexStringToBytes

func HexStringToBytes(value string) ([]byte, error)

func IntToBytes added in v0.2.2

func IntToBytes(n int) []byte

func IsLetterOrNumber added in v0.8.0

func IsLetterOrNumber(s string) bool

check if the string is only letter or number.

func ReadBytes added in v0.2.2

func ReadBytes(r io.Reader, length uint64) ([]byte, error)

func ReadElement added in v0.2.2

func ReadElement(r io.Reader, element interface{}) (err error)

readElement reads the next sequence of bytes from r using little endian depending on the concrete type of element pointed to.

func ReadElements added in v0.2.2

func ReadElements(r io.Reader, elements ...interface{}) error

ReadElements reads multiple items from r. It is equivalent to multiple calls to ReadElement.

func ReadUint16 added in v0.2.2

func ReadUint16(r io.Reader) (uint16, error)

func ReadUint32 added in v0.2.2

func ReadUint32(r io.Reader) (uint32, error)

func ReadUint64 added in v0.2.2

func ReadUint64(r io.Reader) (uint64, error)

func ReadUint8 added in v0.2.2

func ReadUint8(r io.Reader) (uint8, error)

func ReadVarBytes added in v0.2.2

func ReadVarBytes(r io.Reader, maxAllowed uint32, fieldName string) ([]byte, error)

func ReadVarString added in v0.2.2

func ReadVarString(r io.Reader) (string, error)

func ReadVarUint added in v0.2.2

func ReadVarUint(r io.Reader, pver uint32) (uint64, error)

ReadVarUint reads a variable length integer from r and returns it as a uint64.

func Sha256D added in v0.2.2

func Sha256D(data []byte) [32]byte

func SortProgramHashByCodeHash added in v0.2.2

func SortProgramHashByCodeHash(hashes []Uint168)

func SortUint160 added in v0.2.2

func SortUint160(hashes []Uint160)

func ToReversedString added in v0.7.0

func ToReversedString(hash Uint256) string

func VarIntSerializeSize added in v0.5.0

func VarIntSerializeSize(val uint64) int

VarIntSerializeSize returns the number of bytes it would take to serialize val as a variable length integer.

func VarUintSerializeSize added in v0.2.2

func VarUintSerializeSize(val uint64) int

VarUintSerializeSize returns the number of bytes it would take to serialize val as a variable length integer.

func WriteElement added in v0.2.2

func WriteElement(w io.Writer, element interface{}) (err error)

WriteElement writes the little endian representation of element to w.

func WriteElements added in v0.2.2

func WriteElements(w io.Writer, elements ...interface{}) error

WriteElements writes multiple items to w. It is equivalent to multiple calls to WriteElement.

func WriteUint16 added in v0.2.2

func WriteUint16(w io.Writer, val uint16) error

func WriteUint32 added in v0.2.2

func WriteUint32(w io.Writer, val uint32) error

func WriteUint64 added in v0.2.2

func WriteUint64(w io.Writer, val uint64) error

func WriteUint8 added in v0.2.2

func WriteUint8(w io.Writer, val uint8) error

func WriteVarBytes added in v0.2.2

func WriteVarBytes(w io.Writer, bytes []byte) error

func WriteVarString added in v0.2.2

func WriteVarString(w io.Writer, str string) error

func WriteVarUint added in v0.2.2

func WriteVarUint(w io.Writer, val uint64) error

Types

type BinaryFreeList added in v0.5.0

type BinaryFreeList chan []byte

BinaryFreeList defines a concurrent safe free list of byte slices (up to the maximum number defined by the binaryFreeListMaxItems constant) that have a cap of 8 (thus it supports up to a uint64). It is used to provide temporary buffers for serializing and deserializing primitive numbers to and from their binary encoding in order to greatly reduce the number of allocations required.

For convenience, functions are provided for each of the primitive unsigned integers that automatically obtain a buffer from the free list, perform the necessary binary conversion, read from or write to the given io.Reader or io.Writer, and return the buffer to the free list.

var BinarySerializer BinaryFreeList = make(chan []byte, binaryFreeListMaxItems)

BinarySerializer provides a free list of buffers to use for serializing and deserializing primitive integer values to and from io.Readers and io.Writers.

func (BinaryFreeList) Borrow added in v0.5.0

func (l BinaryFreeList) Borrow() []byte

Borrow returns a byte slice from the free list with a length of 8. A new buffer is allocated if there are not any available on the free list.

func (BinaryFreeList) PutUint16 added in v0.5.0

func (l BinaryFreeList) PutUint16(w io.Writer, byteOrder binary.ByteOrder, val uint16) error

PutUint16 serializes the provided uint16 using the given byte order into a buffer from the free list and writes the resulting two bytes to the given writer.

func (BinaryFreeList) PutUint32 added in v0.5.0

func (l BinaryFreeList) PutUint32(w io.Writer, byteOrder binary.ByteOrder, val uint32) error

PutUint32 serializes the provided uint32 using the given byte order into a buffer from the free list and writes the resulting four bytes to the given writer.

func (BinaryFreeList) PutUint64 added in v0.5.0

func (l BinaryFreeList) PutUint64(w io.Writer, byteOrder binary.ByteOrder, val uint64) error

PutUint64 serializes the provided uint64 using the given byte order into a buffer from the free list and writes the resulting eight bytes to the given writer.

func (BinaryFreeList) PutUint8 added in v0.5.0

func (l BinaryFreeList) PutUint8(w io.Writer, val uint8) error

PutUint8 copies the provided uint8 into a buffer from the free list and writes the resulting byte to the given writer.

func (BinaryFreeList) Return added in v0.5.0

func (l BinaryFreeList) Return(buf []byte)

Return puts the provided byte slice back on the free list. The buffer MUST have been obtained via the Borrow function and therefore have a cap of 8.

func (BinaryFreeList) Uint16 added in v0.5.0

func (l BinaryFreeList) Uint16(r io.Reader, byteOrder binary.ByteOrder) (uint16, error)

Uint16 reads two bytes from the provided reader using a buffer from the free list, converts it to a number using the provided byte order, and returns the resulting uint16.

func (BinaryFreeList) Uint32 added in v0.5.0

func (l BinaryFreeList) Uint32(r io.Reader, byteOrder binary.ByteOrder) (uint32, error)

Uint32 reads four bytes from the provided reader using a buffer from the free list, converts it to a number using the provided byte order, and returns the resulting uint32.

func (BinaryFreeList) Uint64 added in v0.5.0

func (l BinaryFreeList) Uint64(r io.Reader, byteOrder binary.ByteOrder) (uint64, error)

Uint64 reads eight bytes from the provided reader using a buffer from the free list, converts it to a number using the provided byte order, and returns the resulting uint64.

func (BinaryFreeList) Uint8 added in v0.5.0

func (l BinaryFreeList) Uint8(r io.Reader) (uint8, error)

Uint8 reads a single byte from the provided reader using a buffer from the free list and returns it as a uint8.

type Fixed64

type Fixed64 int64

the 64 bit fixed-point number, precise 10^-8

func Fixed64FromBytes added in v0.2.2

func Fixed64FromBytes(value []byte) (*Fixed64, error)

func StringToFixed64 added in v0.2.2

func StringToFixed64(s string) (*Fixed64, error)

func (*Fixed64) Bytes added in v0.2.2

func (f *Fixed64) Bytes() ([]byte, error)

func (*Fixed64) Deserialize

func (f *Fixed64) Deserialize(r io.Reader) error

func (Fixed64) IntValue added in v0.2.2

func (f Fixed64) IntValue() int64

func (*Fixed64) Serialize

func (f *Fixed64) Serialize(w io.Writer) error

func (Fixed64) String

func (f Fixed64) String() string

type Serializable added in v0.2.2

type Serializable interface {
	//Write data to writer
	Serialize(w io.Writer) error

	//read data to reader
	Deserialize(r io.Reader) error
}

Serializable describe the data need be serialized.

type Uint160 added in v0.2.2

type Uint160 [UINT160SIZE]uint8

func ToCodeHash added in v0.2.2

func ToCodeHash(code []byte) *Uint160

func Uint160FromBytes added in v0.2.2

func Uint160FromBytes(bytes []byte) (Uint160, error)

func (Uint160) Bytes added in v0.2.2

func (u Uint160) Bytes() []byte

func (Uint160) Compare added in v0.2.2

func (u Uint160) Compare(o Uint160) int

func (*Uint160) Deserialize added in v0.2.2

func (u *Uint160) Deserialize(r io.Reader) error

func (Uint160) IsEqual added in v0.2.2

func (u Uint160) IsEqual(o Uint160) bool

func (*Uint160) Serialize added in v0.2.2

func (u *Uint160) Serialize(w io.Writer) error

type Uint168

type Uint168 [UINT168SIZE]uint8

func ToProgramHash added in v0.2.2

func ToProgramHash(prefix byte, code []byte) *Uint168

func Uint168FromAddress added in v0.2.2

func Uint168FromAddress(address string) (*Uint168, error)

func Uint168FromBytes

func Uint168FromBytes(bytes []byte) (*Uint168, error)

func Uint168FromCodeHash added in v0.2.2

func Uint168FromCodeHash(prefixType byte, codeHash Uint160) Uint168

func (Uint168) Bytes added in v0.2.2

func (u Uint168) Bytes() []byte

func (Uint168) Compare added in v0.2.2

func (u Uint168) Compare(o Uint168) int

func (*Uint168) Deserialize

func (u *Uint168) Deserialize(r io.Reader) error

func (Uint168) IsEqual added in v0.2.2

func (u Uint168) IsEqual(o Uint168) bool

func (*Uint168) Serialize

func (u *Uint168) Serialize(w io.Writer) error

func (Uint168) String

func (u Uint168) String() string

func (Uint168) ToAddress

func (u Uint168) ToAddress() (string, error)

func (Uint168) ToCodeHash added in v0.2.2

func (u Uint168) ToCodeHash() Uint160

type Uint256

type Uint256 [UINT256SIZE]uint8

func Hash added in v0.5.0

func Hash(data []byte) Uint256

func Uint256FromBytes added in v0.2.2

func Uint256FromBytes(f []byte) (*Uint256, error)

func Uint256FromHexString added in v0.0.4

func Uint256FromHexString(hexHash string) (*Uint256, error)

func (Uint256) Bytes added in v0.2.2

func (u Uint256) Bytes() []byte

func (Uint256) Compare added in v0.2.2

func (u Uint256) Compare(o Uint256) int

func (*Uint256) Deserialize

func (u *Uint256) Deserialize(r io.Reader) error

func (Uint256) IsEqual added in v0.2.2

func (u Uint256) IsEqual(o Uint256) bool

func (*Uint256) Serialize

func (u *Uint256) Serialize(w io.Writer) error

func (Uint256) String

func (u Uint256) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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