Skip to content

Commit

Permalink
Merge PR cosmos#1651: Fabo/fix account creation lcd
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgoes committed Jul 12, 2018
2 parents a6cc85a + 8b9f539 commit b3e4541
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ FEATURES

BUG FIXES
* [keys] \#1629 - updating password no longer asks for a new password when the first entered password was incorrect
* [lcd] importing an account would create a random account

## 0.20.0

Expand Down
9 changes: 7 additions & 2 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func printCreate(info keys.Info, seed string) {
type NewKeyBody struct {
Name string `json:"name"`
Password string `json:"password"`
Seed string `json:"seed"`
}

// add new key REST handler
Expand Down Expand Up @@ -205,7 +206,11 @@ func AddNewKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
}

// create account
info, mnemonic, err := kb.CreateMnemonic(m.Name, keys.English, m.Password, keys.Secp256k1)
seed := m.Seed
if seed == "" {
seed = getSeed(keys.Secp256k1)
}
info, err := kb.CreateKey(m.Name, seed, m.Password)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
Expand All @@ -219,7 +224,7 @@ func AddNewKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
return
}

keyOutput.Seed = mnemonic
keyOutput.Seed = seed

bz, err := json.Marshal(keyOutput)
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

cryptoKeys "github.com/cosmos/cosmos-sdk/crypto/keys"
Expand Down Expand Up @@ -52,7 +53,7 @@ func TestKeys(t *testing.T) {
newPassword := "0987654321"

// add key
jsonStr := []byte(fmt.Sprintf(`{"name":"%s", "password":"%s"}`, newName, newPassword))
jsonStr := []byte(fmt.Sprintf(`{"name":"%s", "password":"%s", "seed":"%s"}`, newName, newPassword, seed))
res, body = Request(t, port, "POST", "/keys", jsonStr)

require.Equal(t, http.StatusOK, res.StatusCode, body)
Expand All @@ -64,6 +65,11 @@ func TestKeys(t *testing.T) {
_, err = sdk.AccAddressFromBech32(addr2Bech32)
require.NoError(t, err, "Failed to return a correct bech32 address")

// test if created account is the correct account
expectedInfo, _ := GetKB(t).CreateKey(newName, seed, newPassword)
expectedAccount := sdk.AccAddress(expectedInfo.GetPubKey().Address().Bytes())
assert.Equal(t, expectedAccount.String(), addr2Bech32)

// existing keys
res, body = Request(t, port, "GET", "/keys", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
Expand Down

0 comments on commit b3e4541

Please sign in to comment.