-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhost.go
More file actions
34 lines (29 loc) · 739 Bytes
/
host.go
File metadata and controls
34 lines (29 loc) · 739 Bytes
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
package main
import (
"context"
"crypto/rand"
"log"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/host"
"github.com/multiformats/go-multiaddr"
)
func NewHost(ctx context.Context, config Config) host.Host {
priv, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, rand.Reader)
if err != nil {
log.Fatal(err)
}
h, err := libp2p.New(ctx,
libp2p.ListenAddrs([]multiaddr.Multiaddr(config.ListenAddresses)...),
libp2p.Identity(priv),
)
if err != nil {
log.Fatal(err)
}
log.Printf("Host ID: %s", h.ID().Pretty())
log.Printf("Connect to me on:")
for _, addr := range h.Addrs() {
log.Printf(" - %s/p2p/%s", addr, h.ID().Pretty())
}
return h
}