libp2p 0.44.0-e3deaa40 → 0.44.0-ef7a5f67
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +17 -17
- package/dist/src/address-manager/index.d.ts +4 -0
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +32 -4
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/address-manager/utils.d.ts +2 -0
- package/dist/src/address-manager/utils.d.ts.map +1 -0
- package/dist/src/address-manager/utils.js +12 -0
- package/dist/src/address-manager/utils.js.map +1 -0
- package/dist/src/autonat/constants.d.ts +16 -2
- package/dist/src/autonat/constants.d.ts.map +1 -1
- package/dist/src/autonat/constants.js +16 -2
- package/dist/src/autonat/constants.js.map +1 -1
- package/dist/src/autonat/index.d.ts +9 -30
- package/dist/src/autonat/index.d.ts.map +1 -1
- package/dist/src/autonat/index.js +22 -18
- package/dist/src/autonat/index.js.map +1 -1
- package/dist/src/circuit-relay/server/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/server/index.js +3 -0
- package/dist/src/circuit-relay/server/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/discovery.js +2 -2
- package/dist/src/circuit-relay/transport/discovery.js.map +1 -1
- package/dist/src/circuit-relay/transport/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/index.js +4 -6
- package/dist/src/circuit-relay/transport/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/listener.d.ts +0 -3
- package/dist/src/circuit-relay/transport/listener.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/listener.js +27 -24
- package/dist/src/circuit-relay/transport/listener.js.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts +2 -4
- package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.js +28 -26
- package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +0 -55
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
- package/dist/src/connection-manager/dial-queue.js +11 -0
- package/dist/src/connection-manager/dial-queue.js.map +1 -1
- package/dist/src/identify/identify.d.ts +53 -0
- package/dist/src/identify/identify.d.ts.map +1 -0
- package/dist/src/identify/identify.js +405 -0
- package/dist/src/identify/identify.js.map +1 -0
- package/dist/src/identify/index.d.ts +1 -15
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +2 -387
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/index.d.ts +2 -6
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +0 -1
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/peer-routing.d.ts +1 -38
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +0 -60
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/transport-manager.d.ts +5 -5
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +14 -21
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +3 -2
- package/dist/src/upgrader.js.map +1 -1
- package/dist/src/upnp-nat/index.d.ts +2 -2
- package/dist/src/upnp-nat/index.d.ts.map +1 -1
- package/dist/src/upnp-nat/index.js +1 -1
- package/dist/src/upnp-nat/index.js.map +1 -1
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.d.ts.map +1 -1
- package/dist/src/version.js +1 -1
- package/dist/src/version.js.map +1 -1
- package/package.json +5 -5
- package/src/address-manager/index.ts +42 -4
- package/src/address-manager/utils.ts +14 -0
- package/src/autonat/constants.ts +17 -2
- package/src/autonat/index.ts +37 -30
- package/src/circuit-relay/server/index.ts +4 -0
- package/src/circuit-relay/transport/discovery.ts +2 -2
- package/src/circuit-relay/transport/index.ts +4 -7
- package/src/circuit-relay/transport/listener.ts +35 -29
- package/src/circuit-relay/transport/reservation-store.ts +21 -15
- package/src/config.ts +0 -55
- package/src/connection-manager/dial-queue.ts +11 -0
- package/src/identify/identify.ts +518 -0
- package/src/identify/index.ts +5 -505
- package/src/index.ts +2 -7
- package/src/libp2p.ts +0 -1
- package/src/peer-routing.ts +1 -103
- package/src/transport-manager.ts +17 -25
- package/src/upgrader.ts +3 -3
- package/src/upnp-nat/index.ts +2 -2
- package/src/version.ts +1 -1
package/src/identify/index.ts
CHANGED
|
@@ -1,45 +1,16 @@
|
|
|
1
|
-
import { logger } from '@libp2p/logger'
|
|
2
|
-
import { CodeError } from '@libp2p/interfaces/errors'
|
|
3
|
-
import * as lp from 'it-length-prefixed'
|
|
4
|
-
import { pipe } from 'it-pipe'
|
|
5
|
-
import first from 'it-first'
|
|
6
|
-
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
7
|
-
import { Multiaddr, multiaddr, protocols } from '@multiformats/multiaddr'
|
|
8
1
|
import { Identify } from './pb/message.js'
|
|
9
|
-
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
|
|
10
2
|
import {
|
|
11
|
-
AGENT_VERSION,
|
|
12
3
|
MULTICODEC_IDENTIFY,
|
|
13
|
-
MULTICODEC_IDENTIFY_PUSH
|
|
14
|
-
IDENTIFY_PROTOCOL_VERSION,
|
|
15
|
-
MULTICODEC_IDENTIFY_PROTOCOL_NAME,
|
|
16
|
-
MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME,
|
|
17
|
-
MULTICODEC_IDENTIFY_PROTOCOL_VERSION,
|
|
18
|
-
MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION
|
|
4
|
+
MULTICODEC_IDENTIFY_PUSH
|
|
19
5
|
} from './consts.js'
|
|
20
|
-
import {
|
|
21
|
-
import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
|
|
22
|
-
import type { Connection, Stream } from '@libp2p/interface-connection'
|
|
23
|
-
import type { Startable } from '@libp2p/interfaces/startable'
|
|
24
|
-
import { peerIdFromKeys } from '@libp2p/peer-id'
|
|
25
|
-
import type { AbortOptions } from '@libp2p/interfaces'
|
|
26
|
-
import { abortableDuplex } from 'abortable-iterator'
|
|
27
|
-
import { setMaxListeners } from 'events'
|
|
6
|
+
import type { Registrar } from '@libp2p/interface-registrar'
|
|
28
7
|
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
29
8
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
30
|
-
import type {
|
|
9
|
+
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
31
10
|
import type { AddressManager } from '@libp2p/interface-address-manager'
|
|
32
|
-
import { anySignal } from 'any-signal'
|
|
33
11
|
import type { EventEmitter } from '@libp2p/interfaces/events'
|
|
34
12
|
import type { Libp2pEvents } from '@libp2p/interface-libp2p'
|
|
35
|
-
import {
|
|
36
|
-
import { pbStream } from 'it-pb-stream'
|
|
37
|
-
import { isNode, isBrowser, isWebWorker, isElectronMain, isElectronRenderer, isReactNative } from 'wherearewe'
|
|
38
|
-
|
|
39
|
-
const log = logger('libp2p:identify')
|
|
40
|
-
|
|
41
|
-
// https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/identify/id.go#L52
|
|
42
|
-
const MAX_IDENTIFY_MESSAGE_SIZE = 1024 * 8
|
|
13
|
+
import { DefaultIdentifyService } from './identify.js'
|
|
43
14
|
|
|
44
15
|
export interface IdentifyServiceInit {
|
|
45
16
|
/**
|
|
@@ -79,477 +50,6 @@ export interface IdentifyServiceComponents {
|
|
|
79
50
|
events: EventEmitter<Libp2pEvents>
|
|
80
51
|
}
|
|
81
52
|
|
|
82
|
-
export interface IdentifyService {
|
|
83
|
-
/**
|
|
84
|
-
* Requests the `Identify` message from peer associated with the given `connection`.
|
|
85
|
-
* If the identified peer does not match the `PeerId` associated with the connection,
|
|
86
|
-
* an error will be thrown.
|
|
87
|
-
*/
|
|
88
|
-
identify: (connection: Connection, options?: AbortOptions) => Promise<void>
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Calls `push` on all peer connections
|
|
92
|
-
*/
|
|
93
|
-
push: () => Promise<void>
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const defaultValues = {
|
|
97
|
-
protocolPrefix: 'ipfs',
|
|
98
|
-
agentVersion: AGENT_VERSION,
|
|
99
|
-
// https://github.com/libp2p/go-libp2p/blob/8d2e54e1637041d5cf4fac1e531287560bd1f4ac/p2p/protocol/identify/id.go#L48
|
|
100
|
-
timeout: 60000,
|
|
101
|
-
maxInboundStreams: 1,
|
|
102
|
-
maxOutboundStreams: 1,
|
|
103
|
-
maxPushIncomingStreams: 1,
|
|
104
|
-
maxPushOutgoingStreams: 1,
|
|
105
|
-
maxObservedAddresses: 10,
|
|
106
|
-
maxIdentifyMessageSize: 8192
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
class DefaultIdentifyService implements Startable, IdentifyService {
|
|
110
|
-
private readonly identifyProtocolStr: string
|
|
111
|
-
private readonly identifyPushProtocolStr: string
|
|
112
|
-
public readonly host: {
|
|
113
|
-
protocolVersion: string
|
|
114
|
-
agentVersion: string
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
private started: boolean
|
|
118
|
-
private readonly timeout: number
|
|
119
|
-
private readonly peerId: PeerId
|
|
120
|
-
private readonly peerStore: PeerStore
|
|
121
|
-
private readonly registrar: Registrar
|
|
122
|
-
private readonly connectionManager: ConnectionManager
|
|
123
|
-
private readonly addressManager: AddressManager
|
|
124
|
-
private readonly maxInboundStreams: number
|
|
125
|
-
private readonly maxOutboundStreams: number
|
|
126
|
-
private readonly maxPushIncomingStreams: number
|
|
127
|
-
private readonly maxPushOutgoingStreams: number
|
|
128
|
-
private readonly maxIdentifyMessageSize: number
|
|
129
|
-
private readonly maxObservedAddresses: number
|
|
130
|
-
|
|
131
|
-
constructor (components: IdentifyServiceComponents, init: IdentifyServiceInit) {
|
|
132
|
-
this.started = false
|
|
133
|
-
this.peerId = components.peerId
|
|
134
|
-
this.peerStore = components.peerStore
|
|
135
|
-
this.registrar = components.registrar
|
|
136
|
-
this.addressManager = components.addressManager
|
|
137
|
-
this.connectionManager = components.connectionManager
|
|
138
|
-
|
|
139
|
-
this.identifyProtocolStr = `/${init.protocolPrefix ?? defaultValues.protocolPrefix}/${MULTICODEC_IDENTIFY_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PROTOCOL_VERSION}`
|
|
140
|
-
this.identifyPushProtocolStr = `/${init.protocolPrefix ?? defaultValues.protocolPrefix}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION}`
|
|
141
|
-
this.timeout = init.timeout ?? defaultValues.timeout
|
|
142
|
-
this.maxInboundStreams = init.maxInboundStreams ?? defaultValues.maxInboundStreams
|
|
143
|
-
this.maxOutboundStreams = init.maxOutboundStreams ?? defaultValues.maxOutboundStreams
|
|
144
|
-
this.maxPushIncomingStreams = init.maxPushIncomingStreams ?? defaultValues.maxPushIncomingStreams
|
|
145
|
-
this.maxPushOutgoingStreams = init.maxPushOutgoingStreams ?? defaultValues.maxPushOutgoingStreams
|
|
146
|
-
this.maxIdentifyMessageSize = init.maxIdentifyMessageSize ?? defaultValues.maxIdentifyMessageSize
|
|
147
|
-
this.maxObservedAddresses = init.maxObservedAddresses ?? defaultValues.maxObservedAddresses
|
|
148
|
-
|
|
149
|
-
// Store self host metadata
|
|
150
|
-
this.host = {
|
|
151
|
-
protocolVersion: `${init.protocolPrefix ?? defaultValues.protocolPrefix}/${IDENTIFY_PROTOCOL_VERSION}`,
|
|
152
|
-
agentVersion: init.agentVersion ?? defaultValues.agentVersion
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// When a new connection happens, trigger identify
|
|
156
|
-
components.events.addEventListener('connection:open', (evt) => {
|
|
157
|
-
const connection = evt.detail
|
|
158
|
-
this.identify(connection).catch(err => { log.error('error during identify trigged by connection:open', err) })
|
|
159
|
-
})
|
|
160
|
-
|
|
161
|
-
// When self peer record changes, trigger identify-push
|
|
162
|
-
components.events.addEventListener('self:peer:update', (evt) => {
|
|
163
|
-
void this.push().catch(err => { log.error(err) })
|
|
164
|
-
})
|
|
165
|
-
|
|
166
|
-
// Append user agent version to default AGENT_VERSION depending on the environment
|
|
167
|
-
if (this.host.agentVersion === AGENT_VERSION) {
|
|
168
|
-
if (isNode || isElectronMain) {
|
|
169
|
-
this.host.agentVersion += ` UserAgent=${globalThis.process.version}`
|
|
170
|
-
} else if (isBrowser || isWebWorker || isElectronRenderer || isReactNative) {
|
|
171
|
-
this.host.agentVersion += ` UserAgent=${globalThis.navigator.userAgent}`
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
isStarted (): boolean {
|
|
177
|
-
return this.started
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
async start (): Promise<void> {
|
|
181
|
-
if (this.started) {
|
|
182
|
-
return
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
await this.peerStore.merge(this.peerId, {
|
|
186
|
-
metadata: {
|
|
187
|
-
AgentVersion: uint8ArrayFromString(this.host.agentVersion),
|
|
188
|
-
ProtocolVersion: uint8ArrayFromString(this.host.protocolVersion)
|
|
189
|
-
}
|
|
190
|
-
})
|
|
191
|
-
|
|
192
|
-
await this.registrar.handle(this.identifyProtocolStr, (data) => {
|
|
193
|
-
void this._handleIdentify(data).catch(err => {
|
|
194
|
-
log.error(err)
|
|
195
|
-
})
|
|
196
|
-
}, {
|
|
197
|
-
maxInboundStreams: this.maxInboundStreams,
|
|
198
|
-
maxOutboundStreams: this.maxOutboundStreams
|
|
199
|
-
})
|
|
200
|
-
await this.registrar.handle(this.identifyPushProtocolStr, (data) => {
|
|
201
|
-
void this._handlePush(data).catch(err => {
|
|
202
|
-
log.error(err)
|
|
203
|
-
})
|
|
204
|
-
}, {
|
|
205
|
-
maxInboundStreams: this.maxPushIncomingStreams,
|
|
206
|
-
maxOutboundStreams: this.maxPushOutgoingStreams
|
|
207
|
-
})
|
|
208
|
-
|
|
209
|
-
this.started = true
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
async stop (): Promise<void> {
|
|
213
|
-
await this.registrar.unhandle(this.identifyProtocolStr)
|
|
214
|
-
await this.registrar.unhandle(this.identifyPushProtocolStr)
|
|
215
|
-
|
|
216
|
-
this.started = false
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Send an Identify Push update to the list of connections
|
|
221
|
-
*/
|
|
222
|
-
async pushToConnections (connections: Connection[]): Promise<void> {
|
|
223
|
-
const listenAddresses = this.addressManager.getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code))
|
|
224
|
-
const peerRecord = new PeerRecord({
|
|
225
|
-
peerId: this.peerId,
|
|
226
|
-
multiaddrs: listenAddresses
|
|
227
|
-
})
|
|
228
|
-
const signedPeerRecord = await RecordEnvelope.seal(peerRecord, this.peerId)
|
|
229
|
-
const supportedProtocols = this.registrar.getProtocols()
|
|
230
|
-
const peer = await this.peerStore.get(this.peerId)
|
|
231
|
-
const agentVersion = uint8ArrayToString(peer.metadata.get('AgentVersion') ?? uint8ArrayFromString(this.host.agentVersion))
|
|
232
|
-
const protocolVersion = uint8ArrayToString(peer.metadata.get('ProtocolVersion') ?? uint8ArrayFromString(this.host.protocolVersion))
|
|
233
|
-
|
|
234
|
-
const pushes = connections.map(async connection => {
|
|
235
|
-
let stream: Stream | undefined
|
|
236
|
-
|
|
237
|
-
const signal = AbortSignal.timeout(this.timeout)
|
|
238
|
-
|
|
239
|
-
try {
|
|
240
|
-
// fails on node < 15.4
|
|
241
|
-
setMaxListeners?.(Infinity, signal)
|
|
242
|
-
} catch {}
|
|
243
|
-
|
|
244
|
-
try {
|
|
245
|
-
stream = await connection.newStream([this.identifyPushProtocolStr], {
|
|
246
|
-
signal
|
|
247
|
-
})
|
|
248
|
-
|
|
249
|
-
// make stream abortable
|
|
250
|
-
const source = abortableDuplex(stream, signal)
|
|
251
|
-
|
|
252
|
-
await source.sink(pipe(
|
|
253
|
-
[Identify.encode({
|
|
254
|
-
listenAddrs: listenAddresses.map(ma => ma.bytes),
|
|
255
|
-
signedPeerRecord: signedPeerRecord.marshal(),
|
|
256
|
-
protocols: supportedProtocols,
|
|
257
|
-
agentVersion,
|
|
258
|
-
protocolVersion
|
|
259
|
-
})],
|
|
260
|
-
(source) => lp.encode(source)
|
|
261
|
-
))
|
|
262
|
-
} catch (err: any) {
|
|
263
|
-
// Just log errors
|
|
264
|
-
log.error('could not push identify update to peer', err)
|
|
265
|
-
} finally {
|
|
266
|
-
if (stream != null) {
|
|
267
|
-
stream.close()
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
})
|
|
271
|
-
|
|
272
|
-
await Promise.all(pushes)
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Calls `push` on all peer connections
|
|
277
|
-
*/
|
|
278
|
-
async push (): Promise<void> {
|
|
279
|
-
// Do not try to push if we are not running
|
|
280
|
-
if (!this.isStarted()) {
|
|
281
|
-
return
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
const connections: Connection[] = []
|
|
285
|
-
|
|
286
|
-
await Promise.all(
|
|
287
|
-
this.connectionManager.getConnections().map(async conn => {
|
|
288
|
-
try {
|
|
289
|
-
const peer = await this.peerStore.get(conn.remotePeer)
|
|
290
|
-
|
|
291
|
-
if (!peer.protocols.includes(this.identifyPushProtocolStr)) {
|
|
292
|
-
return
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
connections.push(conn)
|
|
296
|
-
} catch (err: any) {
|
|
297
|
-
if (err.code !== codes.ERR_NOT_FOUND) {
|
|
298
|
-
throw err
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
})
|
|
302
|
-
)
|
|
303
|
-
|
|
304
|
-
await this.pushToConnections(connections)
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
async _identify (connection: Connection, options: AbortOptions = {}): Promise<Identify> {
|
|
308
|
-
let stream: Stream | undefined
|
|
309
|
-
|
|
310
|
-
const signal = anySignal([AbortSignal.timeout(this.timeout), options?.signal])
|
|
311
|
-
|
|
312
|
-
try {
|
|
313
|
-
// fails on node < 15.4
|
|
314
|
-
setMaxListeners?.(Infinity, signal)
|
|
315
|
-
} catch {}
|
|
316
|
-
|
|
317
|
-
try {
|
|
318
|
-
stream = await connection.newStream([this.identifyProtocolStr], {
|
|
319
|
-
signal
|
|
320
|
-
})
|
|
321
|
-
|
|
322
|
-
// make stream abortable
|
|
323
|
-
const source = abortableDuplex(stream, signal)
|
|
324
|
-
|
|
325
|
-
const data = await pipe(
|
|
326
|
-
[],
|
|
327
|
-
source,
|
|
328
|
-
(source) => lp.decode(source, {
|
|
329
|
-
maxDataLength: this.maxIdentifyMessageSize ?? MAX_IDENTIFY_MESSAGE_SIZE
|
|
330
|
-
}),
|
|
331
|
-
async (source) => await first(source)
|
|
332
|
-
)
|
|
333
|
-
|
|
334
|
-
if (data == null) {
|
|
335
|
-
throw new CodeError('No data could be retrieved', codes.ERR_CONNECTION_ENDED)
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
try {
|
|
339
|
-
return Identify.decode(data)
|
|
340
|
-
} catch (err: any) {
|
|
341
|
-
throw new CodeError(String(err), codes.ERR_INVALID_MESSAGE)
|
|
342
|
-
}
|
|
343
|
-
} finally {
|
|
344
|
-
if (stream != null) {
|
|
345
|
-
stream.close()
|
|
346
|
-
}
|
|
347
|
-
signal.clear()
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
async identify (connection: Connection, options: AbortOptions = {}): Promise<void> {
|
|
352
|
-
const message = await this._identify(connection, options)
|
|
353
|
-
const {
|
|
354
|
-
publicKey,
|
|
355
|
-
protocols,
|
|
356
|
-
observedAddr
|
|
357
|
-
} = message
|
|
358
|
-
|
|
359
|
-
if (publicKey == null) {
|
|
360
|
-
throw new CodeError('public key was missing from identify message', codes.ERR_MISSING_PUBLIC_KEY)
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
const id = await peerIdFromKeys(publicKey)
|
|
364
|
-
|
|
365
|
-
if (!connection.remotePeer.equals(id)) {
|
|
366
|
-
throw new CodeError('identified peer does not match the expected peer', codes.ERR_INVALID_PEER)
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
if (this.peerId.equals(id)) {
|
|
370
|
-
throw new CodeError('identified peer is our own peer id?', codes.ERR_INVALID_PEER)
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
// Get the observedAddr if there is one
|
|
374
|
-
const cleanObservedAddr = getCleanMultiaddr(observedAddr)
|
|
375
|
-
|
|
376
|
-
log('identify completed for peer %p and protocols %o', id, protocols)
|
|
377
|
-
log('our observed address is %s', cleanObservedAddr)
|
|
378
|
-
|
|
379
|
-
if (cleanObservedAddr != null &&
|
|
380
|
-
this.addressManager.getObservedAddrs().length < (this.maxObservedAddresses ?? Infinity)) {
|
|
381
|
-
log('storing our observed address %s', cleanObservedAddr?.toString())
|
|
382
|
-
this.addressManager.addObservedAddr(cleanObservedAddr)
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
await this.#consumeIdentifyMessage(connection.remotePeer, message)
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
/**
|
|
389
|
-
* Sends the `Identify` response with the Signed Peer Record
|
|
390
|
-
* to the requesting peer over the given `connection`
|
|
391
|
-
*/
|
|
392
|
-
async _handleIdentify (data: IncomingStreamData): Promise<void> {
|
|
393
|
-
const { connection, stream } = data
|
|
394
|
-
|
|
395
|
-
const signal = AbortSignal.timeout(this.timeout)
|
|
396
|
-
|
|
397
|
-
try {
|
|
398
|
-
// fails on node < 15.4
|
|
399
|
-
setMaxListeners?.(Infinity, signal)
|
|
400
|
-
} catch {}
|
|
401
|
-
|
|
402
|
-
try {
|
|
403
|
-
const publicKey = this.peerId.publicKey ?? new Uint8Array(0)
|
|
404
|
-
const peerData = await this.peerStore.get(this.peerId)
|
|
405
|
-
const multiaddrs = this.addressManager.getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code))
|
|
406
|
-
let signedPeerRecord = peerData.peerRecordEnvelope
|
|
407
|
-
|
|
408
|
-
if (multiaddrs.length > 0 && signedPeerRecord == null) {
|
|
409
|
-
const peerRecord = new PeerRecord({
|
|
410
|
-
peerId: this.peerId,
|
|
411
|
-
multiaddrs
|
|
412
|
-
})
|
|
413
|
-
|
|
414
|
-
const envelope = await RecordEnvelope.seal(peerRecord, this.peerId)
|
|
415
|
-
signedPeerRecord = envelope.marshal().subarray()
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
const message = Identify.encode({
|
|
419
|
-
protocolVersion: this.host.protocolVersion,
|
|
420
|
-
agentVersion: this.host.agentVersion,
|
|
421
|
-
publicKey,
|
|
422
|
-
listenAddrs: multiaddrs.map(addr => addr.bytes),
|
|
423
|
-
signedPeerRecord,
|
|
424
|
-
observedAddr: connection.remoteAddr.bytes,
|
|
425
|
-
protocols: peerData.protocols
|
|
426
|
-
})
|
|
427
|
-
|
|
428
|
-
// make stream abortable
|
|
429
|
-
const source = abortableDuplex(stream, signal)
|
|
430
|
-
|
|
431
|
-
const msgWithLenPrefix = pipe([message], (source) => lp.encode(source))
|
|
432
|
-
await source.sink(msgWithLenPrefix)
|
|
433
|
-
} catch (err: any) {
|
|
434
|
-
log.error('could not respond to identify request', err)
|
|
435
|
-
} finally {
|
|
436
|
-
stream.close()
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
/**
|
|
441
|
-
* Reads the Identify Push message from the given `connection`
|
|
442
|
-
*/
|
|
443
|
-
async _handlePush (data: IncomingStreamData): Promise<void> {
|
|
444
|
-
const { connection, stream } = data
|
|
445
|
-
|
|
446
|
-
try {
|
|
447
|
-
if (this.peerId.equals(connection.remotePeer)) {
|
|
448
|
-
throw new Error('received push from ourselves?')
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
// make stream abortable
|
|
452
|
-
const source = abortableDuplex(stream, AbortSignal.timeout(this.timeout))
|
|
453
|
-
const pb = pbStream(source, {
|
|
454
|
-
maxDataLength: this.maxIdentifyMessageSize ?? MAX_IDENTIFY_MESSAGE_SIZE
|
|
455
|
-
})
|
|
456
|
-
const message = await pb.readPB(Identify)
|
|
457
|
-
|
|
458
|
-
await this.#consumeIdentifyMessage(connection.remotePeer, message)
|
|
459
|
-
} catch (err: any) {
|
|
460
|
-
log.error('received invalid message', err)
|
|
461
|
-
return
|
|
462
|
-
} finally {
|
|
463
|
-
stream.close()
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
log('handled push from %p', connection.remotePeer)
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
async #consumeIdentifyMessage (remotePeer: PeerId, message: Identify): Promise<void> {
|
|
470
|
-
if (message == null) {
|
|
471
|
-
throw new Error('Message was null or undefined')
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
log('received identify from %p', remotePeer)
|
|
475
|
-
|
|
476
|
-
if (message.signedPeerRecord == null) {
|
|
477
|
-
return
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
const envelope = await RecordEnvelope.openAndCertify(message.signedPeerRecord, PeerRecord.DOMAIN)
|
|
481
|
-
const peerRecord = PeerRecord.createFromProtobuf(envelope.payload)
|
|
482
|
-
|
|
483
|
-
// Verify peerId
|
|
484
|
-
if (!peerRecord.peerId.equals(envelope.peerId)) {
|
|
485
|
-
throw new Error('signing key does not match PeerId in the PeerRecord')
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
// Make sure remote peer is the one sending the record
|
|
489
|
-
if (!remotePeer.equals(peerRecord.peerId)) {
|
|
490
|
-
throw new Error('signing key does not match remote PeerId')
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
let peer: Peer | undefined
|
|
494
|
-
|
|
495
|
-
try {
|
|
496
|
-
peer = await this.peerStore.get(peerRecord.peerId)
|
|
497
|
-
} catch (err: any) {
|
|
498
|
-
if (err.code !== 'ERR_NOT_FOUND') {
|
|
499
|
-
throw err
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
log('received signedPeerRecord in push from %p', remotePeer)
|
|
504
|
-
let metadata = new Map()
|
|
505
|
-
|
|
506
|
-
if (peer?.peerRecordEnvelope != null) {
|
|
507
|
-
const storedEnvelope = await RecordEnvelope.createFromProtobuf(peer.peerRecordEnvelope)
|
|
508
|
-
const storedRecord = PeerRecord.createFromProtobuf(storedEnvelope.payload)
|
|
509
|
-
|
|
510
|
-
// ensure seq is greater than, or equal to, the last received
|
|
511
|
-
if (storedRecord.seqNumber >= peerRecord.seqNumber) {
|
|
512
|
-
log('sequence number was lower or equal to existing sequence number - stored: %d received: %d', storedRecord.seqNumber, peerRecord.seqNumber)
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
metadata = peer.metadata
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
if (message.agentVersion != null) {
|
|
519
|
-
metadata.set('AgentVersion', uint8ArrayFromString(message.agentVersion))
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
if (message.protocolVersion != null) {
|
|
523
|
-
metadata.set('ProtocolVersion', uint8ArrayFromString(message.protocolVersion))
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
await this.peerStore.patch(peerRecord.peerId, {
|
|
527
|
-
peerRecordEnvelope: message.signedPeerRecord,
|
|
528
|
-
protocols: message.protocols,
|
|
529
|
-
addresses: peerRecord.multiaddrs.map(multiaddr => ({
|
|
530
|
-
isCertified: true,
|
|
531
|
-
multiaddr
|
|
532
|
-
})),
|
|
533
|
-
metadata
|
|
534
|
-
})
|
|
535
|
-
|
|
536
|
-
log('consumed signedPeerRecord sent in push from %p', remotePeer)
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
/**
|
|
541
|
-
* Takes the `addr` and converts it to a Multiaddr if possible
|
|
542
|
-
*/
|
|
543
|
-
function getCleanMultiaddr (addr: Uint8Array | string | null | undefined): Multiaddr | undefined {
|
|
544
|
-
if (addr != null && addr.length > 0) {
|
|
545
|
-
try {
|
|
546
|
-
return multiaddr(addr)
|
|
547
|
-
} catch {
|
|
548
|
-
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
|
|
553
53
|
/**
|
|
554
54
|
* The protocols the IdentifyService supports
|
|
555
55
|
*/
|
|
@@ -560,6 +60,6 @@ export const multicodecs = {
|
|
|
560
60
|
|
|
561
61
|
export const Message = { Identify }
|
|
562
62
|
|
|
563
|
-
export function identifyService (init: IdentifyServiceInit = {}): (components: IdentifyServiceComponents) =>
|
|
63
|
+
export function identifyService (init: IdentifyServiceInit = {}): (components: IdentifyServiceComponents) => {} {
|
|
564
64
|
return (components) => new DefaultIdentifyService(components, init)
|
|
565
65
|
}
|
package/src/index.ts
CHANGED
|
@@ -32,7 +32,6 @@ import type { Components } from './components.js'
|
|
|
32
32
|
import type { Libp2p, ServiceMap } from '@libp2p/interface-libp2p'
|
|
33
33
|
import type { KeyChainInit } from '@libp2p/keychain'
|
|
34
34
|
import type { AddressManagerInit } from './address-manager/index.js'
|
|
35
|
-
import type { PeerRoutingInit } from './peer-routing.js'
|
|
36
35
|
import type { ConnectionManagerInit } from './connection-manager/index.js'
|
|
37
36
|
import type { PersistentPeerStoreInit } from '@libp2p/peer-store'
|
|
38
37
|
|
|
@@ -81,11 +80,6 @@ export interface Libp2pInit<T extends ServiceMap = {}> {
|
|
|
81
80
|
*/
|
|
82
81
|
peerStore: PersistentPeerStoreInit
|
|
83
82
|
|
|
84
|
-
/**
|
|
85
|
-
* libp2p Peer routing service configuration
|
|
86
|
-
*/
|
|
87
|
-
peerRouting: PeerRoutingInit
|
|
88
|
-
|
|
89
83
|
/**
|
|
90
84
|
* keychain configuration
|
|
91
85
|
*/
|
|
@@ -134,11 +128,12 @@ export type Libp2pOptions<T extends ServiceMap = {}> = RecursivePartial<Libp2pIn
|
|
|
134
128
|
* import { tcp } from '@libp2p/tcp'
|
|
135
129
|
* import { mplex } from '@libp2p/mplex'
|
|
136
130
|
* import { noise } from '@chainsafe/libp2p-noise'
|
|
131
|
+
* import { yamux } from '@chainsafe/libp2p-yamux'
|
|
137
132
|
*
|
|
138
133
|
* // specify options
|
|
139
134
|
* const options = {
|
|
140
135
|
* transports: [tcp()],
|
|
141
|
-
* streamMuxers: [mplex()],
|
|
136
|
+
* streamMuxers: [yamux(), mplex()],
|
|
142
137
|
* connectionEncryption: [noise()]
|
|
143
138
|
* }
|
|
144
139
|
*
|
package/src/libp2p.ts
CHANGED
|
@@ -137,7 +137,6 @@ export class Libp2pNode<T extends ServiceMap = {}> extends EventEmitter<Libp2pEv
|
|
|
137
137
|
// Peer routers
|
|
138
138
|
const peerRouters: PeerRouting[] = (init.peerRouters ?? []).map((fn, index) => this.configureComponent(`peer-router-${index}`, fn(this.components)))
|
|
139
139
|
this.peerRouting = this.components.peerRouting = this.configureComponent('peerRouting', new DefaultPeerRouting(this.components, {
|
|
140
|
-
...init.peerRouting,
|
|
141
140
|
routers: peerRouters
|
|
142
141
|
}))
|
|
143
142
|
|