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

Commit ba6fe77

Browse files
committed
fix: pass correct types to libp2p dht methods
We convert CIDs to Uint8Arrays unnecessarily which breaks things. Fixes #3502
1 parent 6a2c710 commit ba6fe77

File tree

1 file changed

+5
-32
lines changed
  • packages/ipfs-core/src/components

1 file changed

+5
-32
lines changed

packages/ipfs-core/src/components/dht.js

+5-32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const PeerId = require('peer-id')
4-
const { CID } = require('multiformats/cid')
54
const errCode = require('err-code')
65
const { NotEnabledError } = require('../errors')
76
const get = require('dlv')
@@ -19,15 +18,15 @@ module.exports = ({ network, repo }) => {
1918
*/
2019
async get (key, options = {}) {
2120
const { libp2p } = await use(network, options)
22-
return libp2p._dht.get(normalizeCID(key), options)
21+
return libp2p._dht.get(key, options)
2322
},
2423

2524
/**
2625
* @type {import('ipfs-core-types/src/dht').API["put"]}
2726
*/
2827
async * put (key, value, options) {
2928
const { libp2p } = await use(network, options)
30-
yield * libp2p._dht.put(normalizeCID(key), value)
29+
yield * libp2p._dht.put(key, value)
3130
},
3231

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

39-
for await (const peer of libp2p._dht.findProviders(normalizeCID(cid), {
38+
for await (const peer of libp2p._dht.findProviders(cid, {
4039
maxNumProviders: options.numProviders,
4140
signal: options.signal
4241
})) {
@@ -52,7 +51,7 @@ module.exports = ({ network, repo }) => {
5251
*/
5352
async findPeer (peerId, options) {
5453
const { libp2p } = await use(network, options)
55-
const peer = await libp2p._dht.findPeer(PeerId.createFromB58String(peerId))
54+
const peer = await libp2p._dht.findPeer(PeerId.parse(peerId))
5655

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

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

113-
/**
114-
* Turns given cid in some stringifyable representation, to Uint8Array
115-
* representation. Throws an error if given value isn't a valid CID.
116-
*
117-
* @param {any} cid
118-
* @returns {Uint8Array}
119-
*/
120-
const parseCID = cid => {
121-
try {
122-
const cidStr = cid.toString().split('/')
123-
.filter((/** @type {string} */ part) => part && part !== 'ipfs' && part !== 'ipns')[0]
124-
125-
return CID.parse(cidStr).bytes
126-
} catch (error) {
127-
throw errCode(error, 'ERR_INVALID_CID')
128-
}
129-
}
130-
131-
/**
132-
* Turns given cid in some representation to Uint8Array representation
133-
*
134-
* @param {any} cid
135-
*/
136-
const normalizeCID = cid =>
137-
cid instanceof Uint8Array ? cid : parseCID(cid)
138-
139112
/**
140113
* @param {import('../types').NetworkService} network
141114
* @param {import('ipfs-core-types/src/utils').AbortOptions} [options]

0 commit comments

Comments
 (0)