Skip to content
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

R4R: Fix DB Write Permissions for gaia keys new #2593

Merged
merged 2 commits into from
Oct 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions client/keys/new.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package keys

import (
"bufio"
"fmt"
"os"

"github.com/bartekn/go-bip39"
"github.com/pkg/errors"
Expand Down Expand Up @@ -46,15 +44,15 @@ input
output
- armor encrypted private key (saved to file)
*/
// nolint: gocyclo
func runNewCmd(cmd *cobra.Command, args []string) error {
name := args[0]
kb, err := GetKeyBase()
kb, err := GetKeyBaseWithWritePerm()
if err != nil {
return err
}

buf := client.BufferStdin()

_, err = kb.Get(name)
if err == nil {
// account exists, ask for user confirmation
Expand All @@ -73,24 +71,25 @@ func runNewCmd(cmd *cobra.Command, args []string) error {
return err
}

// if we're using ledger, only thing we need is the path.
// generate key and we're done.
// If we're using ledger, only thing we need is the path. So generate key and
// we're done.
if viper.GetBool(client.FlagUseLedger) {

algo := keys.Secp256k1 // SigningAlgo(viper.GetString(flagType))
algo := keys.Secp256k1
path := bip44Params.DerivationPath() // ccrypto.DerivationPath{44, 118, account, 0, index}

info, err := kb.CreateLedger(name, path, algo)
if err != nil {
return err
}

printCreate(info, "")
return nil
}

// get the mnemonic
var mnemonic string

if !useDefaults {
mnemonic, err = client.GetString("> Enter your bip39 mnemonic, or hit enter to generate one.", buf)
mnemonic, err = client.GetString("Enter your bip39 mnemonic, or hit enter to generate one.", buf)
if err != nil {
return err
}
Expand All @@ -102,6 +101,7 @@ func runNewCmd(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

mnemonic, err = bip39.NewMnemonic(entropySeed[:])
if err != nil {
return err
Expand All @@ -113,7 +113,8 @@ func runNewCmd(cmd *cobra.Command, args []string) error {
if !useDefaults {
printStep()
printPrefixed("Enter your bip39 passphrase. This is combined with the mnemonic to derive the seed")
bip39Passphrase, err = client.GetString("> Most users should just hit enter to use the default, \"\"", buf)

bip39Passphrase, err = client.GetString("Most users should just hit enter to use the default, \"\"", buf)
if err != nil {
return err
}
Expand All @@ -124,14 +125,16 @@ func runNewCmd(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

if bip39Passphrase != p2 {
return errors.New("passphrases don't match")
}
}
}

// get the encryption password
printStep()

// get the encryption password
encryptPassword, err := client.GetCheckPassword(
"> Enter a passphrase to encrypt your key to disk:",
"> Repeat the passphrase:", buf)
Expand All @@ -143,24 +146,26 @@ func runNewCmd(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
_ = info

_ = info
return nil
}

func getBIP44ParamsAndPath(path string, flagSet bool) (*hd.BIP44Params, error) {
buf := bufio.NewReader(os.Stdin)
buf := client.BufferStdin()
bip44Path := path

// if it wasnt set in the flag, give it a chance to overide interactively
// if it wasn't set in the flag, give it a chance to overide interactively
if !flagSet {
var err error

printStep()

var err error
bip44Path, err = client.GetString(fmt.Sprintf("> Enter your bip44 path. Default is %s\n", path), buf)
bip44Path, err = client.GetString(fmt.Sprintf("Enter your bip44 path. Default is %s\n", path), buf)
if err != nil {
return nil, err
}

if len(bip44Path) == 0 {
bip44Path = path
}
Expand All @@ -170,6 +175,7 @@ func getBIP44ParamsAndPath(path string, flagSet bool) (*hd.BIP44Params, error) {
if err != nil {
return nil, err
}

return bip44params, nil
}

Expand Down