cosmos-sdk/client/keys/utils.go

114 lines
2.4 KiB
Go
Raw Normal View History

2018-02-22 07:17:19 -08:00
package keys
import (
"fmt"
"path/filepath"
2018-02-22 07:17:19 -08:00
"github.com/spf13/viper"
crypto "github.com/tendermint/go-crypto"
2018-02-22 07:17:19 -08:00
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"
sdk "github.com/cosmos/cosmos-sdk/types"
2018-02-22 07:17:19 -08:00
)
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 sdk.Address `json:"address"`
PubKey crypto.PubKey `json:"pub_key"`
2018-04-20 09:51:47 -07:00
}
func NewKeyOutput(info keys.Info) KeyOutput {
return KeyOutput{
Name: info.Name,
Address: sdk.Address(info.PubKey.Address().Bytes()),
PubKey: info.PubKey,
2018-04-20 09:51:47 -07:00
}
}
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-05-28 19:51:11 -07:00
fmt.Printf("NAME:\tADDRESS:\t\t\t\t\t\tPUBKEY:\n")
2018-05-28 16:27:34 -07:00
printKeyOutput(ko)
2018-02-22 07:17:19 -08:00
case "json":
2018-05-31 12:10:12 -07:00
out, err := MarshalJSON(ko)
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-05-28 19:51:11 -07:00
fmt.Printf("NAME:\tADDRESS:\t\t\t\t\t\tPUBKEY:\n")
2018-04-20 09:51:47 -07:00
for _, ko := range kos {
2018-05-28 16:27:34 -07:00
printKeyOutput(ko)
2018-02-22 07:17:19 -08:00
}
case "json":
2018-05-31 13:33:52 -07:00
out, err := MarshalJSON(kos)
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
}
}
2018-05-28 16:27:34 -07:00
func printKeyOutput(ko KeyOutput) {
2018-06-01 07:23:58 -07:00
bechAccount, err := sdk.Bech32ifyAcc(ko.Address)
2018-05-28 16:27:34 -07:00
if err != nil {
panic(err)
}
2018-06-01 07:23:58 -07:00
bechPubKey, err := sdk.Bech32ifyAccPub(ko.PubKey)
2018-05-28 16:27:34 -07:00
if err != nil {
panic(err)
}
fmt.Printf("%s\t%s\t%s\n", ko.Name, bechAccount, bechPubKey)
}