-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement private keys export/import symmetric functionalities #4436
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9fb6d2d
Implement private keys export/import symmetric functionalities
alessio 108b2e1
Add pending
alessio 2f4ee13
Fix test cases
alessio 18ad760
Promote ASCII-armor export function to Keybase interface member
alessio e2fdc0c
Add test for export command
alessio 6d27dd0
Add import command test
alessio eda5a1f
Update pending
alessio 25b9199
Fix inconsistent style
alessio 326bc03
Update crypto/keys/types.go
alessio 9d544e1
Update crypto/keys/types.go
alessio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#2020 New keys export/import command line utilities to export/import private keys in ASCII format | ||
that rely on Keybase's new underlying ExportPrivKey()/ImportPrivKey() API calls. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package keys | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/input" | ||
) | ||
|
||
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-armored encrypted 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() | ||
decryptPassword, err := input.GetPassword("Enter passphrase to decrypt your key:", buf) | ||
if err != nil { | ||
return err | ||
} | ||
encryptPassword, err := input.GetPassword("Enter passphrase to encrypt the exported key:", buf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
armored, err := kb.ExportPrivKey(args[0], decryptPassword, encryptPassword) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(armored) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package keys | ||
|
||
import ( | ||
"bufio" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/cosmos/cosmos-sdk/client/input" | ||
"github.com/cosmos/cosmos-sdk/tests" | ||
) | ||
|
||
func Test_runExportCmd(t *testing.T) { | ||
exportKeyCommand := exportKeyCommand() | ||
|
||
// Now add a temporary keybase | ||
kbHome, cleanUp := tests.NewTestCaseDir(t) | ||
defer cleanUp() | ||
viper.Set(flags.FlagHome, kbHome) | ||
|
||
// create a key | ||
kb, err := NewKeyBaseFromHomeFlag() | ||
assert.NoError(t, err) | ||
_, err = kb.CreateAccount("keyname1", tests.TestMnemonic, "", "123456789", 0, 0) | ||
assert.NoError(t, err) | ||
|
||
// Now enter password | ||
cleanUp1 := input.OverrideStdin(bufio.NewReader(strings.NewReader("123456789\n123456789\n"))) | ||
defer cleanUp1() | ||
assert.NoError(t, runExportCmd(exportKeyCommand, []string{"keyname1"})) | ||
} | ||
jleni marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package keys | ||
|
||
import ( | ||
"bufio" | ||
"io/ioutil" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/cosmos/cosmos-sdk/client/input" | ||
"github.com/cosmos/cosmos-sdk/tests" | ||
) | ||
|
||
func Test_runImportCmd(t *testing.T) { | ||
importKeyCommand := importKeyCommand() | ||
|
||
// Now add a temporary keybase | ||
kbHome, cleanUp := tests.NewTestCaseDir(t) | ||
defer cleanUp() | ||
viper.Set(flags.FlagHome, kbHome) | ||
|
||
keyfile := filepath.Join(kbHome, "key.asc") | ||
armoredKey := `-----BEGIN TENDERMINT PRIVATE KEY----- | ||
salt: A790BB721D1C094260EA84F5E5B72289 | ||
kdf: bcrypt | ||
|
||
HbP+c6JmeJy9JXe2rbbF1QtCX1gLqGcDQPBXiCtFvP7/8wTZtVOPj8vREzhZ9ElO | ||
3P7YnrzPQThG0Q+ZnRSbl9MAS8uFAM4mqm5r/Ys= | ||
=f3l4 | ||
-----END TENDERMINT PRIVATE KEY----- | ||
` | ||
require.NoError(t, ioutil.WriteFile(keyfile, []byte(armoredKey), 0644)) | ||
|
||
// Now enter password | ||
cleanUp1 := input.OverrideStdin(bufio.NewReader(strings.NewReader("123456789\n"))) | ||
defer cleanUp1() | ||
assert.NoError(t, runImportCmd(importKeyCommand, []string{"keyname1", keyfile})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we discussed earlier, maybe we should open another issue for this, however, I think it would be good to improve
input.GetPassword
to ensure that something like12345678
is not accepted.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zmanian ping