From 1b6cf600a6b1348199457ca1fe6f314b6eff8c46 Mon Sep 17 00:00:00 2001 From: Tejas Kumthekar <6154318+tk26@users.noreply.github.com> Date: Wed, 2 Sep 2020 05:19:50 -0500 Subject: [PATCH] feat: add protocol list to ipfs id (#3250) Adds `.protocols` property to the output of `ipfs.id` which contains all libp2p protocols supported by the current node. Closes https://github.com/ipfs/js-ipfs/issues/3219 --- docs/core-api/MISCELLANEOUS.md | 1 + .../src/miscellaneous/id.js | 19 +++++++++++++++++++ .../ipfs-http-client/test/interface.spec.js | 4 ++++ packages/ipfs/src/cli/commands/id.js | 3 ++- packages/ipfs/src/core/components/id.js | 5 ++++- packages/ipfs/src/http/api/resources/id.js | 3 ++- 6 files changed, 32 insertions(+), 3 deletions(-) diff --git a/docs/core-api/MISCELLANEOUS.md b/docs/core-api/MISCELLANEOUS.md index 7b0b588686..6aafaefe2d 100644 --- a/docs/core-api/MISCELLANEOUS.md +++ b/docs/core-api/MISCELLANEOUS.md @@ -61,6 +61,7 @@ The Peer identity has the following properties: - `addresses: Multiaddr[]` - A list of multiaddrs this node is listening on - `agentVersion: String` - The agent version - `protocolVersion: String` - The supported protocol version +- `protocols: String[]` - The supported protocols ### Example diff --git a/packages/interface-ipfs-core/src/miscellaneous/id.js b/packages/interface-ipfs-core/src/miscellaneous/id.js index d862f85a99..9400ef2979 100644 --- a/packages/interface-ipfs-core/src/miscellaneous/id.js +++ b/packages/interface-ipfs-core/src/miscellaneous/id.js @@ -44,5 +44,24 @@ module.exports = (common, options) => { expect(Multiaddr.isMultiaddr(ma)).to.be.true() } }) + + it('should have protocols property', async () => { + const res = await ipfs.id() + + expect(res).to.have.a.property('protocols').that.is.an('array') + + expect(res.protocols).to.have.members([ + '/floodsub/1.0.0', + '/ipfs/bitswap/1.0.0', + '/ipfs/bitswap/1.1.0', + '/ipfs/bitswap/1.2.0', + '/ipfs/id/1.0.0', + '/ipfs/id/push/1.0.0', + '/ipfs/ping/1.0.0', + '/libp2p/circuit/relay/0.1.0', + '/meshsub/1.0.0', + '/meshsub/1.1.0' + ]) + }) }) } diff --git a/packages/ipfs-http-client/test/interface.spec.js b/packages/ipfs-http-client/test/interface.spec.js index 4517893116..34aaf8029f 100644 --- a/packages/ipfs-http-client/test/interface.spec.js +++ b/packages/ipfs-http-client/test/interface.spec.js @@ -475,6 +475,10 @@ describe('interface-ipfs-core tests', () => { { name: 'should include the interface-ipfs-core version', reason: 'TODO not implemented in go-ipfs yet' + }, + { + name: 'should have protocols property', + reason: 'TODO not implemented in go-ipfs yet' } ] }) diff --git a/packages/ipfs/src/cli/commands/id.js b/packages/ipfs/src/cli/commands/id.js index 42ad74a1eb..cd51718efe 100644 --- a/packages/ipfs/src/cli/commands/id.js +++ b/packages/ipfs/src/cli/commands/id.js @@ -11,7 +11,7 @@ module.exports = { format: { alias: 'f', type: 'string', - describe: 'Print Node ID info in the given format. Allowed tokens: ' + describe: 'Print Node ID info in the given format. Allowed tokens: ' }, timeout: { type: 'string', @@ -31,6 +31,7 @@ module.exports = { .replace('', id.protocolVersion) .replace('', id.publicKey) .replace('', (id.addresses || []).map(addr => addr.toString()).join('\n')) + .replace('', (id.protocols || []).map(protocol => protocol.toString()).join('\n')) ) return diff --git a/packages/ipfs/src/core/components/id.js b/packages/ipfs/src/core/components/id.js index 5e911fbd6d..29dd07ff2d 100644 --- a/packages/ipfs/src/core/components/id.js +++ b/packages/ipfs/src/core/components/id.js @@ -9,10 +9,12 @@ module.exports = ({ peerId, libp2p }) => { return withTimeoutOption(async function id () { // eslint-disable-line require-await const id = peerId.toB58String() let addresses = [] + let protocols = [] if (libp2p) { // only available while the node is running addresses = libp2p.transportManager.getAddrs() + protocols = Array.from(libp2p.upgrader.protocols.keys()) } return { @@ -33,7 +35,8 @@ module.exports = ({ peerId, libp2p }) => { .sort() .map(ma => multiaddr(ma)), agentVersion: `js-ipfs/${pkgversion}`, - protocolVersion: '9000' + protocolVersion: '9000', + protocols: protocols.sort() } }) } diff --git a/packages/ipfs/src/http/api/resources/id.js b/packages/ipfs/src/http/api/resources/id.js index 6b375190c1..bdece8a0f6 100644 --- a/packages/ipfs/src/http/api/resources/id.js +++ b/packages/ipfs/src/http/api/resources/id.js @@ -38,7 +38,8 @@ module.exports = { PublicKey: id.publicKey, Addresses: id.addresses, AgentVersion: id.agentVersion, - ProtocolVersion: id.protocolVersion + ProtocolVersion: id.protocolVersion, + Protocols: id.protocols }) } }