cosmos-sdk/client/keys/utils.go

65 lines
1.2 KiB
Go
Raw Normal View History

2018-02-22 07:17:19 -08:00
package keys
import (
"fmt"
"github.com/spf13/viper"
keys "github.com/tendermint/go-crypto/keys"
"github.com/tendermint/tmlibs/cli"
"github.com/cosmos/cosmos-sdk/client"
)
var (
// keybase is used to make GetKeyBase a singleton
keybase keys.Keybase
)
// GetKeyBase initializes a keybase based on the configuration
func GetKeyBase() (keys.Keybase, error) {
if keybase == nil {
rootDir := viper.GetString(cli.HomeFlag)
2018-02-22 07:49:32 -08:00
kb, err := client.GetKeyBase(rootDir)
2018-02-22 07:17:19 -08:00
if err != nil {
return nil, err
}
2018-02-22 07:49:32 -08:00
keybase = kb
2018-02-22 07:17:19 -08:00
}
return keybase, nil
}
func printInfo(info keys.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)
case "json":
json, err := MarshalJSON(info)
if err != nil {
panic(err) // really shouldn't happen...
}
fmt.Println(string(json))
}
}
func printInfos(infos []keys.Info) {
switch viper.Get(cli.OutputFlag) {
case "text":
fmt.Println("All keys:")
for _, i := range infos {
printInfo(i)
}
case "json":
json, err := MarshalJSON(infos)
if err != nil {
panic(err) // really shouldn't happen...
}
fmt.Println(string(json))
}
}