From ba6fe77b42e16970fac8079782fe04544f22184f Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 11 Aug 2021 18:04:33 +0100 Subject: [PATCH] fix: pass correct types to libp2p dht methods We convert CIDs to Uint8Arrays unnecessarily which breaks things. Fixes #3502 --- packages/ipfs-core/src/components/dht.js | 37 ++++-------------------- 1 file changed, 5 insertions(+), 32 deletions(-) diff --git a/packages/ipfs-core/src/components/dht.js b/packages/ipfs-core/src/components/dht.js index 7186cad605..a62d8f5317 100644 --- a/packages/ipfs-core/src/components/dht.js +++ b/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') @@ -19,7 +18,7 @@ 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) }, /** @@ -27,7 +26,7 @@ module.exports = ({ network, repo }) => { */ async * put (key, value, options) { const { libp2p } = await use(network, options) - yield * libp2p._dht.put(normalizeCID(key), value) + yield * libp2p._dht.put(key, value) }, /** @@ -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 })) { @@ -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(), @@ -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? @@ -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]