Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
}
// check if message is `CAN_HOP`
if (message.type === proto.Type.CAN_HOP) {
this.utils.writeResponse(
sh,
proto.Status.SUCCESS)
return sh.close()
}
// This is a relay request - validate and create a circuit
let srcPeerId = null
let dstPeerId = null
try {
srcPeerId = PeerId.createFromBytes(message.srcPeer.id).toB58String()
dstPeerId = PeerId.createFromBytes(message.dstPeer.id).toB58String()
} catch (err) {
log.err(err)
if (!srcPeerId) {
this.utils.writeResponse(
sh,
proto.Status.HOP_SRC_MULTIADDR_INVALID)
return sh.close()
}
if (!dstPeerId) {
this.utils.writeResponse(
sh,
proto.Status.HOP_DST_MULTIADDR_INVALID)
return sh.close()
}
validate: async (marshalledData, key) => {
const receivedEntry = unmarshal(marshalledData)
const bufferId = key.slice('/ipns/'.length)
const peerId = PeerId.createFromBytes(bufferId)
// extract public key
const pubKey = extractPublicKey(peerId, receivedEntry)
// Record validation
await validate(pubKey, receivedEntry)
return true
},
select: (dataA, dataB) => {
// close/end the source stream if there was an error
if (srcSh) {
srcSh.close()
}
if (dstSh) {
dstSh.close()
}
return callback(err)
}
const src = srcSh.rest()
const dst = dstSh.rest()
const srcIdStr = PeerId.createFromBytes(message.srcPeer.id).toB58String()
const dstIdStr = PeerId.createFromBytes(message.dstPeer.id).toB58String()
// circuit the src and dst streams
pull(
src,
dst,
src
)
log('circuit %s <-> %s established', srcIdStr, dstIdStr)
callback()
})
}
serializedPeerInfos.map(async serializedPeerInfo => {
const peerId = PeerId.createFromBytes(serializedPeerInfo[0])
if (serializedPeerInfo.length === 3) {
peerId.pubKey = libp2p_crypto.unmarshalPublicKey(serializedPeerInfo[2])
}
const peerInfo = await PeerInfo.create(peerId)
serializedPeerInfo[1].forEach(multiaddr => peerInfo.multiaddrs.add(Multiaddr(multiaddr)))
peerBook.put(peerInfo)
})
)
_dialPeer (dstPeer, callback) {
const peerInfo = new PeerInfo(PeerId.createFromBytes(dstPeer.id))
dstPeer.addrs.forEach((a) => peerInfo.multiaddrs.add(a))
this.swarm.dial(peerInfo, multicodec.relay, once((err, conn) => {
if (err) {
log.err(err)
return callback(err)
}
callback(null, conn)
}))
}
}