Skip to content
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

R4R: gaiakeyutil -> gaiacli keys parse #4228

Merged
merged 13 commits into from
May 2, 2019
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ else
go build -mod=readonly $(BUILD_FLAGS) -o build/gaiad ./cmd/gaia/cmd/gaiad
go build -mod=readonly $(BUILD_FLAGS) -o build/gaiacli ./cmd/gaia/cmd/gaiacli
go build -mod=readonly $(BUILD_FLAGS) -o build/gaiareplay ./cmd/gaia/cmd/gaiareplay
go build -mod=readonly $(BUILD_FLAGS) -o build/gaiakeyutil ./cmd/gaia/cmd/gaiakeyutil
endif

build-linux: go.sum
Expand All @@ -94,7 +93,6 @@ install: go.sum check-ledger update_gaia_lite_docs
go install -mod=readonly $(BUILD_FLAGS) ./cmd/gaia/cmd/gaiad
go install -mod=readonly $(BUILD_FLAGS) ./cmd/gaia/cmd/gaiacli
go install -mod=readonly $(BUILD_FLAGS) ./cmd/gaia/cmd/gaiareplay
go install -mod=readonly $(BUILD_FLAGS) ./cmd/gaia/cmd/gaiakeyutil

install_debug: go.sum
go install -mod=readonly $(BUILD_FLAGS) ./cmd/gaia/cmd/gaiadebug
Expand Down
64 changes: 64 additions & 0 deletions client/keys/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package keys

import (
"encoding/hex"
"errors"
"fmt"

"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-string>",
alessio marked this conversation as resolved.
Show resolved Hide resolved
Short: "Parse key hex or bech32 strings and show relevant information",
alessio marked this conversation as resolved.
Show resolved Hide resolved
Long: "Convert and print to stdout key addresses and fingerprints from hexadecimal into" +
alessio marked this conversation as resolved.
Show resolved Hide resolved
" bech32 cosmos prefixed format and vice versa.",
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
if !(runFromBech32(args[0]) || runFromHex(args[0])) {
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 readible part: %v\nBytes (hex): %X\n", hrp, bz)
alessio marked this conversation as resolved.
Show resolved Hide resolved
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.
Show resolved Hide resolved
}
return true
}
1 change: 1 addition & 0 deletions client/keys/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func Commands() *cobra.Command {
client.LineBreak,
deleteKeyCommand(),
updateKeyCommand(),
parseKeyStringCommand(),
)
return cmd
}
2 changes: 1 addition & 1 deletion client/keys/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ func TestCommands(t *testing.T) {
assert.NotNil(t, rootCommands)

// Commands are registered
assert.Equal(t, 7, len(rootCommands.Commands()))
assert.Equal(t, 8, len(rootCommands.Commands()))
}
2 changes: 0 additions & 2 deletions cmd/gaia/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ else
go build -mod=readonly $(BUILD_FLAGS) -o build/gaiad ../../cmd/gaia/cmd/gaiad
go build -mod=readonly $(BUILD_FLAGS) -o build/gaiacli ../../cmd/gaia/cmd/gaiacli
go build -mod=readonly $(BUILD_FLAGS) -o build/gaiareplay ../../cmd/gaia/cmd/gaiareplay
go build -mod=readonly $(BUILD_FLAGS) -o build/gaiakeyutil ../../cmd/gaia/cmd/gaiakeyutil
endif

build-linux: ../../go.sum
Expand All @@ -82,7 +81,6 @@ install: ../../go.sum check-ledger
go install -mod=readonly $(BUILD_FLAGS) ../../cmd/gaia/cmd/gaiad
go install -mod=readonly $(BUILD_FLAGS) ../../cmd/gaia/cmd/gaiacli
go install -mod=readonly $(BUILD_FLAGS) ../../cmd/gaia/cmd/gaiareplay
go install -mod=readonly $(BUILD_FLAGS) ../../cmd/gaia/cmd/gaiakeyutil

install-debug: ../../go.sum
go install -mod=readonly $(BUILD_FLAGS) ../../cmd/gaia/cmd/gaiadebug
Expand Down
60 changes: 0 additions & 60 deletions cmd/gaia/cmd/gaiakeyutil/main.go

This file was deleted.