Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: pass correct types to libp2p dht methods (#3806)
Browse files Browse the repository at this point in the history
We convert CIDs to Uint8Arrays unnecessarily which breaks things.

Fixes #3502
  • Loading branch information
achingbrain committed Aug 12, 2021
1 parent 474523a commit 5c8da9a
Showing 1 changed file with 5 additions and 32 deletions.
37 changes: 5 additions & 32 deletions packages/ipfs-core/src/components/dht.js
@@ -1,7 +1,6 @@
'use strict'

const PeerId = require('peer-id')
const { CID } = require('multiformats/cid')
const errCode = require('err-code')
const { NotEnabledError } = require('../errors')
const get = require('dlv')
Expand All @@ -19,15 +18,15 @@ module.exports = ({ network, repo }) => {
*/
async get (key, options = {}) {
const { libp2p } = await use(network, options)
return libp2p._dht.get(normalizeCID(key), options)
return libp2p._dht.get(key, options)
},

/**
* @type {import('ipfs-core-types/src/dht').API["put"]}
*/
async * put (key, value, options) {
const { libp2p } = await use(network, options)
yield * libp2p._dht.put(normalizeCID(key), value)
yield * libp2p._dht.put(key, value)
},

/**
Expand All @@ -36,7 +35,7 @@ module.exports = ({ network, repo }) => {
async * findProvs (cid, options = { numProviders: 20 }) {
const { libp2p } = await use(network, options)

for await (const peer of libp2p._dht.findProviders(normalizeCID(cid), {
for await (const peer of libp2p._dht.findProviders(cid, {
maxNumProviders: options.numProviders,
signal: options.signal
})) {
Expand All @@ -52,7 +51,7 @@ module.exports = ({ network, repo }) => {
*/
async findPeer (peerId, options) {
const { libp2p } = await use(network, options)
const peer = await libp2p._dht.findPeer(PeerId.createFromB58String(peerId))
const peer = await libp2p._dht.findPeer(PeerId.parse(peerId))

return {
id: peer.id.toB58String(),
Expand Down Expand Up @@ -91,7 +90,7 @@ module.exports = ({ network, repo }) => {
async * query (peerId, options) {
const { libp2p } = await use(network, options)

for await (const closerPeerId of libp2p._dht.getClosestPeers(PeerId.createFromB58String(peerId).toBytes())) {
for await (const closerPeerId of libp2p._dht.getClosestPeers(PeerId.parse(peerId).toBytes())) {
yield {
id: closerPeerId.toB58String(),
addrs: [] // TODO: get addrs?
Expand All @@ -110,32 +109,6 @@ module.exports = ({ network, repo }) => {
}
}

/**
* Turns given cid in some stringifyable representation, to Uint8Array
* representation. Throws an error if given value isn't a valid CID.
*
* @param {any} cid
* @returns {Uint8Array}
*/
const parseCID = cid => {
try {
const cidStr = cid.toString().split('/')
.filter((/** @type {string} */ part) => part && part !== 'ipfs' && part !== 'ipns')[0]

return CID.parse(cidStr).bytes
} catch (error) {
throw errCode(error, 'ERR_INVALID_CID')
}
}

/**
* Turns given cid in some representation to Uint8Array representation
*
* @param {any} cid
*/
const normalizeCID = cid =>
cid instanceof Uint8Array ? cid : parseCID(cid)

/**
* @param {import('../types').NetworkService} network
* @param {import('ipfs-core-types/src/utils').AbortOptions} [options]
Expand Down

0 comments on commit 5c8da9a

Please sign in to comment.