-
Notifications
You must be signed in to change notification settings - Fork 690
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add command bech32-convert (#1845)
* add bech32-convert command to gaiad * add changelog notice * add review fixes * add tests for ConvertBech32Prefix Co-authored-by: Petr Ivanov <[email protected]> Co-authored-by: Danilo Pantani <[email protected]> Co-authored-by: billy rennekamp <[email protected]>
- Loading branch information
1 parent
e76c32a
commit e3c65bd
Showing
5 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
addressutil "github.com/cosmos/gaia/v8/pkg/address" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var flagBech32Prefix = "prefix" | ||
|
||
// AddBech32ConvertCommand returns bech32-convert cobra Command. | ||
func AddBech32ConvertCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "bech32-convert [address]", | ||
Short: "Convert any bech32 string to the cosmos prefix", | ||
Long: `Convert any bech32 string to the cosmos prefix | ||
Example: | ||
gaiad debug bech32-convert akash1a6zlyvpnksx8wr6wz8wemur2xe8zyh0ytz6d88 | ||
gaiad debug bech32-convert stride1673f0t8p893rqyqe420mgwwz92ac4qv6synvx2 --prefix osmo | ||
`, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
bech32prefix, err := cmd.Flags().GetString(flagBech32Prefix) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
address := args[0] | ||
convertedAddress, err := addressutil.ConvertBech32Prefix(address, bech32prefix) | ||
if err != nil { | ||
return fmt.Errorf("convertation failed: %s", err) | ||
} | ||
|
||
cmd.Println(convertedAddress) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().StringP(flagBech32Prefix, "p", "cosmos", "Bech32 Prefix to encode to") | ||
|
||
return cmd | ||
} | ||
|
||
// addDebugCommands injects custom debug commands into another command as children. | ||
func addDebugCommands(cmd *cobra.Command) *cobra.Command { | ||
cmd.AddCommand(AddBech32ConvertCommand()) | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package address | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/types/bech32" | ||
) | ||
|
||
// ConvertBech32Prefix convert bech32 address to specified prefix. | ||
func ConvertBech32Prefix(address, prefix string) (string, error) { | ||
_, bz, err := bech32.DecodeAndConvert(address) | ||
if err != nil { | ||
return "", fmt.Errorf("cannot decode %s address: %s", address, err) | ||
} | ||
|
||
convertedAddress, err := bech32.ConvertAndEncode(prefix, bz) | ||
if err != nil { | ||
return "", fmt.Errorf("cannot convert %s address: %s", address, err) | ||
} | ||
|
||
return convertedAddress, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package address | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConvertBech32Prefix(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
address string | ||
prefix string | ||
converted string | ||
err error | ||
}{ | ||
{ | ||
name: "Convert valid bech 32 address", | ||
address: "akash1a6zlyvpnksx8wr6wz8wemur2xe8zyh0ytz6d88", | ||
converted: "cosmos1a6zlyvpnksx8wr6wz8wemur2xe8zyh0yxeh27a", | ||
prefix: "cosmos", | ||
}, | ||
{ | ||
name: "Convert invalid address", | ||
address: "invalidaddress", | ||
prefix: "cosmos", | ||
err: errors.New("cannot decode invalidaddress address: decoding bech32 failed: invalid separator index -1"), | ||
}, | ||
} | ||
|
||
for _, tt := range cases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
convertedAddress, err := ConvertBech32Prefix(tt.address, tt.prefix) | ||
if tt.err != nil { | ||
require.ErrorContains(t, err, tt.err.Error()) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
require.Equal(t, tt.converted, convertedAddress) | ||
}) | ||
} | ||
} |