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/keychain/index.ts
CHANGED
|
@@ -11,9 +11,9 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
|
11
11
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
12
12
|
import { generateKeyPair, importKey, unmarshalPrivateKey } from '@libp2p/crypto/keys'
|
|
13
13
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
14
|
-
import type { Components } from '@libp2p/components'
|
|
15
14
|
import { pbkdf2, randomBytes } from '@libp2p/crypto'
|
|
16
15
|
import type { Startable } from '@libp2p/interfaces/dist/src/startable'
|
|
16
|
+
import type { Datastore } from 'interface-datastore'
|
|
17
17
|
|
|
18
18
|
const log = logger('libp2p:keychain')
|
|
19
19
|
|
|
@@ -103,6 +103,11 @@ function DsInfoName (name: string) {
|
|
|
103
103
|
return new Key(infoPrefix + name)
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
+
export interface KeyChainComponents {
|
|
107
|
+
peerId: PeerId
|
|
108
|
+
datastore: Datastore
|
|
109
|
+
}
|
|
110
|
+
|
|
106
111
|
/**
|
|
107
112
|
* Manages the lifecycle of a key. Keys are encrypted at rest using PKCS #8.
|
|
108
113
|
*
|
|
@@ -112,14 +117,14 @@ function DsInfoName (name: string) {
|
|
|
112
117
|
*
|
|
113
118
|
*/
|
|
114
119
|
export class KeyChain implements Startable {
|
|
115
|
-
private readonly components:
|
|
120
|
+
private readonly components: KeyChainComponents
|
|
116
121
|
private init: KeyChainInit
|
|
117
122
|
private started: boolean
|
|
118
123
|
|
|
119
124
|
/**
|
|
120
125
|
* Creates a new instance of a key chain
|
|
121
126
|
*/
|
|
122
|
-
constructor (components:
|
|
127
|
+
constructor (components: KeyChainComponents, init: KeyChainInit) {
|
|
123
128
|
this.components = components
|
|
124
129
|
this.init = mergeOptions(defaultOptions, init)
|
|
125
130
|
|
|
@@ -157,8 +162,8 @@ export class KeyChain implements Startable {
|
|
|
157
162
|
async start () {
|
|
158
163
|
const dsname = DsInfoName('self')
|
|
159
164
|
|
|
160
|
-
if (!(await this.components.
|
|
161
|
-
await this.importPeer('self', this.components.
|
|
165
|
+
if (!(await this.components.datastore.has(dsname))) {
|
|
166
|
+
await this.importPeer('self', this.components.peerId)
|
|
162
167
|
}
|
|
163
168
|
|
|
164
169
|
this.started = true
|
|
@@ -229,7 +234,7 @@ export class KeyChain implements Startable {
|
|
|
229
234
|
}
|
|
230
235
|
|
|
231
236
|
const dsname = DsName(name)
|
|
232
|
-
const exists = await this.components.
|
|
237
|
+
const exists = await this.components.datastore.has(dsname)
|
|
233
238
|
if (exists) {
|
|
234
239
|
await randomDelay()
|
|
235
240
|
throw errCode(new Error('Key name already exists'), codes.ERR_KEY_ALREADY_EXISTS)
|
|
@@ -262,7 +267,7 @@ export class KeyChain implements Startable {
|
|
|
262
267
|
name: name,
|
|
263
268
|
id: kid
|
|
264
269
|
}
|
|
265
|
-
const batch = this.components.
|
|
270
|
+
const batch = this.components.datastore.batch()
|
|
266
271
|
batch.put(dsname, uint8ArrayFromString(pem))
|
|
267
272
|
batch.put(DsInfoName(name), uint8ArrayFromString(JSON.stringify(keyInfo)))
|
|
268
273
|
|
|
@@ -286,7 +291,7 @@ export class KeyChain implements Startable {
|
|
|
286
291
|
}
|
|
287
292
|
|
|
288
293
|
const info = []
|
|
289
|
-
for await (const value of this.components.
|
|
294
|
+
for await (const value of this.components.datastore.query(query)) {
|
|
290
295
|
info.push(JSON.parse(uint8ArrayToString(value.value)))
|
|
291
296
|
}
|
|
292
297
|
|
|
@@ -320,7 +325,7 @@ export class KeyChain implements Startable {
|
|
|
320
325
|
|
|
321
326
|
const dsname = DsInfoName(name)
|
|
322
327
|
try {
|
|
323
|
-
const res = await this.components.
|
|
328
|
+
const res = await this.components.datastore.get(dsname)
|
|
324
329
|
return JSON.parse(uint8ArrayToString(res))
|
|
325
330
|
} catch (err: any) {
|
|
326
331
|
await randomDelay()
|
|
@@ -342,7 +347,7 @@ export class KeyChain implements Startable {
|
|
|
342
347
|
}
|
|
343
348
|
const dsname = DsName(name)
|
|
344
349
|
const keyInfo = await this.findKeyByName(name)
|
|
345
|
-
const batch = this.components.
|
|
350
|
+
const batch = this.components.datastore.batch()
|
|
346
351
|
batch.delete(dsname)
|
|
347
352
|
batch.delete(DsInfoName(name))
|
|
348
353
|
await batch.commit()
|
|
@@ -370,19 +375,19 @@ export class KeyChain implements Startable {
|
|
|
370
375
|
const oldInfoName = DsInfoName(oldName)
|
|
371
376
|
const newInfoName = DsInfoName(newName)
|
|
372
377
|
|
|
373
|
-
const exists = await this.components.
|
|
378
|
+
const exists = await this.components.datastore.has(newDsname)
|
|
374
379
|
if (exists) {
|
|
375
380
|
await randomDelay()
|
|
376
381
|
throw errCode(new Error(`Key '${newName}' already exists`), codes.ERR_KEY_ALREADY_EXISTS)
|
|
377
382
|
}
|
|
378
383
|
|
|
379
384
|
try {
|
|
380
|
-
const pem = await this.components.
|
|
381
|
-
const res = await this.components.
|
|
385
|
+
const pem = await this.components.datastore.get(oldDsname)
|
|
386
|
+
const res = await this.components.datastore.get(oldInfoName)
|
|
382
387
|
|
|
383
388
|
const keyInfo = JSON.parse(uint8ArrayToString(res))
|
|
384
389
|
keyInfo.name = newName
|
|
385
|
-
const batch = this.components.
|
|
390
|
+
const batch = this.components.datastore.batch()
|
|
386
391
|
batch.put(newDsname, pem)
|
|
387
392
|
batch.put(newInfoName, uint8ArrayFromString(JSON.stringify(keyInfo)))
|
|
388
393
|
batch.delete(oldDsname)
|
|
@@ -410,7 +415,7 @@ export class KeyChain implements Startable {
|
|
|
410
415
|
|
|
411
416
|
const dsname = DsName(name)
|
|
412
417
|
try {
|
|
413
|
-
const res = await this.components.
|
|
418
|
+
const res = await this.components.datastore.get(dsname)
|
|
414
419
|
const pem = uint8ArrayToString(res)
|
|
415
420
|
const cached = privates.get(this)
|
|
416
421
|
|
|
@@ -445,7 +450,7 @@ export class KeyChain implements Startable {
|
|
|
445
450
|
throw errCode(new Error('PEM encoded key is required'), codes.ERR_PEM_REQUIRED)
|
|
446
451
|
}
|
|
447
452
|
const dsname = DsName(name)
|
|
448
|
-
const exists = await this.components.
|
|
453
|
+
const exists = await this.components.datastore.has(dsname)
|
|
449
454
|
if (exists) {
|
|
450
455
|
await randomDelay()
|
|
451
456
|
throw errCode(new Error(`Key '${name}' already exists`), codes.ERR_KEY_ALREADY_EXISTS)
|
|
@@ -479,7 +484,7 @@ export class KeyChain implements Startable {
|
|
|
479
484
|
name: name,
|
|
480
485
|
id: kid
|
|
481
486
|
}
|
|
482
|
-
const batch = this.components.
|
|
487
|
+
const batch = this.components.datastore.batch()
|
|
483
488
|
batch.put(dsname, uint8ArrayFromString(pem))
|
|
484
489
|
batch.put(DsInfoName(name), uint8ArrayFromString(JSON.stringify(keyInfo)))
|
|
485
490
|
await batch.commit()
|
|
@@ -505,7 +510,7 @@ export class KeyChain implements Startable {
|
|
|
505
510
|
const privateKey = await unmarshalPrivateKey(peer.privateKey)
|
|
506
511
|
|
|
507
512
|
const dsname = DsName(name)
|
|
508
|
-
const exists = await this.components.
|
|
513
|
+
const exists = await this.components.datastore.has(dsname)
|
|
509
514
|
if (exists) {
|
|
510
515
|
await randomDelay()
|
|
511
516
|
throw errCode(new Error(`Key '${name}' already exists`), codes.ERR_KEY_ALREADY_EXISTS)
|
|
@@ -523,7 +528,7 @@ export class KeyChain implements Startable {
|
|
|
523
528
|
name: name,
|
|
524
529
|
id: peer.toString()
|
|
525
530
|
}
|
|
526
|
-
const batch = this.components.
|
|
531
|
+
const batch = this.components.datastore.batch()
|
|
527
532
|
batch.put(dsname, uint8ArrayFromString(pem))
|
|
528
533
|
batch.put(DsInfoName(name), uint8ArrayFromString(JSON.stringify(keyInfo)))
|
|
529
534
|
await batch.commit()
|
|
@@ -545,7 +550,7 @@ export class KeyChain implements Startable {
|
|
|
545
550
|
|
|
546
551
|
try {
|
|
547
552
|
const dsname = DsName(name)
|
|
548
|
-
const res = await this.components.
|
|
553
|
+
const res = await this.components.datastore.get(dsname)
|
|
549
554
|
return uint8ArrayToString(res)
|
|
550
555
|
} catch (err: any) {
|
|
551
556
|
await randomDelay()
|
|
@@ -590,14 +595,14 @@ export class KeyChain implements Startable {
|
|
|
590
595
|
privates.set(this, { dek: newDek })
|
|
591
596
|
const keys = await this.listKeys()
|
|
592
597
|
for (const key of keys) {
|
|
593
|
-
const res = await this.components.
|
|
598
|
+
const res = await this.components.datastore.get(DsName(key.name))
|
|
594
599
|
const pem = uint8ArrayToString(res)
|
|
595
600
|
const privateKey = await importKey(pem, oldDek)
|
|
596
601
|
const password = newDek.toString()
|
|
597
602
|
const keyAsPEM = await privateKey.export(password)
|
|
598
603
|
|
|
599
604
|
// Update stored key
|
|
600
|
-
const batch = this.components.
|
|
605
|
+
const batch = this.components.datastore.batch()
|
|
601
606
|
const keyInfo = {
|
|
602
607
|
name: key.name,
|
|
603
608
|
id: key.id
|
package/src/libp2p.ts
CHANGED
|
@@ -26,8 +26,8 @@ import { PeerRecordUpdater } from './peer-record-updater.js'
|
|
|
26
26
|
import { DHTPeerRouting } from './dht/dht-peer-routing.js'
|
|
27
27
|
import { PersistentPeerStore } from '@libp2p/peer-store'
|
|
28
28
|
import { DHTContentRouting } from './dht/dht-content-routing.js'
|
|
29
|
-
import {
|
|
30
|
-
import {
|
|
29
|
+
import { DefaultComponents } from './components.js'
|
|
30
|
+
import type { Components } from './components.js'
|
|
31
31
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
32
32
|
import type { Connection } from '@libp2p/interface-connection'
|
|
33
33
|
import type { PeerRouting } from '@libp2p/interface-peer-routing'
|
|
@@ -71,15 +71,13 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
71
71
|
|
|
72
72
|
private started: boolean
|
|
73
73
|
private readonly services: Startable[]
|
|
74
|
-
private readonly initializables: Initializable[]
|
|
75
74
|
|
|
76
75
|
constructor (init: Libp2pInit) {
|
|
77
76
|
super()
|
|
78
77
|
|
|
79
|
-
this.initializables = []
|
|
80
78
|
this.started = false
|
|
81
79
|
this.peerId = init.peerId
|
|
82
|
-
this.components = new
|
|
80
|
+
const components = this.components = new DefaultComponents({
|
|
83
81
|
peerId: init.peerId,
|
|
84
82
|
datastore: init.datastore ?? new MemoryDatastore(),
|
|
85
83
|
connectionGater: {
|
|
@@ -95,21 +93,21 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
95
93
|
...init.connectionGater
|
|
96
94
|
}
|
|
97
95
|
})
|
|
98
|
-
|
|
99
|
-
addressFilter: this.components.
|
|
96
|
+
components.peerStore = new PersistentPeerStore(components, {
|
|
97
|
+
addressFilter: this.components.connectionGater.filterMultiaddrForPeer,
|
|
100
98
|
...init.peerStore
|
|
101
|
-
})
|
|
99
|
+
})
|
|
102
100
|
|
|
103
101
|
this.services = [
|
|
104
|
-
|
|
102
|
+
components
|
|
105
103
|
]
|
|
106
104
|
|
|
107
105
|
// Create Metrics
|
|
108
106
|
if (init.metrics.enabled) {
|
|
109
|
-
this.metrics = this.components.
|
|
107
|
+
this.metrics = this.components.metrics = new DefaultMetrics(init.metrics)
|
|
110
108
|
}
|
|
111
109
|
|
|
112
|
-
this.peerStore = this.components.
|
|
110
|
+
this.peerStore = this.components.peerStore
|
|
113
111
|
|
|
114
112
|
this.peerStore.addEventListener('peer', evt => {
|
|
115
113
|
const { detail: peerData } = evt
|
|
@@ -119,30 +117,30 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
119
117
|
|
|
120
118
|
// Set up connection protector if configured
|
|
121
119
|
if (init.connectionProtector != null) {
|
|
122
|
-
this.components.
|
|
120
|
+
this.components.connectionProtector = init.connectionProtector(components)
|
|
123
121
|
}
|
|
124
122
|
|
|
125
123
|
// Set up the Upgrader
|
|
126
|
-
this.components.
|
|
127
|
-
connectionEncryption: (init.connectionEncryption ?? []).map(
|
|
128
|
-
muxers: (init.streamMuxers ?? []).map(
|
|
124
|
+
this.components.upgrader = new DefaultUpgrader(this.components, {
|
|
125
|
+
connectionEncryption: (init.connectionEncryption ?? []).map(fn => this.configureComponent(fn(this.components))),
|
|
126
|
+
muxers: (init.streamMuxers ?? []).map(fn => this.configureComponent(fn(this.components))),
|
|
129
127
|
inboundUpgradeTimeout: init.connectionManager.inboundUpgradeTimeout
|
|
130
|
-
})
|
|
128
|
+
})
|
|
131
129
|
|
|
132
130
|
// Create the dialer
|
|
133
|
-
this.components.
|
|
131
|
+
this.components.dialer = new DefaultDialer(this.components, init.connectionManager)
|
|
134
132
|
|
|
135
133
|
// Create the Connection Manager
|
|
136
|
-
this.connectionManager = this.components.
|
|
134
|
+
this.connectionManager = this.components.connectionManager = new DefaultConnectionManager(this.components, init.connectionManager)
|
|
137
135
|
|
|
138
136
|
// Create the Registrar
|
|
139
|
-
this.registrar = this.components.
|
|
137
|
+
this.registrar = this.components.registrar = new DefaultRegistrar(this.components)
|
|
140
138
|
|
|
141
139
|
// Setup the transport manager
|
|
142
|
-
this.components.
|
|
140
|
+
this.components.transportManager = new DefaultTransportManager(this.components, init.transportManager)
|
|
143
141
|
|
|
144
142
|
// Addresses {listen, announce, noAnnounce}
|
|
145
|
-
this.components.
|
|
143
|
+
this.components.addressManager = new DefaultAddressManager(this.components, init.addresses)
|
|
146
144
|
|
|
147
145
|
// update our peer record when addresses change
|
|
148
146
|
this.configureComponent(new PeerRecordUpdater(this.components))
|
|
@@ -163,29 +161,26 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
163
161
|
// Create the Nat Manager
|
|
164
162
|
this.services.push(new NatManager(this.components, init.nat))
|
|
165
163
|
|
|
166
|
-
init.transports.forEach((
|
|
167
|
-
this.components.
|
|
164
|
+
init.transports.forEach((fn) => {
|
|
165
|
+
this.components.transportManager.add(this.configureComponent(fn(this.components)))
|
|
168
166
|
})
|
|
169
167
|
|
|
170
|
-
//
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
})
|
|
176
|
-
this.configureComponent(this.identifyService)
|
|
177
|
-
}
|
|
168
|
+
// Add the identify service
|
|
169
|
+
this.identifyService = new IdentifyService(this.components, {
|
|
170
|
+
...init.identify
|
|
171
|
+
})
|
|
172
|
+
this.configureComponent(this.identifyService)
|
|
178
173
|
|
|
179
174
|
// dht provided components (peerRouting, contentRouting, dht)
|
|
180
175
|
if (init.dht != null) {
|
|
181
|
-
this.dht = this.components.
|
|
176
|
+
this.dht = this.components.dht = init.dht(this.components)
|
|
182
177
|
} else {
|
|
183
178
|
this.dht = new DummyDHT()
|
|
184
179
|
}
|
|
185
180
|
|
|
186
181
|
// Create pubsub if provided
|
|
187
182
|
if (init.pubsub != null) {
|
|
188
|
-
this.pubsub = this.components.
|
|
183
|
+
this.pubsub = this.components.pubsub = init.pubsub(this.components)
|
|
189
184
|
} else {
|
|
190
185
|
this.pubsub = new DummyPubSub()
|
|
191
186
|
}
|
|
@@ -193,7 +188,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
193
188
|
// Attach remaining APIs
|
|
194
189
|
// peer and content routing will automatically get modules from _modules and _dht
|
|
195
190
|
|
|
196
|
-
const peerRouters: PeerRouting[] = (init.peerRouters ?? []).map(
|
|
191
|
+
const peerRouters: PeerRouting[] = (init.peerRouters ?? []).map(fn => this.configureComponent(fn(this.components)))
|
|
197
192
|
|
|
198
193
|
if (init.dht != null) {
|
|
199
194
|
// add dht to routers
|
|
@@ -205,24 +200,24 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
205
200
|
})
|
|
206
201
|
}
|
|
207
202
|
|
|
208
|
-
this.peerRouting = this.components.
|
|
203
|
+
this.peerRouting = this.components.peerRouting = this.configureComponent(new DefaultPeerRouting(this.components, {
|
|
209
204
|
...init.peerRouting,
|
|
210
205
|
routers: peerRouters
|
|
211
|
-
}))
|
|
206
|
+
}))
|
|
212
207
|
|
|
213
|
-
const contentRouters: ContentRouting[] = (init.contentRouters ?? []).map(
|
|
208
|
+
const contentRouters: ContentRouting[] = (init.contentRouters ?? []).map(fn => this.configureComponent(fn(this.components)))
|
|
214
209
|
|
|
215
210
|
if (init.dht != null) {
|
|
216
211
|
// add dht to routers
|
|
217
212
|
contentRouters.push(this.configureComponent(new DHTContentRouting(this.dht)))
|
|
218
213
|
}
|
|
219
214
|
|
|
220
|
-
this.contentRouting = this.components.
|
|
215
|
+
this.contentRouting = this.components.contentRouting = this.configureComponent(new CompoundContentRouting(this.components, {
|
|
221
216
|
routers: contentRouters
|
|
222
|
-
}))
|
|
217
|
+
}))
|
|
223
218
|
|
|
224
219
|
if (init.relay.enabled) {
|
|
225
|
-
this.components.
|
|
220
|
+
this.components.transportManager.add(this.configureComponent(new Circuit(this.components, init.relay)))
|
|
226
221
|
|
|
227
222
|
this.configureComponent(new Relay(this.components, {
|
|
228
223
|
addressSorter: init.connectionManager.addressSorter,
|
|
@@ -238,23 +233,9 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
238
233
|
...init.ping
|
|
239
234
|
}))
|
|
240
235
|
|
|
241
|
-
const autoDialer = this.configureComponent(new AutoDialer(this.components, {
|
|
242
|
-
enabled: init.connectionManager.autoDial !== false,
|
|
243
|
-
minConnections: init.connectionManager.minConnections,
|
|
244
|
-
dialTimeout: init.connectionManager.dialTimeout ?? 30000
|
|
245
|
-
}))
|
|
246
|
-
|
|
247
|
-
this.addEventListener('peer:discovery', evt => {
|
|
248
|
-
if (!this.isStarted()) {
|
|
249
|
-
return
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
autoDialer.handle(evt)
|
|
253
|
-
})
|
|
254
|
-
|
|
255
236
|
// Discovery modules
|
|
256
|
-
for (const
|
|
257
|
-
this.configureComponent(
|
|
237
|
+
for (const fn of init.peerDiscovery ?? []) {
|
|
238
|
+
const service = this.configureComponent(fn(this.components))
|
|
258
239
|
|
|
259
240
|
service.addEventListener('peer', (evt) => {
|
|
260
241
|
this.onDiscoveryPeer(evt)
|
|
@@ -267,10 +248,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
267
248
|
this.services.push(component)
|
|
268
249
|
}
|
|
269
250
|
|
|
270
|
-
if (isInitializable(component)) {
|
|
271
|
-
this.initializables.push(component)
|
|
272
|
-
}
|
|
273
|
-
|
|
274
251
|
return component
|
|
275
252
|
}
|
|
276
253
|
|
|
@@ -287,11 +264,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
287
264
|
log('libp2p is starting')
|
|
288
265
|
|
|
289
266
|
try {
|
|
290
|
-
// Set available components on all modules interested in components
|
|
291
|
-
this.initializables.forEach(obj => {
|
|
292
|
-
obj.init(this.components)
|
|
293
|
-
})
|
|
294
|
-
|
|
295
267
|
await Promise.all(
|
|
296
268
|
this.services.map(async service => {
|
|
297
269
|
if (service.beforeStart != null) {
|
|
@@ -361,13 +333,13 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
361
333
|
}
|
|
362
334
|
|
|
363
335
|
getConnections (peerId?: PeerId): Connection[] {
|
|
364
|
-
return this.components.
|
|
336
|
+
return this.components.connectionManager.getConnections(peerId)
|
|
365
337
|
}
|
|
366
338
|
|
|
367
339
|
getPeers (): PeerId[] {
|
|
368
340
|
const peerSet = new PeerSet()
|
|
369
341
|
|
|
370
|
-
for (const conn of this.components.
|
|
342
|
+
for (const conn of this.components.connectionManager.getConnections()) {
|
|
371
343
|
peerSet.add(conn.remotePeer)
|
|
372
344
|
}
|
|
373
345
|
|
|
@@ -377,9 +349,9 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
377
349
|
async dial (peer: PeerId | Multiaddr, options: AbortOptions = {}): Promise<Connection> {
|
|
378
350
|
const { id, multiaddrs } = getPeer(peer)
|
|
379
351
|
|
|
380
|
-
await this.components.
|
|
352
|
+
await this.components.peerStore.addressBook.add(id, multiaddrs)
|
|
381
353
|
|
|
382
|
-
return await this.components.
|
|
354
|
+
return await this.components.connectionManager.openConnection(id, options)
|
|
383
355
|
}
|
|
384
356
|
|
|
385
357
|
async dialProtocol (peer: PeerId | Multiaddr, protocols: string | string[], options: AbortOptions = {}) {
|
|
@@ -399,13 +371,13 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
399
371
|
}
|
|
400
372
|
|
|
401
373
|
getMultiaddrs (): Multiaddr[] {
|
|
402
|
-
return this.components.
|
|
374
|
+
return this.components.addressManager.getAddresses()
|
|
403
375
|
}
|
|
404
376
|
|
|
405
377
|
async hangUp (peer: PeerId | Multiaddr | string): Promise<void> {
|
|
406
378
|
const { id } = getPeer(peer)
|
|
407
379
|
|
|
408
|
-
await this.components.
|
|
380
|
+
await this.components.connectionManager.closeConnections(id)
|
|
409
381
|
}
|
|
410
382
|
|
|
411
383
|
/**
|
|
@@ -451,7 +423,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
451
423
|
const { id, multiaddrs } = getPeer(peer)
|
|
452
424
|
|
|
453
425
|
if (multiaddrs != null) {
|
|
454
|
-
await this.components.
|
|
426
|
+
await this.components.peerStore.addressBook.add(id, multiaddrs)
|
|
455
427
|
}
|
|
456
428
|
|
|
457
429
|
return await this.fetchService.fetch(id, key, options)
|
|
@@ -461,7 +433,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
461
433
|
const { id, multiaddrs } = getPeer(peer)
|
|
462
434
|
|
|
463
435
|
if (multiaddrs.length > 0) {
|
|
464
|
-
await this.components.
|
|
436
|
+
await this.components.peerStore.addressBook.add(id, multiaddrs)
|
|
465
437
|
}
|
|
466
438
|
|
|
467
439
|
return await this.pingService.ping(id, options)
|
|
@@ -474,7 +446,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
474
446
|
|
|
475
447
|
await Promise.all(
|
|
476
448
|
protocols.map(async protocol => {
|
|
477
|
-
await this.components.
|
|
449
|
+
await this.components.registrar.handle(protocol, handler, options)
|
|
478
450
|
})
|
|
479
451
|
)
|
|
480
452
|
}
|
|
@@ -486,7 +458,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
486
458
|
|
|
487
459
|
await Promise.all(
|
|
488
460
|
protocols.map(async protocol => {
|
|
489
|
-
await this.components.
|
|
461
|
+
await this.components.registrar.unhandle(protocol)
|
|
490
462
|
})
|
|
491
463
|
)
|
|
492
464
|
}
|
|
@@ -504,11 +476,11 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
504
476
|
}
|
|
505
477
|
|
|
506
478
|
if (peer.multiaddrs.length > 0) {
|
|
507
|
-
void this.components.
|
|
479
|
+
void this.components.peerStore.addressBook.add(peer.id, peer.multiaddrs).catch(err => log.error(err))
|
|
508
480
|
}
|
|
509
481
|
|
|
510
482
|
if (peer.protocols.length > 0) {
|
|
511
|
-
void this.components.
|
|
483
|
+
void this.components.peerStore.protoBook.set(peer.id, peer.protocols).catch(err => log.error(err))
|
|
512
484
|
}
|
|
513
485
|
|
|
514
486
|
this.dispatchEvent(new CustomEvent<PeerInfo>('peer:discovery', { detail: peer }))
|
package/src/nat-manager.ts
CHANGED
|
@@ -8,7 +8,9 @@ import errCode from 'err-code'
|
|
|
8
8
|
import { codes } from './errors.js'
|
|
9
9
|
import { isLoopback } from '@libp2p/utils/multiaddr/is-loopback'
|
|
10
10
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
11
|
-
import type {
|
|
11
|
+
import type { TransportManager } from '@libp2p/interface-transport'
|
|
12
|
+
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
13
|
+
import type { AddressManager } from '@libp2p/interface-address-manager'
|
|
12
14
|
|
|
13
15
|
const log = logger('libp2p:nat')
|
|
14
16
|
const DEFAULT_TTL = 7200
|
|
@@ -61,8 +63,14 @@ export interface NatManagerInit {
|
|
|
61
63
|
gateway?: string
|
|
62
64
|
}
|
|
63
65
|
|
|
66
|
+
export interface NatManagerComponents {
|
|
67
|
+
peerId: PeerId
|
|
68
|
+
transportManager: TransportManager
|
|
69
|
+
addressManager: AddressManager
|
|
70
|
+
}
|
|
71
|
+
|
|
64
72
|
export class NatManager implements Startable {
|
|
65
|
-
private readonly components:
|
|
73
|
+
private readonly components: NatManagerComponents
|
|
66
74
|
private readonly enabled: boolean
|
|
67
75
|
private readonly externalAddress?: string
|
|
68
76
|
private readonly localAddress?: string
|
|
@@ -73,14 +81,14 @@ export class NatManager implements Startable {
|
|
|
73
81
|
private started: boolean
|
|
74
82
|
private client?: NatAPI
|
|
75
83
|
|
|
76
|
-
constructor (components:
|
|
84
|
+
constructor (components: NatManagerComponents, init: NatManagerInit) {
|
|
77
85
|
this.components = components
|
|
78
86
|
|
|
79
87
|
this.started = false
|
|
80
88
|
this.enabled = init.enabled
|
|
81
89
|
this.externalAddress = init.externalAddress
|
|
82
90
|
this.localAddress = init.localAddress
|
|
83
|
-
this.description = init.description ?? `${pkg.name}@${pkg.version} ${this.components.
|
|
91
|
+
this.description = init.description ?? `${pkg.name}@${pkg.version} ${this.components.peerId.toString()}`
|
|
84
92
|
this.ttl = init.ttl ?? DEFAULT_TTL
|
|
85
93
|
this.keepAlive = init.keepAlive ?? true
|
|
86
94
|
this.gateway = init.gateway
|
|
@@ -116,7 +124,7 @@ export class NatManager implements Startable {
|
|
|
116
124
|
}
|
|
117
125
|
|
|
118
126
|
async _start () {
|
|
119
|
-
const addrs = this.components.
|
|
127
|
+
const addrs = this.components.transportManager.getAddrs()
|
|
120
128
|
|
|
121
129
|
for (const addr of addrs) {
|
|
122
130
|
// try to open uPnP ports for each thin waist address
|
|
@@ -157,7 +165,7 @@ export class NatManager implements Startable {
|
|
|
157
165
|
protocol: transport.toUpperCase() === 'TCP' ? 'TCP' : 'UDP'
|
|
158
166
|
})
|
|
159
167
|
|
|
160
|
-
this.components.
|
|
168
|
+
this.components.addressManager.addObservedAddr(fromNodeAddress({
|
|
161
169
|
family: 4,
|
|
162
170
|
address: publicIp,
|
|
163
171
|
port: publicPort
|
|
@@ -1,16 +1,26 @@
|
|
|
1
1
|
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
|
|
2
|
-
import type { Components } from '@libp2p/components'
|
|
3
2
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
4
3
|
import { logger } from '@libp2p/logger'
|
|
5
4
|
import { protocols } from '@multiformats/multiaddr'
|
|
5
|
+
import type { TransportManager } from '@libp2p/interface-transport'
|
|
6
|
+
import type { AddressManager } from '@libp2p/interface-address-manager'
|
|
7
|
+
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
8
|
+
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
6
9
|
|
|
7
10
|
const log = logger('libp2p:peer-record-updater')
|
|
8
11
|
|
|
12
|
+
export interface PeerRecordUpdaterComponents {
|
|
13
|
+
peerId: PeerId
|
|
14
|
+
peerStore: PeerStore
|
|
15
|
+
transportManager: TransportManager
|
|
16
|
+
addressManager: AddressManager
|
|
17
|
+
}
|
|
18
|
+
|
|
9
19
|
export class PeerRecordUpdater implements Startable {
|
|
10
|
-
private readonly components:
|
|
20
|
+
private readonly components: PeerRecordUpdaterComponents
|
|
11
21
|
private started: boolean
|
|
12
22
|
|
|
13
|
-
constructor (components:
|
|
23
|
+
constructor (components: PeerRecordUpdaterComponents) {
|
|
14
24
|
this.components = components
|
|
15
25
|
this.started = false
|
|
16
26
|
this.update = this.update.bind(this)
|
|
@@ -22,16 +32,16 @@ export class PeerRecordUpdater implements Startable {
|
|
|
22
32
|
|
|
23
33
|
async start () {
|
|
24
34
|
this.started = true
|
|
25
|
-
this.components.
|
|
26
|
-
this.components.
|
|
27
|
-
this.components.
|
|
35
|
+
this.components.transportManager.addEventListener('listener:listening', this.update)
|
|
36
|
+
this.components.transportManager.addEventListener('listener:close', this.update)
|
|
37
|
+
this.components.addressManager.addEventListener('change:addresses', this.update)
|
|
28
38
|
}
|
|
29
39
|
|
|
30
40
|
async stop () {
|
|
31
41
|
this.started = false
|
|
32
|
-
this.components.
|
|
33
|
-
this.components.
|
|
34
|
-
this.components.
|
|
42
|
+
this.components.transportManager.removeEventListener('listener:listening', this.update)
|
|
43
|
+
this.components.transportManager.removeEventListener('listener:close', this.update)
|
|
44
|
+
this.components.addressManager.removeEventListener('change:addresses', this.update)
|
|
35
45
|
}
|
|
36
46
|
|
|
37
47
|
/**
|
|
@@ -41,12 +51,12 @@ export class PeerRecordUpdater implements Startable {
|
|
|
41
51
|
Promise.resolve()
|
|
42
52
|
.then(async () => {
|
|
43
53
|
const peerRecord = new PeerRecord({
|
|
44
|
-
peerId: this.components.
|
|
45
|
-
multiaddrs: this.components.
|
|
54
|
+
peerId: this.components.peerId,
|
|
55
|
+
multiaddrs: this.components.addressManager.getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code))
|
|
46
56
|
})
|
|
47
57
|
|
|
48
|
-
const envelope = await RecordEnvelope.seal(peerRecord, this.components.
|
|
49
|
-
await this.components.
|
|
58
|
+
const envelope = await RecordEnvelope.seal(peerRecord, this.components.peerId)
|
|
59
|
+
await this.components.peerStore.addressBook.consumePeerRecord(envelope)
|
|
50
60
|
})
|
|
51
61
|
.catch(err => {
|
|
52
62
|
log.error('Could not update self peer record: %o', err)
|