From 5429c409db09985aad603442425cef1231238bdd Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Tue, 24 Sep 2019 14:26:35 +0200 Subject: [PATCH 1/3] fix: support multiaddr7 addr filtering License: MIT Signed-off-by: Jacob Heun --- package.json | 3 ++- src/index.js | 4 +++- test/filter.spec.js | 7 +++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index ddc480e..b0b60b0 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,8 @@ "chai": "^4.2.0", "dirty-chai": "^2.0.1", "interface-transport": "~0.3.6", - "pull-stream": "^3.6.14" + "pull-stream": "^3.6.14", + "multiaddr7": "npm:multiaddr@^7.0.0" }, "dependencies": { "class-is": "^1.1.0", diff --git a/src/index.js b/src/index.js index b04c223..78a3987 100644 --- a/src/index.js +++ b/src/index.js @@ -73,7 +73,9 @@ class TCP { return false } - if (includes(ma.protoNames(), 'ipfs')) { + if (typeof ma.decapsulateCode === 'function') { + ma = ma.decapsulateCode(421) // multiaddr 7 + } else if (includes(ma.protoNames(), 'ipfs')) { ma = ma.decapsulate('ipfs') } diff --git a/test/filter.spec.js b/test/filter.spec.js index b6ce629..4985a1d 100644 --- a/test/filter.spec.js +++ b/test/filter.spec.js @@ -7,6 +7,7 @@ const expect = chai.expect chai.use(dirtyChai) const TCP = require('../src') const multiaddr = require('multiaddr') +const multiaddr7 = require('multiaddr7') describe('filter addrs', () => { const base = '/ip4/127.0.0.1' @@ -27,11 +28,13 @@ describe('filter addrs', () => { const ma6 = multiaddr('/ip4/127.0.0.1/tcp/9090/p2p-circuit' + ipfs) const ma7 = multiaddr('/dns4/libp2p.io/tcp/9090') const ma8 = multiaddr('/dnsaddr/libp2p.io/tcp/9090') + const ma9 = multiaddr7(base + '/tcp/9090/p2p/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw') - const valid = tcp.filter([ma1, ma2, ma3, ma4, ma5, ma6, ma7, ma8]) - expect(valid.length).to.equal(4) + const valid = tcp.filter([ma1, ma2, ma3, ma4, ma5, ma6, ma7, ma8, ma9]) + expect(valid.length).to.equal(5) expect(valid[0]).to.deep.equal(ma1) expect(valid[1]).to.deep.equal(ma4) + expect(valid[4]).to.deep.equal(ma9) }) it('filter a single addr for this transport', () => { From bf532d181a1efa473f2e7c89dd2c9a7061cc467f Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Tue, 24 Sep 2019 14:29:35 +0200 Subject: [PATCH 2/3] test: add multiaddr7 dial test License: MIT Signed-off-by: Jacob Heun --- test/listen-dial.spec.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/listen-dial.spec.js b/test/listen-dial.spec.js index 7d8f119..8a59872 100644 --- a/test/listen-dial.spec.js +++ b/test/listen-dial.spec.js @@ -9,6 +9,7 @@ chai.use(dirtyChai) const TCP = require('../src') const net = require('net') const multiaddr = require('multiaddr') +const multiaddr7 = require('multiaddr7') const isCI = process.env.CI describe('listen', () => { @@ -256,4 +257,19 @@ describe('dial', () => { }) ) }) + + it('dial on IPv4 with IPFS Id multiaddr7', (done) => { + const ma = multiaddr7('/ip4/127.0.0.1/tcp/9090/p2p/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw') + const conn = tcp.dial(ma) + + pull( + pull.values(['hey']), + conn, + pull.collect((err, res) => { + expect(err).to.not.exist() + expect(res).to.be.eql([Buffer.from('hey!')]) + done() + }) + ) + }) }) From ba0346cbb65e9caeceef290f44ab2b2dcff1af66 Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Tue, 24 Sep 2019 14:36:48 +0200 Subject: [PATCH 3/3] fix: listen with multiaddr7 License: MIT Signed-off-by: Jacob Heun --- src/listener.js | 5 ++++- test/listen-dial.spec.js | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/listener.js b/src/listener.js index 30b3076..232a8f6 100644 --- a/src/listener.js +++ b/src/listener.js @@ -84,7 +84,10 @@ module.exports = (handler) => { listener.listen = (ma, callback) => { listeningAddr = ma - if (includes(ma.protoNames(), 'ipfs')) { + if (typeof ma.decapsulateCode === 'function') { + ipfsId = getIpfsId(ma) + listeningAddr = ma.decapsulateCode(IPFS_CODE) + } else if (includes(ma.protoNames(), 'ipfs')) { ipfsId = getIpfsId(ma) listeningAddr = ma.decapsulate('ipfs') } diff --git a/test/listen-dial.spec.js b/test/listen-dial.spec.js index 8a59872..a8c4174 100644 --- a/test/listen-dial.spec.js +++ b/test/listen-dial.spec.js @@ -127,6 +127,19 @@ describe('listen', () => { }) }) }) + + it('getAddrs preserves IPFS Id (multiaddr 7)', (done) => { + const mh = multiaddr7('/ip4/127.0.0.1/tcp/9090/p2p/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw') + const listener = tcp.createListener((conn) => {}) + listener.listen(mh, () => { + listener.getAddrs((err, multiaddrs) => { + expect(err).to.not.exist() + expect(multiaddrs.length).to.equal(1) + expect(multiaddrs[0]).to.deep.equal(mh) + listener.close(done) + }) + }) + }) }) describe('dial', () => {