2019-05-30 08:44:28 -07:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
2019-06-22 02:24:59 -07:00
|
|
|
"bufio"
|
2019-05-30 08:44:28 -07:00
|
|
|
"io/ioutil"
|
|
|
|
|
2019-06-08 03:04:52 -07:00
|
|
|
"github.com/spf13/cobra"
|
2020-01-22 09:54:56 -08:00
|
|
|
"github.com/spf13/viper"
|
2019-07-22 08:26:42 -07:00
|
|
|
|
2020-01-22 09:54:56 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2019-07-22 08:26:42 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/input"
|
2020-01-22 09:54:56 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-05-30 08:44:28 -07:00
|
|
|
)
|
|
|
|
|
2019-12-12 13:52:24 -08:00
|
|
|
// ImportKeyCommand imports private keys from a keyfile.
|
|
|
|
func ImportKeyCommand() *cobra.Command {
|
2019-12-11 01:45:26 -08:00
|
|
|
return &cobra.Command{
|
2019-05-30 08:44:28 -07:00
|
|
|
Use: "import <name> <keyfile>",
|
|
|
|
Short: "Import private keys into the local keybase",
|
|
|
|
Long: "Import a ASCII armored private key into the local keybase.",
|
|
|
|
Args: cobra.ExactArgs(2),
|
|
|
|
RunE: runImportCmd,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 02:24:59 -07:00
|
|
|
func runImportCmd(cmd *cobra.Command, args []string) error {
|
2019-11-14 06:17:21 -08:00
|
|
|
buf := bufio.NewReader(cmd.InOrStdin())
|
2020-01-23 10:47:11 -08:00
|
|
|
kb, err := keys.NewKeyring(sdk.KeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), buf)
|
2019-05-30 08:44:28 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := ioutil.ReadFile(args[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
passphrase, err := input.GetPassword("Enter passphrase to decrypt your key:", buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return kb.ImportPrivKey(args[0], string(bz), passphrase)
|
|
|
|
}
|