Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 8 additions & 3 deletions src/core/components/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
const pkgversion = require('../../../package.json').version
const multiaddr = require('multiaddr')

module.exports = ({ peerInfo }) => {
module.exports = ({ peerInfo, libp2p }) => {
return async function id () { // eslint-disable-line require-await
const id = peerInfo.id.toB58String()
let addresses = []

if (libp2p) {
// only available while the node is running
addresses = libp2p.transportManager.getAddrs()
}

return {
id,
publicKey: peerInfo.id.pubKey.bytes.toString('base64'),
addresses: peerInfo.multiaddrs
.toArray()
addresses: addresses
.map(ma => {
const str = ma.toString()

Expand Down
2 changes: 1 addition & 1 deletion src/core/components/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ function createApi ({
dns,
files,
get: Components.get({ ipld, preload }),
id: Components.id({ peerInfo }),
id: Components.id({ peerInfo, libp2p }),
init: async () => { throw new AlreadyInitializedError() }, // eslint-disable-line require-await
isOnline,
key: {
Expand Down
52 changes: 52 additions & 0 deletions test/core/id.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* eslint-env mocha */
'use strict'

const { expect } = require('interface-ipfs-core/src/utils/mocha')
const multiaddr = require('multiaddr')
const { isBrowser, isWebWorker } = require('ipfs-utils/src/env')
const factory = require('../utils/factory')

describe('id', function () {
this.timeout(60 * 1000)
const df = factory()
let node

before(async () => {
node = (await df.spawn({
type: 'proc',
ipfsOptions: {
config: {
Addresses: {
Swarm: []
}
}
}
})).api
})

after(async () => {
await node.stop()
})

it('should return swarm ports opened after startup', async function () {
if (isWebWorker) {
// TODO: webworkers are not currently dialable
return this.skip()
}

await expect(node.id()).to.eventually.have.property('addresses').that.is.empty()

let servers = [
multiaddr('/ip4/127.0.0.1/tcp/0')
]

if (isBrowser) {
servers = [
multiaddr('/ip4/127.0.0.1/tcp/14579/wss/p2p-webrtc-star')
]
}

await node.libp2p.transportManager.listen(servers)
await expect(node.id()).to.eventually.have.property('addresses').that.is.not.empty()
})
})