How to use the ipns.unmarshal 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 / 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) {
        log.error(err)
        throw errcode(new Error('found ipns record that we couldn\'t convert to a value'), 'ERR_INVALID_IPNS_RECORD')
      }
    } catch (err) {
      // error handling
      // no need to republish
      if (err && err.notFound) {
        throw errcode(new Error(`no previous entry for record with id: ${peerId.id}`), 'ERR_NO_ENTRY_FOUND')
      }

      throw err
    }
  }
github ipfs / js-ipfs / src / core / ipns / resolver.js View on Github external
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
    try {
      ipnsEntry = ipns.unmarshal(record)
    } catch (err) {
      log.error(err)

      throw errcode(new Error('found ipns record that we couldn\'t convert to a value'), 'ERR_INVALID_RECORD_RECEIVED')
    }

    // if the record has a public key validate it
    if (ipnsEntry.pubKey) {
      return this._validateRecord(peerId, ipnsEntry)
    }

    // Otherwise, try to get the public key from routing
    let pubKey
    try {
      pubKey = await this._routing.get(routingKey.toBuffer())
    } catch (err) {
github ipfs / js-ipfs / test / core / name-pubsub.js View on Github external
function checkMessage (msg) {
      publishedMessage = msg
      publishedMessageData = ipns.unmarshal(msg.data)
      publishedMessageDataValue = publishedMessageData.value.toString('utf8')
    }
github cloudflare / ipfs-ext / src / verify.js View on Github external
async stepIPNS(data) {
    let peer = PeerId.createFromB58String(this.segments[2])
    let parsed = ipns.unmarshal(data)

    let publicKey = await this.ipnsPublicKey(peer, parsed)

    return new Promise((resolve, reject) => {
      ipns.validate(publicKey, parsed, (err) => {
        if (err != null) {
          reject(err)
          return
        }
        let temp = parsed.value.toString().replace(/\/+/gi, "/").replace(/\/$/, "").split("/")
        if (temp.length >= 3 && temp[0] == "" && (temp[1] == "ipfs" || temp[1] == "ipns")) {
          if (temp[1] == "ipfs") {
            temp[2] = this.parse(temp[2])
          }
          this.segments = join(temp, this.segments.slice(3))
        }
github ipfs / js-ipfs / src / core / ipns / publisher.js View on Github external
_unmarshalData (data) {
    try {
      return ipns.unmarshal(data)
    } catch (err) {
      throw errcode(err, 'ERR_INVALID_RECORD_DATA')
    }
  }