How to use the multiaddr.isMultiaddr function in multiaddr

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 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-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))
    }
github ZeroNetJS / zeronet-js / zeronet-swarm / lib / dial / index.js View on Github external
function getPeerIdType(pi) {
  if (Peer.isPeerInfo(pi))
    return "libp2p"

  if (ip2multi.isIp(pi))
    return "zero"

  if (multiaddr.isMultiaddr(pi)) {
    if (pi.toString().indexOf("ipfs") != -1)
      return "libp2p"
    return "zero"
  }

  if (typeof pi == "string") {
    if (pi.match(/\/.+\/.+\/.+\/.+\//) || pi.match(/\/.+\/.+\/.+\/.+/))
      return "zero"

    if (pi.match(/\/.+\/.+\/.+\/.+\/.+\/.+\//) || pi.match(/\/.+\/.+\/.+\/.+\/.+\/.+/))
      return "libp2p"
  }

  return false
}
github ipfs-shipyard / ipfs-postmsg-proxy / src / serialization / multiaddr.js View on Github external
export const isMultiaddr = (obj) => obj && Multiaddr.isMultiaddr(obj)
export const isMultiaddrJson = (obj) => obj && obj.__ipfsPostMsgProxyType === 'Multiaddr'
github libp2p / js-interfaces / src / connection / connection.js View on Github external
constructor ({ localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat }) {
    localAddr && assert(multiaddr.isMultiaddr(localAddr), 'localAddr must be an instance of multiaddr')
    assert(multiaddr.isMultiaddr(remoteAddr), 'remoteAddr must be an instance of multiaddr')
    assert(PeerId.isPeerId(localPeer), 'localPeer must be an instance of peer-id')
    assert(PeerId.isPeerId(remotePeer), 'remotePeer must be an instance of peer-id')
    assert(typeof newStream === 'function', 'new stream must be a function')
    assert(typeof close === 'function', 'close must be a function')
    assert(typeof getStreams === 'function', 'getStreams must be a function')
    assert(stat, 'connection metadata object must be provided')
    assert(stat.direction === 'inbound' || stat.direction === 'outbound', 'direction must be "inbound" or "outbound"')
    assert(stat.timeline, 'connection timeline object must be provided in the stat object')
    assert(stat.timeline.open, 'connection open timestamp must be provided')
    assert(stat.timeline.upgraded, 'connection upgraded timestamp must be provided')

    /**
     * Connection identifier.
     */
    this.id = (parseInt(Math.random() * 1e9)).toString(36) + Date.now()

multiaddr

multiaddr implementation (binary + string representation of network addresses)

MIT
Latest version published 3 years ago

Package Health Score

53 / 100
Full package analysis