2022-12-16 09:48:38 +00:00
|
|
|
// Copyright 2021 Evmos Foundation
|
|
|
|
// This file is part of Evmos' Ethermint library.
|
|
|
|
//
|
|
|
|
// The Ethermint library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The Ethermint library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
|
2020-08-04 18:52:32 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2022-04-11 08:09:39 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2020-08-04 18:52:32 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/input"
|
2021-04-17 10:00:07 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto"
|
2021-04-18 15:54:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
ethcrypto "github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/spf13/cobra"
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2022-09-07 06:36:11 +00:00
|
|
|
"github.com/cerc-io/laconicd/crypto/ethsecp256k1"
|
|
|
|
"github.com/cerc-io/laconicd/crypto/hd"
|
2021-04-18 15:54:18 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2020-08-04 18:52:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2022-10-10 10:38:33 +00:00
|
|
|
clientCtx := client.GetClientContextFromCmd(cmd).WithKeyringOptions(hd.EthSecp256k1Option())
|
|
|
|
clientCtx, err := client.ReadPersistentCommandFlags(clientCtx, cmd.Flags())
|
2020-08-04 18:52:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
decryptPassword := ""
|
|
|
|
conf := true
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
|
|
|
switch clientCtx.Keyring.Backend() {
|
2021-04-17 10:00:07 +00:00
|
|
|
case keyring.BackendFile:
|
2020-08-04 18:52:32 +00:00
|
|
|
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:
|
2020-08-04 18:52:32 +00:00
|
|
|
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())
|
2020-08-04 18:52:32 +00:00
|
|
|
}
|
|
|
|
if err != nil || !conf {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exports private key from keybase using password
|
2022-10-10 10:38:33 +00:00
|
|
|
armor, err := clientCtx.Keyring.ExportPrivKeyArmor(args[0], decryptPassword)
|
2021-04-17 10:00:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
privKey, algo, err := crypto.UnarmorDecryptPrivKey(armor, decryptPassword)
|
2020-08-04 18:52:32 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-09-07 17:29:24 +00:00
|
|
|
// Converts key to Ethermint secp256k1 implementation
|
2021-04-18 15:54:18 +00:00
|
|
|
ethPrivKey, ok := privKey.(*ethsecp256k1.PrivKey)
|
2020-08-04 18:52:32 +00:00
|
|
|
if !ok {
|
2021-04-17 10:00:07 +00:00
|
|
|
return fmt.Errorf("invalid private key type %T, expected %T", privKey, ðsecp256k1.PrivKey{})
|
2020-08-04 18:52:32 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 17:29:24 +00:00
|
|
|
key, err := ethPrivKey.ToECDSA()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-04 18:52:32 +00:00
|
|
|
// Formats key for output
|
2021-09-07 17:29:24 +00:00
|
|
|
privB := ethcrypto.FromECDSA(key)
|
2020-08-04 18:52:32 +00:00
|
|
|
keyS := strings.ToUpper(hexutil.Encode(privB)[2:])
|
|
|
|
|
|
|
|
fmt.Println(keyS)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|