2023-11-29 17:21:21 +00:00
|
|
|
package communities
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2024-01-23 16:56:51 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2023-11-29 17:21:21 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DescriptionEncryptor interface {
|
|
|
|
encryptCommunityDescription(community *Community, d *protobuf.CommunityDescription) (string, []byte, error)
|
|
|
|
encryptCommunityDescriptionChannel(community *Community, channelID string, d *protobuf.CommunityDescription) (string, []byte, error)
|
2024-01-23 16:56:51 +00:00
|
|
|
decryptCommunityDescription(keyIDSeqNo string, d []byte) (*DecryptCommunityResponse, error)
|
2023-11-29 17:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypts members and chats
|
|
|
|
func encryptDescription(encryptor DescriptionEncryptor, community *Community, description *protobuf.CommunityDescription) error {
|
|
|
|
description.PrivateData = make(map[string][]byte)
|
|
|
|
|
|
|
|
for channelID, channel := range description.Chats {
|
|
|
|
if !community.channelEncrypted(channelID) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
descriptionToEncrypt := &protobuf.CommunityDescription{
|
|
|
|
Chats: map[string]*protobuf.CommunityChat{
|
2024-03-20 19:18:18 +00:00
|
|
|
channelID: channel,
|
2023-11-29 17:21:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
keyIDSeqNo, encryptedDescription, err := encryptor.encryptCommunityDescriptionChannel(community, channelID, descriptionToEncrypt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set private data and cleanup unencrypted channel's members
|
|
|
|
description.PrivateData[keyIDSeqNo] = encryptedDescription
|
|
|
|
channel.Members = make(map[string]*protobuf.CommunityMember)
|
|
|
|
}
|
|
|
|
|
|
|
|
if community.Encrypted() {
|
|
|
|
descriptionToEncrypt := &protobuf.CommunityDescription{
|
2024-03-20 19:18:18 +00:00
|
|
|
Members: description.Members,
|
|
|
|
ActiveMembersCount: description.ActiveMembersCount,
|
|
|
|
Chats: description.Chats,
|
|
|
|
Categories: description.Categories,
|
2023-11-29 17:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
keyIDSeqNo, encryptedDescription, err := encryptor.encryptCommunityDescription(community, descriptionToEncrypt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:18:18 +00:00
|
|
|
// Set private data and cleanup unencrypted members, chats and categories
|
2023-11-29 17:21:21 +00:00
|
|
|
description.PrivateData[keyIDSeqNo] = encryptedDescription
|
|
|
|
description.Members = make(map[string]*protobuf.CommunityMember)
|
2024-03-20 19:18:18 +00:00
|
|
|
description.ActiveMembersCount = 0
|
2023-11-29 17:21:21 +00:00
|
|
|
description.Chats = make(map[string]*protobuf.CommunityChat)
|
2024-03-20 19:18:18 +00:00
|
|
|
description.Categories = make(map[string]*protobuf.CommunityCategory)
|
2023-11-29 17:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-23 16:56:51 +00:00
|
|
|
type CommunityPrivateDataFailedToDecrypt struct {
|
|
|
|
GroupID []byte
|
|
|
|
KeyID []byte
|
|
|
|
}
|
|
|
|
|
2023-11-29 17:21:21 +00:00
|
|
|
// Decrypts members and chats
|
2024-01-23 16:56:51 +00:00
|
|
|
func decryptDescription(id types.HexBytes, encryptor DescriptionEncryptor, description *protobuf.CommunityDescription, logger *zap.Logger) ([]*CommunityPrivateDataFailedToDecrypt, error) {
|
|
|
|
if len(description.PrivateData) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var failedToDecrypt []*CommunityPrivateDataFailedToDecrypt
|
|
|
|
|
2023-11-29 17:21:21 +00:00
|
|
|
for keyIDSeqNo, encryptedDescription := range description.PrivateData {
|
2024-01-23 16:56:51 +00:00
|
|
|
decryptedDescriptionResponse, err := encryptor.decryptCommunityDescription(keyIDSeqNo, encryptedDescription)
|
|
|
|
if decryptedDescriptionResponse != nil && !decryptedDescriptionResponse.Decrypted {
|
|
|
|
failedToDecrypt = append(failedToDecrypt, &CommunityPrivateDataFailedToDecrypt{GroupID: id, KeyID: decryptedDescriptionResponse.KeyID})
|
|
|
|
}
|
2023-11-29 17:21:21 +00:00
|
|
|
if err != nil {
|
|
|
|
// ignore error, try to decrypt next data
|
|
|
|
logger.Debug("failed to decrypt community private data", zap.String("keyIDSeqNo", keyIDSeqNo), zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
2024-01-23 16:56:51 +00:00
|
|
|
decryptedDescription := decryptedDescriptionResponse.Description
|
2023-11-29 17:21:21 +00:00
|
|
|
|
2024-03-20 19:18:18 +00:00
|
|
|
if len(decryptedDescription.Members) > 0 {
|
|
|
|
description.Members = decryptedDescription.Members
|
|
|
|
}
|
|
|
|
|
|
|
|
if decryptedDescription.ActiveMembersCount > 0 {
|
|
|
|
description.ActiveMembersCount = decryptedDescription.ActiveMembersCount
|
2023-11-29 17:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for id, decryptedChannel := range decryptedDescription.Chats {
|
|
|
|
if description.Chats == nil {
|
|
|
|
description.Chats = make(map[string]*protobuf.CommunityChat)
|
|
|
|
}
|
|
|
|
|
|
|
|
if channel := description.Chats[id]; channel != nil {
|
2024-03-20 19:18:18 +00:00
|
|
|
if len(decryptedChannel.Members) > 0 {
|
2023-11-29 17:21:21 +00:00
|
|
|
channel.Members = decryptedChannel.Members
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
description.Chats[id] = decryptedChannel
|
|
|
|
}
|
|
|
|
}
|
2024-03-20 19:18:18 +00:00
|
|
|
|
|
|
|
if len(decryptedDescription.Categories) > 0 {
|
|
|
|
description.Categories = decryptedDescription.Categories
|
|
|
|
}
|
2023-11-29 17:21:21 +00:00
|
|
|
}
|
|
|
|
|
2024-01-23 16:56:51 +00:00
|
|
|
return failedToDecrypt, nil
|
2023-11-29 17:21:21 +00:00
|
|
|
}
|