diff --git a/x/users/types.go b/x/users/types.go index c05ffbee..57459d9b 100644 --- a/x/users/types.go +++ b/x/users/types.go @@ -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), } } diff --git a/x/users/types_test.go b/x/users/types_test.go new file mode 100644 index 00000000..13e6985e --- /dev/null +++ b/x/users/types_test.go @@ -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) +}