2020-01-07 14:53:27 +01:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const Libp2p = require('libp2p')
|
|
|
|
const TCP = require('libp2p-tcp')
|
|
|
|
const MPLEX = require('libp2p-mplex')
|
2020-05-15 15:37:47 +02:00
|
|
|
const { NOISE } = require('libp2p-noise')
|
2020-01-07 14:53:27 +01:00
|
|
|
const Protector = require('libp2p/src/pnet')
|
|
|
|
|
|
|
|
/**
|
|
|
|
* privateLibp2pNode returns a libp2p node function that will use the swarm
|
2020-05-25 13:14:07 +02:00
|
|
|
* key with the given `swarmKey` to create the Protector
|
2020-01-07 14:53:27 +01:00
|
|
|
*
|
2020-08-24 11:58:02 +01:00
|
|
|
* @param {Uint8Array} swarmKey
|
2020-01-07 14:53:27 +01:00
|
|
|
* @returns {Promise<libp2p>} Returns a libp2pNode function for use in IPFS creation
|
|
|
|
*/
|
2020-05-25 13:14:07 +02:00
|
|
|
const privateLibp2pNode = async (swarmKey) => {
|
2020-01-07 14:53:27 +01:00
|
|
|
const node = await Libp2p.create({
|
2020-05-15 15:37:47 +02:00
|
|
|
addresses: {
|
|
|
|
listen: ['/ip4/0.0.0.0/tcp/0']
|
|
|
|
},
|
2020-01-07 14:53:27 +01:00
|
|
|
modules: {
|
|
|
|
transport: [TCP], // We're only using the TCP transport for this example
|
|
|
|
streamMuxer: [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
|
2020-10-07 16:16:36 +02:00
|
|
|
connEncryption: [NOISE],
|
2020-01-07 14:53:27 +01:00
|
|
|
// 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: [],
|
2020-05-25 13:14:07 +02:00
|
|
|
connProtector: new Protector(swarmKey)
|
2020-01-07 14:53:27 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = privateLibp2pNode
|