laconicd/types/validation.go
Federico Kunze ba5bf33f19
proto, evm: use string for address and hash protobuf message fields (#23)
* proto, evm: use string for address and hash protobuf message fields

* fix tests

* msg test
2021-05-14 02:52:18 -04:00

37 lines
1.1 KiB
Go

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
}