laconicd/types/validation.go

37 lines
1.1 KiB
Go
Raw Normal View History

package types
import (
"bytes"
ethcmn "github.com/ethereum/go-ethereum/common"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// IsEmptyHash returns true if the hash corresponds to an empty ethereum hex hash.
func IsEmptyHash(hash string) bool {
return bytes.Equal(ethcmn.HexToHash(hash).Bytes(), ethcmn.Hash{}.Bytes())
}
// IsZeroAddress returns true if the address corresponds to an empty ethereum hex address.
func IsZeroAddress(address string) bool {
return bytes.Equal(ethcmn.HexToAddress(address).Bytes(), ethcmn.Address{}.Bytes())
}
// ValidateAddress returns an error if the provided string is either not a hex formatted string address
// the it matches the zero address 0x00000000000000000000.
func ValidateAddress(address string) error {
if !ethcmn.IsHexAddress(address) {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidAddress, "address '%s' is not a valid ethereum hex address",
address,
)
}
if IsZeroAddress(address) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "provided address cannot be the zero address")
}
return nil
}