forked from berty/weshnet
-
Notifications
You must be signed in to change notification settings - Fork 5
/
api_multimember.go
132 lines (104 loc) · 4.44 KB
/
api_multimember.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
package weshnet
import (
"context"
"fmt"
"github.com/libp2p/go-libp2p/core/crypto"
"berty.tech/weshnet/pkg/errcode"
"berty.tech/weshnet/pkg/protocoltypes"
"berty.tech/weshnet/pkg/tyber"
)
// MultiMemberGroupCreate creates a new MultiMember group
func (s *service) MultiMemberGroupCreate(ctx context.Context, req *protocoltypes.MultiMemberGroupCreate_Request) (_ *protocoltypes.MultiMemberGroupCreate_Reply, err error) {
ctx, _, endSection := tyber.Section(ctx, s.logger, "Creating MultiMember group")
defer func() { endSection(err, "") }()
group, groupPrivateKey, err := NewGroupMultiMember()
if err != nil {
return nil, errcode.ErrCryptoKeyGeneration.Wrap(err)
}
accountGroup := s.getAccountGroup()
if accountGroup == nil {
return nil, errcode.ErrGroupMissing
}
_, err = accountGroup.MetadataStore().GroupJoin(ctx, group)
if err != nil {
return nil, errcode.ErrOrbitDBAppend.Wrap(err)
}
if err := s.secretStore.PutGroup(ctx, group); err != nil {
return nil, errcode.ErrInternal.Wrap(err)
}
err = s.activateGroup(ctx, groupPrivateKey.GetPublic(), false)
if err != nil {
return nil, errcode.ErrInternal.Wrap(fmt.Errorf("unable to activate group: %w", err))
}
cg, err := s.GetContextGroupForID(group.PublicKey)
if err != nil {
return nil, errcode.ErrOrbitDBAppend.Wrap(err)
}
_, err = cg.MetadataStore().ClaimGroupOwnership(ctx, groupPrivateKey)
if err != nil {
return nil, errcode.ErrOrbitDBAppend.Wrap(err)
}
return &protocoltypes.MultiMemberGroupCreate_Reply{
GroupPK: group.PublicKey,
}, nil
}
// MultiMemberGroupJoin joins an existing MultiMember group using an invitation
func (s *service) MultiMemberGroupJoin(ctx context.Context, req *protocoltypes.MultiMemberGroupJoin_Request) (_ *protocoltypes.MultiMemberGroupJoin_Reply, err error) {
ctx, _, endSection := tyber.Section(ctx, s.logger, "Joining MultiMember group")
defer func() { endSection(err, "") }()
accountGroup := s.getAccountGroup()
if accountGroup == nil {
return nil, errcode.ErrGroupMissing
}
if _, err := accountGroup.MetadataStore().GroupJoin(ctx, req.Group); err != nil {
return nil, errcode.ErrOrbitDBAppend.Wrap(err)
}
return &protocoltypes.MultiMemberGroupJoin_Reply{}, nil
}
// MultiMemberGroupLeave leaves a previously joined MultiMember group
func (s *service) MultiMemberGroupLeave(ctx context.Context, req *protocoltypes.MultiMemberGroupLeave_Request) (_ *protocoltypes.MultiMemberGroupLeave_Reply, err error) {
ctx, _, endSection := tyber.Section(ctx, s.logger, "Leaving MultiMember group")
defer func() { endSection(err, "") }()
pk, err := crypto.UnmarshalEd25519PublicKey(req.GroupPK)
if err != nil {
return nil, errcode.ErrDeserialization.Wrap(err)
}
accountGroup := s.getAccountGroup()
if accountGroup == nil {
return nil, errcode.ErrGroupMissing
}
_, err = accountGroup.MetadataStore().GroupLeave(ctx, pk)
if err != nil {
return nil, errcode.ErrOrbitDBAppend.Wrap(err)
}
if err := s.deactivateGroup(pk); err != nil {
return nil, errcode.ErrOrbitDBAppend.Wrap(err)
}
return &protocoltypes.MultiMemberGroupLeave_Reply{}, nil
}
// MultiMemberGroupAliasResolverDisclose sends an deviceKeystore identity proof to the group members
func (s *service) MultiMemberGroupAliasResolverDisclose(ctx context.Context, req *protocoltypes.MultiMemberGroupAliasResolverDisclose_Request) (*protocoltypes.MultiMemberGroupAliasResolverDisclose_Reply, error) {
cg, err := s.GetContextGroupForID(req.GroupPK)
if err != nil {
return nil, errcode.ErrGroupMemberUnknownGroupID.Wrap(err)
}
_, err = cg.MetadataStore().SendAliasProof(ctx)
if err != nil {
return nil, errcode.ErrOrbitDBAppend.Wrap(err)
}
return &protocoltypes.MultiMemberGroupAliasResolverDisclose_Reply{}, nil
}
// MultiMemberGroupAdminRoleGrant grants admin role to another member of the group
func (s *service) MultiMemberGroupAdminRoleGrant(context.Context, *protocoltypes.MultiMemberGroupAdminRoleGrant_Request) (*protocoltypes.MultiMemberGroupAdminRoleGrant_Reply, error) {
return nil, errcode.ErrNotImplemented
}
// MultiMemberGroupInvitationCreate creates a group invitation
func (s *service) MultiMemberGroupInvitationCreate(ctx context.Context, req *protocoltypes.MultiMemberGroupInvitationCreate_Request) (*protocoltypes.MultiMemberGroupInvitationCreate_Reply, error) {
cg, err := s.GetContextGroupForID(req.GroupPK)
if err != nil {
return nil, errcode.ErrGroupMemberUnknownGroupID.Wrap(err)
}
return &protocoltypes.MultiMemberGroupInvitationCreate_Reply{
Group: cg.Group(),
}, nil
}