How to use the ipns.getIdKeys 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 / test / core / name-pubsub.js View on Github external
// Wait until a peer subscribes a topic
    const waitForPeerToSubscribe = async (node, topic) => {
      for (let i = 0; i < 5; i++) {
        const res = await node.pubsub.peers(topic)

        if (res && res.length) {
          return
        }

        await delay(2000)
      }

      throw new Error(`Could not find subscription for topic ${topic}`)
    }

    const keys = ipns.getIdKeys(fromB58String(idA.id))
    const topic = `${namespace}${base64url.encode(keys.routingKey.toBuffer())}`

    await expect(nodeB.name.resolve(idA.id))
      .to.eventually.be.rejected()
      .and.to.have.property('code', 'ERR_NO_RECORD_FOUND')

    await waitForPeerToSubscribe(nodeA, topic)
    await nodeB.pubsub.subscribe(topic, checkMessage)
    await nodeA.name.publish(ipfsRef, { resolve: false })
    await waitFor(alreadySubscribed)
    await delay(1000) // guarantee record is written

    const res = await nodeB.name.resolve(idA.id)

    expect(res).to.equal(ipfsRef)
  })
github ipfs / js-ipfs / src / core / ipns / resolver.js View on Github external
async _resolveName (name) {
    const peerId = PeerId.createFromBytes(new CID(name).multihash) // TODO: change to `PeerId.createFromCID` when https://github.com/libp2p/js-peer-id/pull/105 lands and js-ipfs switched to async peer-id lib
    const { routingKey } = ipns.getIdKeys(peerId.toBytes())
    let record

    try {
      record = await this._routing.get(routingKey.toBuffer())
    } catch (err) {
      log.error(err)

      if (err.code === ERR_NOT_FOUND) {
        throw errcode(new Error(`record requested for ${name} was not found in the network`), 'ERR_NO_RECORD_FOUND')
      }

      throw errcode(new Error(`unexpected error getting the ipns record ${peerId.id}`), 'ERR_UNEXPECTED_ERROR_GETTING_RECORD')
    }

    // IPNS entry
    let ipnsEntry
github ipfs / js-ipfs / src / core / ipns / publisher.js View on Github external
async _putRecordToRouting (record, peerId) {
    if (!(PeerId.isPeerId(peerId))) {
      const errMsg = 'peerId received is not valid'
      log.error(errMsg)

      throw errcode(new Error(errMsg), 'ERR_INVALID_PEER_ID')
    }

    const publicKey = peerId._pubKey
    const embedPublicKeyRecord = await ipns.embedPublicKey(publicKey, record)
    const keys = ipns.getIdKeys(peerId.toBytes())

    await this._publishEntry(keys.routingKey, embedPublicKeyRecord || record, peerId)

    // Publish the public key to support old go-ipfs nodes that are looking for it in the routing
    // We will be able to deprecate this part in the future, since the public keys will be only
    // in IPNS record and the peerId.
    await this._publishPublicKey(keys.routingPubKey, publicKey)

    return embedPublicKeyRecord || record
  }
github ipfs / js-ipfs / src / core / ipns / publisher.js View on Github external
return this._unmarshalData(dsVal)
    } catch (err) {
      if (err.code !== ERR_NOT_FOUND) {
        const errMsg = `unexpected error getting the ipns record ${peerId.id} from datastore`
        log.error(errMsg)

        throw errcode(new Error(errMsg), 'ERR_UNEXPECTED_DATASTORE_RESPONSE')
      }

      if (!checkRouting) {
        throw errcode(err)
      }

      // Try to get from routing
      try {
        const keys = ipns.getIdKeys(peerId.toBytes())
        const res = await this._routing.get(keys.routingKey.toBuffer())

        // unmarshal data
        return this._unmarshalData(res)
      } catch (err) {
        log.error(err)

        throw err
      }
    }
  }
github ipfs / js-ipfs / src / core / ipns / routing / pubsub-datastore.js View on Github external
_handleSubscriptionKey (key) {
    if (Buffer.isBuffer(key)) {
      key = toB58String(key)
    }

    const subscriber = this._subscriptions[key]

    if (!subscriber) {
      throw errcode(new Error(`key ${key} does not correspond to a subscription`), 'ERR_INVALID_KEY')
    }

    let keys
    try {
      keys = ipns.getIdKeys(fromB58String(subscriber))
    } catch (err) {
      log.error(err)
      throw err
    }

    return keys.routingKey.toBuffer()
  }