cosmos-sdk/client/keys/utils.go

174 lines
4.0 KiB
Go
Raw Normal View History

2018-02-22 07:17:19 -08:00
package keys
import (
"bufio"
2018-02-22 07:17:19 -08:00
"fmt"
"os"
"path/filepath"
2018-02-22 07:17:19 -08:00
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/cli"
yaml "gopkg.in/yaml.v2"
2018-12-10 06:27:25 -08:00
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
2018-12-10 06:27:25 -08:00
"github.com/cosmos/cosmos-sdk/crypto/keys"
2018-02-22 07:17:19 -08:00
)
// available output formats.
const (
OutputFormatText = "text"
OutputFormatJSON = "json"
// defaultKeyDBName is the client's subdirectory where keys are stored.
defaultKeyDBName = "keys"
)
2018-02-28 06:36:04 -08:00
type bechKeyOutFn func(keyInfo keys.Info) (keys.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) {
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 := bufio.NewReader(os.Stdin)
2018-08-06 11:11:30 -07:00
prompt := fmt.Sprintf("Password to sign with '%s':", name)
passphrase, err := input.GetPassword(prompt, buf)
2018-08-06 11:11:30 -07:00
if err != nil {
2019-08-19 09:06:27 -07:00
return passphrase, fmt.Errorf("error reading passphrase: %v", err)
2018-08-06 11:11:30 -07:00
}
return passphrase, nil
}
// NewKeyBaseFromHomeFlag initializes a Keybase based on the configuration.
func NewKeyBaseFromHomeFlag() (keys.Keybase, error) {
rootDir := viper.GetString(flags.FlagHome)
return NewKeyBaseFromDir(rootDir)
2018-10-22 14:29:27 -07: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
}
// NewInMemoryKeyBase returns a storage-less keybase.
func NewInMemoryKeyBase() keys.Keybase { return keys.NewInMemory() }
func getLazyKeyBaseFromDir(rootDir string) (keys.Keybase, error) {
return keys.New(defaultKeyDBName, filepath.Join(rootDir, "keys")), nil
2018-03-05 08:41:50 -08:00
}
func printKeyInfo(keyInfo keys.Info, bechKeyOut bechKeyOutFn) {
ko, err := bechKeyOut(keyInfo)
if err != nil {
panic(err)
}
2018-02-22 07:17:19 -08:00
switch viper.Get(cli.OutputFlag) {
case OutputFormatText:
printTextInfos([]keys.KeyOutput{ko})
case OutputFormatJSON:
var out []byte
var err error
if viper.GetBool(flags.FlagIndentResponse) {
out, err = cdc.MarshalJSONIndent(ko, "", " ")
} else {
out, err = cdc.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) {
kos, err := keys.Bech32KeysOutput(infos)
if err != nil {
panic(err)
}
2018-02-22 07:17:19 -08:00
switch viper.Get(cli.OutputFlag) {
case OutputFormatText:
printTextInfos(kos)
case OutputFormatJSON:
var out []byte
var err error
if viper.GetBool(flags.FlagIndentResponse) {
out, err = cdc.MarshalJSONIndent(kos, "", " ")
} else {
out, err = cdc.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
}
fmt.Printf("%s", out)
2018-02-22 07:17:19 -08:00
}
}
2018-05-28 16:27:34 -07:00
func printTextInfos(kos []keys.KeyOutput) {
out, err := yaml.Marshal(&kos)
if err != nil {
panic(err)
}
fmt.Println(string(out))
}
func printKeyAddress(info keys.Info, bechKeyOut bechKeyOutFn) {
ko, err := bechKeyOut(info)
if err != nil {
panic(err)
}
fmt.Println(ko.Address)
}
func printPubKey(info keys.Info, bechKeyOut bechKeyOutFn) {
ko, err := bechKeyOut(info)
if err != nil {
panic(err)
}
fmt.Println(ko.PubKey)
}