2018-02-22 07:17:19 -08:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-03-28 08:52:18 -07:00
|
|
|
"path/filepath"
|
2018-02-22 07:17:19 -08:00
|
|
|
|
2018-11-16 14:21:36 -08:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/tendermint/tendermint/libs/cli"
|
2018-12-10 06:27:25 -08:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-02-22 07:17:19 -08:00
|
|
|
)
|
|
|
|
|
2019-02-11 17:18:58 -08:00
|
|
|
// available output formats.
|
2019-02-08 12:45:23 -08:00
|
|
|
const (
|
|
|
|
OutputFormatText = "text"
|
|
|
|
OutputFormatJSON = "json"
|
2019-02-11 17:18:58 -08:00
|
|
|
|
|
|
|
// defaultKeyDBName is the client's subdirectory where keys are stored.
|
|
|
|
defaultKeyDBName = "keys"
|
2019-02-08 12:45:23 -08:00
|
|
|
)
|
2018-02-28 06:36:04 -08:00
|
|
|
|
2018-08-30 21:06:44 -07:00
|
|
|
type bechKeyOutFn func(keyInfo keys.Info) (KeyOutput, error)
|
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
// GetKeyInfo returns key info for a given name. An error is returned if the
|
|
|
|
// keybase cannot be retrieved or getting the info fails.
|
|
|
|
func GetKeyInfo(name string) (keys.Info, error) {
|
2019-02-06 11:23:49 -08:00
|
|
|
keybase, err := NewKeyBaseFromHomeFlag()
|
2018-08-06 11:11:30 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return keybase.Get(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPassphrase returns a passphrase for a given name. It will first retrieve
|
|
|
|
// the key info for that name if the type is local, it'll fetch input from
|
|
|
|
// STDIN. Otherwise, an empty passphrase is returned. An error is returned if
|
|
|
|
// the key info cannot be fetched or reading from STDIN fails.
|
|
|
|
func GetPassphrase(name string) (string, error) {
|
|
|
|
var passphrase string
|
|
|
|
|
|
|
|
keyInfo, err := GetKeyInfo(name)
|
|
|
|
if err != nil {
|
|
|
|
return passphrase, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// we only need a passphrase for locally stored keys
|
|
|
|
// TODO: (ref: #864) address security concerns
|
|
|
|
if keyInfo.GetType() == keys.TypeLocal {
|
|
|
|
passphrase, err = ReadPassphraseFromStdin(name)
|
|
|
|
if err != nil {
|
|
|
|
return passphrase, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return passphrase, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadPassphraseFromStdin attempts to read a passphrase from STDIN return an
|
|
|
|
// error upon failure.
|
|
|
|
func ReadPassphraseFromStdin(name string) (string, error) {
|
|
|
|
buf := client.BufferStdin()
|
|
|
|
prompt := fmt.Sprintf("Password to sign with '%s':", name)
|
|
|
|
|
|
|
|
passphrase, err := client.GetPassword(prompt, buf)
|
|
|
|
if err != nil {
|
|
|
|
return passphrase, fmt.Errorf("Error reading passphrase: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return passphrase, nil
|
|
|
|
}
|
|
|
|
|
2019-02-06 11:23:49 -08:00
|
|
|
// NewKeyBaseFromHomeFlag initializes a Keybase based on the configuration.
|
|
|
|
func NewKeyBaseFromHomeFlag() (keys.Keybase, error) {
|
2018-10-12 19:22:07 -07:00
|
|
|
rootDir := viper.GetString(cli.HomeFlag)
|
2019-02-06 11:23:49 -08:00
|
|
|
return NewKeyBaseFromDir(rootDir)
|
2018-10-22 14:29:27 -07:00
|
|
|
}
|
|
|
|
|
2019-02-06 11:23:49 -08:00
|
|
|
// NewKeyBaseFromDir initializes a keybase at a particular dir.
|
|
|
|
func NewKeyBaseFromDir(rootDir string) (keys.Keybase, error) {
|
|
|
|
return getLazyKeyBaseFromDir(rootDir)
|
2018-02-22 07:17:19 -08:00
|
|
|
}
|
|
|
|
|
2019-02-07 14:38:45 -08:00
|
|
|
// NewInMemoryKeyBase returns a storage-less keybase.
|
|
|
|
func NewInMemoryKeyBase() keys.Keybase { return keys.NewInMemory() }
|
|
|
|
|
2019-02-06 11:23:49 -08:00
|
|
|
func getLazyKeyBaseFromDir(rootDir string) (keys.Keybase, error) {
|
2019-02-11 17:18:58 -08:00
|
|
|
return keys.New(defaultKeyDBName, filepath.Join(rootDir, "keys")), nil
|
2018-03-05 08:41:50 -08:00
|
|
|
}
|
|
|
|
|
2018-06-05 21:53:04 -07:00
|
|
|
// create a list of KeyOutput in bech32 format
|
|
|
|
func Bech32KeysOutput(infos []keys.Info) ([]KeyOutput, error) {
|
|
|
|
kos := make([]KeyOutput, len(infos))
|
|
|
|
for i, info := range infos {
|
|
|
|
ko, err := Bech32KeyOutput(info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
kos[i] = ko
|
2018-04-20 09:51:47 -07:00
|
|
|
}
|
2018-06-05 21:53:04 -07:00
|
|
|
return kos, nil
|
2018-04-20 09:51:47 -07:00
|
|
|
}
|
|
|
|
|
2018-06-05 21:53:04 -07:00
|
|
|
// create a KeyOutput in bech32 format
|
|
|
|
func Bech32KeyOutput(info keys.Info) (KeyOutput, error) {
|
2018-08-30 21:06:44 -07:00
|
|
|
accAddr := sdk.AccAddress(info.GetPubKey().Address().Bytes())
|
2018-06-28 17:54:47 -07:00
|
|
|
bechPubKey, err := sdk.Bech32ifyAccPub(info.GetPubKey())
|
2018-06-05 21:53:04 -07:00
|
|
|
if err != nil {
|
|
|
|
return KeyOutput{}, err
|
|
|
|
}
|
2018-08-30 21:06:44 -07:00
|
|
|
|
2018-06-05 21:53:04 -07:00
|
|
|
return KeyOutput{
|
2018-06-28 17:54:47 -07:00
|
|
|
Name: info.GetName(),
|
2018-08-06 11:11:30 -07:00
|
|
|
Type: info.GetType().String(),
|
2018-08-30 21:06:44 -07:00
|
|
|
Address: accAddr.String(),
|
|
|
|
PubKey: bechPubKey,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bech32ConsKeyOutput returns key output for a consensus node's key
|
|
|
|
// information.
|
|
|
|
func Bech32ConsKeyOutput(keyInfo keys.Info) (KeyOutput, error) {
|
|
|
|
consAddr := sdk.ConsAddress(keyInfo.GetPubKey().Address().Bytes())
|
|
|
|
|
|
|
|
bechPubKey, err := sdk.Bech32ifyConsPub(keyInfo.GetPubKey())
|
|
|
|
if err != nil {
|
|
|
|
return KeyOutput{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return KeyOutput{
|
|
|
|
Name: keyInfo.GetName(),
|
|
|
|
Type: keyInfo.GetType().String(),
|
|
|
|
Address: consAddr.String(),
|
|
|
|
PubKey: bechPubKey,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bech32ValKeyOutput returns key output for a validator's key information.
|
|
|
|
func Bech32ValKeyOutput(keyInfo keys.Info) (KeyOutput, error) {
|
|
|
|
valAddr := sdk.ValAddress(keyInfo.GetPubKey().Address().Bytes())
|
|
|
|
|
|
|
|
bechPubKey, err := sdk.Bech32ifyValPub(keyInfo.GetPubKey())
|
|
|
|
if err != nil {
|
|
|
|
return KeyOutput{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return KeyOutput{
|
|
|
|
Name: keyInfo.GetName(),
|
|
|
|
Type: keyInfo.GetType().String(),
|
|
|
|
Address: valAddr.String(),
|
2018-06-05 21:53:04 -07:00
|
|
|
PubKey: bechPubKey,
|
|
|
|
}, nil
|
2018-04-20 09:51:47 -07:00
|
|
|
}
|
|
|
|
|
2018-08-30 21:06:44 -07:00
|
|
|
func printKeyInfo(keyInfo keys.Info, bechKeyOut bechKeyOutFn) {
|
|
|
|
ko, err := bechKeyOut(keyInfo)
|
2018-06-05 21:53:04 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-08-30 21:06:44 -07:00
|
|
|
|
2018-02-22 07:17:19 -08:00
|
|
|
switch viper.Get(cli.OutputFlag) {
|
2019-02-08 12:45:23 -08:00
|
|
|
case OutputFormatText:
|
2018-06-28 17:54:47 -07:00
|
|
|
fmt.Printf("NAME:\tTYPE:\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-08-30 21:06:44 -07: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-06-05 21:53:04 -07:00
|
|
|
kos, err := Bech32KeysOutput(infos)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-02-22 07:17:19 -08:00
|
|
|
switch viper.Get(cli.OutputFlag) {
|
2019-02-08 12:45:23 -08:00
|
|
|
case OutputFormatText:
|
2018-06-28 17:54:47 -07:00
|
|
|
fmt.Printf("NAME:\tTYPE:\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
|
|
|
}
|
2019-02-08 12:45:23 -08:00
|
|
|
case OutputFormatJSON:
|
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-28 17:54:47 -07:00
|
|
|
fmt.Printf("%s\t%s\t%s\t%s\n", ko.Name, ko.Type, ko.Address, ko.PubKey)
|
2018-05-28 16:27:34 -07:00
|
|
|
}
|
2018-08-10 23:42:57 -07:00
|
|
|
|
2018-08-30 21:06:44 -07:00
|
|
|
func printKeyAddress(info keys.Info, bechKeyOut bechKeyOutFn) {
|
|
|
|
ko, err := bechKeyOut(info)
|
2018-08-10 23:42:57 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-08-30 21:06:44 -07:00
|
|
|
|
|
|
|
fmt.Println(ko.Address)
|
2018-08-10 23:42:57 -07:00
|
|
|
}
|
|
|
|
|
2018-08-30 21:06:44 -07:00
|
|
|
func printPubKey(info keys.Info, bechKeyOut bechKeyOutFn) {
|
|
|
|
ko, err := bechKeyOut(info)
|
2018-08-10 23:42:57 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-08-30 21:06:44 -07:00
|
|
|
|
2018-08-10 23:42:57 -07:00
|
|
|
fmt.Println(ko.PubKey)
|
|
|
|
}
|