utils

package
v0.0.0-...-73326a6 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2018 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package utils is used for the common functions for dealing with conversion to and from hex, bytes, and strings, formatting time.

Index

Constants

View Source
const (
	// FormatBytes encodes the output as bytes
	FormatBytes = "bytes"
	// FormatUint256 encodes the output as bytes containing a uint256
	FormatUint256 = "uint256"
	// FormatInt256 encodes the output as bytes containing an int256
	FormatInt256 = "int256"
	// FormatBool encodes the output as bytes containing a bool
	FormatBool = "bool"
)
View Source
const (
	// HumanTimeFormat is the predefined layout for use in Time.Format and time.Parse
	HumanTimeFormat = "2006-01-02 15:04:05 MST"
	// EVMWordByteLen the length of an EVM Word Byte
	EVMWordByteLen = 32
	// EVMWordHexLen the length of an EVM Word Hex
	EVMWordHexLen = EVMWordByteLen * 2
)
View Source
const NewBytes32Length = 32

NewBytes32Length holds the length of bytes needed for Bytes32ID.

Variables

View Source
var (

	// MaxUint256 represents the largest number represented by an EVM word
	MaxUint256 = &big.Int{}
	// MaxInt256 represents the largest number represented by an EVM word using
	// signed encoding.
	MaxInt256 = &big.Int{}
	// MinInt256 represents the smallest number represented by an EVM word using
	// signed encoding.
	MinInt256 = &big.Int{}
)

"Constants" used by EVM words

View Source
var ZeroAddress = common.Address{}

ZeroAddress is an empty address, otherwise in Ethereum as 0x0000000000000000000000000000000000000000

Functions

func AddHexPrefix

func AddHexPrefix(str string) string

AddHexPrefix adds the previx (0x) to a given hex string.

func CheckPasswordHash

func CheckPasswordHash(password, hash string) bool

CheckPasswordHash wraps around bcrypt.CompareHashAndPassword for a friendlier API.

func CoerceInterfaceMapToStringMap

func CoerceInterfaceMapToStringMap(in interface{}) (interface{}, error)

CoerceInterfaceMapToStringMap converts map[interface{}]interface{} (interface maps) to map[string]interface{} (string maps) and []interface{} with interface maps to string maps. Relevant when serializing between CBOR and JSON.

func ConcatBytes

func ConcatBytes(bufs ...[]byte) ([]byte, error)

ConcatBytes appends a bunch of byte arrays into a single byte array

func DecodeEthereumTx

func DecodeEthereumTx(hex string) (types.Transaction, error)

DecodeEthereumTx takes an RLP hex encoded Ethereum transaction and returns a Transaction struct with all the fields accessible.

func EVMEncodeBytes

func EVMEncodeBytes(input []byte) ([]byte, error)

EVMEncodeBytes encodes arbitrary bytes as bytes expected by the EVM

func EVMTranscodeBool

func EVMTranscodeBool(value gjson.Result) ([]byte, error)

EVMTranscodeBool converts a json input to an EVM bool

func EVMTranscodeBytes

func EVMTranscodeBytes(value gjson.Result) ([]byte, error)

EVMTranscodeBytes converts a json input to an EVM bytes array

func EVMTranscodeInt256

func EVMTranscodeInt256(value gjson.Result) ([]byte, error)

EVMTranscodeInt256 converts a json input to an EVM int256

func EVMTranscodeJSONWithFormat

func EVMTranscodeJSONWithFormat(value gjson.Result, format string) ([]byte, error)

EVMTranscodeJSONWithFormat given a JSON input and a format specifier, encode the value for use by the EVM

func EVMTranscodeUint256

func EVMTranscodeUint256(value gjson.Result) ([]byte, error)

EVMTranscodeUint256 converts a json input to an EVM uint256

func EVMWordBigInt

func EVMWordBigInt(val *big.Int) ([]byte, error)

EVMWordBigInt returns a big.Int as an EVM word byte array, with support for a signed representation. Returns error on overflow.

func EVMWordSignedBigInt

func EVMWordSignedBigInt(val *big.Int) ([]byte, error)

EVMWordSignedBigInt returns a big.Int as an EVM word byte array, with support for a signed representation. Returns error on overflow.

func EVMWordUint64

func EVMWordUint64(val uint64) []byte

EVMWordUint64 returns a uint64 as an EVM word byte array.

func EncodeTxToHex

func EncodeTxToHex(tx *types.Transaction) (string, error)

EncodeTxToHex converts the given Ethereum Transaction type and returns its hex-value string.

func FileExists

func FileExists(name string) bool

FileExists returns true if a file at the passed string exists.

func FormatJSON

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

FormatJSON applies indent to format a JSON response.

func HasHexPrefix

func HasHexPrefix(str string) bool

HasHexPrefix returns true if the string starts with 0x.

func HashPassword

func HashPassword(password string) (string, error)

HashPassword wraps around bcrypt.GenerateFromPassword for a friendlier API.

func HexToUint64

func HexToUint64(hex string) (uint64, error)

HexToUint64 converts a given hex string to 64-bit unsigned integer.

func ISO8601UTC

func ISO8601UTC(t time.Time) string

ISO8601UTC formats given time to ISO8601.

func IsEmptyAddress

func IsEmptyAddress(addr common.Address) bool

IsEmptyAddress checks that the address is empty, synonymous with the zero account/address. No logs can come from this address, as there is no contract present there.

See https://stackoverflow.com/questions/48219716/what-is-address0-in-solidity for the more info on the zero address.

func IsQuoted

func IsQuoted(input []byte) bool

IsQuoted checks if the first and last characters are either " or '.

func Keccak256

func Keccak256(in []byte) ([]byte, error)

Keccak256 is a simplified interface for the legacy SHA3 implementation that Ethereum uses.

func MaxInt

func MaxInt(ints ...int) int

MaxInt finds the maximum value of a list of ints.

func MaxUint64

func MaxUint64(uints ...uint64) uint64

MaxUint64 finds the maximum value of a list of uint64s.

func NewBytes32ID

func NewBytes32ID() string

NewBytes32ID returns a randomly generated UUID that conforms to Ethereum bytes32.

func NormalizedJSON

func NormalizedJSON(val []byte) (string, error)

NormalizedJSON returns a JSON representation of an object that has been normalized to produce a consistent output for hashing.

NOTE: If this string is unmarshalled again, there is no guarantee that the final representation will be consistent with the string produced by this function due to differences in JSON implementations and information loss. e.g:

JSON does not have a requirement to respect object key ordering.

func NullISO8601UTC

func NullISO8601UTC(t null.Time) string

NullISO8601UTC returns formatted time if valid, empty string otherwise.

func RemoveHexPrefix

func RemoveHexPrefix(str string) string

RemoveHexPrefix removes the prefix (0x) of a given hex string.

func RemoveQuotes

func RemoveQuotes(input []byte) []byte

RemoveQuotes removes the first and last character if they are both either " or ', otherwise it is a noop.

func StringToHex

func StringToHex(in string) string

StringToHex converts a standard string to a hex encoded string.

func StripBearer

func StripBearer(authorizationStr string) string

StripBearer removes the 'Bearer: ' prefix from the HTTP Authorization header.

func ToFilterQueryFor

func ToFilterQueryFor(fromBlock *big.Int, addresses []common.Address) ethereum.FilterQuery

ToFilterQueryFor returns a struct that encapsulates desired arguments used to filter event logs.

func Uint64ToHex

func Uint64ToHex(i uint64) string

Uint64ToHex converts the given uint64 value to a hex-value string.

func WithoutZeroAddresses

func WithoutZeroAddresses(addresses []common.Address) []common.Address

WithoutZeroAddresses returns a list of addresses excluding the zero address.

Types

type BackoffSleeper

type BackoffSleeper struct {
	*backoff.Backoff
}

BackoffSleeper is a counter to assist with reattempts.

func NewBackoffSleeper

func NewBackoffSleeper() BackoffSleeper

NewBackoffSleeper returns a BackoffSleeper that is configured to sleep for 1 second minimum, and 10 seconds maximum.

func (BackoffSleeper) After

func (bs BackoffSleeper) After() time.Duration

After returns the duration for the next stop, and increments the backoff.

func (BackoffSleeper) Duration

func (bs BackoffSleeper) Duration() time.Duration

Duration returns the current duration value.

func (BackoffSleeper) Sleep

func (bs BackoffSleeper) Sleep()

Sleep waits for the given duration, incrementing the back off.

type ConstantSleeper

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

ConstantSleeper is to assist with reattempts with the same sleep duration.

func NewConstantSleeper

func NewConstantSleeper(d time.Duration) ConstantSleeper

NewConstantSleeper returns a ConstantSleeper that is configured to sleep for a constant duration based on the input.

func (ConstantSleeper) After

func (cs ConstantSleeper) After() time.Duration

After returns the duration.

func (ConstantSleeper) Duration

func (cs ConstantSleeper) Duration() time.Duration

Duration returns the duration value.

func (ConstantSleeper) Reset

func (cs ConstantSleeper) Reset()

Reset is a no op since sleep time is constant.

func (ConstantSleeper) Sleep

func (cs ConstantSleeper) Sleep()

Sleep waits for the given duration before reattempting.

type Sleeper

type Sleeper interface {
	Reset()
	Sleep()
	After() time.Duration
	Duration() time.Duration
}

Sleeper interface is used for tasks that need to be done on some interval, excluding Cron, like reconnecting.

Jump to

Keyboard shortcuts

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