cosmos-sdk/client/keys/utils.go

102 lines
2.3 KiB
Go
Raw Normal View History

2018-02-22 07:17:19 -08:00
package keys
import (
2018-04-20 09:51:47 -07:00
"encoding/hex"
"encoding/json"
2018-02-22 07:17:19 -08:00
"fmt"
"path/filepath"
2018-04-20 09:51:47 -07:00
"strings"
2018-02-22 07:17:19 -08:00
"github.com/spf13/viper"
keys "github.com/tendermint/go-crypto/keys"
"github.com/tendermint/tmlibs/cli"
2018-02-28 06:36:04 -08:00
dbm "github.com/tendermint/tmlibs/db"
2018-02-22 07:17:19 -08:00
"github.com/cosmos/cosmos-sdk/client"
)
2018-02-28 06:36:04 -08:00
// KeyDBName is the directory under root where we store the keys
const KeyDBName = "keys"
2018-04-20 09:51:47 -07:00
// keybase is used to make GetKeyBase a singleton
var keybase keys.Keybase
2018-04-20 09:51:47 -07:00
// initialize a keybase based on the configuration
2018-02-22 07:17:19 -08:00
func GetKeyBase() (keys.Keybase, error) {
2018-04-25 21:27:40 -07:00
rootDir := viper.GetString(cli.HomeFlag)
return GetKeyBaseFromDir(rootDir)
}
// initialize a keybase based on the configuration
func GetKeyBaseFromDir(rootDir string) (keys.Keybase, error) {
2018-02-22 07:17:19 -08:00
if keybase == nil {
db, err := dbm.NewGoLevelDB(KeyDBName, filepath.Join(rootDir, "keys"))
2018-02-22 07:17:19 -08:00
if err != nil {
return nil, err
}
2018-02-28 06:36:04 -08:00
keybase = client.GetKeyBase(db)
2018-02-22 07:17:19 -08:00
}
return keybase, nil
}
2018-03-05 08:41:50 -08:00
// used to set the keybase manually in test
func SetKeyBase(kb keys.Keybase) {
keybase = kb
}
2018-04-20 09:51:47 -07:00
// 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
}
2018-02-22 07:17:19 -08:00
func printInfo(info keys.Info) {
2018-04-20 09:51:47 -07:00
ko := NewKeyOutput(info)
2018-02-22 07:17:19 -08:00
switch viper.Get(cli.OutputFlag) {
case "text":
2018-04-20 09:51:47 -07:00
fmt.Printf("NAME:\tADDRESS:\t\t\t\t\tPUBKEY:\n")
fmt.Printf("%s\t%s\t%s\n", ko.Name, ko.Address, ko.PubKey)
2018-02-22 07:17:19 -08:00
case "json":
2018-04-20 09:51:47 -07:00
out, err := json.MarshalIndent(ko, "", "\t")
2018-02-22 07:17:19 -08:00
if err != nil {
2018-04-20 09:51:47 -07:00
panic(err)
2018-02-22 07:17:19 -08:00
}
2018-04-20 09:51:47 -07:00
fmt.Println(string(out))
2018-02-22 07:17:19 -08:00
}
}
func printInfos(infos []keys.Info) {
2018-04-20 09:51:47 -07:00
kos := NewKeyOutputs(infos)
2018-02-22 07:17:19 -08:00
switch viper.Get(cli.OutputFlag) {
case "text":
2018-04-20 09:51:47 -07:00
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)
2018-02-22 07:17:19 -08:00
}
case "json":
2018-04-20 09:51:47 -07:00
out, err := json.MarshalIndent(kos, "", "\t")
2018-02-22 07:17:19 -08:00
if err != nil {
2018-04-20 09:51:47 -07:00
panic(err)
2018-02-22 07:17:19 -08:00
}
2018-04-20 09:51:47 -07:00
fmt.Println(string(out))
2018-02-22 07:17:19 -08:00
}
}