diff --git a/examples/echo/main.go b/examples/echo/main.go index cc37453da3f8416e73d525a1e3a49788385c5542..d0e704f932e387dacd74261f97a26b871673148d 100644 --- a/examples/echo/main.go +++ b/examples/echo/main.go @@ -12,6 +12,7 @@ import ( mrand "math/rand" golog "github.com/ipfs/go-log" + libp2p "github.com/libp2p/go-libp2p" crypto "github.com/libp2p/go-libp2p-crypto" host "github.com/libp2p/go-libp2p-host" net "github.com/libp2p/go-libp2p-net" @@ -69,28 +70,13 @@ func makeBasicHost(listenPort int, secio bool, randseed int64) (host.Host, error ps.AddPubKey(pid, pub) } - // Set up stream multiplexer - tpt := msmux.NewBlankTransport() - tpt.AddTransport("/yamux/1.0.0", yamux.DefaultTransport) - - // Create swarm (implements libP2P Network) - swrm, err := swarm.NewSwarmWithProtector( - context.Background(), - []ma.Multiaddr{addr}, - pid, - ps, - nil, - tpt, - nil, - ) + basicHost, err := libp2p.NewWithCfg(ctx, &libp2p.Config{ + ListenAddrs: []ma.Multiaddr{addr}, + }) if err != nil { return nil, err } - netw := (*swarm.Network)(swrm) - - basicHost := bhost.New(netw) - // Build host multiaddress hostAddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ipfs/%s", basicHost.ID().Pretty())) diff --git a/examples/libp2p-host/host.go b/examples/libp2p-host/host.go index 9c028206ec08a2e7312661e70a771f92b1d180d0..b0b53d8bbf26ef612b0506f91d3dd492f5dc9331 100644 --- a/examples/libp2p-host/host.go +++ b/examples/libp2p-host/host.go @@ -5,47 +5,48 @@ import ( "crypto/rand" "fmt" + libp2p "github.com/libp2p/go-libp2p" crypto "github.com/libp2p/go-libp2p-crypto" - peer "github.com/libp2p/go-libp2p-peer" - pstore "github.com/libp2p/go-libp2p-peerstore" - swarm "github.com/libp2p/go-libp2p-swarm" - bhost "github.com/libp2p/go-libp2p/p2p/host/basic" ma "github.com/multiformats/go-multiaddr" ) func main() { - // Generate an identity keypair using go's cryptographic randomness source - priv, pub, err := crypto.GenerateEd25519Key(rand.Reader) + // 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) } - // A peers ID is the hash of its public key - pid, err := peer.IDFromPublicKey(pub) + fmt.Printf("Hello World, my hosts ID is %s\n", h.ID()) + + // If you want more control over the configuration, you can fill out fields + // in the libp2p config, and use `NewWithCfg` + cfg := new(libp2p.Config) + + // Set your own keypair + priv, _, err := crypto.GenerateEd25519Key(rand.Reader) if err != nil { panic(err) } + cfg.PeerKey = priv - // We've created the identity, now we need to store it. - // A peerstore holds information about peers, including your own - ps := pstore.NewPeerstore() - ps.AddPrivKey(pid, priv) - ps.AddPubKey(pid, pub) - + // Set your own listen address maddr, err := ma.NewMultiaddr("/ip4/0.0.0.0/tcp/9000") if err != nil { panic(err) } - // Make a context to govern the lifespan of the swarm - ctx := context.Background() + // The config takes an array of addresses, specify as many as you want. + cfg.ListenAddrs = []ma.Multiaddr{maddr} - // Put all this together - netw, err := swarm.NewNetwork(ctx, []ma.Multiaddr{maddr}, pid, ps, nil) + h2, err := libp2p.NewWithCfg(ctx, cfg) if err != nil { panic(err) } - myhost := bhost.New(netw) - fmt.Printf("Hello World, my hosts ID is %s\n", myhost.ID()) + fmt.Printf("Hello World, my second hosts ID is %s\n", h2.ID()) } diff --git a/libp2p.go b/libp2p.go index 831a9c8ac28037098bfc20bca0186905f67ed1ea..da508732f329281faa1faf779c85c43c5f1ac76e 100644 --- a/libp2p.go +++ b/libp2p.go @@ -22,13 +22,14 @@ import ( // Config describes a set of settings for a libp2p node type Config struct { - Transports []transport.Transport - Muxer mux.Transport - ListenAddrs []ma.Multiaddr - PeerKey crypto.PrivKey - Peerstore pstore.Peerstore - Protector pnet.Protector - Reporter metrics.Reporter + Transports []transport.Transport + Muxer mux.Transport + ListenAddrs []ma.Multiaddr + PeerKey crypto.PrivKey + Peerstore pstore.Peerstore + Protector pnet.Protector + Reporter metrics.Reporter + DisableSecio bool } func New(ctx context.Context) (host.Host, error) { @@ -63,8 +64,11 @@ func NewWithCfg(ctx context.Context, cfg *Config) (host.Host, error) { ps = pstore.NewPeerstore() } - ps.AddPrivKey(pid, cfg.PeerKey) - ps.AddPubKey(pid, cfg.PeerKey.GetPublic()) + // If secio is disabled, don't add our private key to the peerstore + if !cfg.DisableSecio { + ps.AddPrivKey(pid, cfg.PeerKey) + ps.AddPubKey(pid, cfg.PeerKey.GetPublic()) + } swrm, err := swarm.NewSwarmWithProtector(ctx, cfg.ListenAddrs, pid, ps, cfg.Protector, cfg.Muxer, cfg.Reporter) if err != nil {