From 2c23d9a718158c786bd2a7a61509f1e74dd02812 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 27 Mar 2017 14:42:05 +0100 Subject: [PATCH] fix: last touches --- package.json | 4 ++-- src/index.js | 45 ++++++++++++++++++++++----------------------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index ec4696d51d..43f0bc6009 100644 --- a/package.json +++ b/package.json @@ -44,8 +44,8 @@ "libp2p-swarm": "~0.26.19", "multiaddr": "^2.2.3", "peer-book": "~0.3.1", - "peer-id": "~0.8.4", - "peer-info": "~0.8.4" + "peer-id": "~0.8.5", + "peer-info": "~0.8.5" }, "contributors": [ "David Dias ", diff --git a/src/index.js b/src/index.js index 8aab1da8a8..27ad69d4d8 100644 --- a/src/index.js +++ b/src/index.js @@ -160,7 +160,7 @@ class Node extends EventEmitter { if (err) { return callback(err) } - this.peerBook.put(peer) + this.peerBook.put(peerInfo) callback(null, conn) }) } @@ -170,7 +170,7 @@ class Node extends EventEmitter { const peerInfo = this._getPeerInfo(peer) this.peerBook.removeByB58String(peerInfo.id.toB58String()) - this.swarm.hangUp(peer, callback) + this.swarm.hangUp(peerInfo, callback) } handle (protocol, handlerFunc, matchFunc) { @@ -186,27 +186,26 @@ class Node extends EventEmitter { */ _getPeerInfo (peer) { let p - switch (peer) { - case PeerInfo.isPeerInfo(peer): p = peer; break - case multiaddr.isMultiaddr(peer): { - const peerIdB58Str = multiaddr.getPeerId(peer) - try { - p = this.peerBook.getByB58String(peerIdB58Str) - } catch (err) { - p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str)) - } - p.multiaddr.add(peer) - } break - case PeerId.isPeerId(peer): { - const peerIdB58Str = peer.toB58String() - try { - p = this.peerBook.getByB58String(peerIdB58Str) - } catch (err) { - // TODO this is where PeerRouting comes into place - throw new Error('No knowledge about: ' + peerIdB58Str) - } - } break - default: throw new Error('Peer type not recognized') + if (PeerInfo.isPeerInfo(peer)) { + p = peer + } else if (multiaddr.isMultiaddr(peer)) { + const peerIdB58Str = peer.getPeerId() + try { + p = this.peerBook.getByB58String(peerIdB58Str) + } catch (err) { + p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str)) + } + p.multiaddr.add(peer) + } else if (PeerId.isPeerId(peer)) { + const peerIdB58Str = peer.toB58String() + try { + p = this.peerBook.getByB58String(peerIdB58Str) + } catch (err) { + // TODO this is where PeerRouting comes into place + throw new Error('No knowledge about: ' + peerIdB58Str) + } + } else { + throw new Error('peer type not recognized') } return p