node.go 3.87 KB
Newer Older
1
2
3
package main

import (
Aviv Eyal's avatar
Aviv Eyal committed
4
	p2p "github.com/avive/go-libp2p/examples/multipro/pb"
Aviv Eyal's avatar
Aviv Eyal committed
5
	"github.com/gogo/protobuf/proto"
6
	host "gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host"
Aviv Eyal's avatar
Aviv Eyal committed
7
	peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
8
	crypto "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
9
	"log"
Aviv Eyal's avatar
Aviv Eyal committed
10
	"time"
11
12
)

Aviv Eyal's avatar
Aviv Eyal committed
13
14
15
16
17
18
19
20
// Node type - a p2p host implementing one or more p2p protocols
type Node struct {
	host.Host     // lib-p2p host
	*PingProtocol // ping protocol impl
	*EchoProtocol // echo protocol impl
	// add other protocols here...
}

Aviv Eyal's avatar
Aviv Eyal committed
21
// Create a new node with its implemented protocols
Aviv Eyal's avatar
Aviv Eyal committed
22
23
24
25
26
27
28
func NewNode(host host.Host, done chan bool) *Node {
	node := &Node{Host: host}
	node.PingProtocol = NewPingProtocol(node, done)
	node.EchoProtocol = NewEchoProtocol(node, done)
	return node
}

Aviv Eyal's avatar
Aviv Eyal committed
29
30
31
// Authenticate incoming p2p message
// message: a protobufs go data object
// data: common p2p message data
32
func (n *Node) authenticateMessage(message proto.Message, data *p2p.MessageData) bool {
33

Aviv Eyal's avatar
Aviv Eyal committed
34
	// store a temp ref to signature and remove it from message data
35
36
37
	sign := data.Sign
	data.Sign = ""

Aviv Eyal's avatar
Aviv Eyal committed
38
	// marshall data without the signature to protobufs3 binary format
39
40
	bin, err := proto.Marshal(message)
	if err != nil {
Aviv Eyal's avatar
Aviv Eyal committed
41
		log.Println(err, "failed to marshal pb message")
42
43
44
45
46
47
		return false
	}

	// restore sig in message data (for possible future use)
	data.Sign = sign

Aviv Eyal's avatar
Aviv Eyal committed
48
	// restore peer id binary format from base58 encoded node id data
49
50
	peerId, err := peer.IDB58Decode(data.NodeId)
	if err != nil {
Aviv Eyal's avatar
Aviv Eyal committed
51
		log.Println(err, "Failed to decode node id from base58")
52
53
54
		return false
	}

Aviv Eyal's avatar
Aviv Eyal committed
55
56
	// verify the data was authored by the signing peer identified by the public key
	// and signature included in the message
57
	return n.verifyData(bin, []byte(sign), peerId, data.NodePubKey)
58
59
}

Aviv Eyal's avatar
Aviv Eyal committed
60
// sign an outgoing p2p message payload
61
func (n *Node) signProtoMessage(message proto.Message) ([]byte, error) {
Aviv Eyal's avatar
Aviv Eyal committed
62
63
64
65
66
67
68
	data, err := proto.Marshal(message)
	if err != nil {
		return nil, err
	}
	return n.signData(data)
}

Aviv Eyal's avatar
Aviv Eyal committed
69
// sign binary data using the local node's private key
70
func (n *Node) signData(data []byte) ([]byte, error) {
Aviv Eyal's avatar
Aviv Eyal committed
71
72
73
	key := n.Peerstore().PrivKey(n.ID())
	res, err := key.Sign(data)
	return res, err
74
75
}

Aviv Eyal's avatar
Aviv Eyal committed
76
77
78
79
80
// Verify incoming p2p message data integrity
// data: data to verify
// signature: author signature provided in the message payload
// peerId: author peer id from the message payload
// pubKeyData: author public key from the message payload
81
func (n *Node) verifyData(data []byte, signature []byte, peerId peer.ID, pubKeyData []byte) bool {
82
83
84
85
86
87
88
89
	key, err := crypto.UnmarshalPublicKey(pubKeyData)
	if err != nil {
		log.Println(err, "Failed to extract key from message key data")
		return false
	}

	// extract node id from the provided public key
	idFromKey, err := peer.IDFromPublicKey(key)
90

91
92
93
94
	if err != nil {
		log.Println(err, "Failed to extract peer id from public key")
		return false
	}
95

96
97
98
	// verify that message author node id matches the provided public key
	if idFromKey != peerId {
		log.Println(err, "Node id and provided public key mismatch")
Aviv Eyal's avatar
Aviv Eyal committed
99
100
101
102
103
		return false
	}

	res, err := key.Verify(data, signature)
	if err != nil {
104
		log.Println(err, "Error authenticating data")
Aviv Eyal's avatar
Aviv Eyal committed
105
106
107
108
		return false
	}

	return res
109
}
Aviv Eyal's avatar
Aviv Eyal committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129

// helper method - generate message data shared between all node's p2p protocols
// messageId: unique for requests, copied from request for responses
func (n *Node) NewMessageData(messageId string, gossip bool) *p2p.MessageData {

	// Add protobufs bin data for message author public key
	// this is useful for authenticating  messages forwarded by a node authored by another node
	nodePubKey, err := n.Peerstore().PubKey(n.ID()).Bytes()

	if err != nil {
		panic("Failed to get public key for sender from local peer store.")
	}

	return &p2p.MessageData{ClientVersion: clientVersion,
		NodeId:     peer.IDB58Encode(n.ID()),
		NodePubKey: nodePubKey,
		Timestamp:  time.Now().Unix(),
		Id:         messageId,
		Gossip:     gossip}
}