2020-08-04 18:52:32 +00:00
|
|
|
package client
|
2019-11-13 17:00:21 +00:00
|
|
|
|
|
|
|
import (
|
2019-12-13 19:50:19 +00:00
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
|
2020-08-04 18:52:32 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2019-11-13 17:00:21 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
|
|
clientkeys "github.com/cosmos/cosmos-sdk/client/keys"
|
2020-08-23 21:41:54 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys"
|
2020-04-22 19:26:01 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-11-13 17:00:21 +00:00
|
|
|
|
2020-10-06 18:57:55 +00:00
|
|
|
"github.com/cosmos/ethermint/crypto/hd"
|
2019-11-13 17:00:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
flagDryRun = "dry-run"
|
|
|
|
)
|
|
|
|
|
2020-08-04 18:52:32 +00:00
|
|
|
// KeyCommands registers a sub-tree of commands to interact with
|
2019-11-13 17:00:21 +00:00
|
|
|
// local private key storage.
|
2020-08-04 18:52:32 +00:00
|
|
|
func KeyCommands() *cobra.Command {
|
2019-11-13 17:00:21 +00:00
|
|
|
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.`,
|
|
|
|
}
|
2020-08-04 18:52:32 +00:00
|
|
|
|
|
|
|
// support adding Ethereum supported keys
|
2019-11-13 17:00:21 +00:00
|
|
|
addCmd := clientkeys.AddKeyCommand()
|
2020-08-12 14:25:57 +00:00
|
|
|
|
|
|
|
// update the default signing algorithm value to "eth_secp256k1"
|
|
|
|
algoFlag := addCmd.Flag("algo")
|
2020-10-06 18:57:55 +00:00
|
|
|
algoFlag.DefValue = string(hd.EthSecp256k1)
|
|
|
|
err := algoFlag.Value.Set(string(hd.EthSecp256k1))
|
2020-08-25 09:06:47 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-11-13 17:00:21 +00:00
|
|
|
addCmd.RunE = runAddCmd
|
2020-08-04 18:52:32 +00:00
|
|
|
|
2019-11-13 17:00:21 +00:00
|
|
|
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-08-04 18:52:32 +00:00
|
|
|
UnsafeExportEthKeyCommand(),
|
2019-11-13 17:00:21 +00:00
|
|
|
)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2020-08-04 18:52:32 +00:00
|
|
|
func runAddCmd(cmd *cobra.Command, args []string) error {
|
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
|
|
|
kb, err := getKeybase(viper.GetBool(flagDryRun), inBuf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return clientkeys.RunAddCmd(cmd, args, kb, inBuf)
|
|
|
|
}
|
|
|
|
|
2020-08-23 21:41:54 +00:00
|
|
|
func getKeybase(transient bool, buf io.Reader) (keys.Keybase, error) {
|
2020-04-22 19:26:01 +00:00
|
|
|
if transient {
|
2020-08-23 21:41:54 +00:00
|
|
|
return keys.NewInMemory(
|
2020-10-06 18:57:55 +00:00
|
|
|
hd.EthSecp256k1Options()...,
|
2020-08-11 15:01:15 +00:00
|
|
|
), nil
|
2019-11-13 17:00:21 +00:00
|
|
|
}
|
|
|
|
|
2020-08-23 21:41:54 +00:00
|
|
|
return keys.NewKeyring(
|
2020-04-22 19:26:01 +00:00
|
|
|
sdk.KeyringServiceName(),
|
|
|
|
viper.GetString(flags.FlagKeyringBackend),
|
|
|
|
viper.GetString(flags.FlagHome),
|
|
|
|
buf,
|
2020-10-06 18:57:55 +00:00
|
|
|
hd.EthSecp256k1Options()...,
|
2020-08-11 15:01:15 +00:00
|
|
|
)
|
2019-11-13 17:00:21 +00:00
|
|
|
}
|