2023-10-24 10:43:18 +00:00
|
|
|
package protocol
|
|
|
|
|
2023-11-09 18:59:01 +00:00
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
crand "crypto/rand"
|
|
|
|
"errors"
|
|
|
|
"reflect"
|
2023-10-24 10:43:18 +00:00
|
|
|
|
2023-12-22 15:43:27 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2023-11-09 18:59:01 +00:00
|
|
|
|
2024-02-07 09:30:56 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
2023-11-09 18:59:01 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
)
|
2023-10-24 10:43:18 +00:00
|
|
|
|
2024-01-25 16:48:27 +00:00
|
|
|
var errorNoAccountProvidedWithTokenOrCollectible = errors.New("no account provided with tokens or collectible")
|
|
|
|
var errorDublicateAccountAddress = errors.New("duplicate account address")
|
|
|
|
|
|
|
|
// NOTE: this error is temporary unused because we don't know account on this stage
|
|
|
|
// var errorNoAccountAddressForCollectible = errors.New("no account found for collectible")
|
|
|
|
var errorAccountVisibilityLowerThanCollectible = errors.New("account visibility lower than collectible")
|
|
|
|
var errorDecryptingPayloadEncryptionKey = errors.New("decrypting the payload encryption key resulted in no error and a nil key")
|
|
|
|
|
2023-11-09 18:59:01 +00:00
|
|
|
func toProfileShowcaseCommunityProto(preferences []*ProfileShowcaseCommunityPreference, visibility ProfileShowcaseVisibility) []*protobuf.ProfileShowcaseCommunity {
|
|
|
|
communities := []*protobuf.ProfileShowcaseCommunity{}
|
|
|
|
for _, preference := range preferences {
|
|
|
|
if preference.ShowcaseVisibility != visibility {
|
|
|
|
continue
|
2023-10-24 10:43:18 +00:00
|
|
|
}
|
2023-11-09 18:59:01 +00:00
|
|
|
|
|
|
|
communities = append(communities, &protobuf.ProfileShowcaseCommunity{
|
|
|
|
CommunityId: preference.CommunityID,
|
|
|
|
Order: uint32(preference.Order),
|
|
|
|
})
|
2023-10-24 10:43:18 +00:00
|
|
|
}
|
2023-11-09 18:59:01 +00:00
|
|
|
return communities
|
|
|
|
}
|
2023-10-24 10:43:18 +00:00
|
|
|
|
2023-11-09 18:59:01 +00:00
|
|
|
func toProfileShowcaseAccountProto(preferences []*ProfileShowcaseAccountPreference, visibility ProfileShowcaseVisibility) []*protobuf.ProfileShowcaseAccount {
|
|
|
|
accounts := []*protobuf.ProfileShowcaseAccount{}
|
|
|
|
for _, preference := range preferences {
|
|
|
|
if preference.ShowcaseVisibility != visibility {
|
|
|
|
continue
|
2023-10-24 10:43:18 +00:00
|
|
|
}
|
2023-11-09 18:59:01 +00:00
|
|
|
|
|
|
|
accounts = append(accounts, &protobuf.ProfileShowcaseAccount{
|
|
|
|
Address: preference.Address,
|
|
|
|
Name: preference.Name,
|
|
|
|
ColorId: preference.ColorID,
|
|
|
|
Emoji: preference.Emoji,
|
|
|
|
Order: uint32(preference.Order),
|
|
|
|
})
|
2023-10-24 10:43:18 +00:00
|
|
|
}
|
2023-11-09 18:59:01 +00:00
|
|
|
return accounts
|
|
|
|
}
|
2023-10-24 10:43:18 +00:00
|
|
|
|
2023-11-09 18:59:01 +00:00
|
|
|
func toProfileShowcaseCollectibleProto(preferences []*ProfileShowcaseCollectiblePreference, visibility ProfileShowcaseVisibility) []*protobuf.ProfileShowcaseCollectible {
|
|
|
|
collectibles := []*protobuf.ProfileShowcaseCollectible{}
|
|
|
|
for _, preference := range preferences {
|
|
|
|
if preference.ShowcaseVisibility != visibility {
|
|
|
|
continue
|
2023-10-24 10:43:18 +00:00
|
|
|
}
|
2023-11-09 18:59:01 +00:00
|
|
|
|
|
|
|
collectibles = append(collectibles, &protobuf.ProfileShowcaseCollectible{
|
2024-01-25 16:48:27 +00:00
|
|
|
ContractAddress: preference.ContractAddress,
|
|
|
|
ChainId: preference.ChainID,
|
|
|
|
TokenId: preference.TokenID,
|
|
|
|
CommunityId: preference.CommunityID,
|
|
|
|
AccountAddress: preference.AccountAddress,
|
|
|
|
Order: uint32(preference.Order),
|
2023-11-09 18:59:01 +00:00
|
|
|
})
|
2023-10-24 10:43:18 +00:00
|
|
|
}
|
2023-11-09 18:59:01 +00:00
|
|
|
return collectibles
|
|
|
|
}
|
2023-10-24 10:43:18 +00:00
|
|
|
|
2024-01-25 16:48:27 +00:00
|
|
|
func toProfileShowcaseVerifiedTokensProto(preferences []*ProfileShowcaseVerifiedTokenPreference, visibility ProfileShowcaseVisibility) []*protobuf.ProfileShowcaseVerifiedToken {
|
|
|
|
tokens := []*protobuf.ProfileShowcaseVerifiedToken{}
|
2023-11-09 18:59:01 +00:00
|
|
|
for _, preference := range preferences {
|
|
|
|
if preference.ShowcaseVisibility != visibility {
|
|
|
|
continue
|
2023-10-24 10:43:18 +00:00
|
|
|
}
|
2023-11-09 18:59:01 +00:00
|
|
|
|
2024-01-25 16:48:27 +00:00
|
|
|
tokens = append(tokens, &protobuf.ProfileShowcaseVerifiedToken{
|
2023-11-09 18:59:01 +00:00
|
|
|
Symbol: preference.Symbol,
|
|
|
|
Order: uint32(preference.Order),
|
|
|
|
})
|
2023-10-24 10:43:18 +00:00
|
|
|
}
|
2024-01-25 16:48:27 +00:00
|
|
|
return tokens
|
|
|
|
}
|
|
|
|
|
|
|
|
func toProfileShowcaseUnverifiedTokensProto(preferences []*ProfileShowcaseUnverifiedTokenPreference, visibility ProfileShowcaseVisibility) []*protobuf.ProfileShowcaseUnverifiedToken {
|
|
|
|
tokens := []*protobuf.ProfileShowcaseUnverifiedToken{}
|
|
|
|
for _, preference := range preferences {
|
|
|
|
if preference.ShowcaseVisibility != visibility {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens = append(tokens, &protobuf.ProfileShowcaseUnverifiedToken{
|
|
|
|
ContractAddress: preference.ContractAddress,
|
|
|
|
ChainId: preference.ChainID,
|
|
|
|
Order: uint32(preference.Order),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return tokens
|
2023-11-09 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func fromProfileShowcaseCommunityProto(messages []*protobuf.ProfileShowcaseCommunity) []*ProfileShowcaseCommunity {
|
|
|
|
communities := []*ProfileShowcaseCommunity{}
|
|
|
|
for _, entry := range messages {
|
|
|
|
communities = append(communities, &ProfileShowcaseCommunity{
|
|
|
|
CommunityID: entry.CommunityId,
|
|
|
|
Order: int(entry.Order),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return communities
|
2023-10-24 10:43:18 +00:00
|
|
|
}
|
|
|
|
|
2023-11-09 18:59:01 +00:00
|
|
|
func fromProfileShowcaseAccountProto(messages []*protobuf.ProfileShowcaseAccount) []*ProfileShowcaseAccount {
|
|
|
|
accounts := []*ProfileShowcaseAccount{}
|
|
|
|
for _, entry := range messages {
|
|
|
|
accounts = append(accounts, &ProfileShowcaseAccount{
|
|
|
|
Address: entry.Address,
|
|
|
|
Name: entry.Name,
|
|
|
|
ColorID: entry.ColorId,
|
|
|
|
Emoji: entry.Emoji,
|
|
|
|
Order: int(entry.Order),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return accounts
|
|
|
|
}
|
|
|
|
|
|
|
|
func fromProfileShowcaseCollectibleProto(messages []*protobuf.ProfileShowcaseCollectible) []*ProfileShowcaseCollectible {
|
|
|
|
collectibles := []*ProfileShowcaseCollectible{}
|
|
|
|
for _, entry := range messages {
|
|
|
|
collectibles = append(collectibles, &ProfileShowcaseCollectible{
|
2024-01-25 16:48:27 +00:00
|
|
|
ContractAddress: entry.ContractAddress,
|
|
|
|
ChainID: entry.ChainId,
|
|
|
|
TokenID: entry.TokenId,
|
|
|
|
CommunityID: entry.CommunityId,
|
|
|
|
AccountAddress: entry.AccountAddress,
|
|
|
|
Order: int(entry.Order),
|
2023-11-09 18:59:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return collectibles
|
|
|
|
}
|
|
|
|
|
2024-01-25 16:48:27 +00:00
|
|
|
func fromProfileShowcaseVerifiedTokenProto(messages []*protobuf.ProfileShowcaseVerifiedToken) []*ProfileShowcaseVerifiedToken {
|
|
|
|
tokens := []*ProfileShowcaseVerifiedToken{}
|
2023-11-09 18:59:01 +00:00
|
|
|
for _, entry := range messages {
|
2024-01-25 16:48:27 +00:00
|
|
|
tokens = append(tokens, &ProfileShowcaseVerifiedToken{
|
2023-11-09 18:59:01 +00:00
|
|
|
Symbol: entry.Symbol,
|
|
|
|
Order: int(entry.Order),
|
|
|
|
})
|
|
|
|
}
|
2024-01-25 16:48:27 +00:00
|
|
|
return tokens
|
|
|
|
}
|
|
|
|
|
|
|
|
func fromProfileShowcaseUnverifiedTokenProto(messages []*protobuf.ProfileShowcaseUnverifiedToken) []*ProfileShowcaseUnverifiedToken {
|
|
|
|
tokens := []*ProfileShowcaseUnverifiedToken{}
|
|
|
|
for _, entry := range messages {
|
|
|
|
tokens = append(tokens, &ProfileShowcaseUnverifiedToken{
|
|
|
|
ContractAddress: entry.ContractAddress,
|
|
|
|
ChainID: entry.ChainId,
|
|
|
|
Order: int(entry.Order),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return tokens
|
|
|
|
}
|
|
|
|
|
|
|
|
func Validate(preferences *ProfileShowcasePreferences) error {
|
|
|
|
if (len(preferences.VerifiedTokens) > 0 || len(preferences.UnverifiedTokens) > 0 || len(preferences.Collectibles) > 0) &&
|
|
|
|
len(preferences.Accounts) == 0 {
|
|
|
|
return errorNoAccountProvidedWithTokenOrCollectible
|
|
|
|
}
|
|
|
|
|
|
|
|
accountsMap := make(map[string]*ProfileShowcaseAccountPreference)
|
|
|
|
for _, account := range preferences.Accounts {
|
|
|
|
if _, ok := accountsMap[account.Address]; ok {
|
|
|
|
return errorDublicateAccountAddress
|
|
|
|
}
|
|
|
|
accountsMap[account.Address] = account
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, collectible := range preferences.Collectibles {
|
|
|
|
account, ok := accountsMap[collectible.AccountAddress]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
// NOTE: with current wallet collectible implementation we don't know account on this stage
|
|
|
|
// return errorNoAccountAddressForCollectible
|
|
|
|
}
|
|
|
|
if account.ShowcaseVisibility < collectible.ShowcaseVisibility {
|
|
|
|
return errorAccountVisibilityLowerThanCollectible
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-10-24 10:43:18 +00:00
|
|
|
}
|
|
|
|
|
2023-11-09 18:59:01 +00:00
|
|
|
func (m *Messenger) SetProfileShowcasePreferences(preferences *ProfileShowcasePreferences) error {
|
2024-01-25 16:48:27 +00:00
|
|
|
err := Validate(preferences)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.persistence.SaveProfileShowcasePreferences(preferences)
|
2023-10-24 10:43:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-07 09:30:56 +00:00
|
|
|
return m.DispatchProfileShowcase()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) DispatchProfileShowcase() error {
|
2023-11-09 18:59:01 +00:00
|
|
|
return m.publishContactCode()
|
|
|
|
}
|
2023-10-24 10:43:18 +00:00
|
|
|
|
2023-11-09 18:59:01 +00:00
|
|
|
func (m *Messenger) GetProfileShowcasePreferences() (*ProfileShowcasePreferences, error) {
|
|
|
|
return m.persistence.GetProfileShowcasePreferences()
|
|
|
|
}
|
2023-10-24 10:43:18 +00:00
|
|
|
|
2023-11-09 18:59:01 +00:00
|
|
|
func (m *Messenger) GetProfileShowcaseForContact(contactID string) (*ProfileShowcase, error) {
|
|
|
|
return m.persistence.GetProfileShowcaseForContact(contactID)
|
2023-10-24 10:43:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 13:29:29 +00:00
|
|
|
func (m *Messenger) GetProfileShowcaseAccountsByAddress(address string) ([]*ProfileShowcaseAccount, error) {
|
|
|
|
return m.persistence.GetProfileShowcaseAccountsByAddress(address)
|
|
|
|
}
|
|
|
|
|
2023-11-09 18:59:01 +00:00
|
|
|
func (m *Messenger) EncryptProfileShowcaseEntriesWithContactPubKeys(entries *protobuf.ProfileShowcaseEntries, contacts []*Contact) (*protobuf.ProfileShowcaseEntriesEncrypted, error) {
|
|
|
|
// Make AES key
|
|
|
|
AESKey := make([]byte, 32)
|
|
|
|
_, err := crand.Read(AESKey)
|
2023-10-24 10:43:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-09 18:59:01 +00:00
|
|
|
// Encrypt showcase entries with the AES key
|
|
|
|
data, err := proto.Marshal(entries)
|
2023-10-24 10:43:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-09 18:59:01 +00:00
|
|
|
encrypted, err := common.Encrypt(data, AESKey, crand.Reader)
|
2023-10-24 10:43:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-09 18:59:01 +00:00
|
|
|
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 {
|
2024-01-25 16:48:27 +00:00
|
|
|
return nil, errorDecryptingPayloadEncryptionKey
|
2023-11-09 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2023-10-24 10:43:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-09 18:59:01 +00:00
|
|
|
forEveryone := &protobuf.ProfileShowcaseEntries{
|
2024-01-25 16:48:27 +00:00
|
|
|
Communities: toProfileShowcaseCommunityProto(preferences.Communities, ProfileShowcaseVisibilityEveryone),
|
|
|
|
Accounts: toProfileShowcaseAccountProto(preferences.Accounts, ProfileShowcaseVisibilityEveryone),
|
|
|
|
Collectibles: toProfileShowcaseCollectibleProto(preferences.Collectibles, ProfileShowcaseVisibilityEveryone),
|
|
|
|
VerifiedTokens: toProfileShowcaseVerifiedTokensProto(preferences.VerifiedTokens, ProfileShowcaseVisibilityEveryone),
|
|
|
|
UnverifiedTokens: toProfileShowcaseUnverifiedTokensProto(preferences.UnverifiedTokens, ProfileShowcaseVisibilityEveryone),
|
2023-11-09 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
forContacts := &protobuf.ProfileShowcaseEntries{
|
2024-01-25 16:48:27 +00:00
|
|
|
Communities: toProfileShowcaseCommunityProto(preferences.Communities, ProfileShowcaseVisibilityContacts),
|
|
|
|
Accounts: toProfileShowcaseAccountProto(preferences.Accounts, ProfileShowcaseVisibilityContacts),
|
|
|
|
Collectibles: toProfileShowcaseCollectibleProto(preferences.Collectibles, ProfileShowcaseVisibilityContacts),
|
|
|
|
VerifiedTokens: toProfileShowcaseVerifiedTokensProto(preferences.VerifiedTokens, ProfileShowcaseVisibilityContacts),
|
|
|
|
UnverifiedTokens: toProfileShowcaseUnverifiedTokensProto(preferences.UnverifiedTokens, ProfileShowcaseVisibilityContacts),
|
2023-11-09 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
forIDVerifiedContacts := &protobuf.ProfileShowcaseEntries{
|
2024-01-25 16:48:27 +00:00
|
|
|
Communities: toProfileShowcaseCommunityProto(preferences.Communities, ProfileShowcaseVisibilityIDVerifiedContacts),
|
|
|
|
Accounts: toProfileShowcaseAccountProto(preferences.Accounts, ProfileShowcaseVisibilityIDVerifiedContacts),
|
|
|
|
Collectibles: toProfileShowcaseCollectibleProto(preferences.Collectibles, ProfileShowcaseVisibilityIDVerifiedContacts),
|
|
|
|
VerifiedTokens: toProfileShowcaseVerifiedTokensProto(preferences.VerifiedTokens, ProfileShowcaseVisibilityIDVerifiedContacts),
|
|
|
|
UnverifiedTokens: toProfileShowcaseUnverifiedTokensProto(preferences.UnverifiedTokens, ProfileShowcaseVisibilityIDVerifiedContacts),
|
2023-11-09 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
communities := []*ProfileShowcaseCommunity{}
|
|
|
|
accounts := []*ProfileShowcaseAccount{}
|
|
|
|
collectibles := []*ProfileShowcaseCollectible{}
|
2024-01-25 16:48:27 +00:00
|
|
|
verifiedTokens := []*ProfileShowcaseVerifiedToken{}
|
|
|
|
unverifiedTokens := []*ProfileShowcaseUnverifiedToken{}
|
2023-11-09 18:59:01 +00:00
|
|
|
|
|
|
|
communities = append(communities, fromProfileShowcaseCommunityProto(message.ForEveryone.Communities)...)
|
|
|
|
accounts = append(accounts, fromProfileShowcaseAccountProto(message.ForEveryone.Accounts)...)
|
|
|
|
collectibles = append(collectibles, fromProfileShowcaseCollectibleProto(message.ForEveryone.Collectibles)...)
|
2024-01-25 16:48:27 +00:00
|
|
|
verifiedTokens = append(verifiedTokens, fromProfileShowcaseVerifiedTokenProto(message.ForEveryone.VerifiedTokens)...)
|
|
|
|
unverifiedTokens = append(unverifiedTokens, fromProfileShowcaseUnverifiedTokenProto(message.ForEveryone.UnverifiedTokens)...)
|
2023-11-09 18:59:01 +00:00
|
|
|
|
|
|
|
senderPubKey := state.CurrentMessageState.PublicKey
|
|
|
|
contactID := state.CurrentMessageState.Contact.ID
|
|
|
|
|
|
|
|
forContacts, err := m.DecryptProfileShowcaseEntriesWithPubKey(senderPubKey, message.ForContacts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if forContacts != nil {
|
|
|
|
communities = append(communities, fromProfileShowcaseCommunityProto(forContacts.Communities)...)
|
|
|
|
accounts = append(accounts, fromProfileShowcaseAccountProto(forContacts.Accounts)...)
|
|
|
|
collectibles = append(collectibles, fromProfileShowcaseCollectibleProto(forContacts.Collectibles)...)
|
2024-01-25 16:48:27 +00:00
|
|
|
verifiedTokens = append(verifiedTokens, fromProfileShowcaseVerifiedTokenProto(forContacts.VerifiedTokens)...)
|
|
|
|
unverifiedTokens = append(unverifiedTokens, fromProfileShowcaseUnverifiedTokenProto(forContacts.UnverifiedTokens)...)
|
2023-11-09 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
forIDVerifiedContacts, err := m.DecryptProfileShowcaseEntriesWithPubKey(senderPubKey, message.ForIdVerifiedContacts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if forIDVerifiedContacts != nil {
|
|
|
|
communities = append(communities, fromProfileShowcaseCommunityProto(forIDVerifiedContacts.Communities)...)
|
|
|
|
accounts = append(accounts, fromProfileShowcaseAccountProto(forIDVerifiedContacts.Accounts)...)
|
|
|
|
collectibles = append(collectibles, fromProfileShowcaseCollectibleProto(forIDVerifiedContacts.Collectibles)...)
|
2024-01-25 16:48:27 +00:00
|
|
|
verifiedTokens = append(verifiedTokens, fromProfileShowcaseVerifiedTokenProto(forIDVerifiedContacts.VerifiedTokens)...)
|
|
|
|
unverifiedTokens = append(unverifiedTokens, fromProfileShowcaseUnverifiedTokenProto(forIDVerifiedContacts.UnverifiedTokens)...)
|
2023-11-09 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 16:48:27 +00:00
|
|
|
// TODO: validate community membership here (https://github.com/status-im/status-desktop/issues/13081)
|
|
|
|
// TODO: validate collectible ownership here (https://github.com/status-im/status-desktop/issues/13073)
|
|
|
|
|
2023-11-09 18:59:01 +00:00
|
|
|
newShowcase := &ProfileShowcase{
|
2024-01-25 16:48:27 +00:00
|
|
|
ContactID: contactID,
|
|
|
|
Communities: communities,
|
|
|
|
Accounts: accounts,
|
|
|
|
Collectibles: collectibles,
|
|
|
|
VerifiedTokens: verifiedTokens,
|
|
|
|
UnverifiedTokens: unverifiedTokens,
|
2023-11-09 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2023-10-24 10:43:18 +00:00
|
|
|
}
|
2024-02-07 09:30:56 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
profileAccount.Name = account.Name
|
|
|
|
profileAccount.ColorID = string(account.ColorID)
|
|
|
|
profileAccount.Emoji = account.Emoji
|
|
|
|
|
|
|
|
err = m.persistence.SaveProfileShowcaseAccountPreference(profileAccount)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.DispatchProfileShowcase()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) DeleteProfileShowcaseWalletAccount(account *accounts.Account) error {
|
|
|
|
err := m.persistence.DeleteProfileShowcaseAccountPreference(account.Address.Hex())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.DispatchProfileShowcase()
|
|
|
|
}
|