utils

package
v0.0.0-...-91a82d4 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2019 License: LGPL-3.0 Imports: 15 Imported by: 37

README

utils

Package utils contains various helper functions.

Documentation

Overview

Package utils contains various helper functions.

Index

Constants

View Source
const HashLength = 32

HashLength of hashes 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

View Source
var (
	ErrLeadingZero   = &decError{"hex number with leading zero digits"}
	ErrUint64Range   = &decError{"hex number > 64 bits"}
	ErrUintRange     = &decError{fmt.Sprintf("hex number > %d bits", uintBits)}
	ErrBig256Range   = &decError{"hex number > 256 bits"}
	ErrEmptyString   = &decError{"empty hex string"}
	ErrSyntax        = &decError{"invalid hex string"}
	ErrMissingPrefix = &decError{"hex string without 0x prefix"}
	ErrOddLength     = &decError{"hex string of odd length"}
	ErrEmptyNumber   = &decError{"hex string \"0x\""}
)

Errors

View Source
var EmptyAddress = Address{}

EmptyAddress presents an empty address

View Source
var ErrGasLimitReached = errors.New("gas limit reached")

Functions

func BytesToHex

func BytesToHex(d []byte) string

BytesToHex returns the hexadecimal encoding of d.

func CopyBytes

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

CopyBytes returns an exact copy of the provided bytes.

func Decode

func Decode(input string) ([]byte, error)

Decode decodes a hex string with 0x prefix.

func DecodeBig

func DecodeBig(input string) (*big.Int, error)

DecodeBig decodes a hex string with 0x prefix as a quantity. Numbers larger than 256 bits are not accepted.

func DecodeUint64

func DecodeUint64(input string) (uint64, error)

DecodeUint64 decodes a hex string with 0x prefix as a quantity.

func EmptyHash

func EmptyHash(h Hash) bool

func Encode

func Encode(b []byte) string

Encode encodes b as a hex string with 0x prefix.

func EncodeBig

func EncodeBig(bigint *big.Int) string

EncodeBig encodes bigint as a hex string with 0x prefix. The sign of the integer is ignored.

func EncodeUint64ToByte

func EncodeUint64ToByte(i uint64) []byte

EncodeUint64ToByte encodes i as a bytes with 0x prefix.

func EncodeUint64ToHex

func EncodeUint64ToHex(i uint64) string

EncodeUint64ToHex encodes i as a hex string with 0x prefix.

func EnvParse

func EnvParse()

func EnvString

func EnvString(name string, value string) *string

func EnvStringVar

func EnvStringVar(p *string, name string, value string)

func FileExists

func FileExists(file string) bool

FileExists checks if a file or folder exists

func FromHex

func FromHex(s string) []byte

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

func HexToBytes

func HexToBytes(str string) []byte

HexToBytes returns the bytes represented by the hexadecimal string str.

func HexToBytesFixed

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

HexToBytesFixed returns bytes of a specified fixed length flen.

func IsDirExist

func IsDirExist(path string) (bool, error)

IsDirExist determines whether a directory exists

func IsHexAddr

func IsHexAddr(s string) bool

IsHexAddr verifies whether a string can represent a valid hex-encoded address or not.

func IsHexHash

func IsHexHash(s string) bool

IsHexHash verifies whether a string can represent a valid hex-encoded hash or not.

func LeftPadBytes

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

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

func OpenDir

func OpenDir(dir string) (string, error)

OpenDir opens or creates a dir If the dir already exists, open it . If it does not, It will create the file with mode 0700.

func OpenFile

func OpenFile(path string) (*os.File, error)

OpenFile opens or creates a file If the file already exists, open it . If it does not, It will create the file with mode 0644.

func ReferenceBig

func ReferenceBig(s string) *big.Int

func ReferenceBytes

func ReferenceBytes(s string) []byte

func RemovePrefix

func RemovePrefix(s string) string

func RightPadBytes

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

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

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.

func UnmarshalFixedJSON

func UnmarshalFixedJSON(typ reflect.Type, input, out []byte) error

UnmarshalFixedJSON decodes the input as a string with 0x prefix. The length of out determines the required input length. This function is commonly used to implement the UnmarshalJSON method for fixed-size types.

