-
Notifications
You must be signed in to change notification settings - Fork 697
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
Changes from 4 commits
f2eb398
b083d98
df75679
135b5ff
fe06362
f10597f
2019a90
78e71a0
1d1b581
12d3a0f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
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 | ||
} |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please add a test for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
There was a problem hiding this comment.
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
flagThere was a problem hiding this comment.
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?
There was a problem hiding this comment.
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"