Moved protobuf marshalling closer to the structs that get marshalled

This commit is contained in:
Samuel Hawksby-Robinson 2022-08-19 15:07:57 +01:00
parent 06f4b85792
commit 344272ee08
3 changed files with 85 additions and 73 deletions

View File

@ -5,6 +5,7 @@ import (
"errors"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/protocol/protobuf"
)
type IdentityImage struct {
@ -64,6 +65,30 @@ func (i IdentityImage) MarshalJSON() ([]byte, error) {
return json.Marshal(temp)
}
func (i *IdentityImage) ToProtobuf() *protobuf.MultiAccount_IdentityImage {
return &protobuf.MultiAccount_IdentityImage{
KeyUid: i.KeyUID,
Name: i.Name,
Payload: i.Payload,
Width: int64(i.Width),
Height: int64(i.Height),
Filesize: int64(i.FileSize),
ResizeTarget: int64(i.ResizeTarget),
Clock: i.Clock,
}
}
func (i *IdentityImage) FromProtobuf(ii *protobuf.MultiAccount_IdentityImage) {
i.KeyUID = ii.KeyUid
i.Name = ii.Name
i.Payload = ii.Payload
i.Width = int(ii.Width)
i.Height = int(ii.Height)
i.FileSize = int(ii.Filesize)
i.ResizeTarget = int(ii.ResizeTarget)
i.Clock = ii.Clock
}
func (i IdentityImage) IsEmpty() bool {
return i.KeyUID == "" && i.Name == "" && len(i.Payload) == 0 && i.Width == 0 && i.Height == 0 && i.FileSize == 0 && i.ResizeTarget == 0 && i.Clock == 0
}

View File

@ -8,6 +8,7 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/images"
"github.com/status-im/status-go/multiaccounts/migrations"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/sqlite"
)
@ -23,6 +24,62 @@ type Account struct {
Images []images.IdentityImage `json:"images"`
}
func (a *Account) ToProtobuf() *protobuf.MultiAccount {
var colourHashes []*protobuf.MultiAccount_ColourHash
for _, index := range a.ColorHash {
var i []int64
for _, is := range index {
i = append(i, int64(is))
}
colourHashes = append(colourHashes, &protobuf.MultiAccount_ColourHash{Index: i})
}
var identityImages []*protobuf.MultiAccount_IdentityImage
for _, ii := range a.Images {
identityImages = append(identityImages, ii.ToProtobuf())
}
return &protobuf.MultiAccount{
Name: a.Name,
Timestamp: a.Timestamp,
Identicon: a.Identicon,
ColorHash: colourHashes,
ColorId: a.ColorID,
KeycardPairing: a.KeycardPairing,
KeyUid: a.KeyUID,
Images: identityImages,
}
}
func (a *Account) FromProtobuf(ma *protobuf.MultiAccount) {
var colourHash [][]int
for _, index := range ma.ColorHash {
var i []int
for _, is := range index.Index {
i = append(i, int(is))
}
colourHash = append(colourHash, i)
}
var identityImages []images.IdentityImage
for _, ii := range ma.Images {
iii := images.IdentityImage{}
iii.FromProtobuf(ii)
identityImages = append(identityImages, iii)
}
a.Name = ma.Name
a.Timestamp = ma.Timestamp
a.Identicon = ma.Identicon
a.ColorHash = colourHash
a.ColorID = ma.ColorId
a.KeycardPairing = ma.KeycardPairing
a.KeyUID = ma.KeyUid
a.Images = identityImages
}
type MultiAccountMarshaller interface {
ToMultiAccount() *Account
}

View File

@ -13,7 +13,6 @@ import (
"github.com/status-im/status-go/account/generator"
"github.com/status-im/status-go/eth-node/keystore"
"github.com/status-im/status-go/images"
"github.com/status-im/status-go/multiaccounts"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/protobuf"
@ -188,7 +187,7 @@ func NewPairingPayloadMarshaller(p *PairingPayload) *PairingPayloadMarshaller {
func (ppm *PairingPayloadMarshaller) MarshalToProtobuf() ([]byte, error) {
return proto.Marshal(&protobuf.LocalPairingPayload{
Keys: ppm.accountKeysToProtobuf(),
Multiaccount: ppm.multiaccountToProtobuf(),
Multiaccount: ppm.multiaccount.ToProtobuf(),
Password: ppm.password,
})
}
@ -201,43 +200,6 @@ func (ppm *PairingPayloadMarshaller) accountKeysToProtobuf() []*protobuf.LocalPa
return keys
}
func (ppm *PairingPayloadMarshaller) multiaccountToProtobuf() *protobuf.MultiAccount {
var colourHashes []*protobuf.MultiAccount_ColourHash
for _, index := range ppm.multiaccount.ColorHash {
var i []int64
for _, is := range index {
i = append(i, int64(is))
}
colourHashes = append(colourHashes, &protobuf.MultiAccount_ColourHash{Index: i})
}
var identityImages []*protobuf.MultiAccount_IdentityImage
for _, ii := range ppm.multiaccount.Images {
identityImages = append(identityImages, &protobuf.MultiAccount_IdentityImage{
KeyUid: ii.KeyUID,
Name: ii.Name,
Payload: ii.Payload,
Width: int64(ii.Width),
Height: int64(ii.Height),
Filesize: int64(ii.FileSize),
ResizeTarget: int64(ii.ResizeTarget),
Clock: ii.Clock,
})
}
return &protobuf.MultiAccount{
Name: ppm.multiaccount.Name,
Timestamp: ppm.multiaccount.Timestamp,
Identicon: ppm.multiaccount.Identicon,
ColorHash: colourHashes,
ColorId: ppm.multiaccount.ColorID,
KeycardPairing: ppm.multiaccount.KeycardPairing,
KeyUid: ppm.multiaccount.KeyUID,
Images: identityImages,
}
}
func (ppm *PairingPayloadMarshaller) UnmarshalProtobuf(data []byte) error {
pb := new(protobuf.LocalPairingPayload)
err := proto.Unmarshal(data, pb)
@ -262,40 +224,8 @@ func (ppm *PairingPayloadMarshaller) accountKeysFromProtobuf(pbKeys []*protobuf.
}
func (ppm *PairingPayloadMarshaller) multiaccountFromProtobuf(pbMultiAccount *protobuf.MultiAccount) {
var colourHash [][]int
for _, index := range pbMultiAccount.ColorHash {
var i []int
for _, is := range index.Index {
i = append(i, int(is))
}
colourHash = append(colourHash, i)
}
var identityImages []images.IdentityImage
for _, ii := range pbMultiAccount.Images {
identityImages = append(identityImages, images.IdentityImage{
KeyUID: ii.KeyUid,
Name: ii.Name,
Payload: ii.Payload,
Width: int(ii.Width),
Height: int(ii.Height),
FileSize: int(ii.Filesize),
ResizeTarget: int(ii.ResizeTarget),
Clock: ii.Clock,
})
}
ppm.multiaccount = &multiaccounts.Account{
Name: pbMultiAccount.Name,
Timestamp: pbMultiAccount.Timestamp,
Identicon: pbMultiAccount.Identicon,
ColorHash: colourHash,
ColorID: pbMultiAccount.ColorId,
KeycardPairing: pbMultiAccount.KeycardPairing,
KeyUID: pbMultiAccount.KeyUid,
Images: identityImages,
}
ppm.multiaccount = new(multiaccounts.Account)
ppm.multiaccount.FromProtobuf(pbMultiAccount)
}
type PayloadRepository interface {