Skip to content

Commit

Permalink
Support for importing unarmored key (secp256k1 algo only at the moment)
Browse files Browse the repository at this point in the history
  • Loading branch information
pbukva committed Oct 30, 2024
1 parent 09cf7ba commit 69e342d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
5 changes: 3 additions & 2 deletions client/keys/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
)

const (
flagUnarmoredHex = "unarmored-hex"
flagUnsafe = "unsafe"
flagUnarmoredHex = "unarmored-hex"
flagUnarmoredKeyAlgo = "unarmored-key-algo"
flagUnsafe = "unsafe"
)

// ExportKeyCommand exports private keys from the key store.
Expand Down
16 changes: 15 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"
"fmt"

Check failure on line 5 in client/keys/import.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)
"github.com/cosmos/cosmos-sdk/crypto/hd"
"os"

Check failure on line 7 in client/keys/import.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)

"github.com/spf13/cobra"
Expand All @@ -12,7 +14,7 @@ import (

// 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.",
Expand All @@ -29,6 +31,13 @@ func ImportKeyCommand() *cobra.Command {
return err
}

unarmored, _ := cmd.Flags().GetBool(flagUnarmoredHex)
algo, _ := cmd.Flags().GetString(flagUnarmoredKeyAlgo)

if unarmored {
return clientCtx.Keyring.ImportUnarmoredPrivKey(args[0], string(bz), algo)
}

passphrase, err := input.GetPassword("Enter passphrase to decrypt your key:", buf)
if err != nil {
return err
Expand All @@ -37,4 +46,9 @@ func ImportKeyCommand() *cobra.Command {
return clientCtx.Keyring.ImportPrivKey(args[0], string(bz), passphrase)
},
}

cmd.Flags().Bool(flagUnarmoredHex, false, "Import unarmored hex privkey")
cmd.Flags().String(flagUnarmoredKeyAlgo, string(hd.Secp256k1Type), fmt.Sprintf("defines cryptographic scheme algorithm of the private key (%s, %s, %s, %s)", hd.Secp256k1Type, hd.Ed25519Type, hd.Sr25519Type, hd.MultiType))

return cmd
}
41 changes: 41 additions & 0 deletions crypto/keyring/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"encoding/hex"
"fmt"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"

Check failure on line 7 in crypto/keyring/keyring.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -111,6 +112,9 @@ type Importer interface {
// ImportPrivKey imports ASCII armored passphrase-encrypted private keys.
ImportPrivKey(uid, armor, passphrase string) error

// ImportUnarmoredPrivKey imports HEX encoded UNARMORED private keys.
ImportUnarmoredPrivKey(uid, unarmoredPrivKeyHex, algo string) error

// ImportPubKey imports ASCII armored public keys.
ImportPubKey(uid string, armor string) error
}
Expand Down Expand Up @@ -304,6 +308,43 @@ func (ks keystore) ImportPrivKey(uid, armor, passphrase string) error {
return nil
}

func (ks keystore) ImportUnarmoredPrivKey(uid, unarmoredPrivKeyHex, algo string) error {
if _, err := ks.Key(uid); err == nil {
return fmt.Errorf("cannot overwrite key: %s", uid)
}

privKeyRaw, err := hex.DecodeString(unarmoredPrivKeyHex)
if err != nil {
return errors.Wrap(err, "failed to decode provided hex value of private key")
}

var privKey types.PrivKey
switch hd.PubKeyType(algo) {
case hd.Secp256k1Type:
privKey = &secp256k1.PrivKey{Key: privKeyRaw}
case hd.Ed25519Type:
fallthrough
case hd.Sr25519Type:
fallthrough
case hd.MultiType:
fallthrough
default:
return fmt.Errorf("only the \"%s\" algo is supported at the moment", hd.Secp256k1Type)
}

//privKey, err := legacy.PrivKeyFromBytes(privKeyRaw)

Check failure on line 335 in crypto/keyring/keyring.go

View workflow job for this annotation

GitHub Actions / golangci-lint

commentFormatting: put a space between `//` and comment text (gocritic)
//if err != nil {
// return errors.Wrap(err, "failed to create private key from provided hex value")
//}

_, err = ks.writeLocalKey(uid, privKey, hd.PubKeyType(algo))
if err != nil {
return err
}

return nil
}

func (ks keystore) ImportPubKey(uid string, armor string) error {
if _, err := ks.Key(uid); err == nil {
return fmt.Errorf("cannot overwrite key: %s", uid)
Expand Down

0 comments on commit 69e342d

Please sign in to comment.