Skip to content

Commit

Permalink
fix: better support for peer ids (#5)
Browse files Browse the repository at this point in the history
* fix: better support for peer ids

* test: add findPeer peer id test
* fix: update peer-id check logic
  • Loading branch information
jacobheun authored Aug 30, 2018
1 parent 88a02b0 commit 60655a9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ jspm_packages/
# Typescript v1 declaration files
typings/

# Documentation
docs

# Optional npm cache directory
.npm

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"dependencies": {
"ipfs-api": "^24.0.1",
"peer-id": "~0.11.0",
"peer-info": "~0.14.1"
},
"contributors": [
Expand Down
16 changes: 15 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const dht = require('ipfs-api/src/dht')
const defaultConfig = require('ipfs-api/src/utils/default-config')

Expand All @@ -20,7 +21,18 @@ class DelegatedPeerRouting {
this.dht = dht(this.api)
}

/**
* Attempts to find the given peer
*
* @param {PeerID} id
* @param {function(Error, PeerInfo)} callback
* @returns {void}
*/
findPeer (id, callback) {
if (PeerId.isPeerId(id)) {
id = id.toB58String()
}

this.dht.findpeer(id, (err, results) => {
if (err) {
return callback(err)
Expand All @@ -38,7 +50,9 @@ class DelegatedPeerRouting {
return callback(peerNotFoundError(id))
}
const details = wantedResponse.Responses[0]
const info = new PeerInfo(details.ID)
const info = new PeerInfo(
PeerId.createFromB58String(details.ID)
)
details.Addrs.forEach((addr) => info.multiaddrs.add(addr))

// there should be only one of these
Expand Down
20 changes: 18 additions & 2 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const expect = require('chai').expect
const IPFSFactory = require('ipfsd-ctl')
const async = require('async')
const PeerID = require('peer-id')

const DelegatedPeerRouting = require('../src')
const factory = IPFSFactory.create({ type: 'go' })
Expand Down Expand Up @@ -116,7 +117,7 @@ describe('DelegatedPeerRouting', function () {
})

describe('findPeers', () => {
it('should be able to find peers via the delegate', (done) => {
it('should be able to find peers via the delegate with a peer id string', (done) => {
const opts = delegatedNode.apiAddr.toOptions()
const router = new DelegatedPeerRouting({
protocol: 'http',
Expand All @@ -126,7 +127,22 @@ describe('DelegatedPeerRouting', function () {

router.findPeer(peerIdToFind.id, (err, peer) => {
expect(err).to.equal(null)
expect(peer.id).to.eql(peerIdToFind.id)
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
done()
})
})

it('should be able to find peers via the delegate with a peerid', (done) => {
const opts = delegatedNode.apiAddr.toOptions()
const router = new DelegatedPeerRouting({
protocol: 'http',
port: opts.port,
host: opts.host
})

router.findPeer(PeerID.createFromB58String(peerIdToFind.id), (err, peer) => {
expect(err).to.equal(null)
expect(peer.id.toB58String()).to.eql(peerIdToFind.id)
done()
})
})
Expand Down

0 comments on commit 60655a9

Please sign in to comment.