-
Notifications
You must be signed in to change notification settings - Fork 245
/
messenger_profile_showcase.go
706 lines (604 loc) · 25.2 KB
/
messenger_profile_showcase.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
package protocol
import (
"bytes"
"context"
"crypto/ecdsa"
crand "crypto/rand"
"errors"
"math/big"
"reflect"
"sort"
"github.com/golang/protobuf/proto"
"go.uber.org/zap"
eth_common "github.com/ethereum/go-ethereum/common"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/multiaccounts/accounts"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/communities"
"github.com/status-im/status-go/protocol/identity"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/services/wallet/bigint"
w_common "github.com/status-im/status-go/services/wallet/common"
"github.com/status-im/status-go/services/wallet/thirdparty"
)
var errorDecryptingPayloadEncryptionKey = errors.New("decrypting the payload encryption key resulted in no error and a nil key")
var errorConvertCollectibleTokenIDToInt = errors.New("failed to convert collectible token id to bigint")
var errorNoAccountPresentedForCollectible = errors.New("account holding the collectible is not presented in the profile showcase")
var errorDublicateAccountAddress = errors.New("duplicate account address")
var errorAccountVisibilityLowerThanCollectible = errors.New("account visibility lower than collectible")
func sortProfileEntyByOrder(slice interface{}, getOrder func(int) int) {
sort.Slice(slice, func(i, j int) bool {
return getOrder(j) > getOrder(i)
})
}
func toCollectibleUniqueID(contractAddress string, tokenID string, chainID uint64) (thirdparty.CollectibleUniqueID, error) {
tokenIDInt := new(big.Int)
tokenIDInt, isTokenIDOk := tokenIDInt.SetString(tokenID, 10)
if !isTokenIDOk {
return thirdparty.CollectibleUniqueID{}, errorConvertCollectibleTokenIDToInt
}
return thirdparty.CollectibleUniqueID{
ContractID: thirdparty.ContractID{
ChainID: w_common.ChainID(chainID),
Address: eth_common.HexToAddress(contractAddress),
},
TokenID: &bigint.BigInt{Int: tokenIDInt},
}, nil
}
func (m *Messenger) fetchCollectibleOwner(contractAddress string, tokenID string, chainID uint64) ([]thirdparty.AccountBalance, error) {
collectibleID, err := toCollectibleUniqueID(contractAddress, tokenID, chainID)
if err != nil {
return nil, err
}
balance, err := m.communitiesManager.GetCollectiblesManager().GetCollectibleOwnership(collectibleID)
if err != nil {
return nil, err
}
return balance, nil
}
func (m *Messenger) validateCollectiblesOwnership(accounts []*identity.ProfileShowcaseAccountPreference,
collectibles []*identity.ProfileShowcaseCollectiblePreference) error {
accountsMap := make(map[string]identity.ProfileShowcaseVisibility)
for _, accountProfile := range accounts {
if _, ok := accountsMap[accountProfile.Address]; ok {
return errorDublicateAccountAddress
}
accountsMap[accountProfile.Address] = accountProfile.ShowcaseVisibility
}
for _, collectibleProfile := range collectibles {
balances, err := m.fetchCollectibleOwner(collectibleProfile.ContractAddress, collectibleProfile.TokenID,
collectibleProfile.ChainID)
if err != nil {
return err
}
// NOTE: ERC721 tokens can have only a single holder
// but ERC1155 which can be supported later can have more than one holder and balances > 1
found := false
for _, balance := range balances {
if accountShowcaseVisibility, ok := accountsMap[balance.Address.String()]; ok {
if accountShowcaseVisibility < collectibleProfile.ShowcaseVisibility {
return errorAccountVisibilityLowerThanCollectible
}
found = true
break
}
}
if !found {
return errorNoAccountPresentedForCollectible
}
}
return nil
}
func (m *Messenger) toProfileShowcaseCommunityProto(preferences []*identity.ProfileShowcaseCommunityPreference, visibility identity.ProfileShowcaseVisibility) []*protobuf.ProfileShowcaseCommunity {
entries := []*protobuf.ProfileShowcaseCommunity{}
for _, preference := range preferences {
if preference.ShowcaseVisibility != visibility {
continue
}
entry := &protobuf.ProfileShowcaseCommunity{
CommunityId: preference.CommunityID,
Order: uint32(preference.Order),
}
community, err := m.communitiesManager.GetByIDString(preference.CommunityID)
if err != nil {
m.logger.Warn("failed to get community for profile entry ", zap.Error(err))
}
if community != nil && community.Encrypted() {
grant, _, err := m.communitiesManager.GetCommunityGrant(preference.CommunityID)
if err != nil {
m.logger.Warn("failed to get community for profile entry ", zap.Error(err))
}
entry.Grant = grant
}
entries = append(entries, entry)
}
return entries
}
func (m *Messenger) toProfileShowcaseAccountProto(preferences []*identity.ProfileShowcaseAccountPreference, visibility identity.ProfileShowcaseVisibility) []*protobuf.ProfileShowcaseAccount {
entries := []*protobuf.ProfileShowcaseAccount{}
for _, preference := range preferences {
if preference.ShowcaseVisibility != visibility {
continue
}
account, err := m.settings.GetAccountByAddress(types.HexToAddress(preference.Address))
if err != nil {
m.logger.Warn("failed to get account for profile entry ", zap.Error(err))
}
if account == nil {
m.logger.Warn("can not find wallet account for profile entry ")
continue
}
entries = append(entries, &protobuf.ProfileShowcaseAccount{
Address: preference.Address,
Name: account.Name,
ColorId: string(account.ColorID),
Emoji: account.Emoji,
Order: uint32(preference.Order),
})
}
return entries
}
func (m *Messenger) toProfileShowcaseCollectibleProto(preferences []*identity.ProfileShowcaseCollectiblePreference, visibility identity.ProfileShowcaseVisibility) []*protobuf.ProfileShowcaseCollectible {
entries := []*protobuf.ProfileShowcaseCollectible{}
for _, preference := range preferences {
if preference.ShowcaseVisibility != visibility {
continue
}
entries = append(entries, &protobuf.ProfileShowcaseCollectible{
ContractAddress: preference.ContractAddress,
ChainId: preference.ChainID,
TokenId: preference.TokenID,
Order: uint32(preference.Order),
})
}
return entries
}
func (m *Messenger) toProfileShowcaseVerifiedTokensProto(preferences []*identity.ProfileShowcaseVerifiedTokenPreference, visibility identity.ProfileShowcaseVisibility) []*protobuf.ProfileShowcaseVerifiedToken {
entries := []*protobuf.ProfileShowcaseVerifiedToken{}
for _, preference := range preferences {
if preference.ShowcaseVisibility != visibility {
continue
}
entries = append(entries, &protobuf.ProfileShowcaseVerifiedToken{
Symbol: preference.Symbol,
Order: uint32(preference.Order),
})
}
return entries
}
func (m *Messenger) toProfileShowcaseUnverifiedTokensProto(preferences []*identity.ProfileShowcaseUnverifiedTokenPreference, visibility identity.ProfileShowcaseVisibility) []*protobuf.ProfileShowcaseUnverifiedToken {
entries := []*protobuf.ProfileShowcaseUnverifiedToken{}
for _, preference := range preferences {
if preference.ShowcaseVisibility != visibility {
continue
}
entries = append(entries, &protobuf.ProfileShowcaseUnverifiedToken{
ContractAddress: preference.ContractAddress,
ChainId: preference.ChainID,
Order: uint32(preference.Order),
})
}
return entries
}
func (m *Messenger) toProfileShowcaseSocialLinksProto(preferences []*identity.ProfileShowcaseSocialLinkPreference, visibility identity.ProfileShowcaseVisibility) []*protobuf.ProfileShowcaseSocialLink {
entries := []*protobuf.ProfileShowcaseSocialLink{}
for _, preference := range preferences {
if preference.ShowcaseVisibility != visibility {
continue
}
entries = append(entries, &protobuf.ProfileShowcaseSocialLink{
Text: preference.Text,
Url: preference.URL,
Order: uint32(preference.Order),
})
}
return entries
}
func (m *Messenger) fromProfileShowcaseCommunityProto(senderPubKey *ecdsa.PublicKey, messages []*protobuf.ProfileShowcaseCommunity) []*identity.ProfileShowcaseCommunity {
entries := []*identity.ProfileShowcaseCommunity{}
for _, message := range messages {
entry := &identity.ProfileShowcaseCommunity{
CommunityID: message.CommunityId,
Order: int(message.Order),
MembershipStatus: identity.ProfileShowcaseMembershipStatusUnproven,
}
community, err := m.FetchCommunity(&FetchCommunityRequest{
CommunityKey: message.CommunityId,
Shard: nil,
TryDatabase: true,
WaitForResponse: true,
})
if err != nil {
m.logger.Warn("failed to fetch community for profile entry ", zap.Error(err))
}
if community != nil && community.Encrypted() {
grant, err := community.VerifyGrantSignature(message.Grant)
if err != nil {
m.logger.Warn("failed to verify grant signature ", zap.Error(err))
entry.MembershipStatus = identity.ProfileShowcaseMembershipStatusNotAMember
} else {
if grant != nil && bytes.Equal(grant.MemberId, crypto.CompressPubkey(senderPubKey)) {
entry.MembershipStatus = identity.ProfileShowcaseMembershipStatusProvenMember
} else { // Show as not a member if membership can't be proven
entry.MembershipStatus = identity.ProfileShowcaseMembershipStatusNotAMember
}
}
} else if community != nil {
// Use member list as a proof for unecrypted communities
if community.HasMember(senderPubKey) {
entry.MembershipStatus = identity.ProfileShowcaseMembershipStatusProvenMember
} else {
entry.MembershipStatus = identity.ProfileShowcaseMembershipStatusNotAMember
}
}
entries = append(entries, entry)
}
return entries
}
func (m *Messenger) fromProfileShowcaseAccountProto(messages []*protobuf.ProfileShowcaseAccount) []*identity.ProfileShowcaseAccount {
entries := []*identity.ProfileShowcaseAccount{}
for _, entry := range messages {
entries = append(entries, &identity.ProfileShowcaseAccount{
Address: entry.Address,
Name: entry.Name,
ColorID: entry.ColorId,
Emoji: entry.Emoji,
Order: int(entry.Order),
})
}
return entries
}
func (m *Messenger) fromProfileShowcaseCollectibleProto(messages []*protobuf.ProfileShowcaseCollectible) []*identity.ProfileShowcaseCollectible {
entries := []*identity.ProfileShowcaseCollectible{}
for _, message := range messages {
entry := &identity.ProfileShowcaseCollectible{
ContractAddress: message.ContractAddress,
ChainID: message.ChainId,
TokenID: message.TokenId,
Order: int(message.Order),
}
entries = append(entries, entry)
}
return entries
}
func (m *Messenger) fromProfileShowcaseVerifiedTokenProto(messages []*protobuf.ProfileShowcaseVerifiedToken) []*identity.ProfileShowcaseVerifiedToken {
entries := []*identity.ProfileShowcaseVerifiedToken{}
for _, entry := range messages {
entries = append(entries, &identity.ProfileShowcaseVerifiedToken{
Symbol: entry.Symbol,
Order: int(entry.Order),
})
}
return entries
}
func (m *Messenger) fromProfileShowcaseUnverifiedTokenProto(messages []*protobuf.ProfileShowcaseUnverifiedToken) []*identity.ProfileShowcaseUnverifiedToken {
entries := []*identity.ProfileShowcaseUnverifiedToken{}
for _, entry := range messages {
entries = append(entries, &identity.ProfileShowcaseUnverifiedToken{
ContractAddress: entry.ContractAddress,
ChainID: entry.ChainId,
Order: int(entry.Order),
})
}
return entries
}
func (m *Messenger) fromProfileShowcaseSocialLinkProto(messages []*protobuf.ProfileShowcaseSocialLink) []*identity.ProfileShowcaseSocialLink {
entries := []*identity.ProfileShowcaseSocialLink{}
for _, entry := range messages {
entries = append(entries, &identity.ProfileShowcaseSocialLink{
Text: entry.Text,
URL: entry.Url,
Order: int(entry.Order),
})
}
return entries
}
func (m *Messenger) SetProfileShowcasePreferences(preferences *identity.ProfileShowcasePreferences, sync bool) error {
clock, _ := m.getLastClockWithRelatedChat()
preferences.Clock = clock
return m.setProfileShowcasePreferences(preferences, sync)
}
func (m *Messenger) setProfileShowcasePreferences(preferences *identity.ProfileShowcasePreferences, sync bool) error {
err := identity.Validate(preferences)
if err != nil {
return err
}
err = m.validateCollectiblesOwnership(preferences.Accounts, preferences.Collectibles)
if err != nil {
return err
}
err = m.persistence.SaveProfileShowcasePreferences(preferences)
if err != nil {
return err
}
if sync {
err = m.syncProfileShowcasePreferences(context.Background(), m.dispatchMessage)
if err != nil {
return err
}
}
return m.DispatchProfileShowcase()
}
func (m *Messenger) DispatchProfileShowcase() error {
err := m.publishContactCode()
if err != nil {
return err
}
return nil
}
func (m *Messenger) GetProfileShowcasePreferences() (*identity.ProfileShowcasePreferences, error) {
return m.persistence.GetProfileShowcasePreferences()
}
func (m *Messenger) GetProfileShowcaseForContact(contactID string) (*identity.ProfileShowcase, error) {
return m.persistence.GetProfileShowcaseForContact(contactID)
}
func (m *Messenger) GetProfileShowcaseAccountsByAddress(address string) ([]*identity.ProfileShowcaseAccount, error) {
return m.persistence.GetProfileShowcaseAccountsByAddress(address)
}
func (m *Messenger) GetProfileShowcaseSocialLinksLimit() (int, error) {
return identity.MaxProfileShowcaseSocialLinksLimit, nil
}
func (m *Messenger) GetProfileShowcaseEntriesLimit() (int, error) {
return identity.MaxProfileShowcaseEntriesLimit, nil
}
func (m *Messenger) EncryptProfileShowcaseEntriesWithContactPubKeys(entries *protobuf.ProfileShowcaseEntries, contacts []*Contact) (*protobuf.ProfileShowcaseEntriesEncrypted, error) {
// Make AES key
AESKey := make([]byte, 32)
_, err := crand.Read(AESKey)
if err != nil {
return nil, err
}
// Encrypt showcase entries with the AES key
data, err := proto.Marshal(entries)
if err != nil {
return nil, err
}
encrypted, err := common.Encrypt(data, AESKey, crand.Reader)
if err != nil {
return nil, err
}
eAESKeys := [][]byte{}
// Sign for each contact
for _, contact := range contacts {
var pubK *ecdsa.PublicKey
var sharedKey []byte
var eAESKey []byte
pubK, err = contact.PublicKey()
if err != nil {
return nil, err
}
// Generate a Diffie-Helman (DH) between the sender private key and the recipient's public key
sharedKey, err = common.MakeECDHSharedKey(m.identity, pubK)
if err != nil {
return nil, err
}
// Encrypt the main AES key with AES encryption using the DH key
eAESKey, err = common.Encrypt(AESKey, sharedKey, crand.Reader)
if err != nil {
return nil, err
}
eAESKeys = append(eAESKeys, eAESKey)
}
return &protobuf.ProfileShowcaseEntriesEncrypted{
EncryptedEntries: encrypted,
EncryptionKeys: eAESKeys,
}, nil
}
func (m *Messenger) DecryptProfileShowcaseEntriesWithPubKey(senderPubKey *ecdsa.PublicKey, encrypted *protobuf.ProfileShowcaseEntriesEncrypted) (*protobuf.ProfileShowcaseEntries, error) {
for _, eAESKey := range encrypted.EncryptionKeys {
// Generate a Diffie-Helman (DH) between the recipient's private key and the sender's public key
sharedKey, err := common.MakeECDHSharedKey(m.identity, senderPubKey)
if err != nil {
return nil, err
}
// Decrypt the main encryption AES key with AES encryption using the DH key
dAESKey, err := common.Decrypt(eAESKey, sharedKey)
if err != nil {
if err.Error() == ErrCipherMessageAutentificationFailed {
continue
}
return nil, err
}
if dAESKey == nil {
return nil, errorDecryptingPayloadEncryptionKey
}
// Decrypt profile entries with the newly decrypted main encryption AES key
entriesData, err := common.Decrypt(encrypted.EncryptedEntries, dAESKey)
if err != nil {
return nil, err
}
entries := &protobuf.ProfileShowcaseEntries{}
err = proto.Unmarshal(entriesData, entries)
if err != nil {
return nil, err
}
return entries, nil
}
// Return empty if no matching key found
return &protobuf.ProfileShowcaseEntries{}, nil
}
func (m *Messenger) GetProfileShowcaseForSelfIdentity() (*protobuf.ProfileShowcase, error) {
preferences, err := m.GetProfileShowcasePreferences()
if err != nil {
return nil, err
}
forEveryone := &protobuf.ProfileShowcaseEntries{
Communities: m.toProfileShowcaseCommunityProto(preferences.Communities, identity.ProfileShowcaseVisibilityEveryone),
Accounts: m.toProfileShowcaseAccountProto(preferences.Accounts, identity.ProfileShowcaseVisibilityEveryone),
Collectibles: m.toProfileShowcaseCollectibleProto(preferences.Collectibles, identity.ProfileShowcaseVisibilityEveryone),
VerifiedTokens: m.toProfileShowcaseVerifiedTokensProto(preferences.VerifiedTokens, identity.ProfileShowcaseVisibilityEveryone),
UnverifiedTokens: m.toProfileShowcaseUnverifiedTokensProto(preferences.UnverifiedTokens, identity.ProfileShowcaseVisibilityEveryone),
SocialLinks: m.toProfileShowcaseSocialLinksProto(preferences.SocialLinks, identity.ProfileShowcaseVisibilityEveryone),
}
forContacts := &protobuf.ProfileShowcaseEntries{
Communities: m.toProfileShowcaseCommunityProto(preferences.Communities, identity.ProfileShowcaseVisibilityContacts),
Accounts: m.toProfileShowcaseAccountProto(preferences.Accounts, identity.ProfileShowcaseVisibilityContacts),
Collectibles: m.toProfileShowcaseCollectibleProto(preferences.Collectibles, identity.ProfileShowcaseVisibilityContacts),
VerifiedTokens: m.toProfileShowcaseVerifiedTokensProto(preferences.VerifiedTokens, identity.ProfileShowcaseVisibilityContacts),
UnverifiedTokens: m.toProfileShowcaseUnverifiedTokensProto(preferences.UnverifiedTokens, identity.ProfileShowcaseVisibilityContacts),
SocialLinks: m.toProfileShowcaseSocialLinksProto(preferences.SocialLinks, identity.ProfileShowcaseVisibilityContacts),
}
forIDVerifiedContacts := &protobuf.ProfileShowcaseEntries{
Communities: m.toProfileShowcaseCommunityProto(preferences.Communities, identity.ProfileShowcaseVisibilityIDVerifiedContacts),
Accounts: m.toProfileShowcaseAccountProto(preferences.Accounts, identity.ProfileShowcaseVisibilityIDVerifiedContacts),
Collectibles: m.toProfileShowcaseCollectibleProto(preferences.Collectibles, identity.ProfileShowcaseVisibilityIDVerifiedContacts),
VerifiedTokens: m.toProfileShowcaseVerifiedTokensProto(preferences.VerifiedTokens, identity.ProfileShowcaseVisibilityIDVerifiedContacts),
UnverifiedTokens: m.toProfileShowcaseUnverifiedTokensProto(preferences.UnverifiedTokens, identity.ProfileShowcaseVisibilityIDVerifiedContacts),
SocialLinks: m.toProfileShowcaseSocialLinksProto(preferences.SocialLinks, identity.ProfileShowcaseVisibilityIDVerifiedContacts),
}
mutualContacts := []*Contact{}
iDVerifiedContacts := []*Contact{}
m.allContacts.Range(func(_ string, contact *Contact) (shouldContinue bool) {
if contact.mutual() {
mutualContacts = append(mutualContacts, contact)
if contact.IsVerified() {
iDVerifiedContacts = append(iDVerifiedContacts, contact)
}
}
return true
})
forContactsEncrypted, err := m.EncryptProfileShowcaseEntriesWithContactPubKeys(forContacts, mutualContacts)
if err != nil {
return nil, err
}
forIDVerifiedContactsEncrypted, err := m.EncryptProfileShowcaseEntriesWithContactPubKeys(forIDVerifiedContacts, iDVerifiedContacts)
if err != nil {
return nil, err
}
return &protobuf.ProfileShowcase{
ForEveryone: forEveryone,
ForContacts: forContactsEncrypted,
ForIdVerifiedContacts: forIDVerifiedContactsEncrypted,
}, nil
}
func (m *Messenger) BuildProfileShowcaseFromIdentity(state *ReceivedMessageState, message *protobuf.ProfileShowcase) error {
senderPubKey := state.CurrentMessageState.PublicKey
contactID := state.CurrentMessageState.Contact.ID
communities := []*identity.ProfileShowcaseCommunity{}
accounts := []*identity.ProfileShowcaseAccount{}
collectibles := []*identity.ProfileShowcaseCollectible{}
verifiedTokens := []*identity.ProfileShowcaseVerifiedToken{}
unverifiedTokens := []*identity.ProfileShowcaseUnverifiedToken{}
socialLinks := []*identity.ProfileShowcaseSocialLink{}
communities = append(communities, m.fromProfileShowcaseCommunityProto(senderPubKey, message.ForEveryone.Communities)...)
accounts = append(accounts, m.fromProfileShowcaseAccountProto(message.ForEveryone.Accounts)...)
collectibles = append(collectibles, m.fromProfileShowcaseCollectibleProto(message.ForEveryone.Collectibles)...)
verifiedTokens = append(verifiedTokens, m.fromProfileShowcaseVerifiedTokenProto(message.ForEveryone.VerifiedTokens)...)
unverifiedTokens = append(unverifiedTokens, m.fromProfileShowcaseUnverifiedTokenProto(message.ForEveryone.UnverifiedTokens)...)
socialLinks = append(socialLinks, m.fromProfileShowcaseSocialLinkProto(message.ForEveryone.SocialLinks)...)
forContacts, err := m.DecryptProfileShowcaseEntriesWithPubKey(senderPubKey, message.ForContacts)
if err != nil {
return err
}
if forContacts != nil {
communities = append(communities, m.fromProfileShowcaseCommunityProto(senderPubKey, forContacts.Communities)...)
accounts = append(accounts, m.fromProfileShowcaseAccountProto(forContacts.Accounts)...)
collectibles = append(collectibles, m.fromProfileShowcaseCollectibleProto(forContacts.Collectibles)...)
verifiedTokens = append(verifiedTokens, m.fromProfileShowcaseVerifiedTokenProto(forContacts.VerifiedTokens)...)
unverifiedTokens = append(unverifiedTokens, m.fromProfileShowcaseUnverifiedTokenProto(forContacts.UnverifiedTokens)...)
socialLinks = append(socialLinks, m.fromProfileShowcaseSocialLinkProto(forContacts.SocialLinks)...)
}
forIDVerifiedContacts, err := m.DecryptProfileShowcaseEntriesWithPubKey(senderPubKey, message.ForIdVerifiedContacts)
if err != nil {
return err
}
if forIDVerifiedContacts != nil {
communities = append(communities, m.fromProfileShowcaseCommunityProto(senderPubKey, forIDVerifiedContacts.Communities)...)
accounts = append(accounts, m.fromProfileShowcaseAccountProto(forIDVerifiedContacts.Accounts)...)
collectibles = append(collectibles, m.fromProfileShowcaseCollectibleProto(forIDVerifiedContacts.Collectibles)...)
verifiedTokens = append(verifiedTokens, m.fromProfileShowcaseVerifiedTokenProto(forIDVerifiedContacts.VerifiedTokens)...)
unverifiedTokens = append(unverifiedTokens, m.fromProfileShowcaseUnverifiedTokenProto(forIDVerifiedContacts.UnverifiedTokens)...)
socialLinks = append(socialLinks, m.fromProfileShowcaseSocialLinkProto(forIDVerifiedContacts.SocialLinks)...)
}
sortProfileEntyByOrder(communities, func(i int) int { return communities[i].Order })
sortProfileEntyByOrder(accounts, func(i int) int { return accounts[i].Order })
sortProfileEntyByOrder(collectibles, func(i int) int { return collectibles[i].Order })
sortProfileEntyByOrder(verifiedTokens, func(i int) int { return verifiedTokens[i].Order })
sortProfileEntyByOrder(unverifiedTokens, func(i int) int { return unverifiedTokens[i].Order })
sortProfileEntyByOrder(socialLinks, func(i int) int { return socialLinks[i].Order })
newShowcase := &identity.ProfileShowcase{
ContactID: contactID,
Communities: communities,
Accounts: accounts,
Collectibles: collectibles,
VerifiedTokens: verifiedTokens,
UnverifiedTokens: unverifiedTokens,
SocialLinks: socialLinks,
}
oldShowcase, err := m.persistence.GetProfileShowcaseForContact(contactID)
if err != nil {
return err
}
if reflect.DeepEqual(newShowcase, oldShowcase) {
return nil
}
err = m.persistence.ClearProfileShowcaseForContact(contactID)
if err != nil {
return err
}
err = m.persistence.SaveProfileShowcaseForContact(newShowcase)
if err != nil {
return err
}
state.Response.AddProfileShowcase(newShowcase)
return nil
}
func (m *Messenger) UpdateProfileShowcaseWalletAccount(account *accounts.Account) error {
profileAccount, err := m.persistence.GetProfileShowcaseAccountPreference(account.Address.Hex())
if err != nil {
return err
}
if profileAccount == nil {
// No corresponding profile entry, exit
return nil
}
return m.DispatchProfileShowcase()
}
func (m *Messenger) DeleteProfileShowcaseWalletAccount(account *accounts.Account) error {
deleted, err := m.persistence.DeleteProfileShowcaseAccountPreference(account.Address.Hex())
if err != nil {
return err
}
if deleted {
return m.DispatchProfileShowcase()
}
return nil
}
func (m *Messenger) DeleteProfileShowcaseCommunity(community *communities.Community) error {
deleted, err := m.persistence.DeleteProfileShowcaseCommunityPreference(community.IDString())
if err != nil {
return err
}
if deleted {
return m.DispatchProfileShowcase()
}
return nil
}
func (m *Messenger) saveProfileShowcasePreferencesProto(p *protobuf.SyncProfileShowcasePreferences, shouldSync bool) (*identity.ProfileShowcasePreferences, error) {
if p == nil {
return nil, nil
}
preferences := FromProfileShowcasePreferencesProto(p)
return preferences, m.setProfileShowcasePreferences(preferences, shouldSync)
}
func (m *Messenger) syncProfileShowcasePreferences(ctx context.Context, rawMessageHandler RawMessageHandler) error {
preferences, err := m.GetProfileShowcasePreferences()
if err != nil {
return err
}
syncMessage := ToProfileShowcasePreferencesProto(preferences)
encodedMessage, err := proto.Marshal(syncMessage)
if err != nil {
return err
}
_, chat := m.getLastClockWithRelatedChat()
rawMessage := common.RawMessage{
LocalChatID: chat.ID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_SYNC_PROFILE_SHOWCASE_PREFERENCES,
ResendAutomatically: true,
}
_, err = rawMessageHandler(ctx, rawMessage)
return err
}