cosmos-sdk/client/keys/utils.go

269 lines
6.9 KiB
Go
Raw Normal View History

2018-02-22 07:17:19 -08:00
package keys
import (
"fmt"
"net/http"
"path/filepath"
2018-02-22 07:17:19 -08:00
"github.com/spf13/viper"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/tendermint/tendermint/libs/cli"
dbm "github.com/tendermint/tendermint/libs/db"
2018-12-10 06:27:25 -08:00
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys"
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
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) {
keybase, err := GetKeyBase()
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
}
// TODO make keybase take a database not load from the directory
2018-10-24 06:37:06 -07:00
// GetKeyBase initializes a read-only KeyBase based on the configuration.
func GetKeyBase() (keys.Keybase, error) {
rootDir := viper.GetString(cli.HomeFlag)
2018-10-22 14:29:27 -07:00
return GetKeyBaseFromDir(rootDir)
}
2018-10-22 14:29:27 -07:00
// GetKeyBaseWithWritePerm initialize a keybase based on the configuration with write permissions.
func GetKeyBaseWithWritePerm() (keys.Keybase, error) {
rootDir := viper.GetString(cli.HomeFlag)
return GetKeyBaseFromDirWithWritePerm(rootDir)
}
2018-10-22 14:29:27 -07:00
// GetKeyBaseFromDirWithWritePerm initializes a keybase at a particular dir with write permissions.
func GetKeyBaseFromDirWithWritePerm(rootDir string) (keys.Keybase, error) {
2018-10-22 14:29:27 -07:00
return getKeyBaseFromDirWithOpts(rootDir, nil)
}
// GetKeyBaseFromDir initializes a read-only keybase at a particular dir.
func GetKeyBaseFromDir(rootDir string) (keys.Keybase, error) {
// Disabled because of the inability to create a new keys database directory
// in the instance of when ReadOnly is set to true.
//
// ref: syndtr/goleveldb#240
// return getKeyBaseFromDirWithOpts(rootDir, &opt.Options{ReadOnly: true})
return getKeyBaseFromDirWithOpts(rootDir, nil)
}
func getKeyBaseFromDirWithOpts(rootDir string, o *opt.Options) (keys.Keybase, error) {
2018-02-22 07:17:19 -08:00
if keybase == nil {
db, err := dbm.NewGoLevelDBWithOpts(KeyDBName, filepath.Join(rootDir, "keys"), o)
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
}
// 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
}
return kos, nil
2018-04-20 09:51:47 -07:00
}
// create a KeyOutput in bech32 format
func Bech32KeyOutput(info keys.Info) (KeyOutput, error) {
accAddr := sdk.AccAddress(info.GetPubKey().Address().Bytes())
bechPubKey, err := sdk.Bech32ifyAccPub(info.GetPubKey())
if err != nil {
return KeyOutput{}, err
}
return KeyOutput{
Name: info.GetName(),
2018-08-06 11:11:30 -07:00
Type: info.GetType().String(),
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(),
PubKey: bechPubKey,
}, nil
2018-04-20 09:51:47 -07: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 "text":
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-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 := Bech32KeysOutput(infos)
if err != nil {
panic(err)
}
2018-02-22 07:17:19 -08:00
switch viper.Get(cli.OutputFlag) {
case "text":
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
}
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) {
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
}
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)
}
2018-10-04 05:27:43 -07:00
// PostProcessResponse performs post process for rest response
func PostProcessResponse(w http.ResponseWriter, cdc *codec.Codec, response interface{}, indent bool) {
var output []byte
switch response.(type) {
default:
var err error
if indent {
output, err = cdc.MarshalJSONIndent(response, "", " ")
} else {
output, err = cdc.MarshalJSON(response)
}
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
case []byte:
output = response.([]byte)
}
w.Header().Set("Content-Type", "application/json")
w.Write(output)
}