Created
September 19, 2019 05:06
-
-
Save rahullenkala/1549c491ebf15f689f1e58f143f958f4 to your computer and use it in GitHub Desktop.
Bootsrap node code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"flag" | |
"fmt" | |
"github.com/libp2p/go-libp2p" | |
crypto "github.com/libp2p/go-libp2p-crypto" | |
dht "github.com/libp2p/go-libp2p-kad-dht" | |
"github.com/multiformats/go-multiaddr" | |
mrand "math/rand" | |
"os" | |
) | |
func main() { | |
help := flag.Bool("help", false, "Display Help") | |
listenHost := flag.String("host", "0.0.0.0", "The bootstrap node host listen address\n") | |
port := flag.Int("port", 4001, "The bootstrap node listen port") | |
flag.Parse() | |
if *help { | |
fmt.Printf("This is a simple bootstrap node for kad-dht application using libp2p\n\n") | |
fmt.Printf("Usage: \n Run './bootnode'\nor Run './bootnode -host [host] -port [port]'\n") | |
os.Exit(0) | |
} | |
fmt.Printf("[*] Listening on: %s with port: %d\n", *listenHost, *port) | |
ctx := context.Background() | |
r := mrand.New(mrand.NewSource(int64(*port))) | |
// Creates a new RSA key pair for this host. | |
prvKey, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, r) | |
if err != nil { | |
panic(err) | |
} | |
// 0.0.0.0 will listen on any interface device. | |
sourceMultiAddr, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", *listenHost, *port)) | |
// libp2p.New constructs a new libp2p Host. | |
// Other options can be added here. | |
host, err := libp2p.New( | |
ctx, | |
libp2p.ListenAddrs(sourceMultiAddr), | |
libp2p.Identity(prvKey), | |
) | |
if err != nil { | |
panic(err) | |
} | |
_, err = dht.New(ctx, host) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("") | |
fmt.Printf("[*] Your Bootstrap ID Is: /ip4/%s/tcp/%v/p2p/%s\n", *listenHost, *port, host.ID().Pretty()) | |
fmt.Println("") | |
select {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment