common

package
v0.0.9-Bytes-0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2020 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package common contains various helper functions.

Copyright © 2019 Annchain Authors <EMAIL ADDRESS>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2019 Annchain Authors <EMAIL ADDRESS>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2019 Annchain Authors <EMAIL ADDRESS>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	AddressLength = 20
)

Length of Addresses in bytes.

View Source
const (
	HashLength = 32
)

Length of hash in bytes.

Variables

View Source
var (
	Big1   = big.NewInt(1)
	Big2   = big.NewInt(2)
	Big3   = big.NewInt(3)
	Big0   = big.NewInt(0)
	Big32  = big.NewInt(32)
	Big256 = big.NewInt(256)
	Big257 = big.NewInt(257)
)

Common big integers often used

Functions

func ByteInt32 added in v0.0.2

func ByteInt32(i int32) []byte

ByteInt32 convert an int32 to byte array.

func Bytes2Hex

func Bytes2Hex(d []byte) string

Bytes2Hex returns the hexadecimal encoding of d.

func CopyBytes

func CopyBytes(b []byte) (copiedBytes []byte)

CopyBytes returns an exact copy of the provided bytes.

func FromHex

func FromHex(s string) []byte

FromHex returns the bytes represented by the hexadecimal string s. s may be prefixed with "0x".

func GetInt32 added in v0.0.2

func GetInt32(b []byte, pos int) int32

GetInt32 get an int32 from byte array with a start position.

func Hex2Bytes

func Hex2Bytes(str string) []byte

Hex2Bytes returns the bytes represented by the hexadecimal string str.

func Hex2BytesFixed

func Hex2BytesFixed(str string, flen int) []byte

Hex2BytesFixed returns bytes of a specified fixed length flen.

func IsSameBytes

func IsSameBytes(s, t []byte) bool

IsSameBytes compares two byte slice. Return true if they are the same, otherwise false.

func LeftPadBytes

func LeftPadBytes(slice []byte, l int) []byte

LeftPadBytes zero-pads slice to the left up to length l.

func PrettyJson added in v0.0.2

func PrettyJson(data interface{}) (string, error)

dont do this, see above edit

func RightPadBytes

func RightPadBytes(slice []byte, l int) []byte

RightPadBytes zero-pads slice to the right up to length l.

func SetInt32 added in v0.0.2

func SetInt32(b []byte, pos int, i int32)

SetInt32 set an int32 into byte array at a position.

func ToHex deprecated

func ToHex(b []byte) string

ToHex returns the hex representation of b, prefixed with '0x'. For empty slices, the return value is "0x0".

Deprecated: use hexutil.Encode instead.

Types

type Address added in v0.0.2

type Address struct {
	Bytes [AddressLength]byte `msgp:"bytes"`
}

Address represents the 20 byte of address.

func BigToAddress added in v0.0.2

func BigToAddress(b *big.Int) Address

BigToAddress sets byte representation of b to Address. If b is larger than len(h), b will be cropped from the left.

func BytesToAddress added in v0.0.2

func BytesToAddress(b []byte) Address

BytesToAddress sets b to hash. If b is larger than len(h), b will be cropped from the left.

func HexToAddress added in v0.0.2

func HexToAddress(s string) Address

HexToAddress sets byte representation of s to Address. If b is larger than len(h), b will be cropped from the left.

func RandomAddress added in v0.0.2

func RandomAddress() Address

func StringToAddress added in v0.0.2

func StringToAddress(s string) (add Address, err error)

func (Address) Big added in v0.0.2

func (h Address) Big() *big.Int

Big converts an Address to a big integer.

func (*Address) DecodeMsg added in v0.0.2

func (z *Address) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (*Address) EncodeMsg added in v0.0.2

func (z *Address) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (Address) EqualTo added in v0.0.4

func (h Address) EqualTo(b Address) bool

func (Address) Format added in v0.0.2

func (h Address) Format(s fmt.State, c rune)

Format implements fmt.Formatter, forcing the byte slice to be formatted as is, without going through the stringer interface used for logging.

func (Address) Generate added in v0.0.2

func (h Address) Generate(rand *rand.Rand, size int) reflect.Value

Generate implements testing/quick.Generator.

func (Address) Hex added in v0.0.2

func (h Address) Hex() string

Hex converts a Address to a hex string.

