From 26639333b2e25d2aa29bfd8a3c1af7cf4a30ed87 Mon Sep 17 00:00:00 2001 From: dmitriy ryajov Date: Sun, 5 Mar 2017 02:05:59 -0800 Subject: [PATCH 1/6] feat: added getPeerId method to extract the peer id from the address --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0533d36a..072b3711 100644 --- a/package.json +++ b/package.json @@ -62,4 +62,4 @@ "greenkeeper[bot] ", "npm-to-cdn-bot (by Forbes Lindesay) " ] -} \ No newline at end of file +} From 4e0b2750c282ee73ec11b90ff27645dd8bd2ab45 Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 16 Mar 2017 15:04:59 +0000 Subject: [PATCH 2/6] chore: update aegir and fix linting --- test/index.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.spec.js b/test/index.spec.js index df4ad994..d0b9e2c9 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -579,7 +579,7 @@ describe('helpers', () => { const str = '/ip4/127.0.0.1' const addr = multiaddr(str) multiaddr.resolve(addr, (err, multiaddrs) => { - expect(err).to.exist() + expect(err).to.exist done() }) }) From 47d39b5551c3016ff1d7ece0b37070b1dfcc17c7 Mon Sep 17 00:00:00 2001 From: dmitriy ryajov Date: Mon, 27 Mar 2017 11:36:40 -0700 Subject: [PATCH 3/6] feat: don't throw on invalid b58 string in getPeerId --- src/index.js | 18 +++++++++++------- test/index.spec.js | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index 43ab5d15..fd7798b7 100644 --- a/src/index.js +++ b/src/index.js @@ -106,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. }) } @@ -249,13 +249,17 @@ Multiaddr.prototype.decapsulate = function decapsulate (addr) { * } */ Multiaddr.prototype.getPeerId = function getPeerId () { - let b58str = this.stringTuples().filter((tuple) => { - if (tuple[0] === protocols.names['ipfs'].code) { - return true - } - })[0][1] + let b58str + try { + b58str = this.stringTuples().filter((tuple) => { + if (tuple[0] === protocols.names['ipfs'].code) { + return true + } + })[0][1] + + bs58.decode(b58str) + } catch (e) {} - bs58.decode(b58str) return b58str } diff --git a/test/index.spec.js b/test/index.spec.js index d0b9e2c9..df4ad994 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -579,7 +579,7 @@ describe('helpers', () => { const str = '/ip4/127.0.0.1' const addr = multiaddr(str) multiaddr.resolve(addr, (err, multiaddrs) => { - expect(err).to.exist + expect(err).to.exist() done() }) }) From bc74080205241e5f3c6446bd4a0b17941f8589ca Mon Sep 17 00:00:00 2001 From: dmitriy ryajov Date: Mon, 27 Mar 2017 12:51:02 -0700 Subject: [PATCH 4/6] test: adding tests for .getPeerId --- src/index.js | 2 +- test/index.spec.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index fd7798b7..217a1585 100644 --- a/src/index.js +++ b/src/index.js @@ -249,7 +249,7 @@ Multiaddr.prototype.decapsulate = function decapsulate (addr) { * } */ Multiaddr.prototype.getPeerId = function getPeerId () { - let b58str + let b58str = null try { b58str = this.stringTuples().filter((tuple) => { if (tuple[0] === protocols.names['ipfs'].code) { diff --git a/test/index.spec.js b/test/index.spec.js index df4ad994..23f414d2 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -535,6 +535,22 @@ describe('helpers', () => { }) }) + describe('.getPeerId should parse id from multiaddr', () => { + it('parses extracts the peer Id from a multiaddr', () => { + expect( + multiaddr('/p2p-circuit/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC').getPeerId() + ).to.equal('QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC') + }) + }) + + describe('.getPeerId should return null on missing peer id in multiaddr', () => { + it('parses extracts the peer Id from a multiaddr', () => { + expect( + multiaddr('/ip4/0.0.0.0/tcp/1234/utp').getPeerId() + ).to.be.null() + }) + }) + describe('multiaddr.isMultiaddr', () => { it('handles different inputs', () => { expect(multiaddr.isMultiaddr(multiaddr('/'))).to.be.eql(true) From d431981541662152404e345a71a698f7ce7c8961 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 27 Mar 2017 14:03:20 -0700 Subject: [PATCH 5/6] doc: fixed comments to match new implementation --- src/index.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index 217a1585..40153d3a 100644 --- a/src/index.js +++ b/src/index.js @@ -235,18 +235,13 @@ Multiaddr.prototype.decapsulate = function decapsulate (addr) { /** * Extract the peerId if the multiaddr contains one * - * @return {String} peerId - The id of the peer + * @return {String|null} peerId - The id of the peer or null if invalid or missing from the ma * @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 - * } + * // should return QmValidBase58string or null if the id is missing or invalid + * const peerId = mh1.getPeerId() */ Multiaddr.prototype.getPeerId = function getPeerId () { let b58str = null From 32d3bc394a49db95f1d92e95f2195d22873e6f62 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 27 Mar 2017 14:12:30 -0700 Subject: [PATCH 6/6] fix: set b58string to null on error --- src/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 40153d3a..e777f29b 100644 --- a/src/index.js +++ b/src/index.js @@ -253,7 +253,9 @@ Multiaddr.prototype.getPeerId = function getPeerId () { })[0][1] bs58.decode(b58str) - } catch (e) {} + } catch (e) { + b58str = null + } return b58str }