How to use multiaddr - 10 common examples

To help you get started, we’ve selected a few multiaddr 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 ipfs / interface-js-ipfs-core / src / swarm / peers.js View on Github external
it('should list peers this node is connected to with verbose option', async () => {
      const peers = await ipfsA.swarm.peers({ verbose: true })
      expect(peers).to.have.length.above(0)

      const peer = peers[0]
      expect(peer).to.have.a.property('addr')
      expect(multiaddr.isMultiaddr(peer.addr)).to.equal(true)
      expect(peer).to.have.a.property('peer')
      expect(peer).to.have.a.property('latency')
      expect(peer.latency).to.match(/n\/a|[0-9]+[mµ]?s/) // n/a or 3ms or 3µs or 3s

      /* TODO: These assertions must be uncommented as soon as
         https://github.com/ipfs/js-ipfs/issues/2601 gets resolved */
      // expect(peer).to.have.a.property('muxer')
      // expect(peer).to.have.a.property('streams')
    })
github ipfs / is-ipfs / src / index.js View on Github external
function isMultiaddr (input) {
  if (!input) return false
  if (isString(input) || input instanceof Buffer) {
    try {
      new Multiaddr(input) // eslint-disable-line no-new
      return true
    } catch (e) {
      return false
    }
  }
  if (Multiaddr.isMultiaddr(input)) return true
  return false
}
github ipfs / js-ipfs-http-client / src / index.js View on Github external
function ipfsClient (hostOrMultiaddr, port, userOptions) {
  // convert all three params to objects that we can merge.
  let options = {}

  if (!hostOrMultiaddr) {
    // autoconfigure host and port in browser
    if (typeof self !== 'undefined') {
      options = urlToOptions(self.location)
    }
  } else if (multiaddr.isMultiaddr(hostOrMultiaddr)) {
    options = maToOptions(hostOrMultiaddr)
  } else if (typeof hostOrMultiaddr === 'object') {
    options = hostOrMultiaddr
  } else if (typeof hostOrMultiaddr === 'string') {
    if (hostOrMultiaddr[0] === '/') {
      // throws if multiaddr is malformed or can't be converted to a nodeAddress
      options = maToOptions(multiaddr(hostOrMultiaddr))
    } else {
      // hostOrMultiaddr is domain or ip address as a string
      options.host = hostOrMultiaddr
    }
  }

  if (port && typeof port !== 'object') {
    port = { port: port }
  }
github validitylabs / hopr / src / js / testing / index.js View on Github external
publicAddress: (cb) => stunServer.once('bindingResponse', (stunMsg) => {
                stunServer.close()
                server.close(
                    cb(null, Multiaddr.fromNodeAddress(stunMsg.getAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS).value, 'tcp'))
                )
            }),
            stunError: (cb) => stunServer.send(
github ZeroNetJS / zeronet-js / zeronet-common / lib / peer / pool.js View on Github external
add(pi, lazy) {
    let p = isInPool(this.cache, pi)
    if (p.length > 1) throw new Error("Multiple peers found!")
    if (p.length) return p.pop()

    if (PeerInfo.isPeerInfo(pi) && pi.id.toB58String())
      return this.push(new Peer.Lp2pPeer(pi), lazy)

    if (ip2multi.isIp(pi))
      return this.push(new Peer.ZeroPeer(ip2multi(pi)), lazy)

    if (multiaddr.isMultiaddr(pi)) {
      if (pi.toString().indexOf("ipfs") != -1) {
        const id = Id.createFromB58String(pi.toString().split("ipfs/").pop())
        const _pi = new PeerInfo(id)
        _pi.multiaddrs.addSafe(pi)
        return this.push(new Peer.Lp2pPeer(_pi), lazy)
      } else {
        return this.push(new Peer.ZeroPeer(pi.toString()), lazy)
      }
    }

    if (typeof pi == "string") {
      if (pi.match(/\/.+\/.+\/.+\/.+\//) || pi.match(/\/.+\/.+\/.+\/.+/))
        return this.push(new Peer.ZeroPeer(pi), lazy)

      if (pi.match(/\/.+\/.+\/.+\/.+\/.+\/.+\//) || pi.match(/\/.+\/.+\/.+\/.+\/.+\/.+/)) {
        const id = Id.createFromB58String(pi.split("ipfs/").pop())
github libp2p / js-libp2p-webrtc-star / src / webrtc-star / index.js View on Github external
callback = function noop () {}
      }
      listener.io.emit('ss-leave')
      setTimeout(() => {
        listener.emit('close')
        callback()
      }, 100)
    }

    listener.getAddrs = (callback) => {
      process.nextTick(() => {
        callback(null, [maSelf])
      })
    }

    listeners[multiaddr.toString()] = listener
    return listener
  }
github libp2p / js-libp2p-webrtc-star / src / listener.js View on Github external
})

    return defer.promise
  }

  listener.close = () => {
    listener.__connections.forEach(maConn => maConn.close())
    listener.io && listener.io.emit('ss-leave')
    listener.emit('close')
  }

  listener.getAddrs = () => {
    return [WebRTCStar.maSelf]
  }

  WebRTCStar.listenersRefs[multiaddr.toString()] = listener
  return listener
}
github libp2p / js-libp2p-webrtc-star / src / utils.js View on Github external
function cleanUrlSIO (ma) {
  const maStrSplit = ma.toString().split('/')
  const tcpProto = ma.protos()[1].name
  const wsProto = ma.protos()[2].name
  const tcpPort = ma.stringTuples()[1][1]

  if (tcpProto !== 'tcp' || (wsProto !== 'ws' && wsProto !== 'wss')) {
    throw new Error('invalid multiaddr: ' + ma.toString())
  }

  if (!multiaddr.isName(ma)) {
    return 'http://' + maStrSplit[2] + ':' + maStrSplit[4]
  }

  if (wsProto === 'ws') {
    return 'http://' + maStrSplit[2] + (tcpPort === 80 ? '' : ':' + tcpPort)
  }

  if (wsProto === 'wss') {
    return 'https://' + maStrSplit[2] + (tcpPort === 443 ? '' : ':' + tcpPort)
  }
}
github libp2p / js-peer-book / src / index.js View on Github external
function getB58Str (peer) {
  let b58Str

  if (typeof peer === 'string') {
    if (peer.startsWith('/')) {
      b58Str = Multiaddr(peer).getPeerId()
    } else {
      b58Str = peer
    }
  } else if (Buffer.isBuffer(peer)) {
    b58Str = bs58.encode(peer).toString()
  } else if (PeerId.isPeerId(peer)) {
    b58Str = peer.toB58String()
  } else if (PeerInfo.isPeerInfo(peer)) {
    b58Str = peer.id.toB58String()
  } else if (Multiaddr.isMultiaddr(peer)) {
    b58Str = peer.getPeerId()
  } else {
    throw new Error('not valid PeerId or PeerInfo, or B58Str')
  }

  return b58Str
}
github ZeroNetJS / zeronet-js / zeronet-common / lib / peer / index.js View on Github external
      addrs.map(addr => multiaddr.isMultiaddr(addr) ? addr : multiaddr(addr)).forEach(addr => this.pi.multiaddrs.add(addr))
    }

multiaddr

multiaddr implementation (binary + string representation of network addresses)

MIT
Latest version published 3 years ago

Package Health Score

53 / 100
Full package analysis