Skip to content

Commit

Permalink
Merge pull request #1856: gen-tx: Support User Given Key Passwords
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr Bezobchuk committed Jul 27, 2018
1 parent 9812c88 commit 595689d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
5 changes: 4 additions & 1 deletion client/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ func GetPassword(prompt string, buf *bufio.Reader) (pass string, err error) {
} else {
pass, err = readLineFromBuf(buf)
}

if err != nil {
return "", err
}

if len(pass) < MinPassLength {
return "", errors.Errorf("password must be at least %d characters", MinPassLength)
return pass, errors.Errorf("password must be at least %d characters", MinPassLength)
}

return pass, nil
}

Expand Down
46 changes: 33 additions & 13 deletions cmd/gaia/app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package app
import (
"encoding/json"
"errors"
"fmt"

"github.com/spf13/pflag"
"github.com/tendermint/tendermint/crypto"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/server/config"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -20,6 +22,8 @@ var (
// bonded tokens given to genesis validators/accounts
freeFermionVal = int64(100)
freeFermionsAcc = int64(50)

defaultKeyPass = "1234567890"
)

// State to Unmarshal
Expand Down Expand Up @@ -81,30 +85,46 @@ type GaiaGenTx struct {
PubKey string `json:"pub_key"`
}

// Generate a gaia genesis transaction with flags
func GaiaAppGenTx(cdc *wire.Codec, pk crypto.PubKey, genTxConfig config.GenTx) (
appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) {
// GaiaAppGenTx generates a Gaia genesis transaction.
func GaiaAppGenTx(
cdc *wire.Codec, pk crypto.PubKey, genTxConfig config.GenTx,
) (json.RawMessage, json.RawMessage, tmtypes.GenesisValidator, error) {
if genTxConfig.Name == "" {
return nil, nil, tmtypes.GenesisValidator{}, errors.New("Must specify --name (validator moniker)")
}

var addr sdk.AccAddress
var secret string
addr, secret, err = server.GenerateSaveCoinKey(genTxConfig.CliRoot, genTxConfig.Name, "1234567890", genTxConfig.Overwrite)
buf := client.BufferStdin()
prompt := fmt.Sprintf("Password for account '%s' (default %s):", genTxConfig.Name, defaultKeyPass)

keyPass, err := client.GetPassword(prompt, buf)
if err != nil && keyPass != "" {
return nil, nil, tmtypes.GenesisValidator{}, err
}

if keyPass == "" {
keyPass = defaultKeyPass
}

addr, secret, err := server.GenerateSaveCoinKey(
genTxConfig.CliRoot,
genTxConfig.Name,
keyPass,
genTxConfig.Overwrite,
)
if err != nil {
return
return nil, nil, tmtypes.GenesisValidator{}, err
}

mm := map[string]string{"secret": secret}
var bz []byte
bz, err = cdc.MarshalJSON(mm)
bz, err := cdc.MarshalJSON(mm)
if err != nil {
return
return nil, nil, tmtypes.GenesisValidator{}, err
}

cliPrint = json.RawMessage(bz)
cliPrint := json.RawMessage(bz)
appGenTx, _, validator, err := GaiaAppGenTxNF(cdc, pk, addr, genTxConfig.Name)

appGenTx, _, validator, err = GaiaAppGenTxNF(cdc, pk, addr, genTxConfig.Name)
return
return appGenTx, cliPrint, validator, err
}

// Generate a gaia genesis transaction without flags
Expand Down

0 comments on commit 595689d

Please sign in to comment.