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')) } - diff --git a/src/protocols-table.js b/src/protocols-table.js index b47ab9c5..087b0025 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'], + [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)