-
Notifications
You must be signed in to change notification settings - Fork 14
/
checks.go
48 lines (45 loc) · 1.49 KB
/
checks.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
package nodespace
import (
"context"
"github.com/anyproto/any-sync/commonspace/spacesyncproto"
"github.com/anyproto/any-sync/coordinator/coordinatorproto"
"github.com/anyproto/any-sync/net/peer"
"github.com/anyproto/any-sync/nodeconf"
"github.com/gogo/protobuf/proto"
"go.uber.org/zap"
)
// checkResponsible returns err if we are connecting with client, and we are not responsible for the space
func checkResponsible(ctx context.Context, confService nodeconf.Service, spaceId string) (err error) {
peerId, err := peer.CtxPeerId(ctx)
if err != nil {
return
}
isClient := len(confService.NodeTypes(peerId)) == 0
if isClient && !confService.IsResponsible(spaceId) {
return spacesyncproto.ErrPeerIsNotResponsible
}
return
}
func checkReceipt(ctx context.Context, confService nodeconf.Service, spaceId string, credential []byte) (err error) {
accountMarshalled, err := peer.CtxIdentity(ctx)
if err != nil {
return
}
peerId, err := peer.CtxPeerId(ctx)
if err != nil {
return
}
receipt := &coordinatorproto.SpaceReceiptWithSignature{}
err = proto.Unmarshal(credential, receipt)
if err != nil {
log.Debug("space validation failed", zap.Error(err))
return spacesyncproto.ErrReceiptInvalid
}
// checking if the receipt is valid and properly signed
err = coordinatorproto.CheckReceipt(peerId, spaceId, accountMarshalled, confService.Configuration().NetworkId, receipt)
if err != nil {
log.Debug("space validation failed", zap.Error(err))
return spacesyncproto.ErrReceiptInvalid
}
return nil
}