forked from libp2p/js-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibp2p-node.js
34 lines (32 loc) · 1.26 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
import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { mplex } from '@libp2p/mplex'
import { tcp } from '@libp2p/tcp'
import { createLibp2p } from 'libp2p'
import { preSharedKey } from 'libp2p/pnet'
/**
* privateLibp2pNode returns a libp2p node function that will use the swarm
* key with the given `swarmKey` to create the Protector
*
* @param {any} swarmKey
*/
export async function privateLibp2pNode (swarmKey) {
const node = await createLibp2p({
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
},
transports: [tcp()], // We're only using the TCP transport for this example
streamMuxers: [yamux(), mplex()], // We're only using mplex muxing
// Let's make sure to use identifying crypto in our pnet since the protector doesn't
// care about node identity, and only the presence of private keys
connectionEncryption: [noise()],
// Leave peer discovery empty, we don't want to find peers. We could omit the property, but it's
// being left in for explicit readability.
// We should explicitly dial pnet peers, or use a custom discovery service for finding nodes in our pnet
peerDiscovery: [],
connectionProtector: preSharedKey({
psk: swarmKey
})
})
return node
}