Documentation ¶
Overview ¶
Package utils is used for common functions and tools used across the codebase.
Index ¶
- Constants
- Variables
- func AddHexPrefix(str string) string
- func CheckPasswordHash(password, hash string) bool
- func CheckUint256(n *big.Int) error
- func CoerceInterfaceMapToStringMap(in interface{}) (interface{}, error)
- func CombinedContext(signals ...interface{}) (context.Context, context.CancelFunc)
- func ConcatBytes(bufs ...[]byte) []byte
- func ContextFromChan(chStop <-chan struct{}) (context.Context, context.CancelFunc)
- func CopyFileWithMaxPerms(srcPath, dstPath string, perms os.FileMode) error
- func DebugPanic()
- func DecimalFromBigInt(i *big.Int, precision int32) decimal.Decimal
- func DecodeEthereumTx(hex string) (types.Transaction, error)
- func DurationFromNow(t time.Time) time.Duration
- func EIP55CapitalizedAddress(possibleAddressString string) bool
- func EVMBytesToUint64(buf []byte) uint64
- 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 EVMWordUint128(val *big.Int) ([]byte, error)
- func EVMWordUint64(val uint64) []byte
- func EnsureDirAndMaxPerms(path string, perms os.FileMode) error
- func EnsureFileMaxPerms(file *os.File, perms os.FileMode) error
- func EnsureFilepathMaxPerms(filepath string, perms os.FileMode) 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 HexToUint256(s string) (*big.Int, error)
- func HexToUint64(hex string) (uint64, error)
- func ISO8601UTC(t time.Time) string
- func IsEmptyAddress(addr common.Address) bool
- func IsFileOwnedByChainlink(fileInfo os.FileInfo) (bool, error)
- func IsQuoted(input []byte) bool
- func JustError(_ interface{}, err error) error
- func Keccak256(in []byte) ([]byte, error)
- func LogIfError(err *error, msg string)
- func LogListeningAddress(address common.Address) string
- func MaxBigs(first *big.Int, bigs ...*big.Int) *big.Int
- func MaxInt(first int, ints ...int) int
- func MaxUint32(first uint32, uints ...uint32) uint32
- func MinUint(first uint, vals ...uint) uint
- func MustHash(in string) common.Hash
- func MustUnmarshalToMap(input string) map[string]interface{}
- 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 RetryWithBackoff(ctx context.Context, fn func() (retry bool))
- func ReverseString(s string) string
- func Sha256(in string) (string, error)
- func StringToHex(in string) string
- func StripBearer(authorizationStr string) string
- func ToDecimal(input interface{}) (decimal.Decimal, error)
- func TooPermissive(fileMode, maxAllowedPerms os.FileMode) bool
- func Uint256ToBytes(x *big.Int) (uint256 []byte, err error)
- func Uint256ToHex(n *big.Int) (string, error)
- func Uint64ToHex(i uint64) string
- func UnmarshalToMap(input string) (map[string]interface{}, error)
- func WaitGroupChan(wg *sync.WaitGroup) <-chan struct{}
- func WithoutZeroAddresses(addresses []common.Address) []common.Address
- func WrapIfError(err *error, msg string)
- func WriteFileWithMaxPerms(path string, data []byte, perms os.FileMode) error
- type AfterNower
- type Afterer
- type BackoffSleeper
- type Big
- func (b *Big) Hex() string
- func (b *Big) MarshalJSON() ([]byte, error)
- func (b *Big) MarshalText() ([]byte, error)
- func (b *Big) Scan(value interface{}) error
- func (b *Big) String() string
- func (b *Big) ToInt() *big.Int
- func (b *Big) UnmarshalJSON(input []byte) error
- func (b *Big) UnmarshalText(input []byte) error
- func (b Big) Value() (driver.Value, error)
- type BigFloat
- type BigIntSlice
- type BoundedPriorityQueue
- type BoundedQueue
- type Clock
- type DependentAwaiter
- type HTTPRequest
- type HTTPRequestConfig
- type HTTPResponseTooLargeError
- type MaxBytesReader
- type Nower
- type PausableTicker
- type RemoteServerError
- type ResettableTimer
- type ScryptConfigReader
- type ScryptParams
- type Sleeper
- type SleeperTask
- type SleeperTaskFuncWorker
- type StartStopOnce
- type StartStopOnceState
- type Worker
Constants ¶
const ( // FormatBytes encodes the output as bytes FormatBytes = "bytes" // FormatPreformatted encodes the output, assumed to be hex, as bytes. FormatPreformatted = "preformatted" // 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 ( FastN = 2 FastP = 1 )
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 DefaultScryptParams = ScryptParams{N: keystore.StandardScryptN, P: keystore.StandardScryptP}
DefaultScryptParams is for use in production. It used geth's standard level of encryption and is relatively expensive to decode. Avoid using this in tests.
var EmptyHash = common.Hash{}
EmptyHash is a hash of all zeroes, otherwise in Ethereum as 0x0000000000000000000000000000000000000000000000000000000000000000
var FastScryptParams = ScryptParams{N: FastN, P: FastP}
FastScryptParams is for use in tests, where you don't want to wear out your CPU with expensive key derivations, do not use it in production, or your encrypted keys will be easy to brute-force!
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 CheckUint256 ¶ added in v0.8.2
CheckUint256(n) returns an error if n is out of bounds for a uint256
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 CombinedContext ¶ added in v0.9.3
func CombinedContext(signals ...interface{}) (context.Context, context.CancelFunc)
CombinedContext creates a context that finishes when any of the provided signals finish. A signal can be a `context.Context`, a `chan struct{}`, or a `time.Duration` (which is transformed into a `context.WithTimeout`).
func ConcatBytes ¶
ConcatBytes appends a bunch of byte arrays into a single byte array
func ContextFromChan ¶ added in v0.9.3
func ContextFromChan(chStop <-chan struct{}) (context.Context, context.CancelFunc)
ContextFromChan creates a context that finishes when the provided channel receives or is closed.
func CopyFileWithMaxPerms ¶ added in v0.8.5
Copies the file at `srcPath` to `dstPath` and ensures that it has permissions that are no more permissive than the given ones.
func DebugPanic ¶ added in v0.9.3
func DebugPanic()
func DecimalFromBigInt ¶ added in v0.8.2
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 EVMBytesToUint64 ¶ added in v0.9.0
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 EVMWordUint128 ¶ added in v0.8.2
EVMWordUint128 returns a uint128 as an EVM word byte array.
func EVMWordUint64 ¶
EVMWordUint64 returns a uint64 as an EVM word byte array.
func EnsureDirAndMaxPerms ¶ added in v0.8.5
Ensures that the given path exists, that it's a directory, and that it has permissions that are no more permissive than the given ones.
- If the path does not exist, it is created - If the path exists, but is not a directory, an error is returned - If the path exists, and is a directory, but has the wrong perms, it is chmod'ed
func EnsureFileMaxPerms ¶ added in v0.8.5
Ensures that the given file has permissions that are no more permissive than the given ones.
func EnsureFilepathMaxPerms ¶ added in v0.8.5
Ensures that the file at the given filepath has permissions that are no more permissive than the given ones.
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 HexToUint256 ¶ added in v0.8.2
HexToUint256 returns the uint256 represented by s, or an error if it doesn't represent one.
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 IsFileOwnedByChainlink ¶ added in v0.8.9
func Keccak256 ¶
Keccak256 is a simplified interface for the legacy SHA3 implementation that Ethereum uses.
func LogIfError ¶ added in v0.9.3
func LogListeningAddress ¶
LogListeningAddress returns the LogListeningAddress
func MustUnmarshalToMap ¶ added in v0.8.17
MustUnmarshalToMap performs UnmarshalToMap, panics upon failure
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 RetryWithBackoff ¶ added in v0.8.3
func ReverseString ¶ added in v0.8.11
ReverseString returns its argument string reversed rune-wise left to right.
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 TooPermissive ¶ added in v0.8.5
func Uint256ToBytes ¶ added in v0.8.2
Uint256ToBytes(x) is x represented as the bytes of a uint256
func Uint256ToHex ¶ added in v0.8.2
Uint256ToHex returns the hex representation of n, or error if out of bounds
func Uint64ToHex ¶
Uint64ToHex converts the given uint64 value to a hex-value string.
func UnmarshalToMap ¶ added in v0.8.17
UnmarshalToMap takes an input json string and returns a map[string]interface i.e. a raw object
func WaitGroupChan ¶ added in v0.8.3
WaitGroupChan creates a channel that closes when the provided sync.WaitGroup is done.
func WithoutZeroAddresses ¶
WithoutZeroAddresses returns a list of addresses excluding the zero address.
func WrapIfError ¶ added in v0.8.13
WrapIfError decorates an error with the given message. It is intended to be used with `defer` statements, like so:
func SomeFunction() (err error) { defer WrapIfError(&err, "error in SomeFunction:") ... }
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 Big ¶ added in v0.8.2
Big stores large integers and can deserialize a variety of inputs.
func (*Big) MarshalJSON ¶ added in v0.8.2
MarshalJSON marshals this instance to base 10 number as string.
func (*Big) MarshalText ¶ added in v0.8.2
MarshalText marshals this instance to base 10 number as string.
func (*Big) UnmarshalJSON ¶ added in v0.8.2
UnmarshalJSON implements encoding.JSONUnmarshaler.
func (*Big) UnmarshalText ¶ added in v0.8.2
UnmarshalText implements encoding.TextUnmarshaler.
type BigFloat ¶ added in v0.8.2
BigFloat accepts both string and float JSON values.
func (BigFloat) MarshalJSON ¶ added in v0.8.2
MarshalJSON implements the json.Marshaler interface.
func (*BigFloat) UnmarshalJSON ¶ added in v0.8.2
UnmarshalJSON implements the json.Unmarshal interface.
type BigIntSlice ¶ added in v0.8.7
BigIntSlice attaches the methods of sort.Interface to []*big.Int, sorting in increasing order.
func (BigIntSlice) Len ¶ added in v0.8.7
func (s BigIntSlice) Len() int
func (BigIntSlice) Less ¶ added in v0.8.7
func (s BigIntSlice) Less(i, j int) bool
func (BigIntSlice) Max ¶ added in v0.8.7
func (s BigIntSlice) Max() *big.Int
Max returns the largest element
func (BigIntSlice) Sort ¶ added in v0.8.7
func (s BigIntSlice) Sort()
Sort destructively sorts the slice
func (BigIntSlice) Swap ¶ added in v0.8.7
func (s BigIntSlice) Swap(i, j int)
type BoundedPriorityQueue ¶ added in v0.8.3
type BoundedPriorityQueue struct {
// contains filtered or unexported fields
}
func NewBoundedPriorityQueue ¶ added in v0.8.3
func NewBoundedPriorityQueue(capacities map[uint]uint) *BoundedPriorityQueue
func (*BoundedPriorityQueue) Add ¶ added in v0.8.3
func (q *BoundedPriorityQueue) Add(priority uint, x interface{})
func (*BoundedPriorityQueue) Empty ¶ added in v0.8.3
func (q *BoundedPriorityQueue) Empty() bool
func (*BoundedPriorityQueue) Take ¶ added in v0.8.3
func (q *BoundedPriorityQueue) Take() interface{}
type BoundedQueue ¶ added in v0.8.3
type BoundedQueue struct {
// contains filtered or unexported fields
}
FIFO queue that discards older items when it reaches its capacity.
func NewBoundedQueue ¶ added in v0.8.3
func NewBoundedQueue(capacity uint) *BoundedQueue
func (*BoundedQueue) Add ¶ added in v0.8.3
func (q *BoundedQueue) Add(x interface{})
func (*BoundedQueue) Empty ¶ added in v0.8.3
func (q *BoundedQueue) Empty() bool
func (*BoundedQueue) Full ¶ added in v0.8.3
func (q *BoundedQueue) Full() bool
func (*BoundedQueue) Take ¶ added in v0.8.3
func (q *BoundedQueue) Take() interface{}
type Clock ¶
type Clock struct{}
Clock is a basic type for scheduling events in the application.
type DependentAwaiter ¶ added in v0.8.3
type DependentAwaiter interface { AwaitDependents() <-chan struct{} AddDependents(n int) DependentReady() }
func NewDependentAwaiter ¶ added in v0.8.3
func NewDependentAwaiter() DependentAwaiter
type HTTPRequest ¶ added in v0.9.3
type HTTPRequest struct { Request *http.Request Config HTTPRequestConfig }
func (*HTTPRequest) SendRequest ¶ added in v0.9.3
type HTTPRequestConfig ¶ added in v0.9.3
type HTTPRequestConfig struct { Timeout time.Duration MaxAttempts uint SizeLimit int64 AllowUnrestrictedNetworkAccess bool }
HTTPRequestConfig holds the configurable settings for an http request
type HTTPResponseTooLargeError ¶ added in v0.9.3
type HTTPResponseTooLargeError struct {
// contains filtered or unexported fields
}
func (*HTTPResponseTooLargeError) Error ¶ added in v0.9.3
func (e *HTTPResponseTooLargeError) Error() string
type MaxBytesReader ¶ added in v0.9.3
type MaxBytesReader struct {
// contains filtered or unexported fields
}
MaxBytesReader is inspired by https://github.com/gin-contrib/size/blob/master/size.go
func NewMaxBytesReader ¶ added in v0.9.3
func NewMaxBytesReader(rc io.ReadCloser, limit int64) *MaxBytesReader
func (*MaxBytesReader) Close ¶ added in v0.9.3
func (mbr *MaxBytesReader) Close() error
type Nower ¶
Nower is an interface that fulfills the Now method, following the behavior of time.Now.
type PausableTicker ¶ added in v0.9.0
type PausableTicker struct {
// contains filtered or unexported fields
}
func NewPausableTicker ¶ added in v0.9.0
func NewPausableTicker(duration time.Duration) PausableTicker
func (*PausableTicker) Destroy ¶ added in v0.9.0
func (t *PausableTicker) Destroy()
func (*PausableTicker) Pause ¶ added in v0.9.0
func (t *PausableTicker) Pause()
func (*PausableTicker) Resume ¶ added in v0.9.0
func (t *PausableTicker) Resume()
func (PausableTicker) Ticks ¶ added in v0.9.0
func (t PausableTicker) Ticks() <-chan time.Time
type RemoteServerError ¶ added in v0.9.3
type RemoteServerError struct {
// contains filtered or unexported fields
}
func (*RemoteServerError) Error ¶ added in v0.9.3
func (e *RemoteServerError) Error() string
type ResettableTimer ¶ added in v0.9.0
type ResettableTimer struct {
// contains filtered or unexported fields
}
func NewResettableTimer ¶ added in v0.9.0
func NewResettableTimer() ResettableTimer
func (*ResettableTimer) Reset ¶ added in v0.9.0
func (t *ResettableTimer) Reset(duration time.Duration)
func (*ResettableTimer) Stop ¶ added in v0.9.0
func (t *ResettableTimer) Stop()
func (ResettableTimer) Ticks ¶ added in v0.9.0
func (t ResettableTimer) Ticks() <-chan time.Time
type ScryptConfigReader ¶ added in v0.9.3
type ScryptConfigReader interface {
InsecureFastScrypt() bool
}
type ScryptParams ¶ added in v0.9.3
type ScryptParams struct{ N, P int }
func GetScryptParams ¶ added in v0.9.3
func GetScryptParams(config ScryptConfigReader) ScryptParams
type Sleeper ¶
Sleeper interface is used for tasks that need to be done on some interval, excluding Cron, like reconnecting.
type SleeperTask ¶ added in v0.9.3
type SleeperTask interface { Stop() error WakeUp() }
SleeperTask represents a task that waits in the background to process some work.
func NewSleeperTask ¶ added in v0.9.3
func NewSleeperTask(worker Worker) SleeperTask
NewSleeperTask takes a worker and returns a SleeperTask.
SleeperTask is guaranteed to call Work on the worker at least once for every WakeUp call. If the Worker is busy when WakeUp is called, the Worker will be called again immediately after it is finished. For this reason you should take care to make sure that Worker is idempotent. WakeUp does not block.
type SleeperTaskFuncWorker ¶ added in v0.9.3
type SleeperTaskFuncWorker func()
func (SleeperTaskFuncWorker) Work ¶ added in v0.9.3
func (fn SleeperTaskFuncWorker) Work()
type StartStopOnce ¶ added in v0.9.3
func (*StartStopOnce) OkayToStart ¶ added in v0.9.3
func (once *StartStopOnce) OkayToStart() (ok bool)
func (*StartStopOnce) OkayToStop ¶ added in v0.9.3
func (once *StartStopOnce) OkayToStop() (ok bool)
func (*StartStopOnce) StartOnce ¶ added in v0.9.3
func (once *StartStopOnce) StartOnce(name string, fn func() error) error
func (*StartStopOnce) State ¶ added in v0.9.3
func (once *StartStopOnce) State() StartStopOnceState
type StartStopOnceState ¶ added in v0.9.3
type StartStopOnceState int
const ( StartStopOnce_Unstarted StartStopOnceState = iota StartStopOnce_Started StartStopOnce_Stopped )