From 59ea9c388fb56b3dc14ccf74343ccc29e053a4a9 Mon Sep 17 00:00:00 2001 From: David Dias Date: Wed, 29 Mar 2017 20:02:41 +0100 Subject: [PATCH] feat: append peer id to multiaddr if not there --- package.json | 3 ++- src/index.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 8e5aed83..6401e722 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "dependencies": { "libp2p-ping": "~0.3.2", "libp2p-swarm": "~0.28.0", + "mafmt": "^2.1.8", "multiaddr": "^2.3.0", "peer-book": "~0.3.2", "peer-id": "~0.8.5", @@ -54,4 +55,4 @@ "greenkeeperio-bot ", "mayerwin " ] -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index 224ce347..7b85a7da 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,7 @@ const Swarm = require('libp2p-swarm') const PeerId = require('peer-id') const PeerInfo = require('peer-info') +const mafmt = require('mafmt') const PeerBook = require('peer-book') const multiaddr = require('multiaddr') const EventEmitter = require('events').EventEmitter @@ -47,6 +48,8 @@ class Node extends EventEmitter { this.swarm.on('peer-mux-closed', (peerInfo) => { this.emit('peer:disconnect', peerInfo) + // TODO remove this line + this.peerBook.removeByB58String(peerInfo.id.toB58String()) }) } @@ -104,6 +107,13 @@ class Node extends EventEmitter { } }) + // so that we can have webrtc-star addrs without adding manually the id + this.peerInfo.multiaddrs = this.peerInfo.multiaddrs.map((ma) => { + if (!mafmt.IPFS.matches(ma)) { + ma = ma.encapsulate('/ipfs/' + this.peerInfo.id.toB58String()) + } + }) + this.swarm.listen((err) => { if (err) { return callback(err)