forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_group.go
197 lines (154 loc) · 4.11 KB
/
service_group.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
package bertyprotocol
import (
"fmt"
"berty.tech/berty/v2/go/pkg/bertytypes"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/go-orbit-db/stores"
"github.com/libp2p/go-libp2p-core/crypto"
)
func (s *service) indexGroups() error {
s.lock.Lock()
defer s.lock.Unlock()
groups := s.accountGroup.MetadataStore().ListMultiMemberGroups()
for _, g := range groups {
if _, ok := s.groups[string(g.PublicKey)]; ok {
continue
}
s.groups[string(g.PublicKey)] = g
}
contacts := s.accountGroup.MetadataStore().ListContactsByStatus(
bertytypes.ContactStateToRequest,
bertytypes.ContactStateReceived,
bertytypes.ContactStateAdded,
bertytypes.ContactStateRemoved,
bertytypes.ContactStateDiscarded,
bertytypes.ContactStateBlocked,
)
for _, contact := range contacts {
if _, ok := s.groups[string(contact.PK)]; ok {
continue
}
cPK, err := contact.GetPubKey()
if err != nil {
return errcode.TODO.Wrap(err)
}
sk, err := s.deviceKeystore.ContactGroupPrivKey(cPK)
if err != nil {
return errcode.ErrCryptoKeyGeneration.Wrap(err)
}
g, err := getGroupForContact(sk)
if err != nil {
return errcode.ErrOrbitDBOpen.Wrap(err)
}
s.groups[string(g.PublicKey)] = g
}
return nil
}
func (s *service) getContactGroup(key crypto.PubKey) (*bertytypes.Group, error) {
sk, err := s.deviceKeystore.ContactGroupPrivKey(key)
if err != nil {
return nil, errcode.ErrCryptoKeyGeneration.Wrap(err)
}
g, err := getGroupForContact(sk)
if err != nil {
return nil, errcode.ErrOrbitDBOpen.Wrap(err)
}
return g, nil
}
func (s *service) getGroupForPK(pk crypto.PubKey) (*bertytypes.Group, error) {
id, err := pk.Raw()
if err != nil {
return nil, errcode.ErrSerialization.Wrap(err)
}
s.lock.Lock()
g, ok := s.groups[string(id)]
s.lock.Unlock()
if ok {
return g, nil
}
if err = s.indexGroups(); err != nil {
return nil, errcode.TODO.Wrap(err)
}
s.lock.Lock()
g, ok = s.groups[string(id)]
s.lock.Unlock()
if ok {
return g, nil
}
return nil, errcode.ErrMissingInput
}
func (s *service) deactivateGroup(pk crypto.PubKey) error {
id, err := pk.Raw()
if err != nil {
return errcode.ErrSerialization.Wrap(err)
}
cg, err := s.getContextGroupForID(id)
if err != nil || cg == nil {
return nil
}
if cg.Group().GroupType == bertytypes.GroupTypeAccount {
return errcode.ErrInvalidInput.Wrap(fmt.Errorf("can't deactivate deviceKeystore group"))
}
s.lock.Lock()
defer s.lock.Unlock()
delete(s.groups, string(id))
return nil
}
func (s *service) activateGroup(pk crypto.PubKey) error {
id, err := pk.Raw()
if err != nil {
return errcode.ErrSerialization.Wrap(err)
}
cg, err := s.getContextGroupForID(id)
if err == nil && cg != nil {
return nil
}
g, err := s.getGroupForPK(pk)
if err != nil {
return errcode.TODO.Wrap(err)
}
s.lock.Lock()
defer s.lock.Unlock()
switch g.GroupType {
case bertytypes.GroupTypeContact, bertytypes.GroupTypeMultiMember:
cg, err := s.odb.OpenGroup(s.ctx, g, nil)
if err != nil {
return errcode.TODO.Wrap(err)
}
err = ActivateGroupContext(s.ctx, cg)
if err != nil {
return errcode.TODO.Wrap(err)
}
s.openedGroups[string(id)] = cg
go func() {
for e := range cg.metadataStore.Subscribe(s.ctx) {
if evt, ok := e.(*stores.EventNewPeer); ok {
s.ipfsCoreAPI.ConnMgr().TagPeer(evt.Peer, fmt.Sprintf("grp_%s", string(id)), 42)
}
}
}()
go func() {
for e := range cg.messageStore.Subscribe(s.ctx) {
if evt, ok := e.(*stores.EventNewPeer); ok {
s.ipfsCoreAPI.ConnMgr().TagPeer(evt.Peer, fmt.Sprintf("grp_%s", string(id)), 42)
}
}
}()
return nil
case bertytypes.GroupTypeAccount:
return errcode.ErrInternal.Wrap(fmt.Errorf("deviceKeystore group should already be opened"))
}
return errcode.ErrInternal.Wrap(fmt.Errorf("unknown group type"))
}
func (s *service) getContextGroupForID(id []byte) (*groupContext, error) {
if len(id) == 0 {
return nil, errcode.ErrInternal.Wrap(fmt.Errorf("no group id provided"))
}
s.lock.Lock()
defer s.lock.Unlock()
cg, ok := s.openedGroups[string(id)]
if ok {
return cg, nil
}
return nil, errcode.ErrInternal.Wrap(fmt.Errorf("unknown group or not activated yet"))
}