diff --git a/CHANGELOG.md b/CHANGELOG.md index 26f9d105a4f9..e4c141631de2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/client/keys/utils.go b/client/keys/utils.go index d1b3d3f657f7..f86389a896c8 100644 --- a/client/keys/utils.go +++ b/client/keys/utils.go @@ -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 -) - -// 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) @@ -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)) } } diff --git a/cmd/gaia/cli_test/cli_test.go b/cmd/gaia/cli_test/cli_test.go index 6bda6894b4b0..827c7116b4d3 100644 --- a/cmd/gaia/cli_test/cli_test.go +++ b/cmd/gaia/cli_test/cli_test.go @@ -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 { diff --git a/client/keys/README.md b/docs/old/keys.md similarity index 100% rename from client/keys/README.md rename to docs/old/keys.md diff --git a/docs/staking/intro.rst b/docs/staking/intro.rst index 68032c5363fe..961136894121 100644 --- a/docs/staking/intro.rst +++ b/docs/staking/intro.rst @@ -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: diff --git a/server/tm_cmds.go b/server/tm_cmds.go index 648e3d80ddc9..73dca6651e98 100644 --- a/server/tm_cmds.go +++ b/server/tm_cmds.go @@ -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 },