Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
d0426805
Commit
d0426805
authored
Dec 14, 2017
by
Jeromy
Browse files
use new constructor for examples
parent
c937b88e
Changes
3
Hide whitespace changes
Inline
Side-by-side
examples/echo/main.go
View file @
d0426805
...
...
@@ -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
()))
...
...
examples/libp2p-host/host.go
View file @
d0426805
...
...
@@ -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
c
tx
:=
context
.
Background
()
//
The config takes an array of addresses, specify as many as you want.
c
fg
.
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
())
}
libp2p.go
View file @
d0426805
...
...
@@ -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
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment