How to use the peer-id.create function in peer-id

To help you get started, we’ve selected a few peer-id examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github libp2p / js-libp2p / test / transports.browser.js View on Github external
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))
github daviddias / js-ethereum-libp2p / src / spawn-libp2p-node.js View on Github external
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) => {
github libp2p / js-libp2p / examples / level1-swarm / node-2.js View on Github external
'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)
  })
github libp2p / js-libp2p / examples / level1-swarm / node-1.js View on Github external
'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])
github libp2p / js-interfaces / test / peer-discovery / mock-discovery.js View on Github external
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)
  }
}
github ipfs / js-libp2p-ipfs-browser / test / webrtc-star-only.js View on Github external
      (cb) => PeerId.create({ bits: 1024 }, cb),
      (cb) => PeerId.create({ bits: 1024 }, cb)
github libp2p / js-libp2p / test / browser-bundle / webrtc-star-only.js View on Github external
      (cb) => PeerId.create({ bits: 1024 }, cb),
      (cb) => PeerId.create({ bits: 1024 }, cb)
github libp2p / js-libp2p-connection-manager / test / utils / create-libp2p-node.js View on Github external
    (cb) => PeerId.create({ bits: 1024 }, cb),
    (id, cb) => PeerInfo.create(id, cb),
github libp2p / js-libp2p-switch / examples / peerB.js View on Github external
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')
})