Skip to content

Commit

Permalink
fix: allow any address in ValidatePromptAddress (backport cosmos#16312
Browse files Browse the repository at this point in the history
) (cosmos#16313)

Co-authored-by: Julien Robert <julien@rbrt.fr>
  • Loading branch information
2 people authored and roy-dydx committed Jul 11, 2023
1 parent 8d2bd26 commit e595f76
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Bug Fixes

* (cli) [#16312](https://github.com/cosmos/cosmos-sdk/pull/16312) Allow any addresses in `client.ValidatePromptAddress`.
* (x/group) [#16017](https://github.com/cosmos/cosmos-sdk/pull/16017) Correctly apply account number in group v2 migration.

### API Breaking Changes
Expand Down
17 changes: 14 additions & 3 deletions client/prompts.go → client/prompt_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,22 @@ func ValidatePromptURL(input string) error {

// ValidatePromptAddress validates that the input is a valid Bech32 address.
func ValidatePromptAddress(input string) error {
if _, err := sdk.AccAddressFromBech32(input); err != nil {
return fmt.Errorf("invalid address: %w", err)
_, err := sdk.AccAddressFromBech32(input)
if err == nil {
return nil
}

return nil
_, err = sdk.ValAddressFromBech32(input)
if err == nil {
return nil
}

_, err = sdk.ConsAddressFromBech32(input)
if err == nil {
return nil
}

return fmt.Errorf("invalid address: %w", err)
}

// ValidatePromptYesNo validates that the input is valid sdk.COins
Expand Down
38 changes: 38 additions & 0 deletions client/prompt_validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package client_test

import (
"testing"

"github.com/cosmos/cosmos-sdk/client"
"github.com/stretchr/testify/require"
)

func TestValidatePromptNotEmpty(t *testing.T) {
require := require.New(t)

require.NoError(client.ValidatePromptNotEmpty("foo"))
require.ErrorContains(client.ValidatePromptNotEmpty(""), "input cannot be empty")
}

func TestValidatePromptURL(t *testing.T) {
require := require.New(t)

require.NoError(client.ValidatePromptURL("https://example.com"))
require.ErrorContains(client.ValidatePromptURL("foo"), "invalid URL")
}

func TestValidatePromptAddress(t *testing.T) {
require := require.New(t)

require.NoError(client.ValidatePromptAddress("cosmos1huydeevpz37sd9snkgul6070mstupukw00xkw9"))
require.NoError(client.ValidatePromptAddress("cosmosvaloper1sjllsnramtg3ewxqwwrwjxfgc4n4ef9u2lcnj0"))
require.NoError(client.ValidatePromptAddress("cosmosvalcons1ntk8eualewuprz0gamh8hnvcem2nrcdsgz563h"))
require.ErrorContains(client.ValidatePromptAddress("foo"), "invalid address")
}

func TestValidatePromptCoins(t *testing.T) {
require := require.New(t)

require.NoError(client.ValidatePromptCoins("100stake"))
require.ErrorContains(client.ValidatePromptCoins("foo"), "invalid coins")
}

0 comments on commit e595f76

Please sign in to comment.