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