Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

feat: allow passing the id of a network peer to ipfs.id #3386

Merged
merged 7 commits into from
May 13, 2021
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
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'origin/master' into feat/support-peer-i…
…d-arg-to-ipfs-id
  • Loading branch information
achingbrain committed May 12, 2021
commit 6b327fba1de51ad119588ce8e31b6dddd830ad16
11 changes: 9 additions & 2 deletions packages/ipfs-cli/src/commands/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { default: parseDuration } = require('parse-duration')

module.exports = {
command: 'id [peerid]',
command: 'id [peerId]',

describe: 'Shows IPFS Node ID info',

Expand All @@ -23,7 +23,14 @@ module.exports = {
}
},

async handler ({ ctx: { ipfs, print }, format, timeout, peerid: peerId }) {
/**
* @param {object} argv
* @param {import('../types').Context} argv.ctx
* @param {string} argv.format
* @param {number} argv.timeout
* @param {string} [argv.peerId]
*/
async handler ({ ctx: { ipfs, print }, format, timeout, peerId }) {
const id = await ipfs.id({
timeout,
peerId
Expand Down
67 changes: 21 additions & 46 deletions packages/ipfs-core/src/components/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,58 +14,51 @@ const { NotStartedError } = require('../errors')
*/
module.exports = ({ peerId, network }) => {
/**
* Returns the identity of the Peer
*
* @param {IdOptions} [options]
* @returns {Promise<PeerId>}
* @example
* ```js
* const identity = await ipfs.id()
* console.log(identity)
* ```
* @type {import('ipfs-core-types/src/root').API["id"]}
*/
async function id (options = {}) { // eslint-disable-line require-await
options = options || {}

let id = peerId
let publicKey = id.pubKey
/** @type {Multiaddr[]} */
let addresses = []
/** @type {string[]} */
let protocols = []
let agentVersion = `js-ipfs/${pkgversion}`
let protocolVersion = '9000'
let id = peerId
let publicKey = id.pubKey

if (options.peerId) {
if (PeerId.isPeerId(options.peerId)) {
id = options.peerId
} else {
id = PeerId.createFromB58String(options.peerId.toString())
}
const net = network.try()

if (!libp2p) {
if (options.peerId) {
if (!net) {
throw new NotStartedError()
}

const { libp2p } = net

id = PeerId.createFromB58String(options.peerId.toString())

publicKey = libp2p.peerStore.keyBook.get(id)
addresses = libp2p.peerStore.addressBook.getMultiaddrsForPeer(id) || []
protocols = libp2p.peerStore.protoBook.get(id) || []

const meta = libp2p.peerStore.metadataBook.get(id) || {}
agentVersion = meta.agentVersion
protocolVersion = meta.protocolVersion
} else {
if (libp2p) {
// only available while the node is running
addresses = libp2p.transportManager.getAddrs()
protocols = Array.from(libp2p.upgrader.protocols.keys())
}
}

if (net) {
const { libp2p } = net

// only available while the node is running
addresses = libp2p.multiaddrs
protocols = Array.from(libp2p.upgrader.protocols.keys())
}

const idStr = id.toB58String()

return {
id: idStr,
publicKey: publicKey ? uint8ArrayToString(publicKey.bytes, 'base64pad') : undefined,
publicKey: uint8ArrayToString(publicKey.bytes, 'base64pad'),
addresses: addresses
.map(ma => {
const str = ma.toString()
Expand All @@ -79,29 +72,11 @@ module.exports = ({ peerId, network }) => {
return `${str}/p2p/${idStr}`
})
.sort()
.map(ma => multiaddr(ma)),
.map(ma => new Multiaddr(ma)),
agentVersion,
protocolVersion,
protocols: protocols.sort()
}
}
return withTimeoutOption(id)
}

/**
* @typedef {object} ID
* The Peer identity
* @property {string} id - the Peer ID
* @property {string} publicKey - the public key of the peer as a base64 encoded string
* @property {Multiaddr[]} addresses - A list of multiaddrs this node is listening on
* @property {string} agentVersion - The agent version
* @property {string} protocolVersion - The supported protocol version
* @property {string[]} protocols - The supported protocols
*
* @typedef {IdSettings & AbortOptions} IdOptions
*
* @typedef {Object} IdSettings
* @property {string|PeerId} [peerId] - The address of a remote peer
*
* @typedef {import('../utils').AbortOptions} AbortOptions
*/
You are viewing a condensed version of this merge commit. You can view the full changes here.