Skip to content

Commit

Permalink
backport: add support for importing raw agent keys (#69)
Browse files Browse the repository at this point in the history
Original PR: #66
  • Loading branch information
ejfitzgerald authored Feb 11, 2021
1 parent 8ee94b8 commit 2ef228a
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion client/keys/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package keys

import (
"bufio"
"encoding/hex"
"errors"
"io/ioutil"

"github.com/spf13/cobra"
Expand All @@ -11,17 +13,26 @@ import (
"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/cosmos/cosmos-sdk/crypto/keys/mintkey"
)

const (
flagAgentRawKey = "agent-raw-key"
)

// ImportKeyCommand imports private keys from a keyfile.
func ImportKeyCommand() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "import <name> <keyfile>",
Short: "Import private keys into the local keybase",
Long: "Import a ASCII armored private key into the local keybase.",
Args: cobra.ExactArgs(2),
RunE: runImportCmd,
}
cmd.Flags().Bool(flagAgentRawKey, false, "Signal that you want to import an key from the agent framework")

return cmd
}

func runImportCmd(cmd *cobra.Command, args []string) error {
Expand All @@ -36,6 +47,30 @@ func runImportCmd(cmd *cobra.Command, args []string) error {
return err
}

// if the user has requested that a raw agent key is imported then we must read the file as such
if viper.GetBool(flagAgentRawKey) {
// hex decode the input string
rawPrivKey, err := hex.DecodeString(string(bz))
if err != nil {
return err
}

// check the size of the binary data
if len(rawPrivKey) != 32 {
return errors.New("Incorrect raw key size. Please check input path")
}

// create the underlying private key
var priv secp256k1.PrivKeySecp256k1
copy(priv[:], rawPrivKey)

// armor and encrypt the key with a dummy password (to reuse existing private key import path)
passPhrase := "dummy-not-really-used"
armored := mintkey.EncryptArmorPrivKey(priv, passPhrase, "secp256k1")

return kb.ImportPrivKey(args[0], armored, passPhrase)
}

passphrase, err := input.GetPassword("Enter passphrase to decrypt your key:", buf)
if err != nil {
return err
Expand Down

0 comments on commit 2ef228a

Please sign in to comment.