forked from berty/weshnet
-
Notifications
You must be signed in to change notification settings - Fork 5
/
api_app.go
105 lines (85 loc) · 3.57 KB
/
api_app.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
package weshnet
import (
"context"
"encoding/base64"
"fmt"
"github.com/ipfs/go-cid"
"go.uber.org/zap"
"berty.tech/weshnet/pkg/errcode"
"berty.tech/weshnet/pkg/protocoltypes"
"berty.tech/weshnet/pkg/tyber"
)
func (s *service) AppMetadataSend(ctx context.Context, req *protocoltypes.AppMetadataSend_Request) (_ *protocoltypes.AppMetadataSend_Reply, err error) {
ctx, _, endSection := tyber.Section(ctx, s.logger, fmt.Sprintf("Sending app metadata to group %s", base64.RawURLEncoding.EncodeToString(req.GroupPK)))
defer func() { endSection(err, "") }()
gc, err := s.GetContextGroupForID(req.GroupPK)
if err != nil {
return nil, errcode.ErrGroupMissing.Wrap(err)
}
tyberLogGroupContext(ctx, s.logger, gc)
op, err := gc.MetadataStore().SendAppMetadata(ctx, req.Payload)
if err != nil {
return nil, errcode.ErrOrbitDBAppend.Wrap(err)
}
return &protocoltypes.AppMetadataSend_Reply{CID: op.GetEntry().GetHash().Bytes()}, nil
}
func (s *service) AppMessageSend(ctx context.Context, req *protocoltypes.AppMessageSend_Request) (_ *protocoltypes.AppMessageSend_Reply, err error) {
ctx, _, endSection := tyber.Section(ctx, s.logger, fmt.Sprintf("Sending message to group %s", base64.RawURLEncoding.EncodeToString(req.GroupPK)))
defer func() { endSection(err, "") }()
gc, err := s.GetContextGroupForID(req.GroupPK)
if err != nil {
return nil, errcode.ErrGroupMissing.Wrap(err)
}
tyberLogGroupContext(ctx, s.logger, gc)
op, err := gc.MessageStore().AddMessage(ctx, req.Payload)
if err != nil {
return nil, errcode.ErrOrbitDBAppend.Wrap(err)
}
return &protocoltypes.AppMessageSend_Reply{CID: op.GetEntry().GetHash().Bytes()}, nil
}
// OutOfStoreReceive parses a payload received outside a synchronized store
func (s *service) OutOfStoreReceive(ctx context.Context, request *protocoltypes.OutOfStoreReceive_Request) (*protocoltypes.OutOfStoreReceive_Reply, error) {
outOfStoreMessage, group, clearPayload, alreadyDecrypted, err := s.secretStore.OpenOutOfStoreMessage(ctx, request.Payload)
if err != nil {
return nil, errcode.ErrCryptoDecrypt.Wrap(err)
}
return &protocoltypes.OutOfStoreReceive_Reply{
Message: outOfStoreMessage,
Cleartext: clearPayload,
GroupPublicKey: group.PublicKey,
AlreadyReceived: alreadyDecrypted,
}, nil
}
// OutOfStoreSeal creates a payload of a message present in store to be sent outside a synchronized store
func (s *service) OutOfStoreSeal(ctx context.Context, request *protocoltypes.OutOfStoreSeal_Request) (*protocoltypes.OutOfStoreSeal_Reply, error) {
gc, err := s.GetContextGroupForID(request.GroupPublicKey)
if err != nil {
return nil, err
}
_, c, err := cid.CidFromBytes(request.CID)
if err != nil {
return nil, errcode.ErrInvalidInput.Wrap(err)
}
sealedMessageEnvelope, err := gc.messageStore.GetOutOfStoreMessageEnvelope(ctx, c)
if err != nil {
return nil, errcode.ErrInternal.Wrap(err)
}
sealedMessageEnvelopeBytes, err := sealedMessageEnvelope.Marshal()
if err != nil {
return nil, errcode.ErrSerialization.Wrap(err)
}
return &protocoltypes.OutOfStoreSeal_Reply{
Encrypted: sealedMessageEnvelopeBytes,
}, nil
}
func tyberLogGroupContext(ctx context.Context, logger *zap.Logger, gc *GroupContext) {
memberPK, err := gc.MemberPubKey().Raw()
if err != nil {
memberPK = []byte{}
}
logger.Debug("Got group context", tyber.FormatStepLogFields(ctx, []tyber.Detail{
{Name: "GroupType", Description: gc.Group().GetGroupType().String()},
{Name: "GroupPK", Description: base64.RawURLEncoding.EncodeToString(gc.Group().PublicKey)},
{Name: "MemberPK", Description: base64.RawURLEncoding.EncodeToString(memberPK)},
})...)
}