func (*Address) MarshalMsg added in v0.0.2

func (z *Address) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (Address) MarshalText added in v0.0.2

func (h Address) MarshalText() ([]byte, error)

MarshalText returns the hex representation of h.

func (*Address) Msgsize added in v0.0.2

func (z *Address) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*Address) MustSetBytes added in v0.0.2

func (h *Address) MustSetBytes(b []byte)

SetBytes sets the Address to the value of b. If b is larger than len(h), panic. It usually indicates a logic error.

func (*Address) SetBytes added in v0.0.2

func (h *Address) SetBytes(b []byte) error

func (Address) ShortString added in v0.0.2

func (h Address) ShortString() string

func (Address) String added in v0.0.2

func (h Address) String() string

String implements the stringer interface and is used also by the logger when doing full logging into a file.

func (Address) TerminalString added in v0.0.2

func (h Address) TerminalString() string

TerminalString implements log.TerminalStringer, formatting a string for console output during logging.

func (Address) ToBytes added in v0.0.2

func (h Address) ToBytes() []byte

ToBytes convers Address to []byte.

func (*Address) UnmarshalJSON added in v0.0.2

func (h *Address) UnmarshalJSON(input []byte) error

UnmarshalJSON parses an Address in hex syntax.

func (*Address) UnmarshalMsg added in v0.0.2

func (z *Address) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

func (*Address) UnmarshalText added in v0.0.2

func (h *Address) UnmarshalText(input []byte) error

UnmarshalText parses an Address in hex syntax.

type Hash added in v0.0.2

type Hash struct {
	Bytes [HashLength]byte `msgp:"bytes"`
}

Hash represents the 32 byte of Hash.

func BigToHash added in v0.0.2

func BigToHash(b *big.Int) Hash

BigToHash sets byte representation of b to Hash. If b is larger than len(h), b will be cropped from the left.

func BytesToHash added in v0.0.2

func BytesToHash(b []byte) Hash

BytesToHash sets b to hash. If b is larger than len(h), b will be cropped from the left.

func HexStringToHash added in v0.0.2

func HexStringToHash(s string) (Hash, error)

func HexToHash added in v0.0.2

func HexToHash(s string) Hash

HexToHash sets byte representation of s to Hash. If b is larger than len(h), b will be cropped from the left.

func RandomHash added in v0.0.2

func RandomHash() Hash

func (Hash) Big added in v0.0.2

func (h Hash) Big() *big.Int

Big converts an Hash to a big integer.

func (Hash) Cmp added in v0.0.2

func (h Hash) Cmp(another Hash) int

Cmp compares two hashes. Returns 0 if two hashes are same. Returns -1 if the self hash is less than parameter hash. Returns 1 if the self hash is larger than parameter hash.

func (*Hash) DecodeMsg added in v0.0.2

func (z *Hash) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (*Hash) Empty added in v0.0.2

func (h *Hash) Empty() bool

func (*Hash) EncodeMsg added in v0.0.2

func (z *Hash) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (Hash) Generate added in v0.0.2

func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value

Generate implements testing/quick.Generator.

func (Hash) Hex added in v0.0.2

func (h Hash) Hex() string

Hex converts a Hash to a hex string.

func (*Hash) MarshalMsg added in v0.0.2

func (z *Hash) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (Hash) MarshalText added in v0.0.2

func (h Hash) MarshalText() ([]byte, error)

MarshalText returns the hex representation of h.

func (*Hash) Msgsize added in v0.0.2

func (z *Hash) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*Hash) MustSetBytes added in v0.0.2

func (h *Hash) MustSetBytes(b []byte, padding Padding)

SetBytes sets the Hash to the value of b. If b is larger than len(h), panic. It usually indicates a logic error.

func (*Hash) SetBytes added in v0.0.2

func (h *Hash) SetBytes(b []byte) error

func (Hash) String added in v0.0.2

func (h Hash) String() string

String implements the stringer interface and is used also by the logger when doing full logging into a file.

func (Hash) TerminalString added in v0.0.2

func (h Hash) TerminalString() string

TerminalString implements log.TerminalStringer, formatting a string for console output during logging.

func (Hash) ToBytes added in v0.0.2

func (h Hash) ToBytes() []byte

ToBytes convers hash to []byte.

func (*Hash) UnmarshalJSON added in v0.0.2

