Documentation ¶
Overview ¶
Package utils contains various helper functions.
Index ¶
- Constants
- Variables
- func BytesToHex(d []byte) string
- func CopyBytes(b []byte) (copiedBytes []byte)
- func Decode(input string) ([]byte, error)
- func DecodeBig(input string) (*big.Int, error)
- func DecodeUint64(input string) (uint64, error)
- func EmptyHash(h Hash) bool
- func Encode(b []byte) string
- func EncodeBig(bigint *big.Int) string
- func EncodeUint64ToByte(i uint64) []byte
- func EncodeUint64ToHex(i uint64) string
- func EnvParse()
- func EnvString(name string, value string) *string
- func EnvStringVar(p *string, name string, value string)
- func FileExists(file string) bool
- func FromHex(s string) []byte
- func HexToBytes(str string) []byte
- func HexToBytesFixed(str string, flen int) []byte
- func IsDirExist(path string) (bool, error)
- func IsHexAddr(s string) bool
- func IsHexHash(s string) bool
- func LeftPadBytes(slice []byte, l int) []byte
- func OpenDir(dir string) (string, error)
- func OpenFile(path string) (*os.File, error)
- func ReferenceBig(s string) *big.Int
- func ReferenceBytes(s string) []byte
- func RemovePrefix(s string) string
- func RightPadBytes(slice []byte, l int) []byte
- func ToHex(b []byte) stringdeprecated
- func UnmarshalFixedJSON(typ reflect.Type, input, out []byte) error
- func UnmarshalFixedText(typname string, input, out []byte) error
- func UnmarshalFixedUnprefixedText(typname string, input, out []byte) error
- type AbsTime
- type Address
- func (a Address) Big() *big.Int
- func (a Address) Bytes() []byte
- func (a *Address) Equal(b Address) bool
- func (a Address) Hash() Hash
- func (a Address) Hex() string
- func (a *Address) IsEmpty() bool
- func (a Address) MarshalText() ([]byte, error)
- func (a *Address) SetBytes(b []byte)
- func (a Address) String() string
- func (a *Address) UnmarshalJSON(input []byte) error
- func (a *Address) UnmarshalText(input []byte) error
- type Big
- func (z *Big) Add(x *Big, y *Big) *Big
- func (b Big) MarshalText() ([]byte, error)
- func (z *Big) Sadd(y *Big) *Big
- func (z *Big) Ssub(y *Big) *Big
- func (b *Big) String() string
- func (z *Big) Sub(x *Big, y *Big) *Big
- func (b *Big) ToInt() *big.Int
- func (b *Big) UnmarshalJSON(input []byte) error
- func (b *Big) UnmarshalText(input []byte) error
- type Bytes
- type GasPool
- type Hash
- func (h Hash) Big() *big.Int
- func (h Hash) Bytes() []byte
- func (h Hash) Format(s fmt.State, c rune)
- func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value
- func (h Hash) Hex() string
- func (h Hash) MarshalText() ([]byte, error)
- func (h *Hash) SetBytes(b []byte)
- func (h Hash) String() string
- func (h Hash) TerminalString() string
- func (h *Hash) UnmarshalJSON(input []byte) error
- func (h *Hash) UnmarshalText(input []byte) error
- type MixedcaseAddress
- func (ma *MixedcaseAddress) Address() Address
- func (ma *MixedcaseAddress) MarshalJSON() ([]byte, error)
- func (ma *MixedcaseAddress) Original() string
- func (ma *MixedcaseAddress) String() string
- func (ma *MixedcaseAddress) UnmarshalJSON(input []byte) error
- func (ma *MixedcaseAddress) ValidChecksum() bool
- type NoprefixedAddress
- type StorageSize
- type Uint
- type Uint64
- type UnprefixedHash
Constants ¶
const HashLength = 32
HashLength of hashes in bytes.
Variables ¶
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
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
var EmptyAddress = Address{}
EmptyAddress presents an empty address
var ErrGasLimitReached = errors.New("gas limit reached")
Functions ¶
func BytesToHex ¶
BytesToHex returns the hexadecimal encoding of d.
func DecodeBig ¶
DecodeBig decodes a hex string with 0x prefix as a quantity. Numbers larger than 256 bits are not accepted.
func DecodeUint64 ¶
DecodeUint64 decodes a hex string with 0x prefix as a quantity.
func EncodeBig ¶
EncodeBig encodes bigint as a hex string with 0x prefix. The sign of the integer is ignored.
func EncodeUint64ToByte ¶
EncodeUint64ToByte encodes i as a bytes with 0x prefix.
func EncodeUint64ToHex ¶
EncodeUint64ToHex encodes i as a hex string with 0x prefix.
func EnvStringVar ¶
func FromHex ¶
FromHex returns the bytes represented by the hexadecimal string s. s may be prefixed with "0x".
func HexToBytes ¶
HexToBytes returns the bytes represented by the hexadecimal string str.
func HexToBytesFixed ¶
HexToBytesFixed returns bytes of a specified fixed length flen.
func IsDirExist ¶
IsDirExist determines whether a directory exists
func IsHexAddr ¶
IsHexAddr verifies whether a string can represent a valid hex-encoded address or not.
func LeftPadBytes ¶
LeftPadBytes zero-pads slice to the left up to length l.
func OpenDir ¶
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 ¶
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 ReferenceBytes ¶
func RemovePrefix ¶
func RightPadBytes ¶
RightPadBytes zero-pads slice to the right up to length l.
func UnmarshalFixedJSON ¶
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 ¶
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 ¶
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 Address ¶
type Address [addressSize]byte
Address represents the 20 byte address of an account.
func BigToAddress ¶
BigToAddress returns Address with byte values of b.
func BytesToAddress ¶
BytesToAddress returns Address with value b.
func HexToAddress ¶
HexToAddress returns Address with byte values of s.
func (Address) MarshalText ¶
MarshalText returns the hex representation of a.
func (*Address) UnmarshalJSON ¶
UnmarshalJSON parses a hash in hex syntax.
func (*Address) UnmarshalText ¶
UnmarshalText parses a hash in hex syntax.
type Big ¶
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) MarshalText ¶
MarshalText implements encoding.TextMarshaler
func (*Big) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Big) UnmarshalText ¶
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 ¶
MarshalText implements encoding.TextMarshaler
func (*Bytes) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Bytes) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type GasPool ¶
type GasPool uint64
type Hash ¶
type Hash [HashLength]byte
Hash represents the 32 byte Keccak256 hash of arbitrary data.
func BigToHash ¶
BigToHash sets byte representation of b to hash. If b is larger than len(h), b will be cropped from the left.
func BytesToHash ¶
BytesToHash sets b to hash. If b is larger than len(h), b will be cropped from the left.
func HexToHash ¶
HexToHash sets byte representation of s to hash. If b is larger than len(h), b will be cropped from the left.
func (Hash) Format ¶
Format implements fmt.Formatter, forcing the byte slice to be formatted as is, without going through the stringer interface used for logging.
func (Hash) MarshalText ¶
MarshalText returns the hex representation of h.
func (*Hash) SetBytes ¶
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 ¶
String implements the stringer interface and is used also by the logger when doing full logging into a file.
func (Hash) TerminalString ¶
TerminalString implements log.TerminalStringer, formatting a string for console output during logging.
func (*Hash) UnmarshalJSON ¶
UnmarshalJSON parses a hash in hex syntax.
func (*Hash) UnmarshalText ¶
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 ¶
MarshalText implements encoding.TextMarshaler.
func (*Uint) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Uint) UnmarshalText ¶
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 ¶
MarshalText implements encoding.TextMarshaler.
func (*Uint64) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Uint64) UnmarshalText ¶
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.