Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
}
}
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) {
function checkMessage (msg) {
publishedMessage = msg
publishedMessageData = ipns.unmarshal(msg.data)
publishedMessageDataValue = publishedMessageData.value.toString('utf8')
}
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))
}
_unmarshalData (data) {
try {
return ipns.unmarshal(data)
} catch (err) {
throw errcode(err, 'ERR_INVALID_RECORD_DATA')
}
}