fix: account unmarshaling

JSON deserialization hasn't worked correctly, resulting in empty
publicKey. That led to an account created with nil color hash and
color id.
This commit is contained in:
Patryk Osmaczko 2022-05-30 08:19:49 +02:00 committed by osmaczko
parent ee7025a573
commit a471fed6a6
3 changed files with 49 additions and 16 deletions

View File

@ -282,9 +282,16 @@ func SaveAccountAndLogin(accountData, password, settingsJSON, configJSON, subacc
for _, acc := range subaccs {
if acc.Chat {
colorHash, _ := colorhash.GenerateFor(string(acc.PublicKey.Bytes()))
colorID, _ := identityUtils.ToColorID(string(acc.PublicKey.Bytes()))
colorHash, err := colorhash.GenerateFor(string(acc.PublicKey.Bytes()))
if err != nil {
return makeJSONResponse(err)
}
account.ColorHash = colorHash
colorID, err := identityUtils.ToColorID(string(acc.PublicKey.Bytes()))
if err != nil {
return makeJSONResponse(err)
}
account.ColorID = colorID
break

View File

@ -1,6 +1,7 @@
package accounts
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
@ -33,3 +34,28 @@ func TestIsOwnAccount(t *testing.T) {
account = Account{}
require.False(t, account.IsOwnAccount())
}
func TestUnmarshal(t *testing.T) {
data := `
{
"public-key": "0x0465f6d4f1172524fc057954c8a3f8e34f991558b3d1097189975062f67adda7835da61acb5cda3348b41d211ed0cb07aba668eb12e19e29d98745bebf68d93b61",
"address": "0xf09c9f5Fb9faa22d0C6C593e7157Ceac8B2b0fe4",
"color": "#4360df",
"wallet": true,
"path": "m/44'/60'/0'/0/0",
"name": "Status account",
"derived-from": "0x6f015A79890Dcb38eFeC1D83772d57159D2eb58b"
}
`
var account Account
err := json.Unmarshal([]byte(data), &account)
require.NoError(t, err)
require.Equal(t, []byte("0x0465f6d4f1172524fc057954c8a3f8e34f991558b3d1097189975062f67adda7835da61acb5cda3348b41d211ed0cb07aba668eb12e19e29d98745bebf68d93b61"), account.PublicKey.Bytes())
require.Equal(t, "0xf09c9f5Fb9faa22d0C6C593e7157Ceac8B2b0fe4", account.Address.String())
require.Equal(t, "#4360df", account.Color)
require.Equal(t, true, account.Wallet)
require.Equal(t, "m/44'/60'/0'/0/0", account.Path)
require.Equal(t, "Status account", account.Name)
require.Equal(t, "0x6f015A79890Dcb38eFeC1D83772d57159D2eb58b", account.DerivedFrom)
}

View File

@ -19,20 +19,20 @@ const (
)
type Account struct {
Address types.Address
Wallet bool
Chat bool
Type string
Storage string
Path string
PublicKey types.HexBytes
Name string
Emoji string
Color string
Hidden bool
DerivedFrom string
Clock uint64
Removed bool
Address types.Address `json:"address"`
Wallet bool `json:"wallet"`
Chat bool `json:"chat"`
Type string `json:"type,omitempty"`
Storage string `json:"storage,omitempty"`
Path string `json:"path,omitempty"`
PublicKey types.HexBytes `json:"public-key,omitempty"`
Name string `json:"name"`
Emoji string `json:"emoji"`
Color string `json:"color"`
Hidden bool `json:"hidden"`
DerivedFrom string `json:"derived-from,omitempty"`
Clock uint64 `json:"clock,omitempty"`
Removed bool `json:"removed,omitempty"`
}
const (