diff --git a/types/validation.go b/types/validation.go index 15e27ee3..084246f5 100644 --- a/types/validation.go +++ b/types/validation.go @@ -29,6 +29,18 @@ func ValidateAddress(address string) error { 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) { diff --git a/types/validation_test.go b/types/validation_test.go index 86e387d4..ee60a19f 100644 --- a/types/validation_test.go +++ b/types/validation_test.go @@ -85,6 +85,37 @@ func TestValidateAddress(t *testing.T) { } } +func TestValidateNonZeroAddress(t *testing.T) { + testCases := []struct { + name string + address string + expError bool + }{ + { + "empty string", "", true, + }, + { + "invalid address", "0x", true, + }, + { + "zero address", common.Address{}.String(), true, + }, + { + "valid address", tests.GenerateAddress().Hex(), false, + }, + } + + for _, tc := range testCases { + err := ValidateNonZeroAddress(tc.address) + + if tc.expError { + require.Error(t, err, tc.name) + } else { + require.NoError(t, err, tc.name) + } + } +} + func TestSafeInt64(t *testing.T) { testCases := []struct { name string