github.com/status-im/status-go@v1.1.0/account/json/utils.go (about) 1 package json 2 3 import ( 4 "reflect" 5 6 "github.com/status-im/status-go/api/multiformat" 7 "github.com/status-im/status-go/protocol/identity/emojihash" 8 ) 9 10 type PublicKeyData struct { 11 CompressedKey string `json:"compressedKey"` 12 EmojiHash []string `json:"emojiHash"` 13 } 14 15 func getPubKeyData(publicKey string) (*PublicKeyData, error) { 16 compressedKey, err := multiformat.SerializeLegacyKey(publicKey) 17 if err != nil { 18 return nil, err 19 } 20 21 emojiHash, err := emojihash.GenerateFor(publicKey) 22 if err != nil { 23 return nil, err 24 } 25 26 return &PublicKeyData{compressedKey, emojiHash}, nil 27 28 } 29 30 func ExtendStructWithPubKeyData(publicKey string, item any) (any, error) { 31 // If the public key is empty, do not attempt to extend the incoming item 32 if publicKey == "" { 33 return item, nil 34 } 35 36 pkd, err := getPubKeyData(publicKey) 37 if err != nil { 38 return nil, err 39 } 40 41 // Create a struct with 2 embedded substruct fields in order to circumvent 42 // "embedded field type cannot be a (pointer to a) type parameter" 43 // compiler error that arises if we were to use a generic function instead 44 typ := reflect.StructOf([]reflect.StructField{ 45 { 46 Name: "Item", 47 Anonymous: true, 48 Type: reflect.TypeOf(item), 49 }, 50 { 51 Name: "Pkd", 52 Anonymous: true, 53 Type: reflect.TypeOf(pkd), 54 }, 55 }) 56 57 v := reflect.New(typ).Elem() 58 v.Field(0).Set(reflect.ValueOf(item)) 59 v.Field(1).Set(reflect.ValueOf(pkd)) 60 s := v.Addr().Interface() 61 62 return s, nil 63 }