-
Notifications
You must be signed in to change notification settings - Fork 4.2k
R4R: gaiakeyutil -> gaiacli keys parse #4228
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7e6f9a5
gaiakeyutil -> gaiacli keys parse
8a7ddc0
Update test
d0ad4ca
Update client/keys/parse.go
fedekunze b7521c3
Update client/keys/parse.go
fedekunze 90b6af1
Update client/keys/parse.go
fedekunze a970958
Fix free text
287a009
Add changelog entry file
e0b9bdb
ACK fede suggestion
2ec53a6
Add test
4d1522c
Update client/keys/parse.go
fedekunze 95734b9
Update .pending/breaking/gaiacli/4228-merge-gaiakeyutil-into-gaiacli
alexanderbez 3715a01
Honour --output and --indent flags
14d04db
Merge branch 'alessio/remove-gaiakeyutil' of github.com:cosmos/cosmos…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
.pending/breaking/gaiacli/4228-merge-gaiakeyutil-into-gaiacli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| #4228 Merge gaiakeyutil functionality into gaiacli keys. | ||
| In fact, drop `gaiakeyutil` in favor of new `gaiacli keys parse` command. Syntax and semantic are preserved. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package keys | ||
|
|
||
| import ( | ||
| "encoding/hex" | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/tendermint/tendermint/libs/bech32" | ||
|
|
||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
| ) | ||
|
|
||
| var bech32Prefixes = []string{ | ||
| sdk.Bech32PrefixAccAddr, | ||
| sdk.Bech32PrefixAccPub, | ||
| sdk.Bech32PrefixValAddr, | ||
| sdk.Bech32PrefixValPub, | ||
| sdk.Bech32PrefixConsAddr, | ||
| sdk.Bech32PrefixConsPub, | ||
| } | ||
|
|
||
| func parseKeyStringCommand() *cobra.Command { | ||
| return &cobra.Command{ | ||
| Use: "parse <hex-or-bech32-address>", | ||
| Short: "Parse key hex or bech32 strings and show relevant information", | ||
|
alessio marked this conversation as resolved.
Outdated
|
||
| Long: `Convert and print to stdout key addresses and fingerprints from | ||
| hexadecimal into bech32 cosmos prefixed format and vice versa. | ||
| `, | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: parseKey, | ||
|
alessio marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| func parseKey(_ *cobra.Command, args []string) error { | ||
| addr := strings.TrimSpace(args[0]) | ||
| if len(addr) == 0 { | ||
| return errors.New("couldn't parse empty input") | ||
| } | ||
| if !(runFromBech32(addr) || runFromHex(addr)) { | ||
| return errors.New("couldn't find valid bech32 nor hex data") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // print info from bech32 | ||
| func runFromBech32(bech32str string) bool { | ||
| hrp, bz, err := bech32.DecodeAndConvert(bech32str) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| fmt.Printf("Human readable part: %v\nBytes (hex): %X\n", hrp, bz) | ||
| return true | ||
| } | ||
|
|
||
| // print info from hex | ||
| func runFromHex(hexstr string) bool { | ||
| bz, err := hex.DecodeString(hexstr) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| fmt.Println("Bech32 formats:") | ||
| for _, prefix := range bech32Prefixes { | ||
| bech32Addr, err := bech32.ConvertAndEncode(prefix, bz) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| fmt.Println(" - " + bech32Addr) | ||
|
alessio marked this conversation as resolved.
Outdated
|
||
| } | ||
| return true | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package keys | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestParseKey(t *testing.T) { | ||
| bech32str := "cosmos104ytdpvrx9284zd50v9ep8c6j7pua7dkk0x3ek" | ||
| hexstr := "EB5AE9872103497EC092EF901027049E4F39200C60040D3562CD7F104A39F62E6E5A39A818F4" | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| args []string | ||
| wantErr bool | ||
| }{ | ||
| {"empty input", []string{""}, true}, | ||
| {"invalid input", []string{"invalid"}, true}, | ||
| {"bech32", []string{bech32str}, false}, | ||
| {"hex", []string{hexstr}, false}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| require.Equal(t, tt.wantErr, parseKey(nil, tt.args) != nil) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.