Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better support for peer ids #5

Merged
merged 3 commits into from
Aug 30, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
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