laconicd/client/export.go

95 lines
2.6 KiB
Go
Raw Normal View History

package client
import (
"bufio"
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/ethereum/go-ethereum/common/hexutil"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
2021-04-17 10:00:07 +00:00
"github.com/cosmos/cosmos-sdk/crypto"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
2021-04-17 10:00:07 +00:00
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
"github.com/cosmos/ethermint/crypto/hd"
)
// UnsafeExportEthKeyCommand exports a key with the given name as a private key in hex format.
func UnsafeExportEthKeyCommand() *cobra.Command {
return &cobra.Command{
Use: "unsafe-export-eth-key [name]",
Short: "**UNSAFE** Export an Ethereum private key",
Long: `**UNSAFE** Export an Ethereum private key unencrypted to use in dev tooling`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
2021-04-17 10:00:07 +00:00
keyringBackend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)
rootDir, _ := cmd.Flags().GetString(flags.FlagHome)
kr, err := keyring.New(
sdk.KeyringServiceName(),
2021-04-17 10:00:07 +00:00
keyringBackend,
rootDir,
inBuf,
2021-04-17 10:00:07 +00:00
hd.EthSecp256k1Option(),
)
if err != nil {
return err
}
decryptPassword := ""
conf := true
2021-04-17 10:00:07 +00:00
switch keyringBackend {
2021-04-17 10:00:07 +00:00
case keyring.BackendFile:
decryptPassword, err = input.GetPassword(
"**WARNING this is an unsafe way to export your unencrypted private key**\nEnter key password:",
inBuf)
2021-04-17 10:00:07 +00:00
case keyring.BackendOS:
conf, err = input.GetConfirmation(
"**WARNING** this is an unsafe way to export your unencrypted private key, are you sure?",
2021-04-17 10:00:07 +00:00
inBuf, cmd.ErrOrStderr())
}
if err != nil || !conf {
return err
}
// Exports private key from keybase using password
2021-04-17 10:00:07 +00:00
armor, err := kr.ExportPrivKeyArmor(args[0], decryptPassword)
if err != nil {
return err
}
privKey, algo, err := crypto.UnarmorDecryptPrivKey(armor, decryptPassword)
if err != nil {
return err
}
2021-04-17 10:00:07 +00:00
if algo != ethsecp256k1.KeyType {
return fmt.Errorf("invalid key algorithm, got %s, expected %s", algo, ethsecp256k1.KeyType)
}
// Converts key to Ethermint secp256 implementation
2021-04-17 10:00:07 +00:00
ethermintPrivKey, ok := privKey.(*ethsecp256k1.PrivKey)
if !ok {
2021-04-17 10:00:07 +00:00
return fmt.Errorf("invalid private key type %T, expected %T", privKey, &ethsecp256k1.PrivKey{})
}
// Formats key for output
2021-04-17 10:00:07 +00:00
privB := ethcrypto.FromECDSA(ethermintPrivKey.ToECDSA())
keyS := strings.ToUpper(hexutil.Encode(privB)[2:])
fmt.Println(keyS)
return nil
},
}
}