2018-02-22 07:17:19 -08:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-03-30 07:54:01 -07:00
|
|
|
"io"
|
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"
|
2020-03-30 07:54:01 -07: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"
|
2020-03-25 08:20:36 -07:00
|
|
|
cryptokeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
|
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
|
|
|
|
2020-03-25 08:20:36 -07:00
|
|
|
type bechKeyOutFn func(keyInfo cryptokeyring.Info) (cryptokeyring.KeyOutput, error)
|
2018-08-30 21:06:44 -07:00
|
|
|
|
2020-03-30 11:30:50 -07:00
|
|
|
// NewLegacyKeyBaseFromDir initializes a legacy keybase at the rootDir directory. Keybase
|
2019-12-12 13:52:24 -08:00
|
|
|
// options can be applied when generating this new Keybase.
|
2020-03-30 11:30:50 -07:00
|
|
|
func NewLegacyKeyBaseFromDir(rootDir string, opts ...cryptokeyring.KeybaseOption) (cryptokeyring.LegacyKeybase, error) {
|
|
|
|
return getLegacyKeyBaseFromDir(rootDir, opts...)
|
2018-02-22 07:17:19 -08:00
|
|
|
}
|
|
|
|
|
2020-03-30 11:30:50 -07:00
|
|
|
func getLegacyKeyBaseFromDir(rootDir string, opts ...cryptokeyring.KeybaseOption) (cryptokeyring.LegacyKeybase, error) {
|
|
|
|
return cryptokeyring.NewLegacy(defaultKeyDBName, filepath.Join(rootDir, "keys"), opts...)
|
2018-03-05 08:41:50 -08:00
|
|
|
}
|
|
|
|
|
2020-03-30 07:54:01 -07:00
|
|
|
func printKeyInfo(w io.Writer, keyInfo cryptokeyring.Info, bechKeyOut bechKeyOutFn) {
|
2018-08-30 21:06:44 -07:00
|
|
|
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:
|
2020-03-30 07:54:01 -07:00
|
|
|
printTextInfos(w, []cryptokeyring.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
|
|
|
|
2020-03-30 07:54:01 -07:00
|
|
|
fmt.Fprintln(w, string(out))
|
2018-02-22 07:17:19 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 07:54:01 -07:00
|
|
|
func printInfos(w io.Writer, infos []cryptokeyring.Info) {
|
2020-03-25 08:20:36 -07:00
|
|
|
kos, err := cryptokeyring.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:
|
2020-03-30 07:54:01 -07:00
|
|
|
printTextInfos(w, 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
|
|
|
}
|
2020-03-30 07:54:01 -07:00
|
|
|
fmt.Fprintf(w, "%s", out)
|
2018-02-22 07:17:19 -08:00
|
|
|
}
|
|
|
|
}
|
2018-05-28 16:27:34 -07:00
|
|
|
|
2020-03-30 07:54:01 -07:00
|
|
|
func printTextInfos(w io.Writer, kos []cryptokeyring.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
|
|
|
}
|
2020-03-30 07:54:01 -07:00
|
|
|
fmt.Fprintln(w, string(out))
|
2019-03-01 13:29:33 -08:00
|
|
|
}
|
|
|
|
|
2020-03-30 07:54:01 -07:00
|
|
|
func printKeyAddress(w io.Writer, info cryptokeyring.Info, bechKeyOut bechKeyOutFn) {
|
2018-08-30 21:06:44 -07:00
|
|
|
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
|
|
|
|
2020-03-30 07:54:01 -07:00
|
|
|
fmt.Fprintln(w, ko.Address)
|
2018-08-10 23:42:57 -07:00
|
|
|
}
|
|
|
|
|
2020-03-30 07:54:01 -07:00
|
|
|
func printPubKey(w io.Writer, info cryptokeyring.Info, bechKeyOut bechKeyOutFn) {
|
2018-08-30 21:06:44 -07:00
|
|
|
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
|
|
|
|
2020-03-30 07:54:01 -07:00
|
|
|
fmt.Fprintln(w, ko.PubKey)
|
2018-08-10 23:42:57 -07:00
|
|
|
}
|