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

Better key output #886

Merged
merged 2 commits into from
Apr 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ FEATURES:
* Gaia stake commands include, DeclareCandidacy, EditCandidacy, Delegate, Unbond
* MountStoreWithDB without providing a custom store works.
* Repo is now lint compliant / GoMetaLinter with tendermint-lint integrated into CI
* Better key output, pubkey go-amino hex bytes now output by default

BREAKING CHANGES

Expand Down
70 changes: 42 additions & 28 deletions client/keys/utils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package keys

import (
"encoding/hex"
"encoding/json"
"fmt"
"path/filepath"
"strings"

"github.com/spf13/viper"

Expand All @@ -16,20 +19,10 @@ import (
// KeyDBName is the directory under root where we store the keys
const KeyDBName = "keys"

var (
// keybase is used to make GetKeyBase a singleton
keybase keys.Keybase
)

// used for outputting keys.Info over REST
type KeyOutput struct {
Name string `json:"name"`
Address string `json:"address"`
// TODO add pubkey?
// Pubkey string `json:"pubkey"`
}
// keybase is used to make GetKeyBase a singleton
var keybase keys.Keybase

// GetKeyBase initializes a keybase based on the configuration
// initialize a keybase based on the configuration
func GetKeyBase() (keys.Keybase, error) {
if keybase == nil {
rootDir := viper.GetString(cli.HomeFlag)
Expand All @@ -47,36 +40,57 @@ func SetKeyBase(kb keys.Keybase) {
keybase = kb
}

// used for outputting keys.Info over REST
type KeyOutput struct {
Name string `json:"name"`
Address string `json:"address"`
PubKey string `json:"pub_key"`
}

func NewKeyOutput(info keys.Info) KeyOutput {
return KeyOutput{
Name: info.Name,
Address: info.PubKey.Address().String(),
PubKey: strings.ToUpper(hex.EncodeToString(info.PubKey.Bytes())),
}
}

func NewKeyOutputs(infos []keys.Info) []KeyOutput {
kos := make([]KeyOutput, len(infos))
for i, info := range infos {
kos[i] = NewKeyOutput(info)
}
return kos
}

func printInfo(info keys.Info) {
ko := NewKeyOutput(info)
switch viper.Get(cli.OutputFlag) {
case "text":
addr := info.PubKey.Address().String()
sep := "\t\t"
if len(info.Name) > 7 {
sep = "\t"
}
fmt.Printf("%s%s%s\n", info.Name, sep, addr)
fmt.Printf("NAME:\tADDRESS:\t\t\t\t\tPUBKEY:\n")
fmt.Printf("%s\t%s\t%s\n", ko.Name, ko.Address, ko.PubKey)
case "json":
json, err := MarshalJSON(info)
out, err := json.MarshalIndent(ko, "", "\t")
if err != nil {
panic(err) // really shouldn't happen...
panic(err)
}
fmt.Println(string(json))
fmt.Println(string(out))
}
}

func printInfos(infos []keys.Info) {
kos := NewKeyOutputs(infos)
switch viper.Get(cli.OutputFlag) {
case "text":
fmt.Println("All keys:")
for _, i := range infos {
printInfo(i)
fmt.Printf("NAME:\tADDRESS:\t\t\t\t\tPUBKEY:\n")
for _, ko := range kos {
fmt.Printf("%s\t%s\t%s\n", ko.Name, ko.Address, ko.PubKey)
}
case "json":
json, err := MarshalJSON(infos)
out, err := json.MarshalIndent(kos, "", "\t")
if err != nil {
panic(err) // really shouldn't happen...
panic(err)
}
fmt.Println(string(json))
fmt.Println(string(out))
}
}
11 changes: 3 additions & 8 deletions cmd/gaia/cli_test/cli_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package clitest

import (
"encoding/hex"
"encoding/json"
"fmt"
"strings"
Expand All @@ -17,8 +16,6 @@ import (
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/stake"
crypto "github.com/tendermint/go-crypto"
crkeys "github.com/tendermint/go-crypto/keys"
)

func TestGaiaCLISend(t *testing.T) {
Expand Down Expand Up @@ -148,11 +145,9 @@ func executeInit(t *testing.T, cmdStr string) (masterKey, chainID string) {

func executeGetAddrPK(t *testing.T, cmdStr string) (addr, pubKey string) {
out := tests.ExecuteT(t, cmdStr, 2)
var info crkeys.Info
keys.UnmarshalJSON([]byte(out), &info)
pubKey = hex.EncodeToString(info.PubKey.(crypto.PubKeyEd25519).Bytes())
addr = info.PubKey.Address().String()
return
var ko keys.KeyOutput
keys.UnmarshalJSON([]byte(out), &ko)
return ko.Address, ko.PubKey
}

func executeGetAccount(t *testing.T, cmdStr string) auth.BaseAccount {
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions docs/staking/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ which will give you three prompts:
create a password and copy in your seed phrase. The name and address of the key will be output:

::

alice 67997DD03D527EB439B7193F2B813B05B219CC02
NAME: ADDRESS: PUBKEY:
alice 67997DD03D527EB439B7193F2B813B05B219CC02 1624DE6220BB89786C1D597050438C728202436552C6226AB67453CDB2A4D2703402FB52B6

You can see all available keys with:

Expand Down
3 changes: 2 additions & 1 deletion server/tm_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"encoding/hex"
"fmt"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -49,7 +50,7 @@ func ShowValidatorCmd(ctx *Context) *cobra.Command {
fmt.Println(string(pubKeyJSONBytes))
return nil
}
pubKeyHex := hex.EncodeToString(pubKey.Bytes())
pubKeyHex := strings.ToUpper(hex.EncodeToString(pubKey.Bytes()))
fmt.Println(pubKeyHex)
return nil
},
Expand Down