How to use peer-id - 10 common examples

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 libp2p / js-libp2p / test / dialing / direct.js View on Github external
before('create nodes', async () => {
    const [peerId1, peerId2] = await Promise.all([
      createPeerId(PeerIds.shift()),
      createPeerId(PeerIds.shift())
    ])

    const peerInfo1 = new PeerInfo(peerId1)
    peerInfo1.multiaddrs.add('/ip4/0.0.0.0/tcp/8080')
    // peerInfo1.multiaddrs.add('/ip4/0.0.0.0/tcp/0/ws')
    const peerInfo2 = new PeerInfo(peerId2)
    peerInfo2.multiaddrs.add('/ip4/0.0.0.0/tcp/10000')
    // peerInfo2.multiaddrs.add('/ip4/0.0.0.0/tcp/0/ws')

    libp2p = new Libp2p({ peerInfo: peerInfo1, config: { peerDiscovery: { autoDial: false } } })
    remoteLibp2p = new Libp2p({ peerInfo: peerInfo2, config: { peerDiscovery: { autoDial: false } } })
  })
github validitylabs / hopr / src / network / natTraversal / base / index.js View on Github external
.on('connect', async () => {
                        // console.log('[initiator] connected')

                        const peerInfo = await PeerInfo.create(await PeerId.createFromB58String(multiaddr.getPeerId()))

                        peerInfo.multiaddrs.add(multiaddr)
                        peerInfo.connect(multiaddr)

                        conn.setPeerInfo(peerInfo)
                        resolve(conn)
                    })
github IDEO-coLAB / nomad / src / shim-node.js View on Github external
.then((storedPeerInfo) => {
      if (!storedPeerInfo) {
        log.info(`${MODULE_NAME}: GET ${peerId} (No info found)`)
        return null
      }

      log.info(`${MODULE_NAME}: GET ${peerId}`)

      // const peerIdObj = new PeerId(new Buffer(storedPeerInfo.id))
      // const peerInfo = new PeerInfo(peerIdObj)
      const peerInfo = new PeerInfo(PeerId.createFromB58String(storedPeerInfo.id))
      // add multiaddrs to the peer
      // storedPeerInfo.multiaddrs.forEach((mAddr) => {
      //   console.log('madr:', mAddr)
      //   peerInfo.multiaddr.add(mAddr)
      // })
      // peerInfo.multiaddr.add('/ip4/10.2.2.164/tcp/4002')
      // peerInfo.multiaddr.add('/ip4/10.2.2.164/tcp/4002')

      console.log(storedPeerInfo.multiaddrs[1])
      console.log('/ip4/10.2.2.164/tcp/4002')
      console.log('/ip4/10.2.2.164/tcp/4002' === storedPeerInfo.multiaddrs[1])

      // peerInfo.multiaddr.add(storedPeerInfo.multiaddrs[0])
      peerInfo.multiaddr.add(storedPeerInfo.multiaddrs[1])

      console.log('GOT FROM SHIM SERVER:', storedPeerInfo)
github libp2p / js-libp2p / src / circuit / index.js View on Github external
async dial (ma, options) {
    // Check the multiaddr to see if it contains a relay and a destination peer
    const addrs = ma.toString().split('/p2p-circuit')
    const relayAddr = multiaddr(addrs[0])
    const destinationAddr = multiaddr(addrs[addrs.length - 1])
    const relayPeer = PeerId.createFromCID(relayAddr.getPeerId())
    const destinationPeer = PeerId.createFromCID(destinationAddr.getPeerId())

    let disconnectOnFailure = false
    let relayConnection = this._registrar.getConnection(new PeerInfo(relayPeer))
    if (!relayConnection) {
      relayConnection = await this._dialer.connectToMultiaddr(relayAddr, options)
      disconnectOnFailure = true
    }

    try {
      const virtualConnection = await hop({
        connection: relayConnection,
        circuit: this,
        request: {
          type: CircuitPB.Type.HOP,
          srcPeer: {
            id: this.peerInfo.id.toBytes(),
github libp2p / js-libp2p / src / circuit / index.js View on Github external
async dial (ma, options) {
    // Check the multiaddr to see if it contains a relay and a destination peer
    const addrs = ma.toString().split('/p2p-circuit')
    const relayAddr = multiaddr(addrs[0])
    const destinationAddr = multiaddr(addrs[addrs.length - 1])
    const relayPeer = PeerId.createFromCID(relayAddr.getPeerId())
    const destinationPeer = PeerId.createFromCID(destinationAddr.getPeerId())

    let disconnectOnFailure = false
    let relayConnection = this._registrar.getConnection(new PeerInfo(relayPeer))
    if (!relayConnection) {
      relayConnection = await this._dialer.connectToMultiaddr(relayAddr, options)
      disconnectOnFailure = true
    }

    try {
      const virtualConnection = await hop({
        connection: relayConnection,
        circuit: this,
        request: {
          type: CircuitPB.Type.HOP,
          srcPeer: {
github validitylabs / hopr / hopr.js View on Github external
async function checkPeerIdInput(query) {
    let peerId

    try {
        // Throws an error if the Id is invalid
        Multihash.decode(bs58.decode(query))

        peerId = await addPubKey(PeerId.createFromB58String(query))
    } catch (err) {
        throw Error(chalk.red(`Invalid peerId. ${err.message}`))
    }
    return peerId
}
github ipfs / js-ipfs / src / core / ipns / republisher.js View on Github external
async _getPreviousValue (peerId) {
    if (!(PeerId.isPeerId(peerId))) {
      throw errcode(new Error('invalid peer ID'), 'ERR_INVALID_PEER_ID')
    }

    try {
      const dsVal = await this._datastore.get(ipns.getLocalKey(peerId.id))

      if (!Buffer.isBuffer(dsVal)) {
        throw errcode(new Error("found ipns record that we couldn't process"), 'ERR_INVALID_IPNS_RECORD')
      }

      // unmarshal data
      try {
        const record = ipns.unmarshal(dsVal)

        return record.value
      } catch (err) {
github libp2p / js-libp2p / src / peer-store / index.js View on Github external
get (peerId) {
    // TODO: deprecate this and just accept `PeerId` instances
    if (PeerId.isPeerId(peerId)) {
      peerId = peerId.toB58String()
    }

    return this.peers.get(peerId)
  }
github validitylabs / hopr / src / index.js View on Github external
async sendMessage(msg, destination) {
        if (!msg) throw Error(`Expecting a non-empty message.`)

        if (!destination) throw Error(`Expecting a non-empty destination.`)

        if (PeerInfo.isPeerInfo(destination)) destination = destination.id

        if (typeof destination === 'string') destination = PeerId.createFromB58String(destination)

        if (!PeerId.isPeerId(destination))
            throw Error(`Unable to parse given destination to a PeerId instance. Got type ${typeof destination} with value ${destination}.`)

        // Let's try to convert input msg to a Buffer in case it isn't already a Buffer
        if (!Buffer.isBuffer(msg)) {
            switch (typeof msg) {
                default:
                    throw Error(`Invalid input value. Got '${typeof msg}'.`)
                case 'number':
                    msg = msg.toString()
                case 'string':
                    msg = Buffer.from(msg)
            }
        }

        const promises = []