libp2p 0.39.5 → 0.40.0-030dbc8
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 +14 -2
- 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 +19 -6
- 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/config.d.ts.map +1 -1
- package/dist/src/config.js +0 -11
- package/dist/src/config.js.map +1 -1
- 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 -6
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
- package/dist/src/connection-manager/dialer/index.js +12 -17
- package/dist/src/connection-manager/dialer/index.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +41 -17
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +80 -81
- 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 +18 -6
- 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 +13 -12
- 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 +49 -73
- 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 +5 -6
- 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 -75
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +39 -40
- package/src/address-manager/index.ts +34 -10
- package/src/circuit/README.md +6 -6
- 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/config.ts +0 -11
- package/src/connection-manager/auto-dialler.ts +17 -9
- package/src/connection-manager/dialer/index.ts +26 -25
- package/src/connection-manager/index.ts +136 -93
- package/src/content-routing/index.ts +13 -7
- package/src/errors.ts +2 -1
- package/src/fetch/index.ts +28 -10
- 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 +12 -11
- 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 +50 -79
- 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 +16 -10
- package/src/upgrader.ts +78 -87
- 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/dist/src/metrics/index.d.ts +0 -93
- package/dist/src/metrics/index.d.ts.map +0 -1
- package/dist/src/metrics/index.js +0 -234
- package/dist/src/metrics/index.js.map +0 -1
- package/dist/src/metrics/moving-average.d.ts +0 -14
- package/dist/src/metrics/moving-average.d.ts.map +0 -1
- package/dist/src/metrics/moving-average.js +0 -40
- package/dist/src/metrics/moving-average.js.map +0 -1
- package/dist/src/metrics/stats.d.ts +0 -87
- package/dist/src/metrics/stats.d.ts.map +0 -1
- package/dist/src/metrics/stats.js +0 -183
- package/dist/src/metrics/stats.js.map +0 -1
- package/src/connection-manager/dialer/auto-dialer.ts +0 -65
- package/src/metrics/index.ts +0 -311
- package/src/metrics/moving-average.ts +0 -53
- package/src/metrics/stats.ts +0 -238
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
|
}
|
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
|
@@ -14,7 +14,6 @@ import { AutoDialler } from './connection-manager/auto-dialler.js'
|
|
|
14
14
|
import { Circuit } from './circuit/transport.js'
|
|
15
15
|
import { Relay } from './circuit/index.js'
|
|
16
16
|
import { KeyChain } from './keychain/index.js'
|
|
17
|
-
import { DefaultMetrics } from './metrics/index.js'
|
|
18
17
|
import { DefaultTransportManager } from './transport-manager.js'
|
|
19
18
|
import { DefaultUpgrader } from './upgrader.js'
|
|
20
19
|
import { DefaultRegistrar } from './registrar.js'
|
|
@@ -26,8 +25,8 @@ import { PeerRecordUpdater } from './peer-record-updater.js'
|
|
|
26
25
|
import { DHTPeerRouting } from './dht/dht-peer-routing.js'
|
|
27
26
|
import { PersistentPeerStore } from '@libp2p/peer-store'
|
|
28
27
|
import { DHTContentRouting } from './dht/dht-content-routing.js'
|
|
29
|
-
import {
|
|
30
|
-
import {
|
|
28
|
+
import { DefaultComponents } from './components.js'
|
|
29
|
+
import type { Components } from './components.js'
|
|
31
30
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
32
31
|
import type { Connection } from '@libp2p/interface-connection'
|
|
33
32
|
import type { PeerRouting } from '@libp2p/interface-peer-routing'
|
|
@@ -71,15 +70,13 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
71
70
|
|
|
72
71
|
private started: boolean
|
|
73
72
|
private readonly services: Startable[]
|
|
74
|
-
private readonly initializables: Initializable[]
|
|
75
73
|
|
|
76
74
|
constructor (init: Libp2pInit) {
|
|
77
75
|
super()
|
|
78
76
|
|
|
79
|
-
this.initializables = []
|
|
80
77
|
this.started = false
|
|
81
78
|
this.peerId = init.peerId
|
|
82
|
-
this.components = new
|
|
79
|
+
const components = this.components = new DefaultComponents({
|
|
83
80
|
peerId: init.peerId,
|
|
84
81
|
datastore: init.datastore ?? new MemoryDatastore(),
|
|
85
82
|
connectionGater: {
|
|
@@ -95,21 +92,21 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
95
92
|
...init.connectionGater
|
|
96
93
|
}
|
|
97
94
|
})
|
|
98
|
-
|
|
99
|
-
addressFilter: this.components.
|
|
95
|
+
components.peerStore = new PersistentPeerStore(components, {
|
|
96
|
+
addressFilter: this.components.connectionGater.filterMultiaddrForPeer,
|
|
100
97
|
...init.peerStore
|
|
101
|
-
})
|
|
98
|
+
})
|
|
102
99
|
|
|
103
100
|
this.services = [
|
|
104
|
-
|
|
101
|
+
components
|
|
105
102
|
]
|
|
106
103
|
|
|
107
104
|
// Create Metrics
|
|
108
|
-
if (init.metrics
|
|
109
|
-
this.metrics = this.components.
|
|
105
|
+
if (init.metrics != null) {
|
|
106
|
+
this.metrics = this.components.metrics = this.configureComponent(init.metrics(this.components))
|
|
110
107
|
}
|
|
111
108
|
|
|
112
|
-
this.peerStore = this.components.
|
|
109
|
+
this.peerStore = this.components.peerStore
|
|
113
110
|
|
|
114
111
|
this.peerStore.addEventListener('peer', evt => {
|
|
115
112
|
const { detail: peerData } = evt
|
|
@@ -119,30 +116,30 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
119
116
|
|
|
120
117
|
// Set up connection protector if configured
|
|
121
118
|
if (init.connectionProtector != null) {
|
|
122
|
-
this.components.
|
|
119
|
+
this.components.connectionProtector = init.connectionProtector(components)
|
|
123
120
|
}
|
|
124
121
|
|
|
125
122
|
// Set up the Upgrader
|
|
126
|
-
this.components.
|
|
127
|
-
connectionEncryption: (init.connectionEncryption ?? []).map(
|
|
128
|
-
muxers: (init.streamMuxers ?? []).map(
|
|
123
|
+
this.components.upgrader = new DefaultUpgrader(this.components, {
|
|
124
|
+
connectionEncryption: (init.connectionEncryption ?? []).map(fn => this.configureComponent(fn(this.components))),
|
|
125
|
+
muxers: (init.streamMuxers ?? []).map(fn => this.configureComponent(fn(this.components))),
|
|
129
126
|
inboundUpgradeTimeout: init.connectionManager.inboundUpgradeTimeout
|
|
130
|
-
})
|
|
127
|
+
})
|
|
131
128
|
|
|
132
129
|
// Create the dialer
|
|
133
|
-
this.components.
|
|
130
|
+
this.components.dialer = new DefaultDialer(this.components, init.connectionManager)
|
|
134
131
|
|
|
135
132
|
// Create the Connection Manager
|
|
136
|
-
this.connectionManager = this.components.
|
|
133
|
+
this.connectionManager = this.components.connectionManager = new DefaultConnectionManager(this.components, init.connectionManager)
|
|
137
134
|
|
|
138
135
|
// Create the Registrar
|
|
139
|
-
this.registrar = this.components.
|
|
136
|
+
this.registrar = this.components.registrar = new DefaultRegistrar(this.components)
|
|
140
137
|
|
|
141
138
|
// Setup the transport manager
|
|
142
|
-
this.components.
|
|
139
|
+
this.components.transportManager = new DefaultTransportManager(this.components, init.transportManager)
|
|
143
140
|
|
|
144
141
|
// Addresses {listen, announce, noAnnounce}
|
|
145
|
-
this.components.
|
|
142
|
+
this.components.addressManager = new DefaultAddressManager(this.components, init.addresses)
|
|
146
143
|
|
|
147
144
|
// update our peer record when addresses change
|
|
148
145
|
this.configureComponent(new PeerRecordUpdater(this.components))
|
|
@@ -163,29 +160,26 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
163
160
|
// Create the Nat Manager
|
|
164
161
|
this.services.push(new NatManager(this.components, init.nat))
|
|
165
162
|
|
|
166
|
-
init.transports.forEach((
|
|
167
|
-
this.components.
|
|
163
|
+
init.transports.forEach((fn) => {
|
|
164
|
+
this.components.transportManager.add(this.configureComponent(fn(this.components)))
|
|
168
165
|
})
|
|
169
166
|
|
|
170
|
-
//
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
})
|
|
176
|
-
this.configureComponent(this.identifyService)
|
|
177
|
-
}
|
|
167
|
+
// Add the identify service
|
|
168
|
+
this.identifyService = new IdentifyService(this.components, {
|
|
169
|
+
...init.identify
|
|
170
|
+
})
|
|
171
|
+
this.configureComponent(this.identifyService)
|
|
178
172
|
|
|
179
173
|
// dht provided components (peerRouting, contentRouting, dht)
|
|
180
174
|
if (init.dht != null) {
|
|
181
|
-
this.dht = this.components.
|
|
175
|
+
this.dht = this.components.dht = init.dht(this.components)
|
|
182
176
|
} else {
|
|
183
177
|
this.dht = new DummyDHT()
|
|
184
178
|
}
|
|
185
179
|
|
|
186
180
|
// Create pubsub if provided
|
|
187
181
|
if (init.pubsub != null) {
|
|
188
|
-
this.pubsub = this.components.
|
|
182
|
+
this.pubsub = this.components.pubsub = init.pubsub(this.components)
|
|
189
183
|
} else {
|
|
190
184
|
this.pubsub = new DummyPubSub()
|
|
191
185
|
}
|
|
@@ -193,7 +187,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
193
187
|
// Attach remaining APIs
|
|
194
188
|
// peer and content routing will automatically get modules from _modules and _dht
|
|
195
189
|
|
|
196
|
-
const peerRouters: PeerRouting[] = (init.peerRouters ?? []).map(
|
|
190
|
+
const peerRouters: PeerRouting[] = (init.peerRouters ?? []).map(fn => this.configureComponent(fn(this.components)))
|
|
197
191
|
|
|
198
192
|
if (init.dht != null) {
|
|
199
193
|
// add dht to routers
|
|
@@ -205,24 +199,24 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
205
199
|
})
|
|
206
200
|
}
|
|
207
201
|
|
|
208
|
-
this.peerRouting = this.components.
|
|
202
|
+
this.peerRouting = this.components.peerRouting = this.configureComponent(new DefaultPeerRouting(this.components, {
|
|
209
203
|
...init.peerRouting,
|
|
210
204
|
routers: peerRouters
|
|
211
|
-
}))
|
|
205
|
+
}))
|
|
212
206
|
|
|
213
|
-
const contentRouters: ContentRouting[] = (init.contentRouters ?? []).map(
|
|
207
|
+
const contentRouters: ContentRouting[] = (init.contentRouters ?? []).map(fn => this.configureComponent(fn(this.components)))
|
|
214
208
|
|
|
215
209
|
if (init.dht != null) {
|
|
216
210
|
// add dht to routers
|
|
217
211
|
contentRouters.push(this.configureComponent(new DHTContentRouting(this.dht)))
|
|
218
212
|
}
|
|
219
213
|
|
|
220
|
-
this.contentRouting = this.components.
|
|
214
|
+
this.contentRouting = this.components.contentRouting = this.configureComponent(new CompoundContentRouting(this.components, {
|
|
221
215
|
routers: contentRouters
|
|
222
|
-
}))
|
|
216
|
+
}))
|
|
223
217
|
|
|
224
218
|
if (init.relay.enabled) {
|
|
225
|
-
this.components.
|
|
219
|
+
this.components.transportManager.add(this.configureComponent(new Circuit(this.components, init.relay)))
|
|
226
220
|
|
|
227
221
|
this.configureComponent(new Relay(this.components, {
|
|
228
222
|
addressSorter: init.connectionManager.addressSorter,
|
|
@@ -238,23 +232,9 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
238
232
|
...init.ping
|
|
239
233
|
}))
|
|
240
234
|
|
|
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
235
|
// Discovery modules
|
|
256
|
-
for (const
|
|
257
|
-
this.configureComponent(
|
|
236
|
+
for (const fn of init.peerDiscovery ?? []) {
|
|
237
|
+
const service = this.configureComponent(fn(this.components))
|
|
258
238
|
|
|
259
239
|
service.addEventListener('peer', (evt) => {
|
|
260
240
|
this.onDiscoveryPeer(evt)
|
|
@@ -267,10 +247,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
267
247
|
this.services.push(component)
|
|
268
248
|
}
|
|
269
249
|
|
|
270
|
-
if (isInitializable(component)) {
|
|
271
|
-
this.initializables.push(component)
|
|
272
|
-
}
|
|
273
|
-
|
|
274
250
|
return component
|
|
275
251
|
}
|
|
276
252
|
|
|
@@ -287,11 +263,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
287
263
|
log('libp2p is starting')
|
|
288
264
|
|
|
289
265
|
try {
|
|
290
|
-
// Set available components on all modules interested in components
|
|
291
|
-
this.initializables.forEach(obj => {
|
|
292
|
-
obj.init(this.components)
|
|
293
|
-
})
|
|
294
|
-
|
|
295
266
|
await Promise.all(
|
|
296
267
|
this.services.map(async service => {
|
|
297
268
|
if (service.beforeStart != null) {
|
|
@@ -361,13 +332,13 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
361
332
|
}
|
|
362
333
|
|
|
363
334
|
getConnections (peerId?: PeerId): Connection[] {
|
|
364
|
-
return this.components.
|
|
335
|
+
return this.components.connectionManager.getConnections(peerId)
|
|
365
336
|
}
|
|
366
337
|
|
|
367
338
|
getPeers (): PeerId[] {
|
|
368
339
|
const peerSet = new PeerSet()
|
|
369
340
|
|
|
370
|
-
for (const conn of this.components.
|
|
341
|
+
for (const conn of this.components.connectionManager.getConnections()) {
|
|
371
342
|
peerSet.add(conn.remotePeer)
|
|
372
343
|
}
|
|
373
344
|
|
|
@@ -377,9 +348,9 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
377
348
|
async dial (peer: PeerId | Multiaddr, options: AbortOptions = {}): Promise<Connection> {
|
|
378
349
|
const { id, multiaddrs } = getPeer(peer)
|
|
379
350
|
|
|
380
|
-
await this.components.
|
|
351
|
+
await this.components.peerStore.addressBook.add(id, multiaddrs)
|
|
381
352
|
|
|
382
|
-
return await this.components.
|
|
353
|
+
return await this.components.connectionManager.openConnection(id, options)
|
|
383
354
|
}
|
|
384
355
|
|
|
385
356
|
async dialProtocol (peer: PeerId | Multiaddr, protocols: string | string[], options: AbortOptions = {}) {
|
|
@@ -399,13 +370,13 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
399
370
|
}
|
|
400
371
|
|
|
401
372
|
getMultiaddrs (): Multiaddr[] {
|
|
402
|
-
return this.components.
|
|
373
|
+
return this.components.addressManager.getAddresses()
|
|
403
374
|
}
|
|
404
375
|
|
|
405
376
|
async hangUp (peer: PeerId | Multiaddr | string): Promise<void> {
|
|
406
377
|
const { id } = getPeer(peer)
|
|
407
378
|
|
|
408
|
-
await this.components.
|
|
379
|
+
await this.components.connectionManager.closeConnections(id)
|
|
409
380
|
}
|
|
410
381
|
|
|
411
382
|
/**
|
|
@@ -451,7 +422,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
451
422
|
const { id, multiaddrs } = getPeer(peer)
|
|
452
423
|
|
|
453
424
|
if (multiaddrs != null) {
|
|
454
|
-
await this.components.
|
|
425
|
+
await this.components.peerStore.addressBook.add(id, multiaddrs)
|
|
455
426
|
}
|
|
456
427
|
|
|
457
428
|
return await this.fetchService.fetch(id, key, options)
|
|
@@ -461,7 +432,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
461
432
|
const { id, multiaddrs } = getPeer(peer)
|
|
462
433
|
|
|
463
434
|
if (multiaddrs.length > 0) {
|
|
464
|
-
await this.components.
|
|
435
|
+
await this.components.peerStore.addressBook.add(id, multiaddrs)
|
|
465
436
|
}
|
|
466
437
|
|
|
467
438
|
return await this.pingService.ping(id, options)
|
|
@@ -474,7 +445,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
474
445
|
|
|
475
446
|
await Promise.all(
|
|
476
447
|
protocols.map(async protocol => {
|
|
477
|
-
await this.components.
|
|
448
|
+
await this.components.registrar.handle(protocol, handler, options)
|
|
478
449
|
})
|
|
479
450
|
)
|
|
480
451
|
}
|
|
@@ -486,7 +457,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
486
457
|
|
|
487
458
|
await Promise.all(
|
|
488
459
|
protocols.map(async protocol => {
|
|
489
|
-
await this.components.
|
|
460
|
+
await this.components.registrar.unhandle(protocol)
|
|
490
461
|
})
|
|
491
462
|
)
|
|
492
463
|
}
|
|
@@ -504,11 +475,11 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
504
475
|
}
|
|
505
476
|
|
|
506
477
|
if (peer.multiaddrs.length > 0) {
|
|
507
|
-
void this.components.
|
|
478
|
+
void this.components.peerStore.addressBook.add(peer.id, peer.multiaddrs).catch(err => log.error(err))
|
|
508
479
|
}
|
|
509
480
|
|
|
510
481
|
if (peer.protocols.length > 0) {
|
|
511
|
-
void this.components.
|
|
482
|
+
void this.components.peerStore.protoBook.set(peer.id, peer.protocols).catch(err => log.error(err))
|
|
512
483
|
}
|
|
513
484
|
|
|
514
485
|
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
|