-
Notifications
You must be signed in to change notification settings - Fork 1
/
host.go
39 lines (32 loc) · 863 Bytes
/
host.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
package main
import (
"context"
"crypto/rand"
"fmt"
"io"
mrand "math/rand"
"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, seed int64, port int) (host.Host, error) {
// If the seed is zero, use real cryptographic randomness. Otherwise, use a
// deterministic randomness source to make generated keys stay the same
// across multiple runs
var r io.Reader
if seed == 0 {
r = rand.Reader
} else {
r = mrand.New(mrand.NewSource(seed))
}
priv, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, r)
if err != nil {
return nil, err
}
addr, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port))
return libp2p.New(
libp2p.ListenAddrs(addr),
libp2p.Identity(priv),
)
}