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
|
|
|
|
2019-11-14 06:17:21 -08:00
|
|
|
"github.com/99designs/keyring"
|
2018-11-16 14:21:36 -08:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/tendermint/tendermint/libs/cli"
|
2019-11-14 06:17:21 -08:00
|
|
|
"gopkg.in/yaml.v2"
|
2018-12-10 06:27:25 -08:00
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2018-12-10 06:27:25 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys"
|
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
|
|
|
|
2019-03-01 13:29:33 -08:00
|
|
|
type bechKeyOutFn func(keyInfo keys.Info) (keys.KeyOutput, error)
|
2018-08-30 21:06:44 -07:00
|
|
|
|
2019-12-12 13:52:24 -08:00
|
|
|
// NewKeyBaseFromDir initializes a keybase at the rootDir directory. Keybase
|
|
|
|
// options can be applied when generating this new Keybase.
|
|
|
|
func NewKeyBaseFromDir(rootDir string, opts ...keys.KeybaseOption) (keys.Keybase, error) {
|
|
|
|
return getLazyKeyBaseFromDir(rootDir, opts...)
|
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-12-12 13:52:24 -08:00
|
|
|
func getLazyKeyBaseFromDir(rootDir string, opts ...keys.KeybaseOption) (keys.Keybase, error) {
|
|
|
|
return keys.New(defaultKeyDBName, filepath.Join(rootDir, "keys"), opts...), nil
|
2018-03-05 08:41:50 -08: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:
|
2019-05-17 07:15:45 -07:00
|
|
|
printTextInfos([]keys.KeyOutput{ko})
|
2019-03-01 13:29:33 -08:00
|
|
|
|
2019-03-09 13:00:08 -08:00
|
|
|
case OutputFormatJSON:
|
2019-03-13 10:36:52 -07:00
|
|
|
var out []byte
|
|
|
|
var err error
|
2019-05-28 01:44:04 -07:00
|
|
|
if viper.GetBool(flags.FlagIndentResponse) {
|
2019-12-12 13:52:24 -08:00
|
|
|
out, err = KeysCdc.MarshalJSONIndent(ko, "", " ")
|
2019-03-13 10:36:52 -07:00
|
|
|
} else {
|
2019-12-12 13:52:24 -08:00
|
|
|
out, err = KeysCdc.MarshalJSON(ko)
|
2019-03-13 10:36:52 -07:00
|
|
|
}
|
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) {
|
2019-03-01 13:29:33 -08:00
|
|
|
kos, err := keys.Bech32KeysOutput(infos)
|
2018-06-05 21:53:04 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-03-01 13:29:33 -08:00
|
|
|
|
2018-02-22 07:17:19 -08:00
|
|
|
switch viper.Get(cli.OutputFlag) {
|
2019-02-08 12:45:23 -08:00
|
|
|
case OutputFormatText:
|
2019-05-17 07:15:45 -07:00
|
|
|
printTextInfos(kos)
|
2019-03-01 13:29:33 -08:00
|
|
|
|
2019-02-08 12:45:23 -08:00
|
|
|
case OutputFormatJSON:
|
2019-03-13 10:36:52 -07:00
|
|
|
var out []byte
|
|
|
|
var err error
|
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
if viper.GetBool(flags.FlagIndentResponse) {
|
2019-12-12 13:52:24 -08:00
|
|
|
out, err = KeysCdc.MarshalJSONIndent(kos, "", " ")
|
2019-03-13 10:36:52 -07:00
|
|
|
} else {
|
2019-12-12 13:52:24 -08:00
|
|
|
out, err = KeysCdc.MarshalJSON(kos)
|
2019-03-13 10:36:52 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2019-05-31 06:14:35 -07:00
|
|
|
fmt.Printf("%s", out)
|
2018-02-22 07:17:19 -08:00
|
|
|
}
|
|
|
|
}
|
2018-05-28 16:27:34 -07:00
|
|
|
|
2019-05-17 07:15:45 -07:00
|
|
|
func printTextInfos(kos []keys.KeyOutput) {
|
2019-05-31 06:14:35 -07:00
|
|
|
out, err := yaml.Marshal(&kos)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2019-03-01 13:29:33 -08:00
|
|
|
}
|
2019-05-31 06:14:35 -07:00
|
|
|
fmt.Println(string(out))
|
2019-03-01 13:29:33 -08: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)
|
|
|
|
}
|
2019-11-14 06:17:21 -08:00
|
|
|
|
|
|
|
func isRunningUnattended() bool {
|
|
|
|
backends := keyring.AvailableBackends()
|
|
|
|
return len(backends) == 2 && backends[1] == keyring.BackendType("file")
|
|
|
|
}
|