2019-11-13 17:00:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-12-13 19:50:19 +00:00
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
|
2020-06-08 18:33:48 +00:00
|
|
|
"github.com/tendermint/tendermint/crypto"
|
|
|
|
|
2019-11-13 17:00:21 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
|
|
clientkeys "github.com/cosmos/cosmos-sdk/client/keys"
|
2020-04-22 19:26:01 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-11-13 17:00:21 +00:00
|
|
|
|
2020-06-08 18:33:48 +00:00
|
|
|
emintCrypto "github.com/cosmos/ethermint/crypto"
|
2019-11-13 17:00:21 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
flagDryRun = "dry-run"
|
|
|
|
)
|
|
|
|
|
|
|
|
// keyCommands registers a sub-tree of commands to interact with
|
|
|
|
// local private key storage.
|
|
|
|
func keyCommands() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "keys",
|
|
|
|
Short: "Add or view local private keys",
|
|
|
|
Long: `Keys allows you to manage your local keystore for tendermint.
|
|
|
|
|
|
|
|
These keys may be in any format supported by go-crypto and can be
|
|
|
|
used by light-clients, full nodes, or any other application that
|
|
|
|
needs to sign with a private key.`,
|
|
|
|
}
|
|
|
|
addCmd := clientkeys.AddKeyCommand()
|
|
|
|
addCmd.RunE = runAddCmd
|
|
|
|
cmd.AddCommand(
|
|
|
|
clientkeys.MnemonicKeyCommand(),
|
|
|
|
addCmd,
|
|
|
|
clientkeys.ExportKeyCommand(),
|
|
|
|
clientkeys.ImportKeyCommand(),
|
|
|
|
clientkeys.ListKeysCmd(),
|
|
|
|
clientkeys.ShowKeysCmd(),
|
|
|
|
flags.LineBreak,
|
|
|
|
clientkeys.DeleteKeyCommand(),
|
|
|
|
clientkeys.ParseKeyStringCommand(),
|
|
|
|
clientkeys.MigrateCommand(),
|
2019-11-16 17:59:19 +00:00
|
|
|
flags.LineBreak,
|
2020-04-01 18:49:21 +00:00
|
|
|
unsafeExportEthKeyCommand(),
|
2019-11-13 17:00:21 +00:00
|
|
|
)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2020-06-08 18:33:48 +00:00
|
|
|
func getKeybase(transient bool, buf io.Reader) (keyring.Keybase, error) {
|
2020-04-22 19:26:01 +00:00
|
|
|
if transient {
|
2020-06-08 18:33:48 +00:00
|
|
|
return keyring.NewInMemory(keyring.WithKeygenFunc(ethermintKeygenFunc)), nil
|
2019-11-13 17:00:21 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 18:33:48 +00:00
|
|
|
return keyring.NewKeyring(
|
2020-04-22 19:26:01 +00:00
|
|
|
sdk.KeyringServiceName(),
|
|
|
|
viper.GetString(flags.FlagKeyringBackend),
|
|
|
|
viper.GetString(flags.FlagHome),
|
|
|
|
buf,
|
2020-06-08 18:33:48 +00:00
|
|
|
keyring.WithKeygenFunc(ethermintKeygenFunc))
|
2019-11-13 17:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func runAddCmd(cmd *cobra.Command, args []string) error {
|
2019-12-13 19:50:19 +00:00
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
2020-06-08 18:33:48 +00:00
|
|
|
kb, err := getKeybase(viper.GetBool(flagDryRun), inBuf)
|
2019-11-13 17:00:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-08 18:33:48 +00:00
|
|
|
return clientkeys.RunAddCmd(cmd, args, kb, inBuf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ethermintKeygenFunc(bz []byte, algo keyring.SigningAlgo) (crypto.PrivKey, error) {
|
|
|
|
return emintCrypto.PrivKeySecp256k1(bz), nil
|
2019-11-13 17:00:21 +00:00
|
|
|
}
|