libp2p 0.39.5 → 0.40.0
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/README.md +5 -0
- package/dist/src/address-manager/index.d.ts +18 -2
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +18 -5
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/circuit/auto-relay.d.ts +2 -2
- package/dist/src/circuit/auto-relay.d.ts.map +1 -1
- package/dist/src/circuit/auto-relay.js +12 -12
- package/dist/src/circuit/auto-relay.js.map +1 -1
- package/dist/src/circuit/index.d.ts +13 -3
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/index.js +1 -1
- package/dist/src/circuit/index.js.map +1 -1
- package/dist/src/circuit/pb/index.d.ts.map +1 -1
- package/dist/src/circuit/pb/index.js +29 -34
- package/dist/src/circuit/pb/index.js.map +1 -1
- package/dist/src/circuit/transport.d.ts +22 -7
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +26 -17
- package/dist/src/circuit/transport.js.map +1 -1
- package/dist/src/components.d.ts +110 -0
- package/dist/src/components.d.ts.map +1 -0
- package/dist/src/components.js +199 -0
- package/dist/src/components.js.map +1 -0
- package/dist/src/connection-manager/auto-dialler.d.ts +9 -2
- package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dialler.js +6 -6
- package/dist/src/connection-manager/auto-dialler.js.map +1 -1
- package/dist/src/connection-manager/dialer/index.d.ts +12 -5
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
- package/dist/src/connection-manager/dialer/index.js +9 -9
- package/dist/src/connection-manager/dialer/index.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +41 -9
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +82 -39
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/content-routing/index.d.ts +7 -2
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +4 -4
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/content-routing/utils.d.ts +1 -1
- package/dist/src/content-routing/utils.d.ts.map +1 -1
- package/dist/src/errors.d.ts +2 -1
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +1 -0
- package/dist/src/errors.js.map +1 -1
- package/dist/src/fetch/index.d.ts +7 -3
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +3 -3
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/fetch/pb/proto.d.ts.map +1 -1
- package/dist/src/fetch/pb/proto.js +24 -35
- package/dist/src/fetch/pb/proto.js.map +1 -1
- package/dist/src/identify/index.d.ts +13 -3
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +41 -41
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/identify/pb/message.d.ts.map +1 -1
- package/dist/src/identify/pb/message.js +23 -34
- package/dist/src/identify/pb/message.js.map +1 -1
- package/dist/src/index.d.ts +10 -9
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/insecure/index.d.ts +2 -8
- package/dist/src/insecure/index.d.ts.map +1 -1
- package/dist/src/insecure/index.js +5 -3
- package/dist/src/insecure/index.js.map +1 -1
- package/dist/src/insecure/pb/proto.d.ts.map +1 -1
- package/dist/src/insecure/pb/proto.js +24 -29
- package/dist/src/insecure/pb/proto.js.map +1 -1
- package/dist/src/keychain/index.d.ts +6 -2
- package/dist/src/keychain/index.d.ts.map +1 -1
- package/dist/src/keychain/index.js +19 -19
- package/dist/src/keychain/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +1 -2
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +48 -71
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/nat-manager.d.ts +9 -2
- package/dist/src/nat-manager.d.ts.map +1 -1
- package/dist/src/nat-manager.js +3 -3
- package/dist/src/nat-manager.js.map +1 -1
- package/dist/src/peer-record-updater.d.ts +11 -2
- package/dist/src/peer-record-updater.d.ts.map +1 -1
- package/dist/src/peer-record-updater.js +10 -10
- package/dist/src/peer-record-updater.js.map +1 -1
- package/dist/src/peer-routing.d.ts +6 -2
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +4 -4
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/ping/index.d.ts +7 -3
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +3 -3
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/pnet/index.d.ts +2 -17
- package/dist/src/pnet/index.d.ts.map +1 -1
- package/dist/src/pnet/index.js +4 -1
- package/dist/src/pnet/index.js.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.d.ts +2 -1
- package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.js +4 -0
- package/dist/src/pubsub/dummy-pubsub.js.map +1 -1
- package/dist/src/registrar.d.ts +9 -3
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +6 -6
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts +9 -3
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +4 -4
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +17 -5
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +55 -43
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +37 -37
- package/src/address-manager/index.ts +31 -8
- package/src/circuit/auto-relay.ts +15 -15
- package/src/circuit/index.ts +16 -5
- package/src/circuit/pb/index.ts +29 -35
- package/src/circuit/transport.ts +49 -20
- package/src/components.ts +332 -0
- package/src/connection-manager/auto-dialler.ts +17 -9
- package/src/connection-manager/dialer/index.ts +23 -15
- package/src/connection-manager/index.ts +139 -47
- package/src/content-routing/index.ts +13 -7
- package/src/errors.ts +2 -1
- package/src/fetch/index.ts +12 -7
- package/src/fetch/pb/proto.ts +24 -35
- package/src/identify/index.ts +57 -53
- package/src/identify/pb/message.ts +23 -35
- package/src/index.ts +10 -9
- package/src/insecure/index.ts +6 -3
- package/src/insecure/pb/proto.ts +24 -29
- package/src/keychain/index.ts +27 -22
- package/src/libp2p.ts +49 -77
- package/src/nat-manager.ts +14 -6
- package/src/peer-record-updater.ts +23 -13
- package/src/peer-routing.ts +12 -7
- package/src/ping/index.ts +12 -7
- package/src/pnet/index.ts +5 -1
- package/src/pubsub/dummy-pubsub.ts +3 -1
- package/src/registrar.ts +17 -10
- package/src/transport-manager.ts +15 -8
- package/src/upgrader.ts +75 -49
- package/src/version.ts +1 -1
- package/dist/src/connection-manager/dialer/auto-dialer.d.ts +0 -16
- package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +0 -1
- package/dist/src/connection-manager/dialer/auto-dialer.js +0 -43
- package/dist/src/connection-manager/dialer/auto-dialer.js.map +0 -1
- package/src/connection-manager/dialer/auto-dialer.ts +0 -65
package/src/identify/index.ts
CHANGED
|
@@ -2,7 +2,6 @@ import { logger } from '@libp2p/logger'
|
|
|
2
2
|
import errCode from 'err-code'
|
|
3
3
|
import * as lp from 'it-length-prefixed'
|
|
4
4
|
import { pipe } from 'it-pipe'
|
|
5
|
-
import drain from 'it-drain'
|
|
6
5
|
import first from 'it-first'
|
|
7
6
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
8
7
|
import { multiaddr, protocols } from '@multiformats/multiaddr'
|
|
@@ -18,15 +17,18 @@ import {
|
|
|
18
17
|
MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION
|
|
19
18
|
} from './consts.js'
|
|
20
19
|
import { codes } from '../errors.js'
|
|
21
|
-
import type { IncomingStreamData } from '@libp2p/interface-registrar'
|
|
20
|
+
import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
|
|
22
21
|
import type { Connection, Stream } from '@libp2p/interface-connection'
|
|
23
22
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
24
23
|
import { peerIdFromKeys } from '@libp2p/peer-id'
|
|
25
|
-
import type { Components } from '@libp2p/components'
|
|
26
24
|
import { TimeoutController } from 'timeout-abort-controller'
|
|
27
25
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
28
26
|
import { abortableDuplex } from 'abortable-iterator'
|
|
29
27
|
import { setMaxListeners } from 'events'
|
|
28
|
+
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
29
|
+
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
30
|
+
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
31
|
+
import type { AddressManager } from '@libp2p/interface-address-manager'
|
|
30
32
|
|
|
31
33
|
const log = logger('libp2p:identify')
|
|
32
34
|
|
|
@@ -65,8 +67,16 @@ export interface IdentifyServiceInit {
|
|
|
65
67
|
maxPushOutgoingStreams: number
|
|
66
68
|
}
|
|
67
69
|
|
|
70
|
+
export interface IdentifyServiceComponents {
|
|
71
|
+
peerId: PeerId
|
|
72
|
+
peerStore: PeerStore
|
|
73
|
+
connectionManager: ConnectionManager
|
|
74
|
+
registrar: Registrar
|
|
75
|
+
addressManager: AddressManager
|
|
76
|
+
}
|
|
77
|
+
|
|
68
78
|
export class IdentifyService implements Startable {
|
|
69
|
-
private readonly components:
|
|
79
|
+
private readonly components: IdentifyServiceComponents
|
|
70
80
|
private readonly identifyProtocolStr: string
|
|
71
81
|
private readonly identifyPushProtocolStr: string
|
|
72
82
|
private readonly host: {
|
|
@@ -77,7 +87,7 @@ export class IdentifyService implements Startable {
|
|
|
77
87
|
private readonly init: IdentifyServiceInit
|
|
78
88
|
private started: boolean
|
|
79
89
|
|
|
80
|
-
constructor (components:
|
|
90
|
+
constructor (components: IdentifyServiceComponents, init: IdentifyServiceInit) {
|
|
81
91
|
this.components = components
|
|
82
92
|
this.started = false
|
|
83
93
|
this.init = init
|
|
@@ -92,25 +102,25 @@ export class IdentifyService implements Startable {
|
|
|
92
102
|
}
|
|
93
103
|
|
|
94
104
|
// When a new connection happens, trigger identify
|
|
95
|
-
this.components.
|
|
105
|
+
this.components.connectionManager.addEventListener('peer:connect', (evt) => {
|
|
96
106
|
const connection = evt.detail
|
|
97
107
|
this.identify(connection).catch(log.error)
|
|
98
108
|
})
|
|
99
109
|
|
|
100
110
|
// When self multiaddrs change, trigger identify-push
|
|
101
|
-
this.components.
|
|
111
|
+
this.components.peerStore.addEventListener('change:multiaddrs', (evt) => {
|
|
102
112
|
const { peerId } = evt.detail
|
|
103
113
|
|
|
104
|
-
if (this.components.
|
|
114
|
+
if (this.components.peerId.equals(peerId)) {
|
|
105
115
|
void this.pushToPeerStore().catch(err => log.error(err))
|
|
106
116
|
}
|
|
107
117
|
})
|
|
108
118
|
|
|
109
119
|
// When self protocols change, trigger identify-push
|
|
110
|
-
this.components.
|
|
120
|
+
this.components.peerStore.addEventListener('change:protocols', (evt) => {
|
|
111
121
|
const { peerId } = evt.detail
|
|
112
122
|
|
|
113
|
-
if (this.components.
|
|
123
|
+
if (this.components.peerId.equals(peerId)) {
|
|
114
124
|
void this.pushToPeerStore().catch(err => log.error(err))
|
|
115
125
|
}
|
|
116
126
|
})
|
|
@@ -125,10 +135,10 @@ export class IdentifyService implements Startable {
|
|
|
125
135
|
return
|
|
126
136
|
}
|
|
127
137
|
|
|
128
|
-
await this.components.
|
|
129
|
-
await this.components.
|
|
138
|
+
await this.components.peerStore.metadataBook.setValue(this.components.peerId, 'AgentVersion', uint8ArrayFromString(this.host.agentVersion))
|
|
139
|
+
await this.components.peerStore.metadataBook.setValue(this.components.peerId, 'ProtocolVersion', uint8ArrayFromString(this.host.protocolVersion))
|
|
130
140
|
|
|
131
|
-
await this.components.
|
|
141
|
+
await this.components.registrar.handle(this.identifyProtocolStr, (data) => {
|
|
132
142
|
void this._handleIdentify(data).catch(err => {
|
|
133
143
|
log.error(err)
|
|
134
144
|
})
|
|
@@ -136,7 +146,7 @@ export class IdentifyService implements Startable {
|
|
|
136
146
|
maxInboundStreams: this.init.maxInboundStreams,
|
|
137
147
|
maxOutboundStreams: this.init.maxOutboundStreams
|
|
138
148
|
})
|
|
139
|
-
await this.components.
|
|
149
|
+
await this.components.registrar.handle(this.identifyPushProtocolStr, (data) => {
|
|
140
150
|
void this._handlePush(data).catch(err => {
|
|
141
151
|
log.error(err)
|
|
142
152
|
})
|
|
@@ -149,8 +159,8 @@ export class IdentifyService implements Startable {
|
|
|
149
159
|
}
|
|
150
160
|
|
|
151
161
|
async stop () {
|
|
152
|
-
await this.components.
|
|
153
|
-
await this.components.
|
|
162
|
+
await this.components.registrar.unhandle(this.identifyProtocolStr)
|
|
163
|
+
await this.components.registrar.unhandle(this.identifyPushProtocolStr)
|
|
154
164
|
|
|
155
165
|
this.started = false
|
|
156
166
|
}
|
|
@@ -159,9 +169,9 @@ export class IdentifyService implements Startable {
|
|
|
159
169
|
* Send an Identify Push update to the list of connections
|
|
160
170
|
*/
|
|
161
171
|
async push (connections: Connection[]): Promise<void> {
|
|
162
|
-
const signedPeerRecord = await this.components.
|
|
163
|
-
const listenAddrs = this.components.
|
|
164
|
-
const protocols = await this.components.
|
|
172
|
+
const signedPeerRecord = await this.components.peerStore.addressBook.getRawEnvelope(this.components.peerId)
|
|
173
|
+
const listenAddrs = this.components.addressManager.getAddresses().map((ma) => ma.bytes)
|
|
174
|
+
const protocols = await this.components.peerStore.protoBook.get(this.components.peerId)
|
|
165
175
|
|
|
166
176
|
const pushes = connections.map(async connection => {
|
|
167
177
|
let stream: Stream | undefined
|
|
@@ -180,16 +190,14 @@ export class IdentifyService implements Startable {
|
|
|
180
190
|
// make stream abortable
|
|
181
191
|
const source = abortableDuplex(stream, timeoutController.signal)
|
|
182
192
|
|
|
183
|
-
await pipe(
|
|
193
|
+
await source.sink(pipe(
|
|
184
194
|
[Identify.encode({
|
|
185
195
|
listenAddrs,
|
|
186
196
|
signedPeerRecord,
|
|
187
197
|
protocols
|
|
188
198
|
})],
|
|
189
|
-
lp.encode()
|
|
190
|
-
|
|
191
|
-
drain
|
|
192
|
-
)
|
|
199
|
+
lp.encode()
|
|
200
|
+
))
|
|
193
201
|
} catch (err: any) {
|
|
194
202
|
// Just log errors
|
|
195
203
|
log.error('could not push identify update to peer', err)
|
|
@@ -216,9 +224,9 @@ export class IdentifyService implements Startable {
|
|
|
216
224
|
|
|
217
225
|
const connections: Connection[] = []
|
|
218
226
|
|
|
219
|
-
for (const conn of this.components.
|
|
227
|
+
for (const conn of this.components.connectionManager.getConnections()) {
|
|
220
228
|
const peerId = conn.remotePeer
|
|
221
|
-
const peer = await this.components.
|
|
229
|
+
const peer = await this.components.peerStore.get(peerId)
|
|
222
230
|
|
|
223
231
|
if (!peer.protocols.includes(this.identifyPushProtocolStr)) {
|
|
224
232
|
continue
|
|
@@ -311,7 +319,7 @@ export class IdentifyService implements Startable {
|
|
|
311
319
|
throw errCode(new Error('identified peer does not match the expected peer'), codes.ERR_INVALID_PEER)
|
|
312
320
|
}
|
|
313
321
|
|
|
314
|
-
if (this.components.
|
|
322
|
+
if (this.components.peerId.equals(id)) {
|
|
315
323
|
throw errCode(new Error('identified peer is our own peer id?'), codes.ERR_INVALID_PEER)
|
|
316
324
|
}
|
|
317
325
|
|
|
@@ -328,15 +336,15 @@ export class IdentifyService implements Startable {
|
|
|
328
336
|
throw errCode(new Error('identified peer does not match the expected peer'), codes.ERR_INVALID_PEER)
|
|
329
337
|
}
|
|
330
338
|
|
|
331
|
-
if (await this.components.
|
|
332
|
-
await this.components.
|
|
339
|
+
if (await this.components.peerStore.addressBook.consumePeerRecord(envelope)) {
|
|
340
|
+
await this.components.peerStore.protoBook.set(id, protocols)
|
|
333
341
|
|
|
334
342
|
if (agentVersion != null) {
|
|
335
|
-
await this.components.
|
|
343
|
+
await this.components.peerStore.metadataBook.setValue(id, 'AgentVersion', uint8ArrayFromString(agentVersion))
|
|
336
344
|
}
|
|
337
345
|
|
|
338
346
|
if (protocolVersion != null) {
|
|
339
|
-
await this.components.
|
|
347
|
+
await this.components.peerStore.metadataBook.setValue(id, 'ProtocolVersion', uint8ArrayFromString(protocolVersion))
|
|
340
348
|
}
|
|
341
349
|
|
|
342
350
|
log('identify completed for peer %p and protocols %o', id, protocols)
|
|
@@ -354,26 +362,26 @@ export class IdentifyService implements Startable {
|
|
|
354
362
|
|
|
355
363
|
// LEGACY: Update peers data in PeerStore
|
|
356
364
|
try {
|
|
357
|
-
await this.components.
|
|
365
|
+
await this.components.peerStore.addressBook.set(id, listenAddrs.map((addr) => multiaddr(addr)))
|
|
358
366
|
} catch (err: any) {
|
|
359
367
|
log.error('received invalid addrs', err)
|
|
360
368
|
}
|
|
361
369
|
|
|
362
|
-
await this.components.
|
|
370
|
+
await this.components.peerStore.protoBook.set(id, protocols)
|
|
363
371
|
|
|
364
372
|
if (agentVersion != null) {
|
|
365
|
-
await this.components.
|
|
373
|
+
await this.components.peerStore.metadataBook.setValue(id, 'AgentVersion', uint8ArrayFromString(agentVersion))
|
|
366
374
|
}
|
|
367
375
|
|
|
368
376
|
if (protocolVersion != null) {
|
|
369
|
-
await this.components.
|
|
377
|
+
await this.components.peerStore.metadataBook.setValue(id, 'ProtocolVersion', uint8ArrayFromString(protocolVersion))
|
|
370
378
|
}
|
|
371
379
|
|
|
372
380
|
log('identify completed for peer %p and protocols %o', id, protocols)
|
|
373
381
|
|
|
374
382
|
// TODO: Add and score our observed addr
|
|
375
383
|
log('received observed address of %s', cleanObservedAddr?.toString())
|
|
376
|
-
// this.components.
|
|
384
|
+
// this.components.addressManager.addObservedAddr(observedAddr)
|
|
377
385
|
}
|
|
378
386
|
|
|
379
387
|
/**
|
|
@@ -390,19 +398,19 @@ export class IdentifyService implements Startable {
|
|
|
390
398
|
} catch {}
|
|
391
399
|
|
|
392
400
|
try {
|
|
393
|
-
const publicKey = this.components.
|
|
394
|
-
const peerData = await this.components.
|
|
395
|
-
const multiaddrs = this.components.
|
|
401
|
+
const publicKey = this.components.peerId.publicKey ?? new Uint8Array(0)
|
|
402
|
+
const peerData = await this.components.peerStore.get(this.components.peerId)
|
|
403
|
+
const multiaddrs = this.components.addressManager.getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code))
|
|
396
404
|
let signedPeerRecord = peerData.peerRecordEnvelope
|
|
397
405
|
|
|
398
406
|
if (multiaddrs.length > 0 && signedPeerRecord == null) {
|
|
399
407
|
const peerRecord = new PeerRecord({
|
|
400
|
-
peerId: this.components.
|
|
408
|
+
peerId: this.components.peerId,
|
|
401
409
|
multiaddrs
|
|
402
410
|
})
|
|
403
411
|
|
|
404
|
-
const envelope = await RecordEnvelope.seal(peerRecord, this.components.
|
|
405
|
-
await this.components.
|
|
412
|
+
const envelope = await RecordEnvelope.seal(peerRecord, this.components.peerId)
|
|
413
|
+
await this.components.peerStore.addressBook.consumePeerRecord(envelope)
|
|
406
414
|
signedPeerRecord = envelope.marshal().subarray()
|
|
407
415
|
}
|
|
408
416
|
|
|
@@ -419,12 +427,8 @@ export class IdentifyService implements Startable {
|
|
|
419
427
|
// make stream abortable
|
|
420
428
|
const source = abortableDuplex(stream, timeoutController.signal)
|
|
421
429
|
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
lp.encode(),
|
|
425
|
-
source,
|
|
426
|
-
drain
|
|
427
|
-
)
|
|
430
|
+
const msgWithLenPrefix = pipe([message], lp.encode())
|
|
431
|
+
await source.sink(msgWithLenPrefix)
|
|
428
432
|
} catch (err: any) {
|
|
429
433
|
log.error('could not respond to identify request', err)
|
|
430
434
|
} finally {
|
|
@@ -475,7 +479,7 @@ export class IdentifyService implements Startable {
|
|
|
475
479
|
|
|
476
480
|
const id = connection.remotePeer
|
|
477
481
|
|
|
478
|
-
if (this.components.
|
|
482
|
+
if (this.components.peerId.equals(id)) {
|
|
479
483
|
log('received push from ourselves?')
|
|
480
484
|
return
|
|
481
485
|
}
|
|
@@ -488,10 +492,10 @@ export class IdentifyService implements Startable {
|
|
|
488
492
|
try {
|
|
489
493
|
const envelope = await RecordEnvelope.openAndCertify(message.signedPeerRecord, PeerRecord.DOMAIN)
|
|
490
494
|
|
|
491
|
-
if (await this.components.
|
|
495
|
+
if (await this.components.peerStore.addressBook.consumePeerRecord(envelope)) {
|
|
492
496
|
log('consumed signedPeerRecord sent in push')
|
|
493
497
|
|
|
494
|
-
await this.components.
|
|
498
|
+
await this.components.peerStore.protoBook.set(id, message.protocols)
|
|
495
499
|
return
|
|
496
500
|
} else {
|
|
497
501
|
log('failed to consume signedPeerRecord sent in push')
|
|
@@ -505,7 +509,7 @@ export class IdentifyService implements Startable {
|
|
|
505
509
|
|
|
506
510
|
// LEGACY: Update peers data in PeerStore
|
|
507
511
|
try {
|
|
508
|
-
await this.components.
|
|
512
|
+
await this.components.peerStore.addressBook.set(id,
|
|
509
513
|
message.listenAddrs.map((addr) => multiaddr(addr)))
|
|
510
514
|
} catch (err: any) {
|
|
511
515
|
log.error('received invalid addrs', err)
|
|
@@ -513,7 +517,7 @@ export class IdentifyService implements Startable {
|
|
|
513
517
|
|
|
514
518
|
// Update the protocols
|
|
515
519
|
try {
|
|
516
|
-
await this.components.
|
|
520
|
+
await this.components.peerStore.protoBook.set(id, message.protocols)
|
|
517
521
|
} catch (err: any) {
|
|
518
522
|
log.error('received invalid protocols', err)
|
|
519
523
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/* eslint-disable import/export */
|
|
2
|
+
/* eslint-disable complexity */
|
|
2
3
|
/* eslint-disable @typescript-eslint/no-namespace */
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
|
|
3
5
|
|
|
4
6
|
import { encodeMessage, decodeMessage, message } from 'protons-runtime'
|
|
5
7
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
@@ -20,59 +22,58 @@ export namespace Identify {
|
|
|
20
22
|
|
|
21
23
|
export const codec = (): Codec<Identify> => {
|
|
22
24
|
if (_codec == null) {
|
|
23
|
-
_codec = message<Identify>((obj,
|
|
25
|
+
_codec = message<Identify>((obj, w, opts = {}) => {
|
|
24
26
|
if (opts.lengthDelimited !== false) {
|
|
25
|
-
|
|
27
|
+
w.fork()
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
if (obj.protocolVersion != null) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
w.uint32(42)
|
|
32
|
+
w.string(obj.protocolVersion)
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
if (obj.agentVersion != null) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
+
w.uint32(50)
|
|
37
|
+
w.string(obj.agentVersion)
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
if (obj.publicKey != null) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
+
w.uint32(10)
|
|
42
|
+
w.bytes(obj.publicKey)
|
|
41
43
|
}
|
|
42
44
|
|
|
43
45
|
if (obj.listenAddrs != null) {
|
|
44
46
|
for (const value of obj.listenAddrs) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
+
w.uint32(18)
|
|
48
|
+
w.bytes(value)
|
|
47
49
|
}
|
|
48
|
-
} else {
|
|
49
|
-
throw new Error('Protocol error: required field "listenAddrs" was not found in object')
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
if (obj.observedAddr != null) {
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
w.uint32(34)
|
|
54
|
+
w.bytes(obj.observedAddr)
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
if (obj.protocols != null) {
|
|
58
58
|
for (const value of obj.protocols) {
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
w.uint32(26)
|
|
60
|
+
w.string(value)
|
|
61
61
|
}
|
|
62
|
-
} else {
|
|
63
|
-
throw new Error('Protocol error: required field "protocols" was not found in object')
|
|
64
62
|
}
|
|
65
63
|
|
|
66
64
|
if (obj.signedPeerRecord != null) {
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
w.uint32(66)
|
|
66
|
+
w.bytes(obj.signedPeerRecord)
|
|
69
67
|
}
|
|
70
68
|
|
|
71
69
|
if (opts.lengthDelimited !== false) {
|
|
72
|
-
|
|
70
|
+
w.ldelim()
|
|
73
71
|
}
|
|
74
72
|
}, (reader, length) => {
|
|
75
|
-
const obj: any = {
|
|
73
|
+
const obj: any = {
|
|
74
|
+
listenAddrs: [],
|
|
75
|
+
protocols: []
|
|
76
|
+
}
|
|
76
77
|
|
|
77
78
|
const end = length == null ? reader.len : reader.pos + length
|
|
78
79
|
|
|
@@ -90,14 +91,12 @@ export namespace Identify {
|
|
|
90
91
|
obj.publicKey = reader.bytes()
|
|
91
92
|
break
|
|
92
93
|
case 2:
|
|
93
|
-
obj.listenAddrs = obj.listenAddrs ?? []
|
|
94
94
|
obj.listenAddrs.push(reader.bytes())
|
|
95
95
|
break
|
|
96
96
|
case 4:
|
|
97
97
|
obj.observedAddr = reader.bytes()
|
|
98
98
|
break
|
|
99
99
|
case 3:
|
|
100
|
-
obj.protocols = obj.protocols ?? []
|
|
101
100
|
obj.protocols.push(reader.string())
|
|
102
101
|
break
|
|
103
102
|
case 8:
|
|
@@ -109,17 +108,6 @@ export namespace Identify {
|
|
|
109
108
|
}
|
|
110
109
|
}
|
|
111
110
|
|
|
112
|
-
obj.listenAddrs = obj.listenAddrs ?? []
|
|
113
|
-
obj.protocols = obj.protocols ?? []
|
|
114
|
-
|
|
115
|
-
if (obj.listenAddrs == null) {
|
|
116
|
-
throw new Error('Protocol error: value for required field "listenAddrs" was not found in protobuf')
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
if (obj.protocols == null) {
|
|
120
|
-
throw new Error('Protocol error: value for required field "protocols" was not found in protobuf')
|
|
121
|
-
}
|
|
122
|
-
|
|
123
111
|
return obj
|
|
124
112
|
})
|
|
125
113
|
}
|
package/src/index.ts
CHANGED
|
@@ -26,6 +26,7 @@ import type { KeyChain } from './keychain/index.js'
|
|
|
26
26
|
import type { ConnectionManagerInit } from './connection-manager/index.js'
|
|
27
27
|
import type { PingServiceInit } from './ping/index.js'
|
|
28
28
|
import type { FetchServiceInit } from './fetch/index.js'
|
|
29
|
+
import type { Components } from './components.js'
|
|
29
30
|
|
|
30
31
|
export interface PersistentPeerStoreOptions {
|
|
31
32
|
threshold?: number
|
|
@@ -113,15 +114,15 @@ export interface Libp2pInit {
|
|
|
113
114
|
ping: PingServiceInit
|
|
114
115
|
fetch: FetchServiceInit
|
|
115
116
|
|
|
116
|
-
transports: Transport
|
|
117
|
-
streamMuxers?: StreamMuxerFactory
|
|
118
|
-
connectionEncryption?: ConnectionEncrypter
|
|
119
|
-
peerDiscovery?: PeerDiscovery
|
|
120
|
-
peerRouters?: PeerRouting
|
|
121
|
-
contentRouters?: ContentRouting
|
|
122
|
-
dht?: DualDHT
|
|
123
|
-
pubsub?: PubSub
|
|
124
|
-
connectionProtector?: ConnectionProtector
|
|
117
|
+
transports: Array<(components: Components) => Transport>
|
|
118
|
+
streamMuxers?: Array<(components: Components) => StreamMuxerFactory>
|
|
119
|
+
connectionEncryption?: Array<(components: Components) => ConnectionEncrypter>
|
|
120
|
+
peerDiscovery?: Array<(components: Components) => PeerDiscovery>
|
|
121
|
+
peerRouters?: Array<(components: Components) => PeerRouting>
|
|
122
|
+
contentRouters?: Array<(components: Components) => ContentRouting>
|
|
123
|
+
dht?: (components: Components) => DualDHT
|
|
124
|
+
pubsub?: (components: Components) => PubSub
|
|
125
|
+
connectionProtector?: (components: Components) => ConnectionProtector
|
|
125
126
|
}
|
|
126
127
|
|
|
127
128
|
export interface Libp2pEvents {
|
package/src/insecure/index.ts
CHANGED
|
@@ -88,12 +88,11 @@ async function encrypt (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: Pe
|
|
|
88
88
|
sink: shake.stream.sink,
|
|
89
89
|
source: map(shake.stream.source, (buf) => buf.subarray())
|
|
90
90
|
},
|
|
91
|
-
remotePeer: peerId
|
|
92
|
-
remoteEarlyData: new Uint8Array()
|
|
91
|
+
remotePeer: peerId
|
|
93
92
|
}
|
|
94
93
|
}
|
|
95
94
|
|
|
96
|
-
|
|
95
|
+
class Plaintext implements ConnectionEncrypter {
|
|
97
96
|
public protocol: string = PROTOCOL
|
|
98
97
|
|
|
99
98
|
async secureInbound (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: PeerId): Promise<SecuredConnection> {
|
|
@@ -104,3 +103,7 @@ export class Plaintext implements ConnectionEncrypter {
|
|
|
104
103
|
return await encrypt(localId, conn, remoteId)
|
|
105
104
|
}
|
|
106
105
|
}
|
|
106
|
+
|
|
107
|
+
export function plaintext (): () => ConnectionEncrypter {
|
|
108
|
+
return () => new Plaintext()
|
|
109
|
+
}
|
package/src/insecure/pb/proto.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/* eslint-disable import/export */
|
|
2
|
+
/* eslint-disable complexity */
|
|
2
3
|
/* eslint-disable @typescript-eslint/no-namespace */
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
|
|
3
5
|
|
|
4
6
|
import { encodeMessage, decodeMessage, message, enumeration } from 'protons-runtime'
|
|
5
7
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
@@ -15,23 +17,25 @@ export namespace Exchange {
|
|
|
15
17
|
|
|
16
18
|
export const codec = (): Codec<Exchange> => {
|
|
17
19
|
if (_codec == null) {
|
|
18
|
-
_codec = message<Exchange>((obj,
|
|
20
|
+
_codec = message<Exchange>((obj, w, opts = {}) => {
|
|
19
21
|
if (opts.lengthDelimited !== false) {
|
|
20
|
-
|
|
22
|
+
w.fork()
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
if (obj.id != null) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
+
w.uint32(10)
|
|
27
|
+
w.bytes(obj.id)
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
if (obj.pubkey != null) {
|
|
29
|
-
|
|
30
|
-
PublicKey.codec().encode(obj.pubkey,
|
|
31
|
+
w.uint32(18)
|
|
32
|
+
PublicKey.codec().encode(obj.pubkey, w, {
|
|
33
|
+
writeDefaults: false
|
|
34
|
+
})
|
|
31
35
|
}
|
|
32
36
|
|
|
33
37
|
if (opts.lengthDelimited !== false) {
|
|
34
|
-
|
|
38
|
+
w.ldelim()
|
|
35
39
|
}
|
|
36
40
|
}, (reader, length) => {
|
|
37
41
|
const obj: any = {}
|
|
@@ -99,30 +103,29 @@ export namespace PublicKey {
|
|
|
99
103
|
|
|
100
104
|
export const codec = (): Codec<PublicKey> => {
|
|
101
105
|
if (_codec == null) {
|
|
102
|
-
_codec = message<PublicKey>((obj,
|
|
106
|
+
_codec = message<PublicKey>((obj, w, opts = {}) => {
|
|
103
107
|
if (opts.lengthDelimited !== false) {
|
|
104
|
-
|
|
108
|
+
w.fork()
|
|
105
109
|
}
|
|
106
110
|
|
|
107
|
-
if (obj.Type != null) {
|
|
108
|
-
|
|
109
|
-
KeyType.codec().encode(obj.Type,
|
|
110
|
-
} else {
|
|
111
|
-
throw new Error('Protocol error: required field "Type" was not found in object')
|
|
111
|
+
if (opts.writeDefaults === true || (obj.Type != null && __KeyTypeValues[obj.Type] !== 0)) {
|
|
112
|
+
w.uint32(8)
|
|
113
|
+
KeyType.codec().encode(obj.Type, w)
|
|
112
114
|
}
|
|
113
115
|
|
|
114
|
-
if (obj.Data != null) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
} else {
|
|
118
|
-
throw new Error('Protocol error: required field "Data" was not found in object')
|
|
116
|
+
if (opts.writeDefaults === true || (obj.Data != null && obj.Data.byteLength > 0)) {
|
|
117
|
+
w.uint32(18)
|
|
118
|
+
w.bytes(obj.Data)
|
|
119
119
|
}
|
|
120
120
|
|
|
121
121
|
if (opts.lengthDelimited !== false) {
|
|
122
|
-
|
|
122
|
+
w.ldelim()
|
|
123
123
|
}
|
|
124
124
|
}, (reader, length) => {
|
|
125
|
-
const obj: any = {
|
|
125
|
+
const obj: any = {
|
|
126
|
+
Type: KeyType.RSA,
|
|
127
|
+
Data: new Uint8Array(0)
|
|
128
|
+
}
|
|
126
129
|
|
|
127
130
|
const end = length == null ? reader.len : reader.pos + length
|
|
128
131
|
|
|
@@ -142,14 +145,6 @@ export namespace PublicKey {
|
|
|
142
145
|
}
|
|
143
146
|
}
|
|
144
147
|
|
|
145
|
-
if (obj.Type == null) {
|
|
146
|
-
throw new Error('Protocol error: value for required field "Type" was not found in protobuf')
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (obj.Data == null) {
|
|
150
|
-
throw new Error('Protocol error: value for required field "Data" was not found in protobuf')
|
|
151
|
-
}
|
|
152
|
-
|
|
153
148
|
return obj
|
|
154
149
|
})
|
|
155
150
|
}
|