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
- Variables
- func AddHexPrefix(str string) string
- func CheckPasswordHash(password, hash string) bool
- func CoerceInterfaceMapToStringMap(in interface{}) (interface{}, error)
- func DecodeEthereumTx(hex string) (types.Transaction, error)
- func EVMHexNumber(val interface{}) string
- func EVMSignedHexNumber(val *big.Int) (string, error)
- func EncodeTxToHex(tx *types.Transaction) (string, error)
- func FileExists(name string) bool
- func FormatJSON(v interface{}) ([]byte, error)
- func HashPassword(password string) (string, error)
- func HexConcat(strs ...string) string
- func HexToBytes(strs ...string) ([]byte, error)
- func HexToString(hex string) (string, error)
- func HexToUint64(hex string) (uint64, error)
- func ISO8601UTC(t time.Time) string
- func IsEmptyAddress(addr common.Address) bool
- func IsHex(str string) bool
- func Keccak256(in []byte) ([]byte, error)
- func MaxUint64(uints ...uint64) uint64
- func NewBytes32ID() string
- func NormalizedJSON(val []byte) (string, error)
- func NullISO8601UTC(t null.Time) string
- func ParseUintHex(hex string) (*big.Int, error)
- func RemoveHexPrefix(str string) string
- func RemoveQuotes(input []byte) []byte
- func StringToHex(in string) string
- func StripBearer(authorizationStr string) string
- func ToFilterArg(q ethereum.FilterQuery) interface{}
- func ToFilterQueryFor(fromBlock *big.Int, addresses []common.Address) ethereum.FilterQuery
- func Uint64ToHex(i uint64) string
- func WithoutZeroAddresses(addresses []common.Address) []common.Address
- type BackoffSleeper
- type ConstantSleeper
- type Sleeper
Constants ¶
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 )
Variables ¶
var ZeroAddress = common.Address{}
ZeroAddress is an empty address, otherwise in Ethereum as 0x0000000000000000000000000000000000000000
Functions ¶
func AddHexPrefix ¶
AddHexPrefix adds the previx (0x) to a given hex string.
func CheckPasswordHash ¶
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 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 EVMHexNumber ¶
func EVMHexNumber(val interface{}) string
EVMHexNumber formats a number as a 32 byte hex string.
func EVMSignedHexNumber ¶
EVMSignedHexNumber formats a number as a 32 byte hex string Twos compliment representation if a minus number
func EncodeTxToHex ¶
func EncodeTxToHex(tx *types.Transaction) (string, error)
EncodeTxToHex converts the given Ethereum Transaction type and returns its hex-value string.
func FileExists ¶
FileExists returns true if a file at the passed string exists.
func FormatJSON ¶
FormatJSON applies indent to format a JSON response.
func HashPassword ¶
HashPassword wraps around bcrypt.GenerateFromPassword for a friendlier API.
func HexToBytes ¶
HexToBytes converts the given array of strings and returns bytes.
func HexToString ¶
HexToString decodes a hex encoded string.
func HexToUint64 ¶
HexToUint64 converts a given hex string to 64-bit unsigned integer.
func IsEmptyAddress ¶
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 Keccak256 ¶
Keccak256 is a simplified interface for the legacy SHA3 implementation that Ethereum uses.
func NewBytes32ID ¶
func NewBytes32ID() string
NewBytes32ID returns a randomly generated UUID that conforms to Ethereum bytes32.
func NormalizedJSON ¶
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 ¶
NullISO8601UTC returns formatted time if valid, empty string otherwise.
func ParseUintHex ¶
ParseUintHex parses an unsigned integer out of a hex string.
func RemoveHexPrefix ¶
RemoveHexPrefix removes the prefix (0x) of a given hex string.
func RemoveQuotes ¶
RemoveQuotes removes the first and last character if they are both either " or ', otherwise it is a noop.
func StringToHex ¶
StringToHex converts a standard string to a hex encoded string.
func StripBearer ¶
StripBearer removes the 'Bearer: ' prefix from the HTTP Authorization header.
func ToFilterArg ¶
func ToFilterArg(q ethereum.FilterQuery) interface{}
ToFilterArg filters logs with the given FilterQuery https://github.com/ethereum/go-ethereum/blob/762f3a48a00da02fe58063cb6ce8dc2d08821f15/ethclient/ethclient.go#L363
func ToFilterQueryFor ¶
ToFilterQueryFor returns a struct that encapsulates desired arguments used to filter event logs.
func Uint64ToHex ¶
Uint64ToHex converts the given uint64 value to a hex-value string.
Types ¶
type BackoffSleeper ¶
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) 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 before reattempting.
type ConstantSleeper ¶
type ConstantSleeper struct { Sleeper // 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) Duration ¶
func (cs ConstantSleeper) Duration() time.Duration
Duration returns the duration value.
func (ConstantSleeper) Sleep ¶
func (cs ConstantSleeper) Sleep()
Sleep waits for the given duration before reattempting.