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

Commit adde8c1

Browse files
authored
fix: use peer store for id (#3973)
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.
1 parent dec9e4c commit adde8c1

File tree

1 file changed

+62
-20
lines changed
  • packages/ipfs-core/src/components

1 file changed

+62
-20
lines changed

packages/ipfs-core/src/components/id.js

+62-20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import { withTimeoutOption } from 'ipfs-core-utils/with-timeout-option'
44
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
55
import PeerId from 'peer-id'
66
import { NotStartedError } from '../errors.js'
7+
import errCode from 'err-code'
8+
9+
/**
10+
* @typedef {import('libp2p')} Libp2p
11+
* @typedef {import('ipfs-core-types/src/utils').AbortOptions} AbortOptions
12+
*/
713

814
/**
915
* @param {Object} config
@@ -15,10 +21,6 @@ export function createId ({ peerId, network }) {
1521
* @type {import('ipfs-core-types/src/root').API<{}>["id"]}
1622
*/
1723
async function id (options = {}) { // eslint-disable-line require-await
18-
if (options.peerId === peerId.toB58String()) {
19-
delete options.peerId
20-
}
21-
2224
const net = network.try()
2325

2426
if (!net) {
@@ -38,25 +40,17 @@ export function createId ({ peerId, network }) {
3840
}
3941
}
4042

41-
const id = options.peerId ? PeerId.createFromB58String(options.peerId.toString()) : peerId
4243
const { libp2p } = net
43-
44-
let publicKey = id.pubKey ? id.pubKey : libp2p.peerStore.keyBook.get(id)
45-
46-
if (!publicKey) {
47-
publicKey = await libp2p._dht.getPublicKey(id, options)
48-
}
49-
50-
const addresses = options.peerId ? libp2p.peerStore.addressBook.getMultiaddrsForPeer(id) : libp2p.multiaddrs
51-
const protocols = options.peerId ? libp2p.peerStore.protoBook.get(id) : Array.from(libp2p.upgrader.protocols.keys())
52-
const agentVersion = uint8ArrayToString(libp2p.peerStore.metadataBook.getValue(id, 'AgentVersion') || new Uint8Array())
53-
const protocolVersion = uint8ArrayToString(libp2p.peerStore.metadataBook.getValue(id, 'ProtocolVersion') || new Uint8Array())
54-
const idStr = id.toB58String()
44+
const peerIdToId = options.peerId ? PeerId.parse(options.peerId) : peerId
45+
const peer = await findPeer(peerIdToId, libp2p, options)
46+
const agentVersion = uint8ArrayToString(peer.metadata.get('AgentVersion') || new Uint8Array())
47+
const protocolVersion = uint8ArrayToString(peer.metadata.get('ProtocolVersion') || new Uint8Array())
48+
const idStr = peer.id.toB58String()
5549

5650
return {
5751
id: idStr,
58-
publicKey: uint8ArrayToString(publicKey.bytes, 'base64pad'),
59-
addresses: (addresses || [])
52+
publicKey: uint8ArrayToString(peer.publicKey.bytes, 'base64pad'),
53+
addresses: (peer.addresses || [])
6054
.map(ma => {
6155
const str = ma.toString()
6256

@@ -72,8 +66,56 @@ export function createId ({ peerId, network }) {
7266
.map(ma => new Multiaddr(ma)),
7367
agentVersion,
7468
protocolVersion,
75-
protocols: (protocols || []).sort()
69+
protocols: (peer.protocols || []).sort()
7670
}
7771
}
7872
return withTimeoutOption(id)
7973
}
74+
75+
/**
76+
* @param {PeerId} peerId
77+
* @param {Libp2p} libp2p
78+
* @param {AbortOptions} options
79+
*/
80+
async function findPeer (peerId, libp2p, options) {
81+
let peer = libp2p.peerStore.get(peerId)
82+
83+
if (!peer) {
84+
peer = await findPeerOnDht(peerId, libp2p, options)
85+
}
86+
87+
let publicKey = peerId.pubKey ? peerId.pubKey : libp2p.peerStore.keyBook.get(peerId)
88+
89+
if (!publicKey) {
90+
publicKey = await libp2p._dht.getPublicKey(peerId, options)
91+
}
92+
93+
return {
94+
...peer,
95+
publicKey,
96+
metadata: peer.metadata || new Map(),
97+
addresses: peer.addresses.map(addr => addr.multiaddr)
98+
}
99+
}
100+
101+
/**
102+
*
103+
* @param {PeerId} peerId
104+
* @param {Libp2p} libp2p
105+
* @param {AbortOptions} options
106+
*/
107+
async function findPeerOnDht (peerId, libp2p, options) {
108+
for await (const event of libp2p._dht.findPeer(peerId, options)) {
109+
if (event.name === 'FINAL_PEER') {
110+
break
111+
}
112+
}
113+
114+
const peer = libp2p.peerStore.get(peerId)
115+
116+
if (!peer) {
117+
throw errCode(new Error('Could not find peer'), 'ERR_NOT_FOUND')
118+
}
119+
120+
return peer
121+
}

0 commit comments

Comments
 (0)