Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

refactor: return peer ids as strings #581

Merged
merged 5 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions SPEC/MISCELLANEOUS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
| -------- | -------- |
| `Promise<Object>` | An object with the Peer identity |

The Peer identity has the following properties:

- `id: String` - the Peer ID
- `publicKey: String` - the public key of the peer as a base64 encoded string
- `addresses: String[]` - A list of multiaddrs this node is listening on as strings
achingbrain marked this conversation as resolved.
Show resolved Hide resolved
- `agentVersion: String` - The agent version
- `protocolVersion: String` - The supported protocol version

**Example:**

```JavaScript
Expand Down
2 changes: 1 addition & 1 deletion SPEC/SWARM.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ A great source of [examples][] can be found in the tests for this API.
The returned array has the following form:

- `addr: Multiaddr`
alanshaw marked this conversation as resolved.
Show resolved Hide resolved
- `peer: CID`
- `peer: String`
- `latency: String` - Only if `verbose: true` was passed
- `muxer: String` - The type of stream muxer the peer is usng
- `streams: string[]` - Only if `verbose: true`, a list of currently open streams
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"dependencies": {
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"chai-things": "^0.2.0",
"cids": "~0.7.1",
"delay": "^4.3.0",
"dirty-chai": "^2.0.1",
Expand Down
16 changes: 15 additions & 1 deletion src/miscellaneous/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict'

const { getDescribe, getIt, expect } = require('../utils/mocha')
const Multiaddr = require('multiaddr')

/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
Expand All @@ -24,8 +25,21 @@ module.exports = (common, options) => {

it('should get the node ID', async () => {
const res = await ipfs.id()
expect(res).to.have.a.property('id')
expect(res).to.have.a.property('id').that.is.a('string')
expect(res).to.have.a.property('publicKey')
expect(res).to.have.a.property('addresses').that.is.an('array').and.all.satisfy(ma => {
const isString = (ma instanceof String || typeof ma === 'string')
const asString = new Multiaddr(ma).toString()

// TODO: remove when go-IPFS returns nu-school /p2p/ multiaddrs
if (ma.includes('/ipfs/')) {
ma = ma.replace(/ipfs/g, 'p2p')
}

return isString && asString === ma
})
expect(res).to.have.a.property('agentVersion').that.is.a('string')
expect(res).to.have.a.property('protocolVersion').that.is.a('string')
})
})
}
4 changes: 1 addition & 3 deletions src/swarm/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
'use strict'

const multiaddr = require('multiaddr')
const CID = require('cids')
const delay = require('delay')
const { isNode } = require('ipfs-utils/src/env')
const { getDescribe, getIt, expect } = require('../utils/mocha')
Expand Down Expand Up @@ -41,8 +40,7 @@ module.exports = (common, options) => {

expect(peer).to.have.a.property('addr')
expect(multiaddr.isMultiaddr(peer.addr)).to.equal(true)
expect(peer).to.have.a.property('peer')
expect(CID.isCID(peer.peer)).to.equal(true)
expect(peer).to.have.a.property('peer').that.is.a('string')
achingbrain marked this conversation as resolved.
Show resolved Hide resolved
expect(peer).to.not.have.a.property('latency')

/* TODO: These assertions must be uncommented as soon as
Expand Down
1 change: 1 addition & 0 deletions src/utils/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const chai = require('chai')
// Do not reorder these statements - https://github.com/chaijs/chai/issues/1298
chai.use(require('chai-as-promised'))
chai.use(require('dirty-chai'))
chai.use(require('chai-things'))

module.exports.expect = chai.expect

Expand Down