github.com/Finschia/finschia-sdk@v0.48.1/client/keys/export.go (about)

     1  package keys
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  
     7  	"github.com/spf13/cobra"
     8  
     9  	"github.com/Finschia/finschia-sdk/client"
    10  	"github.com/Finschia/finschia-sdk/client/input"
    11  	"github.com/Finschia/finschia-sdk/crypto/keyring"
    12  )
    13  
    14  const (
    15  	flagUnarmoredHex = "unarmored-hex"
    16  	flagUnsafe       = "unsafe"
    17  )
    18  
    19  // ExportKeyCommand exports private keys from the key store.
    20  func ExportKeyCommand() *cobra.Command {
    21  	cmd := &cobra.Command{
    22  		Use:   "export <name>",
    23  		Short: "Export private keys",
    24  		Long: `Export a private key from the local keyring in ASCII-armored encrypted format.
    25  
    26  When both the --unarmored-hex and --unsafe flags are selected, cryptographic
    27  private key material is exported in an INSECURE fashion that is designed to
    28  allow users to import their keys in hot wallets. This feature is for advanced
    29  users only that are confident about how to handle private keys work and are
    30  FULLY AWARE OF THE RISKS. If you are unsure, you may want to do some research
    31  and export your keys in ASCII-armored encrypted format.`,
    32  		Args: cobra.ExactArgs(1),
    33  		RunE: func(cmd *cobra.Command, args []string) error {
    34  			clientCtx, err := client.GetClientQueryContext(cmd)
    35  			if err != nil {
    36  				return err
    37  			}
    38  			buf := bufio.NewReader(clientCtx.Input)
    39  			unarmored, _ := cmd.Flags().GetBool(flagUnarmoredHex)
    40  			unsafe, _ := cmd.Flags().GetBool(flagUnsafe)
    41  
    42  			if unarmored && unsafe {
    43  				return exportUnsafeUnarmored(cmd, args[0], buf, clientCtx.Keyring)
    44  			} else if unarmored || unsafe {
    45  				return fmt.Errorf("the flags %s and %s must be used together", flagUnsafe, flagUnarmoredHex)
    46  			}
    47  
    48  			encryptPassword, err := input.GetPassword("Enter passphrase to encrypt the exported key:", buf)
    49  			if err != nil {
    50  				return err
    51  			}
    52  
    53  			armored, err := clientCtx.Keyring.ExportPrivKeyArmor(args[0], encryptPassword)
    54  			if err != nil {
    55  				return err
    56  			}
    57  
    58  			cmd.Println(armored)
    59  
    60  			return nil
    61  		},
    62  	}
    63  
    64  	cmd.Flags().Bool(flagUnarmoredHex, false, "Export unarmored hex privkey. Requires --unsafe.")
    65  	cmd.Flags().Bool(flagUnsafe, false, "Enable unsafe operations. This flag must be switched on along with all unsafe operation-specific options.")
    66  
    67  	return cmd
    68  }
    69  
    70  func exportUnsafeUnarmored(cmd *cobra.Command, uid string, buf *bufio.Reader, kr keyring.Keyring) error {
    71  	// confirm deletion, unless -y is passed
    72  	if yes, err := input.GetConfirmation("WARNING: The private key will be exported as an unarmored hexadecimal string. USE AT YOUR OWN RISK. Continue?", buf, cmd.ErrOrStderr()); err != nil {
    73  		return err
    74  	} else if !yes {
    75  		return nil
    76  	}
    77  
    78  	hexPrivKey, err := keyring.NewUnsafe(kr).UnsafeExportPrivKeyHex(uid)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	cmd.Println(hexPrivKey)
    84  
    85  	return nil
    86  }