-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathlibp2p.js
58 lines (52 loc) · 1.38 KB
/
libp2p.js
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
48
49
50
51
52
53
54
55
56
57
58
// Libp2p Core
import Libp2p from 'libp2p'
// Transports
import Websockets from 'libp2p-websockets'
import WebrtcStar from 'libp2p-webrtc-star'
// Stream Muxer
import Mplex from 'pull-mplex'
// Connection Encryption
import Secio from 'libp2p-secio'
// Peer Discovery
import Bootstrap from 'libp2p-bootstrap'
import KadDHT from 'libp2p-kad-dht'
const createLibp2p = (peerInfo) => {
// Listen on the signaling server
peerInfo.multiaddrs.add(`/ip4/127.0.0.1/tcp/15555/ws/p2p-webrtc-star/p2p/${peerInfo.id.toB58String()}`)
// Create webrtcStar here, so we can pass `webrtcStar.discovery`
// to the peerDiscovery configuration
const webrtcStar = new WebrtcStar()
// Create the Node
const libp2p = new Libp2p({
peerInfo,
modules: {
transport: [ Websockets, webrtcStar ],
streamMuxer: [ Mplex ],
connEncryption: [ Secio ],
peerDiscovery: [ Bootstrap, webrtcStar.discovery ],
dht: KadDHT
},
config: {
peerDiscovery: {
bootstrap: {
list: [ '/ip4/127.0.0.1/tcp/63786/ws/p2p/QmWjz6xb8v9K4KnYEwP5Yk75k5mMBCehzWFLCvvQpYxF3d' ]
}
},
dht: {
enabled: true,
randomWalk: {
enabled: true
}
},
EXPERIMENTAL: {
pubsub: true
}
}
})
// Automatically start libp2p
libp2p.start((err) => {
if (err) throw err
})
return libp2p
}
export default createLibp2p