func UnmarshalFixedText

func UnmarshalFixedText(typname string, input, out []byte) error

UnmarshalFixedText decodes the input as a string with 0x prefix. The length of out determines the required input length. This function is commonly used to implement the UnmarshalText method for fixed-size types.

func UnmarshalFixedUnprefixedText

func UnmarshalFixedUnprefixedText(typname string, input, out []byte) error

UnmarshalFixedUnprefixedText decodes the input as a string with optional 0x prefix. The length of out determines the required input length. This function is commonly used to implement the UnmarshalText method for fixed-size types.

Types

type AbsTime

type AbsTime time.Duration

AbsTime represents absolute monotonic time.

func Now

func Now() AbsTime

Now returns the current absolute monotonic time.

type Address

type Address [addressSize]byte

Address represents the 20 byte address of an account.

func BigToAddress

func BigToAddress(b *big.Int) Address

BigToAddress returns Address with byte values of b.

func BytesToAddress

func BytesToAddress(b []byte) Address

BytesToAddress returns Address with value b.

func HexToAddress

func HexToAddress(s string) Address

HexToAddress returns Address with byte values of s.

func (Address) Big

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

Big converts an address to a big integer.

func (Address) Bytes

func (a Address) Bytes() []byte

Bytes gets the string representation of the underlying address.

func (*Address) Equal

func (a *Address) Equal(b Address) bool

Equal checks if this address is the same with the specified address b.

func (Address) Hash

func (a Address) Hash() Hash

Hash converts an address to a hash by left-padding it with zeros.

func (Address) Hex

func (a Address) Hex() string

Hex returns an EIP55-compliant hex string representation of the address.

func (*Address) IsEmpty

func (a *Address) IsEmpty() bool

IsEmpty returns true if this address is empty. Otherwise, false.

func (Address) MarshalText

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

MarshalText returns the hex representation of a.

func (*Address) SetBytes

func (a *Address) SetBytes(b []byte)

SetBytes sets the address to the value of b.

func (Address) String

func (a Address) String() string

String implements fmt.Stringer.

func (*Address) UnmarshalJSON

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

UnmarshalJSON parses a hash in hex syntax.

func (*Address) UnmarshalText

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

UnmarshalText parses a hash in hex syntax.

type Big

type Big big.Int

Big marshals/unmarshals as a JSON string with 0x prefix. The zero value marshals as "0x0".

Negative integers are not supported at this time. Attempting to marshal them will return an error. Values larger than 256bits are rejected by Unmarshal but will be marshaled without error.

func (*Big) Add

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

Add z = x+y; return z

func (Big) MarshalText

func (b Big) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler

func (*Big) Sadd

func (z *Big) Sadd(y *Big) *Big

Sadd z+=y, return z

func (*Big) Ssub

func (z *Big) Ssub(y *Big) *Big

Ssub z-= y; return z

func (*Big) String

func (b *Big) String() string

String returns the hex encoding of b.

func (*Big) Sub

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

Sub z = x = y; return z

func (*Big) ToInt

func (b *Big) ToInt() *big.Int

ToInt converts b to a big.Int.

func (*Big) UnmarshalJSON

func (b *Big) UnmarshalJSON(input []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Big) UnmarshalText

func (b *Big) UnmarshalText(input []byte) error

UnmarshalText implements encoding.TextUnmarshaler

type Bytes

type Bytes []byte

Bytes marshals/unmarshals as a JSON string with 0x prefix. The empty slice marshals as "0x".

func (Bytes) MarshalText

func (b Bytes) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler

func (Bytes) String

func (b Bytes) String() string

String returns the hex encoding of b.

func (*Bytes) UnmarshalJSON

func (b *Bytes) UnmarshalJSON(input []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Bytes) UnmarshalText

func (b *Bytes) UnmarshalText(input []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type GasPool

type GasPool uint64

func (*GasPool) AddGas

func (gp *GasPool) AddGas(amount uint64) *GasPool

AddGas makes gas available for execution.

func (*GasPool) Gas

func (gp *GasPool) Gas() uint64

Gas returns the amount of gas remaining in the pool.

func (GasPool) String

func (gp GasPool) String() string

func (*GasPool) SubGas

func (gp *GasPool) SubGas(amount uint64) error

SubGas deducts the given amount from the pool

type Hash

type Hash [HashLength]byte

Hash represents the 32 byte Keccak256 hash of arbitrary data.

func BigToHash

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

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 HexToHash

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 (Hash) Big

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

Big converts a hash to a big integer.

func (Hash) Bytes

func (h Hash) Bytes() []byte

Bytes gets the byte representation of the underlying hash.

func (Hash) Format

func (h Hash) 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 (Hash) Generate

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

Generate implements testing/quick.Generator.

func (Hash) Hex

func (h Hash) Hex() string

Hex converts a hash to a hex string.

func (Hash) MarshalText

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

MarshalText returns the hex representation of h.

func (*Hash) SetBytes

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

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

func (Hash) String

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

func (h Hash) TerminalString() string

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

func (*Hash) UnmarshalJSON

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

UnmarshalJSON parses a hash in hex syntax.

func (*Hash) UnmarshalText

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

UnmarshalText parses a hash in hex syntax.

type MixedcaseAddress

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

MixedcaseAddress retains the original string, which may or may not becorrectly checksummed.

func NewMixedcaseAddress

func NewMixedcaseAddress(addr Address) MixedcaseAddress

NewMixedcaseAddress constructor (mainly for testing)

func NewMixedcaseAddressFromString

func NewMixedcaseAddressFromString(hexaddr string) (*MixedcaseAddress, error)

NewMixedcaseAddressFromString is mainly meant for unit-testing

func (*MixedcaseAddress) Address

func (ma *MixedcaseAddress) Address() Address

Address returns the address

func (*MixedcaseAddress) MarshalJSON

func (ma *MixedcaseAddress) MarshalJSON() ([]byte, error)

MarshalJSON marshals the original value

func (*MixedcaseAddress) Original

func (ma *MixedcaseAddress) Original() string

Original returns the mixed-case input string

func (*MixedcaseAddress) String

func (ma *MixedcaseAddress) String() string

String implements fmt.Stringer

func (*MixedcaseAddress) UnmarshalJSON

func (ma *MixedcaseAddress) UnmarshalJSON(input []byte) error

UnmarshalJSON parses MixedcaseAddress

func (*MixedcaseAddress) ValidChecksum

func (ma *MixedcaseAddress) ValidChecksum() bool

ValidChecksum returns true if the address has valid checksum

type NoprefixedAddress

type NoprefixedAddress Address

NoprefixedAddress allows marshaling an Address without 0x prefix.

func (NoprefixedAddress) MarshalText

func (a NoprefixedAddress) MarshalText() ([]byte, error)

MarshalText encodes the address as hex.

func (*NoprefixedAddress) UnmarshalText

func (a *NoprefixedAddress) UnmarshalText(input []byte) error

UnmarshalText decodes the address from hex. The 0x prefix is optional.

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.

type Uint

type Uint uint

Uint marshals/unmarshals as a JSON string with 0x prefix. The zero value marshals as "0x0".

func (Uint) MarshalText

func (b Uint) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (Uint) String

func (b Uint) String() string

String returns the hex encoding of b.

func (*Uint) UnmarshalJSON

func (b *Uint) UnmarshalJSON(input []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Uint) UnmarshalText

func (b *Uint) UnmarshalText(input []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Uint64

type Uint64 uint64

Uint64 marshals/unmarshals as a JSON string with 0x prefix. The zero value marshals as "0x0".

func (Uint64) MarshalText

func (b Uint64) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (Uint64) String

func (b Uint64) String() string

String returns the hex encoding of b.

func (*Uint64) UnmarshalJSON

func (b *Uint64) UnmarshalJSON(input []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Uint64) UnmarshalText

func (b *Uint64) UnmarshalText(input []byte) error

UnmarshalText implements encoding.TextUnmarshaler

type UnprefixedHash

type UnprefixedHash Hash

UnprefixedHash allows marshaling a Hash without 0x prefix.

func (UnprefixedHash) MarshalText

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

MarshalText encodes the hash as hex.

func (*UnprefixedHash) UnmarshalText

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

UnmarshalText decodes the hash from hex. The 0x prefix is optional.

Jump to

Keyboard shortcuts

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