github.com/cosmos/cosmos-sdk@v0.50.10/client/keys/import.go (about)

     1  package keys
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/spf13/cobra"
     9  
    10  	"github.com/cosmos/cosmos-sdk/client"
    11  	"github.com/cosmos/cosmos-sdk/client/flags"
    12  	"github.com/cosmos/cosmos-sdk/client/input"
    13  	"github.com/cosmos/cosmos-sdk/crypto/hd"
    14  	"github.com/cosmos/cosmos-sdk/version"
    15  )
    16  
    17  // ImportKeyCommand imports private keys from a keyfile.
    18  func ImportKeyCommand() *cobra.Command {
    19  	return &cobra.Command{
    20  		Use:   "import <name> <keyfile>",
    21  		Short: "Import private keys into the local keybase",
    22  		Long:  "Import a ASCII armored private key into the local keybase.",
    23  		Args:  cobra.ExactArgs(2),
    24  		RunE: func(cmd *cobra.Command, args []string) error {
    25  			clientCtx, err := client.GetClientQueryContext(cmd)
    26  			if err != nil {
    27  				return err
    28  			}
    29  			buf := bufio.NewReader(clientCtx.Input)
    30  
    31  			bz, err := os.ReadFile(args[1])
    32  			if err != nil {
    33  				return err
    34  			}
    35  
    36  			passphrase, err := input.GetPassword("Enter passphrase to decrypt your key:", buf)
    37  			if err != nil {
    38  				return err
    39  			}
    40  
    41  			return clientCtx.Keyring.ImportPrivKey(args[0], string(bz), passphrase)
    42  		},
    43  	}
    44  }
    45  
    46  func ImportKeyHexCommand() *cobra.Command {
    47  	cmd := &cobra.Command{
    48  		Use:   "import-hex <name> <hex>",
    49  		Short: "Import private keys into the local keybase",
    50  		Long:  fmt.Sprintf("Import hex encoded private key into the local keybase.\nSupported key-types can be obtained with:\n%s list-key-types", version.AppName),
    51  		Args:  cobra.ExactArgs(2),
    52  		RunE: func(cmd *cobra.Command, args []string) error {
    53  			clientCtx, err := client.GetClientQueryContext(cmd)
    54  			if err != nil {
    55  				return err
    56  			}
    57  			keyType, _ := cmd.Flags().GetString(flags.FlagKeyType)
    58  			return clientCtx.Keyring.ImportPrivKeyHex(args[0], args[1], keyType)
    59  		},
    60  	}
    61  	cmd.Flags().String(flags.FlagKeyType, string(hd.Secp256k1Type), "private key signing algorithm kind")
    62  	return cmd
    63  }