Samuel Hawksby-Robinson 465afd0131 Refactored BasePayloadReceiver to handle Receive()
Additionally to allow this process flow I refactored RawMessagePayloadReceiver and InstallationPayloadHandler to use a dedicated Marshaller type. Also added a fix to struct extention functionality, we want to ignore the process if there is no public key because that will key encoding. Seems an unnecessary bug to have to handle when you know there is no key.
2023-04-04 11:56:40 +01:00

64 lines
1.4 KiB
Go

package json
import (
"reflect"
"github.com/status-im/status-go/api/multiformat"
"github.com/status-im/status-go/protocol/identity/emojihash"
)
type PublicKeyData struct {
CompressedKey string `json:"compressedKey"`
EmojiHash []string `json:"emojiHash"`
}
func getPubKeyData(publicKey string) (*PublicKeyData, error) {
compressedKey, err := multiformat.SerializeLegacyKey(publicKey)
if err != nil {
return nil, err
}
emojiHash, err := emojihash.GenerateFor(publicKey)
if err != nil {
return nil, err
}
return &PublicKeyData{compressedKey, emojiHash}, nil
}
func ExtendStructWithPubKeyData(publicKey string, item any) (any, error) {
// If the public key is empty, do not attempt to extend the incoming item
if publicKey == "" {
return item, nil
}
pkd, err := getPubKeyData(publicKey)
if err != nil {
return nil, err
}
// Create a struct with 2 embedded substruct fields in order to circumvent
// "embedded field type cannot be a (pointer to a) type parameter"
// compiler error that arises if we were to use a generic function instead
typ := reflect.StructOf([]reflect.StructField{
{
Name: "Item",
Anonymous: true,
Type: reflect.TypeOf(item),
},
{
Name: "Pkd",
Anonymous: true,
Type: reflect.TypeOf(pkd),
},
})
v := reflect.New(typ).Elem()
v.Field(0).Set(reflect.ValueOf(item))
v.Field(1).Set(reflect.ValueOf(pkd))
s := v.Addr().Interface()
return s, nil
}