@@ -37,7 +37,6 @@ import (
37
37
"github.com/status-im/status-go/protocol/communities"
38
38
"github.com/status-im/status-go/protocol/communities/token"
39
39
"github.com/status-im/status-go/protocol/discord"
40
- "github.com/status-im/status-go/protocol/encryption"
41
40
"github.com/status-im/status-go/protocol/protobuf"
42
41
"github.com/status-im/status-go/protocol/requests"
43
42
"github.com/status-im/status-go/protocol/transport"
@@ -511,6 +510,10 @@ func (m *Messenger) Communities() ([]*communities.Community, error) {
511
510
return m .communitiesManager .All ()
512
511
}
513
512
513
+ func (m * Messenger ) ControlledCommunities () ([]* communities.Community , error ) {
514
+ return m .communitiesManager .Controlled ()
515
+ }
516
+
514
517
func (m * Messenger ) JoinedCommunities () ([]* communities.Community , error ) {
515
518
return m .communitiesManager .Joined ()
516
519
}
@@ -2302,9 +2305,6 @@ func (m *Messenger) ImportCommunity(ctx context.Context, key *ecdsa.PrivateKey)
2302
2305
return nil , err
2303
2306
}
2304
2307
2305
- // TODO Init hash ratchet for community
2306
- _ , err = m .encryptor .GenerateHashRatchetKey (community .ID ())
2307
-
2308
2308
if err != nil {
2309
2309
return nil , err
2310
2310
}
@@ -5733,33 +5733,8 @@ func chunkAttachmentsByByteSize(slice []*protobuf.DiscordMessageAttachment, maxF
5733
5733
return chunks
5734
5734
}
5735
5735
5736
- // GetCurrentKeyForGroup returns the latest key timestampID belonging to a key group
5737
- func (m * Messenger ) GetCurrentKeyForGroup (groupID []byte ) (* encryption.HashRatchetKeyCompatibility , error ) {
5738
- return m .sender .GetCurrentKeyForGroup (groupID )
5739
- }
5740
-
5741
- // RekeyCommunity takes a communities.Community.config.ID and triggers a force rekey event for that community
5742
- func (m * Messenger ) RekeyCommunity (cID types.HexBytes ) error {
5743
- // Get the community as the member list could have changed
5744
- c , err := m .GetCommunityByID (cID )
5745
- if err != nil {
5746
- return err
5747
- }
5748
-
5749
- // RekeyCommunity
5750
- return m .communitiesKeyDistributor .Rekey (c )
5751
- }
5752
-
5753
- // NOTE: disabling rekey loop as it rekeys too aggressively
5754
-
5755
- var enableRekeyLoop = false
5756
-
5757
5736
// startCommunityRekeyLoop creates a 5-minute ticker and starts a routine that attempts to rekey every community every tick
5758
5737
func (m * Messenger ) startCommunityRekeyLoop () {
5759
- if ! enableRekeyLoop {
5760
- return
5761
- }
5762
-
5763
5738
logger := m .logger .Named ("CommunityRekeyLoop" )
5764
5739
var d time.Duration
5765
5740
if m .communitiesManager .RekeyInterval != 0 {
@@ -5777,7 +5752,7 @@ func (m *Messenger) startCommunityRekeyLoop() {
5777
5752
for {
5778
5753
select {
5779
5754
case <- ticker .C :
5780
- m .rekeyAllCommunities (logger )
5755
+ m .rekeyCommunities (logger )
5781
5756
case <- m .quit :
5782
5757
ticker .Stop ()
5783
5758
logger .Debug ("CommunityRekeyLoop stopped" )
@@ -5787,47 +5762,60 @@ func (m *Messenger) startCommunityRekeyLoop() {
5787
5762
}()
5788
5763
}
5789
5764
5790
- // rekeyAllCommunities attempts to rekey every community in persistence.
5791
- // A community will be rekeyed if it meets all the following criteria:
5792
- // - Community.IsAdmin()
5793
- // - Community.Encrypted()
5794
- // - Community.RekeyedAt().Add(rki).Before(time.Now()) where rki is a defined rekey interval
5795
- func (m * Messenger ) rekeyAllCommunities (logger * zap.Logger ) {
5796
- // Determine the rekey interval, if the value is not set as a property of m.communitiesManager
5797
- // default to one hour
5765
+ // rekeyCommunities loops over controlled communities and rekeys if rekey interval elapsed
5766
+ func (m * Messenger ) rekeyCommunities (logger * zap.Logger ) {
5798
5767
// TODO in future have a community level rki rather than a global rki
5799
- /*
5800
- var rki time.Duration
5801
- if m.communitiesManager.RekeyInterval == 0 {
5802
- rki = time.Hour
5803
- } else {
5804
- rki = m.communitiesManager.RekeyInterval
5805
- }*/
5768
+ var rekeyInterval time.Duration
5769
+ if m .communitiesManager .RekeyInterval == 0 {
5770
+ rekeyInterval = 48 * time .Hour
5771
+ } else {
5772
+ rekeyInterval = m .communitiesManager .RekeyInterval
5773
+ }
5806
5774
5807
- // Get and loop over all communities in persistence
5808
- cs , err := m .Communities ()
5775
+ shouldRekey := func (hashRatchetGroupID []byte ) bool {
5776
+ key , err := m .sender .GetCurrentKeyForGroup (hashRatchetGroupID )
5777
+ if err != nil {
5778
+ logger .Error ("failed to get current hash ratchet key" , zap .Error (err ))
5779
+ return false
5780
+ }
5781
+
5782
+ keyDistributedAt := time .UnixMilli (int64 (key .Timestamp ))
5783
+ return time .Now ().After (keyDistributedAt .Add (rekeyInterval ))
5784
+ }
5785
+
5786
+ controlledCommunities , err := m .ControlledCommunities ()
5809
5787
if err != nil {
5810
5788
logger .Error ("error getting communities" , zap .Error (err ))
5811
5789
return
5812
5790
}
5813
- for _ , c := range cs {
5814
- if err != nil {
5815
- logger .Error ("error getting current keyTimestampID for community" , zap .Error (err ), zap .Binary ("community ID" , c .ID ()))
5816
- continue
5791
+
5792
+ for _ , c := range controlledCommunities {
5793
+ keyActions := & communities.EncryptionKeyActions {
5794
+ CommunityKeyAction : communities.EncryptionKeyAction {},
5795
+ ChannelKeysActions : map [string ]communities.EncryptionKeyAction {},
5817
5796
}
5818
5797
5819
- // TODO add functionality to encryptor.go that compares the timestamps and returns a bool
5820
- // c.RekeyedAt().Add(rki).Before(time.Now())
5821
- // keyTimestampID + rki < time.Now()
5822
- // Just using the vars that will be used later
5798
+ if c .Encrypted () && shouldRekey (c .ID ()) {
5799
+ keyActions .CommunityKeyAction = communities.EncryptionKeyAction {
5800
+ ActionType : communities .EncryptionKeyRekey ,
5801
+ Members : c .Members (),
5802
+ }
5803
+ }
5823
5804
5824
- if c .IsControlNode () && c .Encrypted () { // && c.RekeyedAt().Add(rki).Before(time.Now())
5825
- err := m .RekeyCommunity (c .ID ())
5826
- if err != nil {
5827
- logger .Error ("error sending rekey message" , zap .Error (err ), zap .Binary ("community ID" , c .ID ()))
5828
- continue
5805
+ for channelID , channel := range c .Chats () {
5806
+ if c .ChannelEncrypted (channelID ) && shouldRekey ([]byte (c .IDString ()+ channelID )) {
5807
+ keyActions .ChannelKeysActions [channelID ] = communities.EncryptionKeyAction {
5808
+ ActionType : communities .EncryptionKeyRekey ,
5809
+ Members : channel .Members ,
5810
+ }
5829
5811
}
5830
5812
}
5813
+
5814
+ err = m .communitiesKeyDistributor .Distribute (c , keyActions )
5815
+ if err != nil {
5816
+ logger .Error ("failed to rekey community" , zap .Error (err ), zap .String ("community ID" , c .IDString ()))
5817
+ continue
5818
+ }
5831
5819
}
5832
5820
}
5833
5821
0 commit comments