Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const DelegatedPeerRouting = require('libp2p-delegated-peer-routing')
const ipfsHttpClient = require('ipfs-http-client')

// default is to use ipfs.io
const routing = new DelegatedPeerRouting(ipfsHttpClient({
const routing = new DelegatedPeerRouting(ipfsHttpClient.create({
// use default api settings
protocol: 'https',
port: 443,
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
],
"scripts": {
"lint": "aegir lint",
"prepare": "npm run build",
"build": "aegir build",
"test": "aegir test -f test/**/*.spec.js",
"test:node": "aegir test -t node -f test/**/*.spec.js",
Expand All @@ -24,17 +25,17 @@
"devDependencies": {
"aegir": "^33.0.0",
"go-ipfs": "^0.8.0",
"ipfs-http-client": "^49.0.4",
"ipfs-utils": "^6.0.6",
"ipfsd-ctl": "^7.0.2",
"ipfs-http-client": "^50.1.0",
"ipfs-utils": "^8.1.2",
"ipfsd-ctl": "^8.0.2",
"it-all": "^1.0.2"
},
"dependencies": {
"cids": "^1.0.0",
"debug": "^4.3.1",
"multiformats": "^9.0.2",
"p-defer": "^3.0.0",
"p-queue": "^6.3.0",
"peer-id": "^0.14.0"
"peer-id": "libp2p/js-peer-id#chore/update-to-new-multiformats"
},
"browser": {
"go-ipfs": false
Expand Down
9 changes: 4 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const PeerId = require('peer-id')
const CID = require('cids')
const { base58btc } = require('multiformats/bases/base58')
const { default: PQueue } = require('p-queue')
const defer = require('p-defer')
const debug = require('debug')
Expand Down Expand Up @@ -89,8 +89,7 @@ class DelegatedPeerRouting {
* @returns {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>}
*/
async * getClosestPeers (key, options = {}) {
key = new CID(key)
const keyStr = key.toString()
const keyStr = base58btc.encode(key).substring(1)

log('getClosestPeers starts:', keyStr)
options.timeout = options.timeout || DEFAULT_TIMEOUT
Expand All @@ -116,14 +115,14 @@ class DelegatedPeerRouting {
// Track the addresses, so we can yield them when done
result.responses.forEach(response => {
peers.set(response.id, {
id: PeerId.createFromCID(response.id),
id: PeerId.createFromB58String(response.id),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to make this change? I remember we wanted to move towards not using b58 peer IDs and this will mean changing this back again

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PeerId.createFromCID(response.id) here was accidentally taking a base58 encoded string so can't be used any more. Annoyingly the DHT result type 2 has reponse.id as a CID instance whereas type 1 has it as a string.

I've changed it to use PeerId.parse so we can add support for extra bases there without having to refactor everything in future.

multiaddrs: response.addrs
})
})
break
case 2: // Final Peer
yield peers.get(result.id.string) || {
id: PeerId.createFromCID(result.id),
id: PeerId.createFromB58String(result.id),
multiaddrs: []
}
break
Expand Down
20 changes: 10 additions & 10 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('DelegatedPeerRouting', function () {
})

it('should accept an http api client instance at construction time', () => {
const client = ipfsHttpClient({
const client = ipfsHttpClient.create({
protocol: 'http',
port: 8000,
host: 'localhost'
Expand All @@ -97,7 +97,7 @@ describe('DelegatedPeerRouting', function () {
it('should be able to find peers via the delegate with a peer id string', async () => {
const opts = delegatedNode.apiAddr.toOptions()

const router = new DelegatedPeerRouting(ipfsHttpClient({
const router = new DelegatedPeerRouting(ipfsHttpClient.create({
protocol: 'http',
port: opts.port,
host: opts.host
Expand All @@ -114,7 +114,7 @@ describe('DelegatedPeerRouting', function () {

it('should be able to find peers via the delegate with a peerid', async () => {
const opts = delegatedNode.apiAddr.toOptions()
const router = new DelegatedPeerRouting(ipfsHttpClient({
const router = new DelegatedPeerRouting(ipfsHttpClient.create({
protocol: 'http',
port: opts.port,
host: opts.host
Expand All @@ -132,7 +132,7 @@ describe('DelegatedPeerRouting', function () {

it('should be able to specify a timeout', async () => {
const opts = delegatedNode.apiAddr.toOptions()
const router = new DelegatedPeerRouting(ipfsHttpClient({
const router = new DelegatedPeerRouting(ipfsHttpClient.create({
protocol: 'http',
port: opts.port,
host: opts.host
Expand All @@ -150,7 +150,7 @@ describe('DelegatedPeerRouting', function () {

it('should not be able to find peers not on the network', async () => {
const opts = delegatedNode.apiAddr.toOptions()
const router = new DelegatedPeerRouting(ipfsHttpClient({
const router = new DelegatedPeerRouting(ipfsHttpClient.create({
protocol: 'http',
port: opts.port,
host: opts.host
Expand All @@ -167,14 +167,14 @@ describe('DelegatedPeerRouting', function () {
it('should be able to query for the closest peers', async () => {
const opts = delegatedNode.apiAddr.toOptions()

const router = new DelegatedPeerRouting(ipfsHttpClient({
const router = new DelegatedPeerRouting(ipfsHttpClient.create({
protocol: 'http',
port: opts.port,
host: opts.host
}))

const nodeId = await delegatedNode.api.id()
const delegatePeerId = PeerID.createFromCID(nodeId.id)
const delegatePeerId = PeerID.createFromB58String(nodeId.id)

const key = PeerID.createFromB58String(peerIdToFind.id).id
const results = await concat(router.getClosestPeers(key))
Expand All @@ -188,17 +188,17 @@ describe('DelegatedPeerRouting', function () {
})
})

it('should find closest peers even if the peer doesnt exist', async () => {
it('should find closest peers even if the peer does not exist', async () => {
const opts = delegatedNode.apiAddr.toOptions()

const router = new DelegatedPeerRouting(ipfsHttpClient({
const router = new DelegatedPeerRouting(ipfsHttpClient.create({
protocol: 'http',
port: opts.port,
host: opts.host
}))

const nodeId = await delegatedNode.api.id()
const delegatePeerId = PeerID.createFromCID(nodeId.id)
const delegatePeerId = PeerID.createFromB58String(nodeId.id)

const peerId = await PeerID.create({ keyType: 'ed25519' })
const results = await concat(router.getClosestPeers(peerId.id))
Expand Down