diff --git a/README.md b/README.md index ea7159850..c9e0df980 100644 --- a/README.md +++ b/README.md @@ -259,8 +259,8 @@ const ipfs = ipfsClient({ - [`ipfs.bitswap.unwant(cid)`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/BITSWAP.md#bitswapunwant) - [dht](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md) - - [`ipfs.dht.findpeer(peerId, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindpeer) - - [`ipfs.dht.findprovs(hash, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindprovs) + - [`ipfs.dht.findPeer(peerId, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindpeer) + - [`ipfs.dht.findProvs(hash, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtfindprovs) - [`ipfs.dht.get(key, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtget) - [`ipfs.dht.provide(cid, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtprovide) - [`ipfs.dht.put(key, value, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DHT.md#dhtput) diff --git a/package.json b/package.json index 911fafc8f..72374523e 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ "eslint-plugin-react": "^7.11.1", "go-ipfs-dep": "~0.4.18", "gulp": "^3.9.1", - "interface-ipfs-core": "ipfs/interface-ipfs-core#fix/update-dht-responses", + "interface-ipfs-core": "~0.91.0", "ipfsd-ctl": "~0.40.0", "nock": "^10.0.2", "pull-stream": "^3.6.9", diff --git a/src/dht/findpeer.js b/src/dht/findpeer.js index fa34a7a92..98611468c 100644 --- a/src/dht/findpeer.js +++ b/src/dht/findpeer.js @@ -3,6 +3,9 @@ const promisify = require('promisify-es6') const streamToValueWithTransformer = require('../utils/stream-to-value-with-transformer') +const multiaddr = require('multiaddr') +const PeerId = require('peer-id') +const PeerInfo = require('peer-info') const errcode = require('err-code') module.exports = (send) => { @@ -25,40 +28,23 @@ module.exports = (send) => { res = res[0] } - // Type 2 keys (inconsistencies between go core and js core) - if (res.Type !== 2 && res.type !== 2) { + // Type 2 keys + if (res.Type !== 2) { const errMsg = `key was not found (type 2)` return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_2_NOT_FOUND')) } - // inconsistencies between go core and js core - let id - let addrs + const responseReceived = res.Responses[0] + const peerInfo = new PeerInfo(PeerId.createFromB58String(responseReceived.ID)) - if (res.Responses) { - id = res.Responses[0].ID - addrs = res.Responses[0].Addrs - } else { - id = res.responses[0].id - addrs = res.responses[0].addrs - } + responseReceived.Addrs.forEach((addr) => { + const ma = multiaddr(addr) - // inconsistencies js / go - go does not add `/ipfs/{id}` to the address - addrs = addrs.map((addr) => { - if (addr.split('/ipfs/') > -1) { - return addr - } else { - return `${addr}/ipfs/${id}` - } + peerInfo.multiaddrs.add(ma) }) - callback(null, { - responses: [{ - id, - addrs - }] - }) + callback(null, peerInfo) } send({ diff --git a/src/dht/findprovs.js b/src/dht/findprovs.js index 24c946c64..8585b054c 100644 --- a/src/dht/findprovs.js +++ b/src/dht/findprovs.js @@ -3,6 +3,9 @@ const promisify = require('promisify-es6') const streamToValueWithTransformer = require('../utils/stream-to-value-with-transformer') +const multiaddr = require('multiaddr') +const PeerId = require('peer-id') +const PeerInfo = require('peer-info') const errcode = require('err-code') module.exports = (send) => { @@ -25,23 +28,26 @@ module.exports = (send) => { res = res[0] } - // Type 4 keys (inconsistencies between go core and js core) - if (res.Type !== 4 && res.type !== 4) { + // Type 4 keys + if (res.Type !== 4) { const errMsg = `key was not found (type 4)` return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_4_NOT_FOUND')) } - // inconsistencies between go core and js core - const recResponses = res.Responses || res.responses + const responses = res.Responses.map((r) => { + const peerInfo = new PeerInfo(PeerId.createFromB58String(r.ID)) - // providers array (handling inconsistencies) - const responses = recResponses.map((r) => ({ - id: r.ID || r.id, - addrs: r.Addrs || r.addrs - })) + r.Addrs.forEach((addr) => { + const ma = multiaddr(addr) - callback(null, { responses }) + peerInfo.multiaddrs.add(ma) + }) + + return peerInfo + }) + + callback(null, responses) } send({ diff --git a/src/dht/index.js b/src/dht/index.js index d3677c49f..b4ab8c640 100644 --- a/src/dht/index.js +++ b/src/dht/index.js @@ -8,8 +8,8 @@ module.exports = (arg) => { return { get: require('./get')(send), put: require('./put')(send), - findprovs: require('./findprovs')(send), - findpeer: require('./findpeer')(send), + findProvs: require('./findprovs')(send), + findPeer: require('./findpeer')(send), provide: require('./provide')(send), // find closest peerId to given peerId query: require('./query')(send) diff --git a/src/dht/query.js b/src/dht/query.js index f1cdb32d0..2dbc62a47 100644 --- a/src/dht/query.js +++ b/src/dht/query.js @@ -1,7 +1,10 @@ 'use strict' const promisify = require('promisify-es6') -const streamToValue = require('../utils/stream-to-value') +const streamToValueWithTransformer = require('../utils/stream-to-value-with-transformer') + +const PeerId = require('peer-id') +const PeerInfo = require('peer-info') module.exports = (send) => { return promisify((peerId, opts, callback) => { @@ -17,6 +20,12 @@ module.exports = (send) => { opts = {} } + const handleResult = (res, callback) => { + const peerIds = res.map((r) => (new PeerInfo(PeerId.createFromB58String(r.ID)))) + + callback(null, peerIds) + } + send({ path: 'dht/query', args: peerId, @@ -26,11 +35,7 @@ module.exports = (send) => { return callback(err) } - if (typeof result.pipe === 'function') { - streamToValue(result, callback) - } else { - callback(null, result) - } + streamToValueWithTransformer(result, handleResult, callback) }) }) } diff --git a/test/sub-modules.spec.js b/test/sub-modules.spec.js index 0401eccfe..1700a1097 100644 --- a/test/sub-modules.spec.js +++ b/test/sub-modules.spec.js @@ -49,8 +49,8 @@ describe('submodules', () => { expect(dht.get).to.be.a('function') expect(dht.put).to.be.a('function') - expect(dht.findprovs).to.be.a('function') - expect(dht.findpeer).to.be.a('function') + expect(dht.findProvs).to.be.a('function') + expect(dht.findPeer).to.be.a('function') expect(dht.provide).to.be.a('function') expect(dht.query).to.be.a('function') })