node.go 2.57 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
9

	crypto "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
10
	"log"
11
12
)

Aviv Eyal's avatar
Aviv Eyal committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 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...
}

// create a new node with its implemented protocols
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
}

29
func (n *Node) authenticateMessage(message proto.Message, data *p2p.MessageData) bool {
30
31
32
33
34
35
36
37

	// store a temp ref to sig and remove it from data
	sign := data.Sign
	data.Sign = ""

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

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

	peerId, err := peer.IDB58Decode(data.NodeId)
	if err != nil {
Aviv Eyal's avatar
Aviv Eyal committed
47
		log.Println(err, "Failed to decode node id from base58")
48
49
50
		return false
	}

51
	return n.verifyData(bin, []byte(sign), peerId, data.NodePubKey)
52
53
}

54
func (n *Node) signProtoMessage(message proto.Message) ([]byte, error) {
Aviv Eyal's avatar
Aviv Eyal committed
55
56
57
58
59
60
61
	data, err := proto.Marshal(message)
	if err != nil {
		return nil, err
	}
	return n.signData(data)
}

62
func (n *Node) signData(data []byte) ([]byte, error) {
Aviv Eyal's avatar
Aviv Eyal committed
63
64
65
	key := n.Peerstore().PrivKey(n.ID())
	res, err := key.Sign(data)
	return res, err
66
67
}

68
// precondition: we have info about the signer peer in the local peer store
69
func (n *Node) verifyData(data []byte, signature []byte, peerId peer.ID, pubKeyData []byte) bool {
Aviv Eyal's avatar
Aviv Eyal committed
70

71
72
73
74
75
76
77
78
79
	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)
80

81
82
83
84
	if err != nil {
		log.Println(err, "Failed to extract peer id from public key")
		return false
	}
85

86
87
88
	// 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
89
90
91
92
		return false
	}

	res, err := key.Verify(data, signature)
93

Aviv Eyal's avatar
Aviv Eyal committed
94
	if err != nil {
95
		log.Println(err, "Error authenticating data")
Aviv Eyal's avatar
Aviv Eyal committed
96
97
98
99
		return false
	}

	return res
100
}