2019-05-30 08:44:28 -07:00
package keys
import (
2019-06-22 02:24:59 -07:00
"bufio"
2020-11-30 12:55:39 -08:00
"fmt"
2019-05-30 08:44:28 -07:00
"github.com/spf13/cobra"
2020-10-08 10:41:35 -07:00
"github.com/cosmos/cosmos-sdk/client"
2019-05-30 08:44:28 -07:00
"github.com/cosmos/cosmos-sdk/client/input"
2020-11-30 12:55:39 -08:00
"github.com/cosmos/cosmos-sdk/crypto/keyring"
)
const (
flagUnarmoredHex = "unarmored-hex"
flagUnsafe = "unsafe"
2019-05-30 08:44:28 -07:00
)
2019-12-12 13:52:24 -08:00
// ExportKeyCommand exports private keys from the key store.
func ExportKeyCommand ( ) * cobra . Command {
2020-11-30 12:55:39 -08:00
cmd := & cobra . Command {
2019-05-30 08:44:28 -07:00
Use : "export <name>" ,
Short : "Export private keys" ,
2020-11-30 12:55:39 -08:00
Long : ` Export a private key from the local keyring in ASCII - armored encrypted format .
When both the -- unarmored - hex and -- unsafe flags are selected , cryptographic
private key material is exported in an INSECURE fashion that is designed to
allow users to import their keys in hot wallets . This feature is for advanced
users only that are confident about how to handle private keys work and are
FULLY AWARE OF THE RISKS . If you are unsure , you may want to do some research
and export your keys in ASCII - armored encrypted format . ` ,
Args : cobra . ExactArgs ( 1 ) ,
2020-07-06 12:50:09 -07:00
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2020-12-14 14:09:51 -08:00
clientCtx , err := client . GetClientQueryContext ( cmd )
if err != nil {
return err
}
2021-08-09 03:35:01 -07:00
buf := bufio . NewReader ( clientCtx . Input )
2020-11-30 12:55:39 -08:00
unarmored , _ := cmd . Flags ( ) . GetBool ( flagUnarmoredHex )
unsafe , _ := cmd . Flags ( ) . GetBool ( flagUnsafe )
if unarmored && unsafe {
return exportUnsafeUnarmored ( cmd , args [ 0 ] , buf , clientCtx . Keyring )
} else if unarmored || unsafe {
return fmt . Errorf ( "the flags %s and %s must be used together" , flagUnsafe , flagUnarmoredHex )
}
2020-07-06 12:50:09 -07:00
encryptPassword , err := input . GetPassword ( "Enter passphrase to encrypt the exported key:" , buf )
if err != nil {
return err
}
2020-10-08 10:41:35 -07:00
armored , err := clientCtx . Keyring . ExportPrivKeyArmor ( args [ 0 ] , encryptPassword )
2020-07-06 12:50:09 -07:00
if err != nil {
return err
}
cmd . Println ( armored )
2020-11-30 12:55:39 -08:00
2020-07-06 12:50:09 -07:00
return nil
} ,
2019-05-30 08:44:28 -07:00
}
2020-11-30 12:55:39 -08:00
cmd . Flags ( ) . Bool ( flagUnarmoredHex , false , "Export unarmored hex privkey. Requires --unsafe." )
cmd . Flags ( ) . Bool ( flagUnsafe , false , "Enable unsafe operations. This flag must be switched on along with all unsafe operation-specific options." )
return cmd
}
func exportUnsafeUnarmored ( cmd * cobra . Command , uid string , buf * bufio . Reader , kr keyring . Keyring ) error {
// confirm deletion, unless -y is passed
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 {
return err
} else if ! yes {
return nil
}
hexPrivKey , err := keyring . NewUnsafe ( kr ) . UnsafeExportPrivKeyHex ( uid )
if err != nil {
return err
}
cmd . Println ( hexPrivKey )
return nil
2019-05-30 08:44:28 -07:00
}