2021-04-18 15:54:18 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2022-04-11 08:09:39 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2021-04-18 15:54:18 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/input"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-06-22 10:49:18 +00:00
|
|
|
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
|
2021-04-18 15:54:18 +00:00
|
|
|
|
2021-06-22 10:49:18 +00:00
|
|
|
"github.com/tharsis/ethermint/crypto/hd"
|
2021-04-18 15:54:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// UnsafeImportKeyCommand imports private keys from a keyfile.
|
|
|
|
func UnsafeImportKeyCommand() *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "unsafe-import-eth-key <name> <pk>",
|
|
|
|
Short: "**UNSAFE** Import Ethereum private keys into the local keybase",
|
|
|
|
Long: "**UNSAFE** Import a hex-encoded Ethereum private key into the local keybase.",
|
|
|
|
Args: cobra.ExactArgs(2),
|
|
|
|
RunE: runImportCmd,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runImportCmd(cmd *cobra.Command, args []string) error {
|
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
|
|
|
keyringBackend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)
|
|
|
|
rootDir, _ := cmd.Flags().GetString(flags.FlagHome)
|
2022-04-11 08:09:39 +00:00
|
|
|
clientCtx := client.GetClientContextFromCmd(cmd)
|
2021-04-18 15:54:18 +00:00
|
|
|
|
|
|
|
kb, err := keyring.New(
|
|
|
|
sdk.KeyringServiceName(),
|
|
|
|
keyringBackend,
|
|
|
|
rootDir,
|
|
|
|
inBuf,
|
2022-04-11 08:09:39 +00:00
|
|
|
clientCtx.Codec,
|
2021-04-18 15:54:18 +00:00
|
|
|
hd.EthSecp256k1Option(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
passphrase, err := input.GetPassword("Enter passphrase to encrypt your key:", inBuf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
privKey := ðsecp256k1.PrivKey{
|
|
|
|
Key: common.FromHex(args[1]),
|
|
|
|
}
|
|
|
|
|
|
|
|
armor := crypto.EncryptArmorPrivKey(privKey, passphrase, "eth_secp256k1")
|
|
|
|
|
|
|
|
return kb.ImportPrivKey(args[0], armor, passphrase)
|
|
|
|
}
|