-
Notifications
You must be signed in to change notification settings - Fork 1
/
libp2p-node.js
53 lines (48 loc) · 1.62 KB
/
libp2p-node.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
'use strict'
const TCP = require('libp2p-tcp')
const mplex = require('libp2p-mplex')
const { NOISE } = require('@chainsafe/libp2p-noise')
const defaultsDeep = require('@nodeutils/defaults-deep')
const libp2p = require('libp2p')
const KadDHT = require('libp2p-kad-dht')
const Bootstrap = require('libp2p-bootstrap')
const Websockets = require('libp2p-websockets')
const filters = require('libp2p-websockets/src/filters')
const WebrtcStar = require('libp2p-webrtc-star')
const transportKey = Websockets.prototype[Symbol.toStringTag]
async function createLibp2p(_options) {
const defaults = {
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
},
modules: {
transport: [TCP, Websockets, WebrtcStar],
streamMuxer: [mplex],
connEncryption: [NOISE],
dht: KadDHT, // todo: undo?
peerDiscovery: [Bootstrap],
},
config: {
dht: {
enabled: true,
randomWalk: {
enabled: true
}
},
peerDiscovery: {
bootstrap: {
list: ['/ip4/127.0.0.1/tcp/63786/ws/p2p/QmWjz6xb8v9K4KnYEwP5Yk75k5mMBCehzWFLCvvQpYxF3d']
}
},
transport: {
[transportKey]: {
// by default websockets do not allow localhost dials
// let's enable it for testing purposes in this example
filter: filters.all
}
}
}
}
return libp2p.create(defaultsDeep(_options, defaults))
}
module.exports = createLibp2p