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 ConcatBytes(bufs ...[]byte) []byte
- func DecodeEthereumTx(hex string) (types.Transaction, error)
- func DurationFromNow(t time.Time) time.Duration
- func EIP55CapitalizedAddress(possibleAddressString string) bool
- func EVMEncodeBytes(input []byte) []byte
- func EVMTranscodeBool(value gjson.Result) ([]byte, error)
- func EVMTranscodeBytes(value gjson.Result) ([]byte, error)
- func EVMTranscodeInt256(value gjson.Result) ([]byte, error)
- func EVMTranscodeJSONWithFormat(value gjson.Result, format string) ([]byte, error)
- func EVMTranscodeUint256(value gjson.Result) ([]byte, error)
- func EVMWordBigInt(val *big.Int) ([]byte, error)
- func EVMWordSignedBigInt(val *big.Int) ([]byte, error)
- func EVMWordUint64(val uint64) []byte
- func EncodeTxToHex(tx *types.Transaction) (string, error)
- func FileContents(path string) (string, error)
- func FileExists(name string) bool
- func FilesInDir(dir string) ([]string, error)
- func FormatJSON(v interface{}) ([]byte, error)
- func HasHexPrefix(str string) bool
- func HashPassword(password string) (string, error)
- func HexToUint64(hex string) (uint64, error)
- func ISO8601UTC(t time.Time) string
- func IsEmptyAddress(addr common.Address) bool
- func IsQuoted(input []byte) bool
- func JustError(_ interface{}, err error) error
- func Keccak256(in []byte) ([]byte, error)
- func LogListeningAddress(address common.Address) string
- func MaxBigs(first *big.Int, bigs ...*big.Int) *big.Int
- func MaxInt(ints ...int) int
- func MaxUint32(uints ...uint32) uint32
- func MinBigs(first *big.Int, bigs ...*big.Int) *big.Int
- func MustHash(in string) common.Hash
- func NewBytes32ID() string
- func NewSecret(n int) string
- func NormalizedJSON(val []byte) (string, error)
- func NullISO8601UTC(t null.Time) string
- func ParseEthereumAddress(addressString string) (common.Address, error)
- func RemoveHexPrefix(str string) string
- func RemoveQuotes(input []byte) []byte
- func Sha256(in string) (string, error)
- func StringToHex(in string) string
- func StripBearer(authorizationStr string) string
- func ToFilterArg(q ethereum.FilterQuery) interface{}
- func Uint64ToHex(i uint64) string
- func WithoutZeroAddresses(addresses []common.Address) []common.Address
- type AfterNower
- type Afterer
- type BackoffSleeper
- type Clock
- type Nower
- type Sleeper
Constants ¶
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" )
const ( // DefaultSecretSize is the entroy in bytes to generate a base64 string of 64 characters. DefaultSecretSize = 48 // EVMWordByteLen the length of an EVM Word Byte EVMWordByteLen = 32 // EVMWordHexLen the length of an EVM Word Hex EVMWordHexLen = EVMWordByteLen * 2 )
Variables ¶
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
var EmptyHash = common.Hash{}
EmptyHash is a hash of all zeroes, otherwise in Ethereum as 0x0000000000000000000000000000000000000000000000000000000000000000
var ZeroAddress = common.Address{}
ZeroAddress is an address of all zeroes, 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 ConcatBytes ¶
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 DurationFromNow ¶
DurationFromNow returns the amount of time since the Time field was last updated.
func EIP55CapitalizedAddress ¶
EIP55CapitalizedAddress returns true iff possibleAddressString has the correct capitalization for an Ethereum address, per EIP 55
func EVMEncodeBytes ¶
EVMEncodeBytes encodes arbitrary bytes as bytes expected by the EVM
func EVMTranscodeBool ¶
EVMTranscodeBool converts a json input to an EVM bool
func EVMTranscodeBytes ¶
EVMTranscodeBytes converts a json input to an EVM bytes array
func EVMTranscodeInt256 ¶
EVMTranscodeInt256 converts a json input to an EVM int256
func EVMTranscodeJSONWithFormat ¶
EVMTranscodeJSONWithFormat given a JSON input and a format specifier, encode the value for use by the EVM
func EVMTranscodeUint256 ¶
EVMTranscodeUint256 converts a json input to an EVM uint256
func EVMWordBigInt ¶
EVMWordBigInt returns a big.Int as an EVM word byte array, with support for a signed representation. Returns error on overflow.
func EVMWordSignedBigInt ¶
EVMWordSignedBigInt returns a big.Int as an EVM word byte array, with support for a signed representation. Returns error on overflow.
func EVMWordUint64 ¶
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 FileContents ¶
FileContents returns the contents of a file as a string.
func FileExists ¶
FileExists returns true if a file at the passed string exists.
func FilesInDir ¶
FilesInDir returns an array of filenames in the directory.
func FormatJSON ¶
FormatJSON applies indent to format a JSON response.
func HasHexPrefix ¶
HasHexPrefix returns true if the string starts with 0x.
func HashPassword ¶
HashPassword wraps around bcrypt.GenerateFromPassword for a friendlier API.
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 LogListeningAddress ¶
LogListeningAddress returns the LogListeningAddress
func NewBytes32ID ¶
func NewBytes32ID() string
NewBytes32ID returns a randomly generated UUID that conforms to Ethereum bytes32.
func NewSecret ¶ added in v0.6.6
NewSecret returns a new securely random sequence of n bytes of entropy. The result is a base64 encoded string.
Panics on failed attempts to read from system's PRNG.
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 ParseEthereumAddress ¶
ParseEthereumAddress returns addressString as a go-ethereum Address, or an error if it's invalid, e.g. if EIP 55 capitalization check fails
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 Uint64ToHex ¶
Uint64ToHex converts the given uint64 value to a hex-value string.
Types ¶
type AfterNower ¶
AfterNower is an interface that fulfills the `After()` and `Now()` methods.
type Afterer ¶
Afterer is an interface that fulfills the After method, following the behavior of time.After.
type BackoffSleeper ¶
BackoffSleeper is a sleeper that backs off on subsequent attempts.
func NewBackoffSleeper ¶
func NewBackoffSleeper() *BackoffSleeper
NewBackoffSleeper returns a BackoffSleeper that is configured to sleep for 0 seconds initially, then backs off from 1 second minimum to 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) Reset ¶
func (bs *BackoffSleeper) Reset()
Reset resets the backoff intervals.
func (*BackoffSleeper) Sleep ¶
func (bs *BackoffSleeper) Sleep()
Sleep waits for the given duration, incrementing the back off.
type Clock ¶
type Clock struct{}
Clock is a basic type for scheduling events in the application.