func (h *Hash) UnmarshalJSON(input []byte) error

UnmarshalJSON parses an Hash in hex syntax.

func (*Hash) UnmarshalMsg added in v0.0.2

func (z *Hash) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

func (*Hash) UnmarshalText added in v0.0.2

func (h *Hash) UnmarshalText(input []byte) error

UnmarshalText parses an Hash in hex syntax.

type HashBytes added in v0.0.2

type HashBytes [HashLength]byte

func (*HashBytes) DecodeMsg added in v0.0.2

func (z *HashBytes) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (*HashBytes) EncodeMsg added in v0.0.2

func (z *HashBytes) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (*HashBytes) MarshalMsg added in v0.0.2

func (z *HashBytes) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (*HashBytes) Msgsize added in v0.0.2

func (z *HashBytes) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*HashBytes) UnmarshalMsg added in v0.0.2

func (z *HashBytes) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

type Hashes added in v0.0.2

type Hashes []Hash

func (*Hashes) DecodeMsg added in v0.0.2

func (z *Hashes) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (Hashes) EncodeMsg added in v0.0.2

func (z Hashes) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (Hashes) Len added in v0.0.2

func (a Hashes) Len() int

func (Hashes) Less added in v0.0.2

func (a Hashes) Less(i, j int) bool

func (Hashes) MarshalMsg added in v0.0.2

func (z Hashes) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (Hashes) Msgsize added in v0.0.2

func (z Hashes) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (Hashes) String added in v0.0.2

func (h Hashes) String() string

func (Hashes) Swap added in v0.0.2

func (a Hashes) Swap(i, j int)

func (*Hashes) UnmarshalMsg added in v0.0.2

func (z *Hashes) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

type IOSize added in v0.0.2

type IOSize float64

StorageSize is a wrapper around a float value that supports user friendly formatting.

func (IOSize) String added in v0.0.2

func (s IOSize) String() string

String implements the stringer interface.

func (IOSize) TerminalString added in v0.0.2

func (s IOSize) TerminalString() string

TerminalString implements log.TerminalStringer, formatting a string for console output during logging.

type Padding added in v0.0.2

type Padding uint
const (
	PaddingLeft Padding = iota
	PaddingRight
	PaddingNone
)

func (*Padding) DecodeMsg added in v0.0.2

func (z *Padding) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (Padding) EncodeMsg added in v0.0.2

func (z Padding) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (Padding) MarshalMsg added in v0.0.2

func (z Padding) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (Padding) Msgsize added in v0.0.2

func (z Padding) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*Padding) UnmarshalMsg added in v0.0.2

func (z *Padding) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

type PrettyDuration

type PrettyDuration time.Duration

PrettyDuration is a pretty printed version of a time.Duration value that cuts the unnecessary precision off from the formatted textual representation.

func (PrettyDuration) String

func (d PrettyDuration) String() string

String implements the Stringer interface, allowing pretty printing of duration values rounded to three decimals.

type StorageSize

type StorageSize float64

StorageSize is a wrapper around a float value that supports user friendly formatting.

func (StorageSize) String

func (s StorageSize) String() string

String implements the stringer interface.

func (StorageSize) TerminalString

func (s StorageSize) TerminalString() string

TerminalString implements log.TerminalStringer, formatting a string for console output during logging.

Directories

Path Synopsis
Package bitutil implements fast bitwise operations.
Package bitutil implements fast bitwise operations.
bn256
Package bn256 implements the Optimal Ate pairing over a 256-bit Barreto-Naehrig curve.
Package bn256 implements the Optimal Ate pairing over a 256-bit Barreto-Naehrig curve.
bn256/cloudflare
Package bn256 implements a particular bilinear group at the 128-bit security level.
Package bn256 implements a particular bilinear group at the 128-bit security level.
bn256/google
Package bn256 implements a particular bilinear group.
Package bn256 implements a particular bilinear group.
secp256k1
Package secp256k1 wraps the bitcoin secp256k1 C library.
Package secp256k1 wraps the bitcoin secp256k1 C library.
Package hexutil implements hex encoding with 0x prefix.
Package hexutil implements hex encoding with 0x prefix.
Package mclock is a wrapper for a monotonic clock source
Package mclock is a wrapper for a monotonic clock source

Jump to

Keyboard shortcuts

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