e1512c52e9
* Add ValidateNonZeroAddress validation utility * Update types/validation.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package types
|
|
|
|
import (
|
|
"bytes"
|
|
"math"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
"github.com/ethereum/go-ethereum/common"
|
|
)
|
|
|
|
// IsEmptyHash returns true if the hash corresponds to an empty ethereum hex hash.
|
|
func IsEmptyHash(hash string) bool {
|
|
return bytes.Equal(common.HexToHash(hash).Bytes(), common.Hash{}.Bytes())
|
|
}
|
|
|
|
// IsZeroAddress returns true if the address corresponds to an empty ethereum hex address.
|
|
func IsZeroAddress(address string) bool {
|
|
return bytes.Equal(common.HexToAddress(address).Bytes(), common.Address{}.Bytes())
|
|
}
|
|
|
|
// ValidateAddress returns an error if the provided string is either not a hex formatted string address
|
|
func ValidateAddress(address string) error {
|
|
if !common.IsHexAddress(address) {
|
|
return sdkerrors.Wrapf(
|
|
sdkerrors.ErrInvalidAddress, "address '%s' is not a valid ethereum hex address",
|
|
address,
|
|
)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ValidateNonZeroAddress returns an error if the provided string is not a hex
|
|
// formatted string address or is equal to zero
|
|
func ValidateNonZeroAddress(address string) error {
|
|
if IsZeroAddress(address) {
|
|
return sdkerrors.Wrapf(
|
|
sdkerrors.ErrInvalidAddress, "address '%s' must not be zero",
|
|
address,
|
|
)
|
|
}
|
|
return ValidateAddress(address)
|
|
}
|
|
|
|
// SafeInt64 checks for overflows while casting a uint64 to int64 value.
|
|
func SafeInt64(value uint64) (int64, error) {
|
|
if value > uint64(math.MaxInt64) {
|
|
return 0, sdkerrors.Wrapf(sdkerrors.ErrInvalidHeight, "uint64 value %v cannot exceed %v", value, int64(math.MaxInt64))
|
|
}
|
|
|
|
return int64(value), nil
|
|
}
|