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

Commit

Permalink
fix: use peer store for id (#3973)
Browse files Browse the repository at this point in the history
Instead of using all the various books, just get the peer from the libp2p peer store, trying to fetch the public key from the dht if it's missing.
  • Loading branch information
achingbrain committed Dec 13, 2021
1 parent dec9e4c commit adde8c1
Showing 1 changed file with 62 additions and 20 deletions.
82 changes: 62 additions & 20 deletions packages/ipfs-core/src/components/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { withTimeoutOption } from 'ipfs-core-utils/with-timeout-option'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import PeerId from 'peer-id'
import { NotStartedError } from '../errors.js'
import errCode from 'err-code'

/**
* @typedef {import('libp2p')} Libp2p
* @typedef {import('ipfs-core-types/src/utils').AbortOptions} AbortOptions
*/

/**
* @param {Object} config
Expand All @@ -15,10 +21,6 @@ export function createId ({ peerId, network }) {
* @type {import('ipfs-core-types/src/root').API<{}>["id"]}
*/
async function id (options = {}) { // eslint-disable-line require-await
if (options.peerId === peerId.toB58String()) {
delete options.peerId
}

const net = network.try()

if (!net) {
Expand All @@ -38,25 +40,17 @@ export function createId ({ peerId, network }) {
}
}

const id = options.peerId ? PeerId.createFromB58String(options.peerId.toString()) : peerId
const { libp2p } = net

let publicKey = id.pubKey ? id.pubKey : libp2p.peerStore.keyBook.get(id)

if (!publicKey) {
publicKey = await libp2p._dht.getPublicKey(id, options)
}

const addresses = options.peerId ? libp2p.peerStore.addressBook.getMultiaddrsForPeer(id) : libp2p.multiaddrs
const protocols = options.peerId ? libp2p.peerStore.protoBook.get(id) : Array.from(libp2p.upgrader.protocols.keys())
const agentVersion = uint8ArrayToString(libp2p.peerStore.metadataBook.getValue(id, 'AgentVersion') || new Uint8Array())
const protocolVersion = uint8ArrayToString(libp2p.peerStore.metadataBook.getValue(id, 'ProtocolVersion') || new Uint8Array())
const idStr = id.toB58String()
const peerIdToId = options.peerId ? PeerId.parse(options.peerId) : peerId
const peer = await findPeer(peerIdToId, libp2p, options)
const agentVersion = uint8ArrayToString(peer.metadata.get('AgentVersion') || new Uint8Array())
const protocolVersion = uint8ArrayToString(peer.metadata.get('ProtocolVersion') || new Uint8Array())
const idStr = peer.id.toB58String()

return {
id: idStr,
publicKey: uint8ArrayToString(publicKey.bytes, 'base64pad'),
addresses: (addresses || [])
publicKey: uint8ArrayToString(peer.publicKey.bytes, 'base64pad'),
addresses: (peer.addresses || [])
.map(ma => {
const str = ma.toString()

Expand All @@ -72,8 +66,56 @@ export function createId ({ peerId, network }) {
.map(ma => new Multiaddr(ma)),
agentVersion,
protocolVersion,
protocols: (protocols || []).sort()
protocols: (peer.protocols || []).sort()
}
}
return withTimeoutOption(id)
}

/**
* @param {PeerId} peerId
* @param {Libp2p} libp2p
* @param {AbortOptions} options
*/
async function findPeer (peerId, libp2p, options) {
let peer = libp2p.peerStore.get(peerId)

if (!peer) {
peer = await findPeerOnDht(peerId, libp2p, options)
}

let publicKey = peerId.pubKey ? peerId.pubKey : libp2p.peerStore.keyBook.get(peerId)

if (!publicKey) {
publicKey = await libp2p._dht.getPublicKey(peerId, options)
}

return {
...peer,
publicKey,
metadata: peer.metadata || new Map(),
addresses: peer.addresses.map(addr => addr.multiaddr)
}
}

/**
*
* @param {PeerId} peerId
* @param {Libp2p} libp2p
* @param {AbortOptions} options
*/
async function findPeerOnDht (peerId, libp2p, options) {
for await (const event of libp2p._dht.findPeer(peerId, options)) {
if (event.name === 'FINAL_PEER') {
break
}
}

const peer = libp2p.peerStore.get(peerId)

if (!peer) {
throw errCode(new Error('Could not find peer'), 'ERR_NOT_FOUND')
}

return peer
}

0 comments on commit adde8c1

Please sign in to comment.