Commit d0426805 authored by Jeromy's avatar Jeromy
Browse files

use new constructor for examples

parent c937b88e
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
mrand "math/rand" mrand "math/rand"
golog "github.com/ipfs/go-log" golog "github.com/ipfs/go-log"
libp2p "github.com/libp2p/go-libp2p"
crypto "github.com/libp2p/go-libp2p-crypto" crypto "github.com/libp2p/go-libp2p-crypto"
host "github.com/libp2p/go-libp2p-host" host "github.com/libp2p/go-libp2p-host"
net "github.com/libp2p/go-libp2p-net" net "github.com/libp2p/go-libp2p-net"
...@@ -69,28 +70,13 @@ func makeBasicHost(listenPort int, secio bool, randseed int64) (host.Host, error ...@@ -69,28 +70,13 @@ func makeBasicHost(listenPort int, secio bool, randseed int64) (host.Host, error
ps.AddPubKey(pid, pub) ps.AddPubKey(pid, pub)
} }
// Set up stream multiplexer basicHost, err := libp2p.NewWithCfg(ctx, &libp2p.Config{
tpt := msmux.NewBlankTransport() ListenAddrs: []ma.Multiaddr{addr},
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,
)
if err != nil { if err != nil {
return nil, err return nil, err
} }
netw := (*swarm.Network)(swrm)
basicHost := bhost.New(netw)
// Build host multiaddress // Build host multiaddress
hostAddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ipfs/%s", basicHost.ID().Pretty())) hostAddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ipfs/%s", basicHost.ID().Pretty()))
......
...@@ -5,47 +5,48 @@ import ( ...@@ -5,47 +5,48 @@ import (
"crypto/rand" "crypto/rand"
"fmt" "fmt"
libp2p "github.com/libp2p/go-libp2p"
crypto "github.com/libp2p/go-libp2p-crypto" 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" ma "github.com/multiformats/go-multiaddr"
) )
func main() { func main() {
// Generate an identity keypair using go's cryptographic randomness source // The context governs the lifetime of the libp2p node
priv, pub, err := crypto.GenerateEd25519Key(rand.Reader) 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 { if err != nil {
panic(err) panic(err)
} }
// A peers ID is the hash of its public key fmt.Printf("Hello World, my hosts ID is %s\n", h.ID())
pid, err := peer.IDFromPublicKey(pub)
// 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 { if err != nil {
panic(err) panic(err)
} }
cfg.PeerKey = priv
// We've created the identity, now we need to store it. // Set your own listen address
// A peerstore holds information about peers, including your own
ps := pstore.NewPeerstore()
ps.AddPrivKey(pid, priv)
ps.AddPubKey(pid, pub)
maddr, err := ma.NewMultiaddr("/ip4/0.0.0.0/tcp/9000") maddr, err := ma.NewMultiaddr("/ip4/0.0.0.0/tcp/9000")
if err != nil { if err != nil {
panic(err) panic(err)
} }
// Make a context to govern the lifespan of the swarm // The config takes an array of addresses, specify as many as you want.
ctx := context.Background() cfg.ListenAddrs = []ma.Multiaddr{maddr}
// Put all this together h2, err := libp2p.NewWithCfg(ctx, cfg)
netw, err := swarm.NewNetwork(ctx, []ma.Multiaddr{maddr}, pid, ps, nil)
if err != nil { if err != nil {
panic(err) panic(err)
} }
myhost := bhost.New(netw) fmt.Printf("Hello World, my second hosts ID is %s\n", h2.ID())
fmt.Printf("Hello World, my hosts ID is %s\n", myhost.ID())
} }
...@@ -22,13 +22,14 @@ import ( ...@@ -22,13 +22,14 @@ import (
// Config describes a set of settings for a libp2p node // Config describes a set of settings for a libp2p node
type Config struct { type Config struct {
Transports []transport.Transport Transports []transport.Transport
Muxer mux.Transport Muxer mux.Transport
ListenAddrs []ma.Multiaddr ListenAddrs []ma.Multiaddr
PeerKey crypto.PrivKey PeerKey crypto.PrivKey
Peerstore pstore.Peerstore Peerstore pstore.Peerstore
Protector pnet.Protector Protector pnet.Protector
Reporter metrics.Reporter Reporter metrics.Reporter
DisableSecio bool
} }
func New(ctx context.Context) (host.Host, error) { func New(ctx context.Context) (host.Host, error) {
...@@ -63,8 +64,11 @@ func NewWithCfg(ctx context.Context, cfg *Config) (host.Host, error) { ...@@ -63,8 +64,11 @@ func NewWithCfg(ctx context.Context, cfg *Config) (host.Host, error) {
ps = pstore.NewPeerstore() ps = pstore.NewPeerstore()
} }
ps.AddPrivKey(pid, cfg.PeerKey) // If secio is disabled, don't add our private key to the peerstore
ps.AddPubKey(pid, cfg.PeerKey.GetPublic()) 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) swrm, err := swarm.NewSwarmWithProtector(ctx, cfg.ListenAddrs, pid, ps, cfg.Protector, cfg.Muxer, cfg.Reporter)
if err != nil { if err != nil {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment