Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('create a third node and check that discovery works', (done) => {
PeerId.create({ bits: 512 }, (err, id3) => {
expect(err).to.not.exist()
const b58Id = id3.toB58String()
function check () {
// Verify both nodes are connected to node 3
if (node1._switch.connection.getAllById(b58Id) && node2._switch.connection.getAllById(b58Id)) {
done()
}
}
const peer3 = new PeerInfo(id3)
const ma3 = '/ip4/127.0.0.1/tcp/15555/ws/p2p-webrtc-star/p2p/' + b58Id
peer3.multiaddrs.add(ma3)
node1.on('peer:discovery', (peerInfo) => node1.dial(peerInfo, check))
function spawnNode (peerInfo, callback) {
if (typeof peerInfo === 'function') {
callback = peerInfo
peerInfo = undefined
}
if (!peerInfo) {
const id = PeerId.create()
peerInfo = new PeerInfo(id)
if (isNode) {
peerInfo.multiaddr.add(multiaddr('/ip4/0.0.0.0/tcp/0'))
// until libp2p-websockets supports port 0
const wsPort = _random(10000, 19000)
peerInfo.multiaddr.add(multiaddr(`/ip4/127.0.0.1/tcp/${wsPort}/ws`))
} else {
const sig = multiaddr('/libp2p-webrtc-star/ip4/127.0.0.1/tcp/20000/ws')
peerInfo.multiaddr.add(sig.encapsulate(`/ipfs/${id.toB58String()}`))
}
}
const node = new libp2p.Node(peerInfo)
node.start((err) => {
'use strict'
var Swarm = require('libp2p-swarm')
var tcp = require('libp2p-tcp')
var multiaddr = require('multiaddr')
var Id = require('peer-id')
var Spdy = require('libp2p-spdy')
var Libp2p = require('../../src')
var Peer = require('peer-info')
// set up
var mh = multiaddr('/ip4/127.0.0.1/tcp/8020')
var p = new Peer(Id.create(), [])
var sw = new Swarm(p)
sw.addTransport('tcp', tcp, {multiaddr: mh}, {}, {port: 8020}, function () {
// Ready to receive incoming connections
sw.addStreamMuxer('spdy', Spdy, {})
// create a libp2p node
var node = new Libp2p(sw)
// handle/mount a protocol
node.swarm.handleProtocol('/sparkles/1.0.0', function (conn) {
process.stdin.pipe(conn).pipe(process.stdout)
})
'use strict'
var Swarm = require('libp2p-swarm')
var tcp = require('libp2p-tcp')
var multiaddr = require('multiaddr')
var Id = require('peer-id')
var Spdy = require('libp2p-spdy')
var Libp2p = require('../../src')
var Peer = require('peer-info')
// set up
var mh = multiaddr('/ip4/127.0.0.1/tcp/8010')
var p = new Peer(Id.create(), [])
var sw = new Swarm(p)
// create a libp2p node
var node = new Libp2p(sw)
node.swarm.addTransport('tcp', tcp, {multiaddr: mh}, {}, {port: 8010}, function () {
// Ready to receive incoming connections
sw.addStreamMuxer('spdy', Spdy, {})
// dial to another node
var mh2 = multiaddr('/ip4/127.0.0.1/tcp/8020')
var p2 = new Peer(Id.create(), [mh2])
async _discoverPeer () {
if (!this._isRunning) return
const peerId = await PeerId.create({ bits: 512 })
const peerInfo = new PeerInfo(peerId)
this._timer = setTimeout(() => {
this.emit('peer', peerInfo)
}, this.options.discoveryDelay || 1000)
}
}
(cb) => PeerId.create({ bits: 1024 }, cb),
(cb) => PeerId.create({ bits: 1024 }, cb)
(cb) => PeerId.create({ bits: 1024 }, cb),
(cb) => PeerId.create({ bits: 1024 }, cb)
(cb) => PeerId.create({ bits: 1024 }, cb),
(id, cb) => PeerInfo.create(id, cb),
var Swarm = require('./../src')
var Peer = require('peer-info')
var Id = require('peer-id')
var multiaddr = require('multiaddr')
var tcp = require('libp2p-tcp')
var mh = multiaddr('/ip4/127.0.0.1/tcp/8010')
var p = new Peer(Id.create(), [])
var sw = new Swarm(p)
sw.addTransport('tcp', tcp, { multiaddr: mh }, {}, {port: 8010}, function () {
console.log('transport added')
})