-
Notifications
You must be signed in to change notification settings - Fork 8
/
peer.go
63 lines (51 loc) · 1.21 KB
/
peer.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package peer
import (
"crypto/rand"
"github.com/libp2p/go-libp2p-core/crypto"
lpeer "github.com/libp2p/go-libp2p-core/peer"
)
// Peer implements common functionalities between peer types.
// Includes: identity.
type Peer interface {
// GetPubKey returns the public key of the peer.
GetPubKey() crypto.PubKey
// GetPrivKey returns the private key.
GetPrivKey() crypto.PrivKey
// GetPeerID returns the peer ID.
GetPeerID() ID
}
// NewPeer builds a new Peer object.
// If privKey is nil, one will be generated.
func NewPeer(privKey crypto.PrivKey) (Peer, error) {
if privKey == nil {
var err error
privKey, _, err = crypto.GenerateEd25519Key(rand.Reader)
if err != nil {
return nil, err
}
}
id, err := lpeer.IDFromPrivateKey(privKey)
if err != nil {
return nil, err
}
return &peer{
privKey: privKey,
peerID: id,
}, nil
}
type peer struct {
privKey crypto.PrivKey
peerID ID
}
// GetPubKey returns the public key of the node.
func (p *peer) GetPubKey() crypto.PubKey {
return p.privKey.GetPublic()
}
// GetPrivKey returns the private key.
func (p *peer) GetPrivKey() crypto.PrivKey {
return p.privKey
}
// GetPeerID returns the peer ID.
func (p *peer) GetPeerID() ID {
return p.peerID
}