2023-07-13 17:49:19 +00:00
|
|
|
package communities
|
|
|
|
|
2024-01-23 16:56:51 +00:00
|
|
|
import (
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
)
|
2023-07-13 17:49:19 +00:00
|
|
|
|
2023-11-29 17:21:21 +00:00
|
|
|
type KeyDistributor interface {
|
|
|
|
Generate(community *Community, keyActions *EncryptionKeyActions) error
|
|
|
|
Distribute(community *Community, keyActions *EncryptionKeyActions) error
|
|
|
|
}
|
|
|
|
|
2023-07-13 17:49:19 +00:00
|
|
|
type EncryptionKeyActionType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
EncryptionKeyNone EncryptionKeyActionType = iota
|
|
|
|
EncryptionKeyAdd
|
|
|
|
EncryptionKeyRemove
|
|
|
|
EncryptionKeyRekey
|
|
|
|
EncryptionKeySendToMembers
|
|
|
|
)
|
|
|
|
|
|
|
|
type EncryptionKeyAction struct {
|
2024-01-23 16:56:51 +00:00
|
|
|
ActionType EncryptionKeyActionType
|
|
|
|
Members map[string]*protobuf.CommunityMember
|
|
|
|
RemovedMembers map[string]*protobuf.CommunityMember
|
2023-07-13 17:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type EncryptionKeyActions struct {
|
|
|
|
// community-level encryption key action
|
|
|
|
CommunityKeyAction EncryptionKeyAction
|
|
|
|
|
|
|
|
// channel-level encryption key actions
|
|
|
|
ChannelKeysActions map[string]EncryptionKeyAction // key is: chatID
|
|
|
|
}
|
|
|
|
|
|
|
|
func EvaluateCommunityEncryptionKeyActions(origin, modified *Community) *EncryptionKeyActions {
|
2023-07-17 16:40:09 +00:00
|
|
|
if origin == nil {
|
|
|
|
// `modified` is a new community, create empty `origin` community
|
|
|
|
origin = &Community{
|
|
|
|
config: &Config{
|
2023-10-26 20:04:18 +00:00
|
|
|
ID: modified.config.ID,
|
2023-07-17 16:40:09 +00:00
|
|
|
CommunityDescription: &protobuf.CommunityDescription{
|
|
|
|
Members: map[string]*protobuf.CommunityMember{},
|
|
|
|
Permissions: &protobuf.CommunityPermissions{},
|
|
|
|
Identity: &protobuf.ChatIdentity{},
|
|
|
|
Chats: map[string]*protobuf.CommunityChat{},
|
|
|
|
Categories: map[string]*protobuf.CommunityCategory{},
|
|
|
|
AdminSettings: &protobuf.CommunityAdminSettings{},
|
|
|
|
TokenPermissions: map[string]*protobuf.CommunityTokenPermission{},
|
|
|
|
CommunityTokensMetadata: []*protobuf.CommunityTokenMetadata{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 17:14:23 +00:00
|
|
|
changes := EvaluateCommunityChanges(origin, modified)
|
2023-07-13 17:49:19 +00:00
|
|
|
|
|
|
|
result := &EncryptionKeyActions{
|
|
|
|
CommunityKeyAction: *evaluateCommunityLevelEncryptionKeyAction(origin, modified, changes),
|
|
|
|
ChannelKeysActions: *evaluateChannelLevelEncryptionKeyActions(origin, modified, changes),
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func evaluateCommunityLevelEncryptionKeyAction(origin, modified *Community, changes *CommunityChanges) *EncryptionKeyAction {
|
2023-10-18 17:01:02 +00:00
|
|
|
return evaluateEncryptionKeyAction(
|
2023-10-26 20:04:18 +00:00
|
|
|
origin.Encrypted(),
|
|
|
|
modified.Encrypted(),
|
|
|
|
changes.ControlNodeChanged != nil,
|
2023-10-18 17:01:02 +00:00
|
|
|
modified.config.CommunityDescription.Members,
|
|
|
|
changes.MembersAdded,
|
|
|
|
changes.MembersRemoved,
|
|
|
|
)
|
2023-07-13 17:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func evaluateChannelLevelEncryptionKeyActions(origin, modified *Community, changes *CommunityChanges) *map[string]EncryptionKeyAction {
|
|
|
|
result := make(map[string]EncryptionKeyAction)
|
|
|
|
|
2023-06-23 10:49:26 +00:00
|
|
|
for channelID := range modified.config.CommunityDescription.Chats {
|
2023-07-13 17:49:19 +00:00
|
|
|
membersAdded := make(map[string]*protobuf.CommunityMember)
|
|
|
|
membersRemoved := make(map[string]*protobuf.CommunityMember)
|
|
|
|
|
2023-06-23 10:49:26 +00:00
|
|
|
chatChanges, ok := changes.ChatsModified[channelID]
|
2023-07-13 17:49:19 +00:00
|
|
|
if ok {
|
|
|
|
membersAdded = chatChanges.MembersAdded
|
|
|
|
membersRemoved = chatChanges.MembersRemoved
|
|
|
|
}
|
|
|
|
|
2023-10-18 17:01:02 +00:00
|
|
|
result[channelID] = *evaluateEncryptionKeyAction(
|
2023-10-26 20:04:18 +00:00
|
|
|
origin.ChannelEncrypted(channelID),
|
|
|
|
modified.ChannelEncrypted(channelID),
|
2023-10-18 17:01:02 +00:00
|
|
|
changes.ControlNodeChanged != nil,
|
2023-10-26 20:04:18 +00:00
|
|
|
modified.config.CommunityDescription.Chats[channelID].Members,
|
|
|
|
membersAdded,
|
|
|
|
membersRemoved,
|
2023-10-18 17:01:02 +00:00
|
|
|
)
|
2023-07-13 17:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2023-10-26 20:04:18 +00:00
|
|
|
func evaluateEncryptionKeyAction(originEncrypted, modifiedEncrypted, controlNodeChanged bool,
|
|
|
|
allMembers, membersAdded, membersRemoved map[string]*protobuf.CommunityMember) *EncryptionKeyAction {
|
2023-07-13 17:49:19 +00:00
|
|
|
result := &EncryptionKeyAction{
|
|
|
|
ActionType: EncryptionKeyNone,
|
|
|
|
Members: map[string]*protobuf.CommunityMember{},
|
|
|
|
}
|
|
|
|
|
|
|
|
copyMap := func(source map[string]*protobuf.CommunityMember) map[string]*protobuf.CommunityMember {
|
|
|
|
to := make(map[string]*protobuf.CommunityMember)
|
|
|
|
for pubKey, member := range source {
|
|
|
|
to[pubKey] = member
|
|
|
|
}
|
|
|
|
return to
|
|
|
|
}
|
|
|
|
|
2023-10-18 17:01:02 +00:00
|
|
|
// control node changed on closed community/channel
|
2023-10-26 20:04:18 +00:00
|
|
|
if controlNodeChanged && modifiedEncrypted {
|
2023-10-18 17:01:02 +00:00
|
|
|
result.ActionType = EncryptionKeyRekey
|
|
|
|
result.Members = copyMap(allMembers)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-10-26 20:04:18 +00:00
|
|
|
// encryption was just added
|
|
|
|
if modifiedEncrypted && !originEncrypted {
|
2023-07-13 17:49:19 +00:00
|
|
|
result.ActionType = EncryptionKeyAdd
|
|
|
|
result.Members = copyMap(allMembers)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-10-26 20:04:18 +00:00
|
|
|
// encryption was just removed
|
|
|
|
if !modifiedEncrypted && originEncrypted {
|
2023-07-13 17:49:19 +00:00
|
|
|
result.ActionType = EncryptionKeyRemove
|
|
|
|
result.Members = copyMap(allMembers)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// open community/channel does not require any actions
|
2023-10-26 20:04:18 +00:00
|
|
|
if !modifiedEncrypted {
|
2023-07-13 17:49:19 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(membersRemoved) > 0 {
|
|
|
|
result.ActionType = EncryptionKeyRekey
|
|
|
|
result.Members = copyMap(allMembers)
|
2024-01-23 16:56:51 +00:00
|
|
|
result.RemovedMembers = copyMap(membersRemoved)
|
2023-07-13 17:49:19 +00:00
|
|
|
} else if len(membersAdded) > 0 {
|
|
|
|
result.ActionType = EncryptionKeySendToMembers
|
|
|
|
result.Members = copyMap(membersAdded)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|