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"
"github.com/cosmos/cosmos-sdk/types/bech32"
ivanovpetr marked this conversation as resolved.
Show resolved Hide resolved
"github.com/spf13/cobra"
)

var flagBech32Prefix = "prefix"

// AddBech32ConvertCommand returns bech32-convert cobra Command.
func AddBech32ConvertCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "bech32-convert [bech32 string]",
ivanovpetr marked this conversation as resolved.
Show resolved Hide resolved
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"

`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
bech32prefix, err := cmd.Flags().GetString(flagBech32Prefix)
if err != nil {
return err
}

_, bz, err := bech32.DecodeAndConvert(args[0])
if err != nil {
return fmt.Errorf("cannot decode address", err)
ivanovpetr marked this conversation as resolved.
Show resolved Hide resolved
}

bech32Addr, err := bech32.ConvertAndEncode(bech32prefix, bz)
if err != nil {
return fmt.Errorf("cannot convert address", err)
ivanovpetr marked this conversation as resolved.
Show resolved Hide resolved
}

cmd.Println(bech32Addr)

return nil
},
}

cmd.Flags().StringP(flagBech32Prefix, "p", "cosmos", "Bech32 Prefix to encode to")

return cmd
}

// injectConvertBech32Command injects bech32-convert command into another command as a child.
func injectConvertBech32Command(cmd *cobra.Command) *cobra.Command {
ivanovpetr marked this conversation as resolved.
Show resolved Hide resolved
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(),
injectConvertBech32Command(debug.Cmd()),
config.Cmd(),
)

Expand Down