node.go 952 Bytes
Newer Older
1
2
3
4
package main

import (
	host "gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host"
Aviv Eyal's avatar
Aviv Eyal committed
5
	peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
6
7
)

Aviv Eyal's avatar
Aviv Eyal committed
8
9
10
11
func (n Node) signData(data []byte) ([]byte, error) {
	key := n.Peerstore().PrivKey(n.ID())
	res, err := key.Sign(data)
	return res, err
12
13
}

Aviv Eyal's avatar
Aviv Eyal committed
14
15
16
17
func (n Node) verifyData(data []byte, signature []byte, signerHostId peer.ID) bool {
	key := n.Peerstore().PubKey(signerHostId)
	res, err := key.Verify(data, signature)
	return res == true && err == nil
18
19
20
21
}

// Node type - implements one or more p2p protocols
type Node struct {
Aviv Eyal's avatar
Aviv Eyal committed
22
23
24
	host.Host     // lib-p2p host
	*PingProtocol // ping protocol impl
	*EchoProtocol // echo protocol impl
25
26
}

Aviv Eyal's avatar
Aviv Eyal committed
27
// create a new node with its implemented protocols
28
func NewNode(host host.Host, done chan bool) *Node {
Aviv Eyal's avatar
Aviv Eyal committed
29
30
31
32
	node := &Node{Host: host}
	node.PingProtocol = NewPingProtocol(node, done)
	node.EchoProtocol = NewEchoProtocol(node, done)
	return node
33
}