forked from libp2p/go-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhost.go
47 lines (37 loc) · 1.03 KB
/
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
40
41
42
43
44
45
46
47
package main
import (
"context"
"crypto/rand"
"fmt"
libp2p "github.com/libp2p/go-libp2p"
crypto "github.com/libp2p/go-libp2p-crypto"
)
func main() {
// The context governs the lifetime of the libp2p node
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// To construct a simple host with all the default settings, just use `New`
h, err := libp2p.New(ctx)
if err != nil {
panic(err)
}
fmt.Printf("Hello World, my hosts ID is %s\n", h.ID())
// If you want more control over the configuration, you can specify some
// options to the constructor
// Set your own keypair
priv, _, err := crypto.GenerateEd25519Key(rand.Reader)
if err != nil {
panic(err)
}
h2, err := libp2p.New(ctx,
// Use your own created keypair
libp2p.Identity(priv),
// Set your own listen address
// The config takes an array of addresses, specify as many as you want.
libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/9000"),
)
if err != nil {
panic(err)
}
fmt.Printf("Hello World, my second hosts ID is %s\n", h2.ID())
}