Skip to content

Commit

Permalink
Implement private keys export/import symmetric functionalities
Browse files Browse the repository at this point in the history
Ref #2020
  • Loading branch information
alessio committed May 29, 2019
1 parent 8b1d75c commit 9fb6d2d
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 0 deletions.
43 changes: 43 additions & 0 deletions client/keys/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package keys

import (
"fmt"

"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/crypto/keys/mintkey"
"github.com/spf13/cobra"
)

func exportKeyCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "export <name>",
Short: "Export private keys",
Long: `Export a private key from the local keybase in ASCII armor format.`,
Args: cobra.ExactArgs(1),
RunE: runExportCmd,
}
return cmd
}

func runExportCmd(_ *cobra.Command, args []string) error {
kb, err := NewKeyBaseFromHomeFlag()
if err != nil {
return err
}

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

priv, err := kb.ExportPrivateKeyObject(args[0], encryptPassword)
if err != nil {
return err
}

armored := mintkey.EncryptArmorPrivKey(priv, encryptPassword)
fmt.Println(armored)

return nil
}
39 changes: 39 additions & 0 deletions client/keys/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package keys

import (
"io/ioutil"

"github.com/cosmos/cosmos-sdk/client/input"
"github.com/spf13/cobra"
)

func importKeyCommand() *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,
}
return cmd
}

func runImportCmd(_ *cobra.Command, args []string) error {
kb, err := NewKeyBaseFromHomeFlag()
if err != nil {
return err
}

bz, err := ioutil.ReadFile(args[1])
if err != nil {
return err
}

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

return kb.ImportPrivKey(args[0], string(bz), passphrase)
}
2 changes: 2 additions & 0 deletions client/keys/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func Commands() *cobra.Command {
cmd.AddCommand(
mnemonicKeyCommand(),
addKeyCommand(),
exportKeyCommand(),
importKeyCommand(),
listKeysCmd(),
showKeysCmd(),
flags.LineBreak,
Expand Down
14 changes: 14 additions & 0 deletions crypto/keys/keybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,20 @@ func (kb dbKeybase) ExportPubKey(name string) (armor string, err error) {
return mintkey.ArmorPubKeyBytes(info.GetPubKey().Bytes()), nil
}

func (kb dbKeybase) ImportPrivKey(name string, armor string, passphrase string) error {
if _, err := kb.Get(name); err == nil {
return errors.New("Cannot overwrite key " + name)
}

privKey, err := mintkey.UnarmorDecryptPrivKey(armor, passphrase)
if err != nil {
return errors.Wrap(err, "couldn't import private key")
}

kb.writeLocalKey(name, privKey, passphrase)
return nil
}

func (kb dbKeybase) Import(name string, armor string) (err error) {
bz := kb.db.Get(infoKey(name))
if len(bz) > 0 {
Expand Down
10 changes: 10 additions & 0 deletions crypto/keys/lazy_keybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ func (lkb lazyKeybase) Import(name string, armor string) (err error) {
return newDbKeybase(db).Import(name, armor)
}

func (lkb lazyKeybase) ImportPrivKey(name string, armor string, passphrase string) error {
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
if err != nil {
return err
}
defer db.Close()

return newDbKeybase(db).ImportPrivKey(name, armor, passphrase)
}

func (lkb lazyKeybase) ImportPubKey(name string, armor string) (err error) {
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions crypto/keys/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Keybase interface {
// The following operations will *only* work on locally-stored keys
Update(name, oldpass string, getNewpass func() (string, error)) error
Import(name string, armor string) (err error)
ImportPrivKey(name string, armor string, passphrase string) error
ImportPubKey(name string, armor string) (err error)
Export(name string) (armor string, err error)
ExportPubKey(name string) (armor string, err error)
Expand Down

0 comments on commit 9fb6d2d

Please sign in to comment.