How to use the ipns.namespaceLength function in ipns

To help you get started, we’ve selected a few ipns 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 / js-ipfs / src / core / ipns / routing / pubsub-datastore.js View on Github external
async get (key) {
    let res
    let err

    try {
      res = await this._pubsubDs.get(key)
    } catch (e) {
      err = e
    }

    // Add topic subscribed
    const ns = key.slice(0, ipns.namespaceLength)

    if (ns.toString() === ipns.namespace) {
      const stringifiedTopic = toB58String(key)
      const id = toB58String(key.slice(ipns.namespaceLength))

      this._subscriptions[stringifiedTopic] = id

      log(`subscribed to pubsub topic ${stringifiedTopic}, id ${id}`)
    }

    // If no data was obtained, after storing the subscription, return the error.
    if (err) {
      throw err
    }

    return res
  }
github ipfs / js-ipfs / src / core / ipns / routing / pubsub-datastore.js View on Github external
async cancel (name) { // eslint-disable-line require-await
    if (typeof name !== 'string') {
      throw errcode(new Error('invalid subscription name'), 'ERR_INVALID_SUBSCRIPTION_NAME')
    }

    // Trim /ipns/ prefix from the name
    if (name.startsWith(ipns.namespace)) {
      name = name.substring(ipns.namespaceLength)
    }

    const stringifiedTopic = Object.keys(this._subscriptions).find((key) => this._subscriptions[key] === name)

    // Not found topic
    if (!stringifiedTopic) {
      return {
        canceled: false
      }
    }

    // Unsubscribe topic
    const bufTopic = Buffer.from(stringifiedTopic)

    this._pubsubDs.unsubscribe(bufTopic)
github ipfs / js-ipfs / src / core / ipns / routing / dns-datastore.js View on Github external
function dohBinary (key, callback) {
  const cid = new Cid(key.slice(ipns.namespaceLength))
  const buf = dnsPacket.encode({
    type: 'query',
    id: getRandomInt(1, 65534),
    flags: dnsPacket.RECURSION_DESIRED,
    questions: [{
      type: 'TXT',
      name: `${cid.toV1().toString()}.dns.ipns.dev`
    }]
  })
  // https://dns.google.com/experimental
  // https://cloudflare-dns.com/dns-query
  // https://mozilla.cloudflare-dns.com/dns-query
  ky
    .get('https://cloudflare-dns.com/dns-query', {
      searchParams: {
        dns: buf.toString('base64')
github ipfs / js-ipfs / src / core / ipns / routing / experimental / utils.js View on Github external
function keyToBase32 (key) {
  const cid = new Cid(key.slice(ipns.namespaceLength))
  return cid.toV1().toString()
}