Skip to content

Commit

Permalink
Fixed panic in NewUser when public key is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
jhernandezb authored and shanev committed Feb 7, 2019
1 parent bf681d4 commit a9b6dd4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
9 changes: 8 additions & 1 deletion x/users/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ type User struct {

// NewUser creates a new User struct from an auth.Account (like AppAccount)
func NewUser(acc auth.Account) User {
var pubKey []byte

// GetPubKey can return nil and Bytes() will panic due to nil pointer
if acc.GetPubKey() != nil {
pubKey = acc.GetPubKey().Bytes()
}

return User{
Address: acc.GetAddress().String(),
AccountNumber: acc.GetAccountNumber(),
Coins: acc.GetCoins(),
Sequence: acc.GetSequence(),
Pubkey: tcmn.HexBytes(acc.GetPubKey().Bytes()),
Pubkey: tcmn.HexBytes(pubKey),
}
}
26 changes: 26 additions & 0 deletions x/users/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package users

import (
"testing"

"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/tendermint/tendermint/crypto/ed25519"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/stretchr/testify/assert"
)

func Test_NewUserEmptyPublicKey(t *testing.T) {
// Check that NewUser don't panick by checking that recover() result is nil
defer func() {
assert.Nil(t, recover())
}()
key := ed25519.GenPrivKey()
pub := key.PubKey()
addr := sdk.AccAddress(pub.Address())
acc := auth.ProtoBaseAccount()
acc.SetAddress(addr)
user := NewUser(acc)
assert.Nil(t, user.Pubkey)
}

0 comments on commit a9b6dd4

Please sign in to comment.