From 7304605a92c52bd107ae3b043f4a11b884df68f9 Mon Sep 17 00:00:00 2001 From: dmitriy ryajov Date: Sat, 25 Feb 2017 23:44:39 -0800 Subject: [PATCH 1/4] feat: adding p2p-circuit proto --- src/protocols-table.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/protocols-table.js b/src/protocols-table.js index b47ab9c5..1a090c72 100644 --- a/src/protocols-table.js +++ b/src/protocols-table.js @@ -42,7 +42,8 @@ Protocols.table = [ [477, 0, 'ws'], [478, 0, 'wss'], [275, 0, 'libp2p-webrtc-star'], - [276, 0, 'libp2p-webrtc-direct'] + [276, 0, 'libp2p-webrtc-direct'], + [277, 0, 'p2p-circuit'] ] Protocols.names = {} From 2037bbe8d4409e8d460b08c0c3bc241b4537465b Mon Sep 17 00:00:00 2001 From: dmitriy ryajov Date: Sun, 5 Mar 2017 02:05:59 -0800 Subject: [PATCH 2/4] feat: added getPeerId method to extract the peer id from the address --- package.json | 2 +- src/index.js | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 5dcbe7a5..9e260e92 100644 --- a/package.json +++ b/package.json @@ -60,4 +60,4 @@ "greenkeeper[bot] ", "npm-to-cdn-bot (by Forbes Lindesay) " ] -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index e64f6e8b..43ab5d15 100644 --- a/src/index.js +++ b/src/index.js @@ -5,6 +5,7 @@ const extend = require('xtend') const codec = require('./codec') const protocols = require('./protocols-table') const varint = require('varint') +const bs58 = require('bs58') const NotImplemented = new Error('Sorry, Not Implemented Yet.') @@ -83,8 +84,8 @@ Multiaddr.prototype.toOptions = function toOptions () { */ Multiaddr.prototype.inspect = function inspect () { return '' + this.buffer.toString('hex') + ' - ' + + codec.bufferToString(this.buffer) + '>' } /** @@ -105,7 +106,7 @@ Multiaddr.prototype.inspect = function inspect () { Multiaddr.prototype.protos = function protos () { return map(this.protoCodes(), function (code) { return extend(protocols(code)) - // copy to prevent users from modifying the internal objs. + // copy to prevent users from modifying the internal objs. }) } @@ -231,6 +232,33 @@ Multiaddr.prototype.decapsulate = function decapsulate (addr) { return Multiaddr(s.slice(0, i)) } +/** + * Extract the peerId if the multiaddr contains one + * + * @return {String} peerId - The id of the peer + * @example + * const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string') + * // + * + * const peerId + * + * try { + * peerId = mh1.getPeerId() + * } catch (err) { + * // not a valid base58 address + * } + */ +Multiaddr.prototype.getPeerId = function getPeerId () { + let b58str = this.stringTuples().filter((tuple) => { + if (tuple[0] === protocols.names['ipfs'].code) { + return true + } + })[0][1] + + bs58.decode(b58str) + return b58str +} + /** * Checks if two Multiaddrs are the same * @@ -428,4 +456,3 @@ Multiaddr.resolve = function resolve (addr, callback) { */ return callback(new Error('not implemented yet')) } - From 569b1746826c9c8e93c9878e2a9e206c84c9be79 Mon Sep 17 00:00:00 2001 From: dmitriy ryajov Date: Sat, 11 Mar 2017 10:36:13 -0800 Subject: [PATCH 3/4] fix: adjust p2p-circuit to match the spec --- src/protocols-table.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/protocols-table.js b/src/protocols-table.js index 1a090c72..82e669c0 100644 --- a/src/protocols-table.js +++ b/src/protocols-table.js @@ -43,7 +43,7 @@ Protocols.table = [ [478, 0, 'wss'], [275, 0, 'libp2p-webrtc-star'], [276, 0, 'libp2p-webrtc-direct'], - [277, 0, 'p2p-circuit'] + [290, V, 'p2p-circuit'] ] Protocols.names = {} From 7165b5f0f29d9d56ff4da5f2d90b20facb01b872 Mon Sep 17 00:00:00 2001 From: dmitriy ryajov Date: Tue, 14 Mar 2017 12:12:48 -0700 Subject: [PATCH 4/4] fix: changing p2p-circiot protocol from V to 0 --- src/protocols-table.js | 2 +- test/index.spec.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/protocols-table.js b/src/protocols-table.js index 82e669c0..087b0025 100644 --- a/src/protocols-table.js +++ b/src/protocols-table.js @@ -43,7 +43,7 @@ Protocols.table = [ [478, 0, 'wss'], [275, 0, 'libp2p-webrtc-star'], [276, 0, 'libp2p-webrtc-direct'], - [290, V, 'p2p-circuit'] + [290, 0, 'p2p-circuit'] ] Protocols.names = {} diff --git a/test/index.spec.js b/test/index.spec.js index aebea0ec..cc8417e1 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -282,6 +282,20 @@ describe('variants', () => { expect(addr.toString()).to.equal(str) }) + it('p2p-circuit', () => { + const str = '/p2p-circuit/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC' + const addr = multiaddr(str) + expect(addr).to.have.property('buffer') + expect(addr.toString()).to.equal(str) + }) + + it('p2p-circuit ipfs', () => { + const str = '/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/p2p-circuit' + const addr = multiaddr(str) + expect(addr).to.have.property('buffer') + expect(addr.toString()).to.equal(str) + }) + it('webrtc-star', () => { const str = '/libp2p-webrtc-star/ip4/127.0.0.1/tcp/9090/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC' const addr = multiaddr(str)