Skip to content

Commit

Permalink
Merge PR cosmos#6907: Return error when the address is an empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
dauTT authored Aug 4, 2020
1 parent 801b648 commit f11d052
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
30 changes: 27 additions & 3 deletions types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func VerifyAddressFormat(bz []byte) error {
// AccAddressFromBech32 creates an AccAddress from a Bech32 string.
func AccAddressFromBech32(address string) (addr AccAddress, err error) {
if len(strings.TrimSpace(address)) == 0 {
return AccAddress{}, nil
return AccAddress{}, errors.New("empty address string is not allowed")
}

bech32PrefixAccAddr := GetConfig().GetBech32AccountAddrPrefix()
Expand Down Expand Up @@ -186,6 +186,10 @@ func (aa *AccAddress) UnmarshalJSON(data []byte) error {
if err != nil {
return err
}
if s == "" {
*aa = AccAddress{}
return nil
}

aa2, err := AccAddressFromBech32(s)
if err != nil {
Expand All @@ -203,6 +207,10 @@ func (aa *AccAddress) UnmarshalYAML(data []byte) error {
if err != nil {
return err
}
if s == "" {
*aa = AccAddress{}
return nil
}

aa2, err := AccAddressFromBech32(s)
if err != nil {
Expand Down Expand Up @@ -264,7 +272,7 @@ func ValAddressFromHex(address string) (addr ValAddress, err error) {
// ValAddressFromBech32 creates a ValAddress from a Bech32 string.
func ValAddressFromBech32(address string) (addr ValAddress, err error) {
if len(strings.TrimSpace(address)) == 0 {
return ValAddress{}, nil
return ValAddress{}, errors.New("empty address string is not allowed")
}

bech32PrefixValAddr := GetConfig().GetBech32ValidatorAddrPrefix()
Expand Down Expand Up @@ -332,6 +340,10 @@ func (va *ValAddress) UnmarshalJSON(data []byte) error {
if err != nil {
return err
}
if s == "" {
*va = ValAddress{}
return nil
}

va2, err := ValAddressFromBech32(s)
if err != nil {
Expand All @@ -350,6 +362,10 @@ func (va *ValAddress) UnmarshalYAML(data []byte) error {
if err != nil {
return err
}
if s == "" {
*va = ValAddress{}
return nil
}

va2, err := ValAddressFromBech32(s)
if err != nil {
Expand Down Expand Up @@ -411,7 +427,7 @@ func ConsAddressFromHex(address string) (addr ConsAddress, err error) {
// ConsAddressFromBech32 creates a ConsAddress from a Bech32 string.
func ConsAddressFromBech32(address string) (addr ConsAddress, err error) {
if len(strings.TrimSpace(address)) == 0 {
return ConsAddress{}, nil
return ConsAddress{}, errors.New("empty address string is not allowed")
}

bech32PrefixConsAddr := GetConfig().GetBech32ConsensusAddrPrefix()
Expand Down Expand Up @@ -484,6 +500,10 @@ func (ca *ConsAddress) UnmarshalJSON(data []byte) error {
if err != nil {
return err
}
if s == "" {
*ca = ConsAddress{}
return nil
}

ca2, err := ConsAddressFromBech32(s)
if err != nil {
Expand All @@ -502,6 +522,10 @@ func (ca *ConsAddress) UnmarshalYAML(data []byte) error {
if err != nil {
return err
}
if s == "" {
*ca = ConsAddress{}
return nil
}

ca2, err := ConsAddressFromBech32(s)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions types/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ func TestEmptyAddresses(t *testing.T) {

accAddr, err := types.AccAddressFromBech32("")
require.True(t, accAddr.Empty())
require.Nil(t, err)
require.Error(t, err)

valAddr, err := types.ValAddressFromBech32("")
require.True(t, valAddr.Empty())
require.Nil(t, err)
require.Error(t, err)

consAddr, err := types.ConsAddressFromBech32("")
require.True(t, consAddr.Empty())
require.Nil(t, err)
require.Error(t, err)
}

func TestRandBech32PubkeyConsistency(t *testing.T) {
Expand Down

0 comments on commit f11d052

Please sign in to comment.