Newer
Older
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
package gossip
import (
"crypto/ed25519"
"github.com/golang/protobuf/proto"
"github.com/iotaledger/autopeering-sim/peer"
pb "github.com/iotaledger/goshimmer/packages/gossip/proto"
)
type Local struct {
id peer.ID
key peer.PrivateKey
}
type Protocol struct {
version uint32
local *Local
mgr *Manager
neighbors map[string]*peer.Peer
}
func sendRequest(p *Protocol, to *peer.Peer) error {
msg := &pb.ConnectionRequest{
Version: p.version,
From: p.local.id.String(),
To: to.ID().String(),
Timestamp: 1,
}
return nil
}
func verifyRequest(p *Protocol, msg *pb.ConnectionRequest, signature []byte) error {
requester, ok := p.neighbors[msg.GetFrom()]
if !ok {
return errSender
}
if msg.GetVersion() != p.version {
return errVersion
}
if msg.GetTo() != p.local.id.String() {
return errRecipient
}
msgBytes, err := proto.Marshal(msg)
if err != nil {
return err
}
if !ed25519.Verify(ed25519.PublicKey(requester.PublicKey()), msgBytes, signature) {
return errSignature
}
return nil
}
func signRequest(key peer.PrivateKey, msg *pb.ConnectionRequest) (signature []byte, err error) {
msgByte, err := proto.Marshal(msg)
if err != nil {
return signature, err
}
return ed25519.Sign(ed25519.PrivateKey(key), msgByte), nil
}