@@ -4,6 +4,12 @@ import { withTimeoutOption } from 'ipfs-core-utils/with-timeout-option'
4
4
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
5
5
import PeerId from 'peer-id'
6
6
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
+ */
7
13
8
14
/**
9
15
* @param {Object } config
@@ -15,10 +21,6 @@ export function createId ({ peerId, network }) {
15
21
* @type {import('ipfs-core-types/src/root').API<{}>["id"] }
16
22
*/
17
23
async function id ( options = { } ) { // eslint-disable-line require-await
18
- if ( options . peerId === peerId . toB58String ( ) ) {
19
- delete options . peerId
20
- }
21
-
22
24
const net = network . try ( )
23
25
24
26
if ( ! net ) {
@@ -38,25 +40,17 @@ export function createId ({ peerId, network }) {
38
40
}
39
41
}
40
42
41
- const id = options . peerId ? PeerId . createFromB58String ( options . peerId . toString ( ) ) : peerId
42
43
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 ( )
55
49
56
50
return {
57
51
id : idStr ,
58
- publicKey : uint8ArrayToString ( publicKey . bytes , 'base64pad' ) ,
59
- addresses : ( addresses || [ ] )
52
+ publicKey : uint8ArrayToString ( peer . publicKey . bytes , 'base64pad' ) ,
53
+ addresses : ( peer . addresses || [ ] )
60
54
. map ( ma => {
61
55
const str = ma . toString ( )
62
56
@@ -72,8 +66,56 @@ export function createId ({ peerId, network }) {
72
66
. map ( ma => new Multiaddr ( ma ) ) ,
73
67
agentVersion,
74
68
protocolVersion,
75
- protocols : ( protocols || [ ] ) . sort ( )
69
+ protocols : ( peer . protocols || [ ] ) . sort ( )
76
70
}
77
71
}
78
72
return withTimeoutOption ( id )
79
73
}
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