forked from berty/weshnet
-
Notifications
You must be signed in to change notification settings - Fork 5
/
events.go
179 lines (145 loc) · 7.71 KB
/
events.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
package weshnet
import (
"fmt"
cid "github.com/ipfs/go-cid"
"golang.org/x/crypto/nacl/secretbox"
"google.golang.org/protobuf/proto"
ipfslog "berty.tech/go-ipfs-log"
"berty.tech/weshnet/v2/pkg/cryptoutil"
"berty.tech/weshnet/v2/pkg/errcode"
"berty.tech/weshnet/v2/pkg/protocoltypes"
)
var eventTypesMapper = map[protocoltypes.EventType]struct {
Message proto.Message
SigChecker sigChecker
}{
protocoltypes.EventType_EventTypeGroupMemberDeviceAdded: {Message: &protocoltypes.GroupMemberDeviceAdded{}, SigChecker: sigCheckerGroupMemberDeviceAdded},
protocoltypes.EventType_EventTypeGroupDeviceChainKeyAdded: {Message: &protocoltypes.GroupDeviceChainKeyAdded{}, SigChecker: sigCheckerDeviceSigned},
protocoltypes.EventType_EventTypeAccountGroupJoined: {Message: &protocoltypes.AccountGroupJoined{}, SigChecker: sigCheckerDeviceSigned},
protocoltypes.EventType_EventTypeAccountGroupLeft: {Message: &protocoltypes.AccountGroupLeft{}, SigChecker: sigCheckerDeviceSigned},
protocoltypes.EventType_EventTypeAccountContactRequestDisabled: {Message: &protocoltypes.AccountContactRequestDisabled{}, SigChecker: sigCheckerDeviceSigned},
protocoltypes.EventType_EventTypeAccountContactRequestEnabled: {Message: &protocoltypes.AccountContactRequestEnabled{}, SigChecker: sigCheckerDeviceSigned},
protocoltypes.EventType_EventTypeAccountContactRequestReferenceReset: {Message: &protocoltypes.AccountContactRequestReferenceReset{}, SigChecker: sigCheckerDeviceSigned},
protocoltypes.EventType_EventTypeAccountContactRequestOutgoingEnqueued: {Message: &protocoltypes.AccountContactRequestOutgoingEnqueued{}, SigChecker: sigCheckerDeviceSigned},
protocoltypes.EventType_EventTypeAccountContactRequestOutgoingSent: {Message: &protocoltypes.AccountContactRequestOutgoingSent{}, SigChecker: sigCheckerDeviceSigned},
protocoltypes.EventType_EventTypeAccountContactRequestIncomingReceived: {Message: &protocoltypes.AccountContactRequestIncomingReceived{}, SigChecker: sigCheckerDeviceSigned},
protocoltypes.EventType_EventTypeAccountContactRequestIncomingDiscarded: {Message: &protocoltypes.AccountContactRequestIncomingDiscarded{}, SigChecker: sigCheckerDeviceSigned},
protocoltypes.EventType_EventTypeAccountContactRequestIncomingAccepted: {Message: &protocoltypes.AccountContactRequestIncomingAccepted{}, SigChecker: sigCheckerDeviceSigned},
protocoltypes.EventType_EventTypeAccountContactBlocked: {Message: &protocoltypes.AccountContactBlocked{}, SigChecker: sigCheckerDeviceSigned},
protocoltypes.EventType_EventTypeAccountContactUnblocked: {Message: &protocoltypes.AccountContactUnblocked{}, SigChecker: sigCheckerDeviceSigned},
protocoltypes.EventType_EventTypeContactAliasKeyAdded: {Message: &protocoltypes.ContactAliasKeyAdded{}, SigChecker: sigCheckerDeviceSigned},
protocoltypes.EventType_EventTypeMultiMemberGroupAliasResolverAdded: {Message: &protocoltypes.MultiMemberGroupAliasResolverAdded{}, SigChecker: sigCheckerDeviceSigned},
protocoltypes.EventType_EventTypeMultiMemberGroupInitialMemberAnnounced: {Message: &protocoltypes.MultiMemberGroupInitialMemberAnnounced{}, SigChecker: sigCheckerGroupSigned},
protocoltypes.EventType_EventTypeMultiMemberGroupAdminRoleGranted: {Message: &protocoltypes.MultiMemberGroupAdminRoleGranted{}, SigChecker: sigCheckerDeviceSigned},
protocoltypes.EventType_EventTypeGroupMetadataPayloadSent: {Message: &protocoltypes.GroupMetadataPayloadSent{}, SigChecker: sigCheckerDeviceSigned},
protocoltypes.EventType_EventTypeGroupReplicating: {Message: &protocoltypes.GroupReplicating{}, SigChecker: sigCheckerDeviceSigned},
protocoltypes.EventType_EventTypeAccountVerifiedCredentialRegistered: {Message: &protocoltypes.AccountVerifiedCredentialRegistered{}, SigChecker: sigCheckerDeviceSigned},
}
func newEventContext(eventID cid.Cid, parentIDs []cid.Cid, g *protocoltypes.Group) *protocoltypes.EventContext {
parentIDsBytes := make([][]byte, len(parentIDs))
for i, parentID := range parentIDs {
parentIDsBytes[i] = parentID.Bytes()
}
return &protocoltypes.EventContext{
Id: eventID.Bytes(),
ParentIds: parentIDsBytes,
GroupPk: g.PublicKey,
}
}
// FIXME(gfanton): getParentsCID use a lot of ressources
// nolint:unused
func getParentsForCID(log ipfslog.Log, c cid.Cid) []cid.Cid {
if log == nil {
// TODO: this should not happen
return []cid.Cid{}
}
parent, ok := log.Get(c)
// Can't fetch parent entry
if !ok {
return []cid.Cid{}
}
nextEntries := parent.GetNext()
// Parent has only one or no parents, returning its id
if len(nextEntries) <= 1 {
return []cid.Cid{parent.GetHash()}
}
// Parent has more than one parent, returning parent entries
var ret []cid.Cid
for _, n := range nextEntries {
ret = append(ret, getParentsForCID(log, n)...)
}
return ret
}
func newGroupMetadataEventFromEntry(_ ipfslog.Log, e ipfslog.Entry, metadata *protocoltypes.GroupMetadata, event proto.Message, g *protocoltypes.Group) (*protocoltypes.GroupMetadataEvent, error) {
// TODO: if parent is a merge node we should return the next nodes of it
eventBytes, err := proto.Marshal(event)
if err != nil {
return nil, errcode.ErrCode_ErrSerialization
}
// TODO(gfanton): getParentsCID use a lot of ressources, disable it until we need it
// evtCtx := newEventContext(e.GetHash(), getParentsForCID(log, e.GetHash()), group, attachmentsCIDs)
evtCtx := newEventContext(e.GetHash(), []cid.Cid{}, g)
gme := protocoltypes.GroupMetadataEvent{
EventContext: evtCtx,
Metadata: metadata,
Event: eventBytes,
}
return &gme, nil
}
func openGroupEnvelope(g *protocoltypes.Group, envelopeBytes []byte) (*protocoltypes.GroupMetadata, proto.Message, error) {
env := &protocoltypes.GroupEnvelope{}
if err := proto.Unmarshal(envelopeBytes, env); err != nil {
return nil, nil, errcode.ErrCode_ErrInvalidInput.Wrap(err)
}
nonce, err := cryptoutil.NonceSliceToArray(env.Nonce)
if err != nil {
return nil, nil, errcode.ErrCode_ErrSerialization.Wrap(err)
}
data, ok := secretbox.Open(nil, env.Event, nonce, g.GetSharedSecret())
if !ok {
return nil, nil, errcode.ErrCode_ErrGroupMemberLogEventOpen
}
metadataEvent := &protocoltypes.GroupMetadata{}
err = proto.Unmarshal(data, metadataEvent)
if err != nil {
return nil, nil, errcode.ErrCode_TODO.Wrap(err)
}
et, ok := eventTypesMapper[metadataEvent.EventType]
if !ok {
return nil, nil, errcode.ErrCode_ErrInvalidInput.Wrap(fmt.Errorf("event type not found"))
}
payload := proto.Clone(et.Message)
if err := proto.Unmarshal(metadataEvent.Payload, payload); err != nil {
return nil, nil, errcode.ErrCode_ErrDeserialization.Wrap(err)
}
if err := et.SigChecker(g, metadataEvent, payload); err != nil {
return nil, nil, errcode.ErrCode_ErrCryptoSignatureVerification.Wrap(err)
}
return metadataEvent, payload, nil
}
func sealGroupEnvelope(g *protocoltypes.Group, eventType protocoltypes.EventType, payload proto.Message, payloadSig []byte) ([]byte, error) {
payloadBytes, err := proto.Marshal(payload)
if err != nil {
return nil, errcode.ErrCode_TODO.Wrap(err)
}
nonce, err := cryptoutil.GenerateNonce()
if err != nil {
return nil, errcode.ErrCode_ErrCryptoNonceGeneration.Wrap(err)
}
event := &protocoltypes.GroupMetadata{
EventType: eventType,
Payload: payloadBytes,
Sig: payloadSig,
ProtocolMetadata: &protocoltypes.ProtocolMetadata{},
}
eventClearBytes, err := proto.Marshal(event)
if err != nil {
return nil, errcode.ErrCode_ErrSerialization.Wrap(err)
}
eventBytes := secretbox.Seal(nil, eventClearBytes, nonce, g.GetSharedSecret())
env := &protocoltypes.GroupEnvelope{
Event: eventBytes,
Nonce: nonce[:],
}
return proto.Marshal(env)
}