better key output

This commit is contained in:
rigelrozanski 2018-04-20 12:51:47 -04:00
parent 45a97e1206
commit ca3d3be3af
4 changed files with 48 additions and 37 deletions

View File

@ -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

View File

@ -1,8 +1,11 @@
package keys
import (
"encoding/hex"
"encoding/json"
"fmt"
"path/filepath"
"strings"
"github.com/spf13/viper"
@ -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
)
// keybase is used to make GetKeyBase a singleton
var 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"`
}
// 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)
@ -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))
}
}

View File

@ -1,7 +1,6 @@
package clitest
import (
"encoding/hex"
"encoding/json"
"fmt"
"strings"
@ -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) {
@ -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 {

View File

@ -3,6 +3,7 @@ package server
import (
"encoding/hex"
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -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
},