Skip to content

Commit dd6b7ab

Browse files
committedJun 2, 2023
add bech32 convert command
1 parent a383ad8 commit dd6b7ab

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed
 

‎cmd/nyxd/bech32.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
// DONTCOVER
4+
5+
import (
6+
"github.com/spf13/cobra"
7+
8+
"github.com/cosmos/cosmos-sdk/types/bech32"
9+
)
10+
11+
var flagBech32Prefix = "prefix"
12+
13+
// get cmd to convert any bech32 address to an osmo prefix.
14+
func ConvertBech32Cmd() *cobra.Command {
15+
cmd := &cobra.Command{
16+
Use: "bech32-convert [bech32 string]",
17+
Short: "Convert any bech32 string to the osmo prefix",
18+
Long: `Convert any bech32 string to the osmo prefix
19+
Especially useful for converting cosmos addresses to osmo addresses
20+
21+
Example:
22+
osmosisd bech32-convert cosmos1ey69r37gfxvxg62sh4r0ktpuc46pzjrmz29g45
23+
`,
24+
Args: cobra.ExactArgs(1),
25+
RunE: func(cmd *cobra.Command, args []string) error {
26+
bech32prefix, err := cmd.Flags().GetString(flagBech32Prefix)
27+
if err != nil {
28+
return err
29+
}
30+
31+
_, bz, err := bech32.DecodeAndConvert(args[0])
32+
if err != nil {
33+
return err
34+
}
35+
36+
bech32Addr, err := bech32.ConvertAndEncode(bech32prefix, bz)
37+
if err != nil {
38+
panic(err)
39+
}
40+
41+
cmd.Println(bech32Addr)
42+
43+
return nil
44+
},
45+
}
46+
47+
cmd.Flags().StringP(flagBech32Prefix, "p", "n", "Bech32 Prefix to encode to")
48+
49+
return cmd
50+
}

‎cmd/nyxd/root.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
9393
}
9494

9595
func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
96+
debugCmd := debug.Cmd()
97+
debugCmd.AddCommand(ConvertBech32Cmd())
9698
rootCmd.AddCommand(
9799
genutilcli.InitCmd(app.ModuleBasics, app.DefaultNodeHome),
98100
genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome),
@@ -102,7 +104,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
102104
AddGenesisWasmMsgCmd(app.DefaultNodeHome),
103105
tmcli.NewCompletionCmd(rootCmd, true),
104106
// testnetCmd(app.ModuleBasics, banktypes.GenesisBalancesIterator{}),
105-
debug.Cmd(),
107+
debugCmd,
106108
config.Cmd(),
107109
)
108110

0 commit comments

Comments
 (0)