-
Notifications
You must be signed in to change notification settings - Fork 28
/
bech32_convert.go
67 lines (51 loc) · 1.68 KB
/
bech32_convert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package cmd
import (
"fmt"
"github.com/cosmos/cosmos-sdk/types/bech32"
"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 omniflix prefix",
Long: `Convert any bech32 string to the omniflix prefix
Example:
omniflixhubd debug bech32-convert juno1a6zlyvpnksx8wr6wz8wemur2xe8zyh0yst53ep
omniflixhubd debug bech32-convert cosmos1673f0t8p893rqyqe420mgwwz92ac4qv6n0nsjx --prefix omniflix
`,
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 := 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
}
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
}
// addDebugCommands adds the custom debug commands to the application.
func addDebugCommands(cmd *cobra.Command) *cobra.Command {
cmd.AddCommand(AddBech32ConvertCommand())
return cmd
}