Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add command bech32-convert #1845

Merged
merged 10 commits into from
Oct 31, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (gaia-rho) Add [Groups Module](https://docs.cosmos.network/main/modules/group/#group-module).
* (tests) Add E2E test for Bank Send.
* (tests) Update liveness tests to use Ignite v0.21.1.
* (gaia-rho) Add bech32-convert command to gaiad

## [v7.0.2] -2022-05-09

Expand Down
53 changes: 53 additions & 0 deletions cmd/gaiad/cmd/bech32_convert.go
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please provide an example with a different prefix? Using the prefix flag

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added one more example with prefix flag. Did I get your idea correctly?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!! The example is good. We can also update the description,

Convert any bech32 string prefix based on prefix flag. Default: "cosmos"


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
}
2 changes: 1 addition & 1 deletion cmd/gaiad/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
AddGenesisAccountCmd(gaia.DefaultNodeHome),
tmcli.NewCompletionCmd(rootCmd, true),
testnetCmd(gaia.ModuleBasics, banktypes.GenesisBalancesIterator{}),
debug.Cmd(),
addDebugCommands(debug.Cmd()),
config.Cmd(),
)

Expand Down
22 changes: 22 additions & 0 deletions pkg/address/address.go
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way it is not 100% covered is it okay?

_, 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
}