Skip to content

Commit

Permalink
add bech32 convert command
Browse files Browse the repository at this point in the history
  • Loading branch information
farbanas committed Jun 2, 2023
1 parent 554f30f commit 92baac4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
50 changes: 50 additions & 0 deletions cmd/nyxd/bech32.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

// DONTCOVER

import (
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/types/bech32"
)

var flagBech32Prefix = "prefix"

// get cmd to convert any bech32 address to an osmo prefix.
func ConvertBech32Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "bech32-convert [bech32 string]",
Short: "Convert any bech32 string to the osmo prefix",
Long: `Convert any bech32 string to the osmo prefix
Especially useful for converting cosmos addresses to osmo addresses
Example:
osmosisd bech32-convert cosmos1ey69r37gfxvxg62sh4r0ktpuc46pzjrmz29g45
`,
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 err
}

bech32Addr, err := bech32.ConvertAndEncode(bech32prefix, bz)
if err != nil {
panic(err)
}

cmd.Println(bech32Addr)

return nil
},
}

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

return cmd
}
4 changes: 3 additions & 1 deletion cmd/nyxd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
}

func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
debugCmd := debug.Cmd()
debugCmd.AddCommand(ConvertBech32Cmd())
rootCmd.AddCommand(
genutilcli.InitCmd(app.ModuleBasics, app.DefaultNodeHome),
genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome),
Expand All @@ -101,7 +103,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
AddGenesisAccountCmd(app.DefaultNodeHome),
tmcli.NewCompletionCmd(rootCmd, true),
// testnetCmd(app.ModuleBasics, banktypes.GenesisBalancesIterator{}),
debug.Cmd(),
debugCmd,
config.Cmd(),
)

Expand Down

0 comments on commit 92baac4

Please sign in to comment.