/
validation.go
39 lines (32 loc) · 1.17 KB
/
validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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())
}
// ValidateHexAddress returns an error if the provided string is either not a hex formatted string address
func ValidateHexAddress(address string) error {
if !common.IsHexAddress(address) {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidAddress, "address '%s' is not a valid ethereum hex address",
address,
)
}
return nil
}
// 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
}