master
go 24 lines 741 Bytes
Raw
1 package libp2p
2
3 import (
4 "fmt"
5
6 "github.com/libp2p/go-libp2p"
7 "github.com/libp2p/go-libp2p/core/host"
8 "github.com/libp2p/go-libp2p/core/peer"
9 "github.com/libp2p/go-libp2p/core/peerstore"
10 )
11
12 type HostOption func(id peer.ID, ps peerstore.Peerstore, options ...libp2p.Option) (host.Host, error)
13
14 var DefaultHostOption HostOption = constructPeerHost
15
16 // isolates the complex initialization steps
17 func constructPeerHost(id peer.ID, ps peerstore.Peerstore, options ...libp2p.Option) (host.Host, error) {
18 pkey := ps.PrivKey(id)
19 if pkey == nil {
20 return nil, fmt.Errorf("missing private key for node ID: %s", id)
21 }
22 options = append([]libp2p.Option{libp2p.Identity(pkey), libp2p.Peerstore(ps)}, options...)
23 return libp2p.New(options...)
24 }