@@ -20,6 +20,7 @@ import (
20
20
yamux "github.com/whyrusleeping/go-smux-yamux"
21
21
)
22
22
23
+ // Config describes a set of settings for a libp2p node
23
24
type Config struct {
24
25
Transports []transport.Transport
25
26
Muxer mux.Transport
@@ -30,11 +31,18 @@ type Config struct {
30
31
Reporter metrics.Reporter
31
32
}
32
33
33
- func Construct (ctx context.Context , cfg * Config ) (host.Host , error ) {
34
+ func New (ctx context.Context ) (host.Host , error ) {
35
+ return NewWithCfg (ctx , DefaultConfig ())
36
+ }
37
+
38
+ // Construct instantiates a libp2p host using information from the given
39
+ // config. `nil` may be passed to use default options.
40
+ func NewWithCfg (ctx context.Context , cfg * Config ) (host.Host , error ) {
34
41
if cfg == nil {
35
42
cfg = DefaultConfig ()
36
43
}
37
44
45
+ // If no key was given, generate a random 2048 bit RSA key
38
46
if cfg .PeerKey == nil {
39
47
priv , _ , err := crypto .GenerateKeyPairWithReader (crypto .RSA , 2048 , rand .Reader )
40
48
if err != nil {
@@ -49,6 +57,7 @@ func Construct(ctx context.Context, cfg *Config) (host.Host, error) {
49
57
return nil , err
50
58
}
51
59
60
+ // Create a new blank peerstore if none was passed in
52
61
ps := cfg .Peerstore
53
62
if ps == nil {
54
63
ps = pstore .NewPeerstore ()
@@ -70,14 +79,17 @@ func Construct(ctx context.Context, cfg *Config) (host.Host, error) {
70
79
func DefaultMuxer () mux.Transport {
71
80
// Set up stream multiplexer
72
81
tpt := msmux .NewBlankTransport ()
82
+
83
+ // By default, support yamux and multiplex
73
84
tpt .AddTransport ("/yamux/1.0.0" , yamux .DefaultTransport )
74
85
tpt .AddTransport ("/mplex/6.3.0" , mplex .DefaultTransport )
86
+
75
87
return tpt
76
88
}
77
89
78
90
func DefaultConfig () * Config {
79
- // Create a multiaddress
80
- addr , err := ma .NewMultiaddr ("/ip4/127 .0.0.1 /tcp/0" )
91
+ // Create a multiaddress that listens on a random port on all interfaces
92
+ addr , err := ma .NewMultiaddr ("/ip4/0 .0.0.0 /tcp/0" )
81
93
if err != nil {
82
94
panic (err )
83
95
}
0 commit comments