2020-01-07 14:53:27 +01:00
|
|
|
/* eslint no-console: ["off"] */
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const { generate } = require('libp2p/src/pnet')
|
|
|
|
const privateLibp2pNode = require('./libp2p-node')
|
|
|
|
|
|
|
|
const pipe = require('it-pipe')
|
|
|
|
|
2020-08-24 11:58:02 +01:00
|
|
|
// Create a Uint8Array and write the swarm key to it
|
|
|
|
const swarmKey = new Uint8Array(95)
|
2020-01-07 14:53:27 +01:00
|
|
|
generate(swarmKey)
|
|
|
|
|
|
|
|
// This key is for testing a different key not working
|
2020-08-24 11:58:02 +01:00
|
|
|
const otherSwarmKey = new Uint8Array(95)
|
2020-01-07 14:53:27 +01:00
|
|
|
generate(otherSwarmKey)
|
|
|
|
|
|
|
|
;(async () => {
|
|
|
|
const node1 = await privateLibp2pNode(swarmKey)
|
|
|
|
|
2020-04-24 16:10:40 +01:00
|
|
|
// TASK: switch the commented out line below so we're using a different key, to see the nodes fail to connect
|
2020-01-07 14:53:27 +01:00
|
|
|
const node2 = await privateLibp2pNode(swarmKey)
|
|
|
|
// const node2 = await privateLibp2pNode(otherSwarmKey)
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
node1.start(),
|
|
|
|
node2.start()
|
|
|
|
])
|
|
|
|
|
|
|
|
console.log('nodes started...')
|
|
|
|
|
2020-05-15 15:37:47 +02:00
|
|
|
// Add node 2 data to node1's PeerStore
|
|
|
|
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
|
|
|
await node1.dial(node2.peerId)
|
2020-01-07 14:53:27 +01:00
|
|
|
|
|
|
|
node2.handle('/private', ({ stream }) => {
|
|
|
|
pipe(
|
|
|
|
stream,
|
|
|
|
async function (source) {
|
|
|
|
for await (const msg of source) {
|
|
|
|
console.log(msg.toString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-15 15:37:47 +02:00
|
|
|
const { stream } = await node1.dialProtocol(node2.peerId, '/private')
|
2020-01-07 14:53:27 +01:00
|
|
|
|
|
|
|
await pipe(
|
|
|
|
['This message is sent on a private network'],
|
|
|
|
stream
|
|
|
|
)
|
2020-04-24 16:10:40 +01:00
|
|
|
})()
|