libp2p 0.42.2 → 0.43.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 +4 -4
- package/dist/index.min.js +29 -29
- package/dist/src/address-manager/index.d.ts +1 -10
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +46 -29
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/autonat/constants.d.ts +4 -0
- package/dist/src/autonat/constants.d.ts.map +1 -0
- package/dist/src/autonat/constants.js +4 -0
- package/dist/src/autonat/constants.js.map +1 -0
- package/dist/src/autonat/index.d.ts +64 -0
- package/dist/src/autonat/index.d.ts.map +1 -0
- package/dist/src/autonat/index.js +417 -0
- package/dist/src/autonat/index.js.map +1 -0
- package/dist/src/autonat/pb/index.d.ts +57 -0
- package/dist/src/autonat/pb/index.d.ts.map +1 -0
- package/dist/src/autonat/pb/index.js +250 -0
- package/dist/src/autonat/pb/index.js.map +1 -0
- package/dist/src/circuit/constants.d.ts +31 -6
- package/dist/src/circuit/constants.d.ts.map +1 -1
- package/dist/src/circuit/constants.js +37 -7
- package/dist/src/circuit/constants.js.map +1 -1
- package/dist/src/circuit/index.d.ts +16 -61
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/index.js +2 -68
- package/dist/src/circuit/index.js.map +1 -1
- package/dist/src/circuit/pb/index.d.ts +83 -43
- package/dist/src/circuit/pb/index.d.ts.map +1 -1
- package/dist/src/circuit/pb/index.js +363 -127
- package/dist/src/circuit/pb/index.js.map +1 -1
- package/dist/src/circuit/server/advert-service.d.ts +43 -0
- package/dist/src/circuit/server/advert-service.d.ts.map +1 -0
- package/dist/src/circuit/server/advert-service.js +69 -0
- package/dist/src/circuit/server/advert-service.js.map +1 -0
- package/dist/src/circuit/server/index.d.ts +60 -0
- package/dist/src/circuit/server/index.d.ts.map +1 -0
- package/dist/src/circuit/server/index.js +287 -0
- package/dist/src/circuit/server/index.js.map +1 -0
- package/dist/src/circuit/server/reservation-store.d.ts +49 -0
- package/dist/src/circuit/server/reservation-store.d.ts.map +1 -0
- package/dist/src/circuit/server/reservation-store.js +57 -0
- package/dist/src/circuit/server/reservation-store.js.map +1 -0
- package/dist/src/circuit/server/reservation-voucher.d.ts +18 -0
- package/dist/src/circuit/server/reservation-voucher.d.ts.map +1 -0
- package/dist/src/circuit/server/reservation-voucher.js +33 -0
- package/dist/src/circuit/server/reservation-voucher.js.map +1 -0
- package/dist/src/circuit/transport/discovery.d.ts +45 -0
- package/dist/src/circuit/transport/discovery.d.ts.map +1 -0
- package/dist/src/circuit/transport/discovery.js +82 -0
- package/dist/src/circuit/transport/discovery.js.map +1 -0
- package/dist/src/circuit/transport/index.d.ts +33 -0
- package/dist/src/circuit/transport/index.d.ts.map +1 -0
- package/dist/src/circuit/transport/index.js +225 -0
- package/dist/src/circuit/transport/index.js.map +1 -0
- package/dist/src/circuit/transport/listener.d.ts +9 -0
- package/dist/src/circuit/transport/listener.d.ts.map +1 -0
- package/dist/src/circuit/transport/listener.js +76 -0
- package/dist/src/circuit/transport/listener.js.map +1 -0
- package/dist/src/circuit/transport/reservation-store.d.ts +60 -0
- package/dist/src/circuit/transport/reservation-store.d.ts.map +1 -0
- package/dist/src/circuit/transport/reservation-store.js +165 -0
- package/dist/src/circuit/transport/reservation-store.js.map +1 -0
- package/dist/src/circuit/utils.d.ts +7 -0
- package/dist/src/circuit/utils.d.ts.map +1 -1
- package/dist/src/circuit/utils.js +87 -0
- package/dist/src/circuit/utils.js.map +1 -1
- package/dist/src/components.d.ts +2 -1
- package/dist/src/components.d.ts.map +1 -1
- package/dist/src/components.js +13 -13
- package/dist/src/components.js.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +21 -25
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection/index.d.ts +2 -2
- package/dist/src/connection/index.d.ts.map +1 -1
- package/dist/src/connection/index.js +4 -4
- package/dist/src/connection/index.js.map +1 -1
- package/dist/src/connection-manager/auto-dialler.d.ts +0 -4
- package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dialler.js +18 -18
- package/dist/src/connection-manager/auto-dialler.js.map +1 -1
- package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -1
- package/dist/src/connection-manager/dialer/dial-request.js +13 -6
- package/dist/src/connection-manager/dialer/dial-request.js.map +1 -1
- package/dist/src/connection-manager/dialer/index.d.ts +10 -1
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
- package/dist/src/connection-manager/dialer/index.js +24 -9
- package/dist/src/connection-manager/dialer/index.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +1 -4
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +8 -5
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/latency-monitor.d.ts.map +1 -1
- package/dist/src/connection-manager/latency-monitor.js +1 -1
- package/dist/src/connection-manager/latency-monitor.js.map +1 -1
- package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +1 -1
- package/dist/src/content-routing/index.d.ts +6 -4
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +9 -9
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/content-routing/utils.d.ts +3 -3
- package/dist/src/content-routing/utils.d.ts.map +1 -1
- package/dist/src/content-routing/utils.js +2 -2
- package/dist/src/content-routing/utils.js.map +1 -1
- package/dist/src/dht/dht-content-routing.d.ts +2 -1
- package/dist/src/dht/dht-content-routing.d.ts.map +1 -1
- package/dist/src/dht/dht-content-routing.js +2 -2
- package/dist/src/dht/dht-content-routing.js.map +1 -1
- package/dist/src/dht/dht-peer-routing.d.ts +3 -2
- package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
- package/dist/src/dht/dht-peer-routing.js +2 -2
- package/dist/src/dht/dht-peer-routing.js.map +1 -1
- package/dist/src/dht/dummy-dht.d.ts +1 -1
- package/dist/src/dht/dummy-dht.d.ts.map +1 -1
- package/dist/src/dht/dummy-dht.js +12 -12
- package/dist/src/dht/dummy-dht.js.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.map +1 -1
- package/dist/src/fetch/index.js +6 -6
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/fetch/pb/proto.d.ts +3 -3
- package/dist/src/fetch/pb/proto.d.ts.map +1 -1
- package/dist/src/fetch/pb/proto.js +4 -3
- package/dist/src/fetch/pb/proto.js.map +1 -1
- package/dist/src/get-peer.js +2 -2
- package/dist/src/get-peer.js.map +1 -1
- package/dist/src/identify/index.d.ts +3 -1
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +19 -14
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/identify/pb/message.d.ts +2 -2
- package/dist/src/identify/pb/message.d.ts.map +1 -1
- package/dist/src/identify/pb/message.js +1 -0
- package/dist/src/identify/pb/message.js.map +1 -1
- package/dist/src/index.d.ts +14 -8
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/insecure/index.d.ts.map +1 -1
- package/dist/src/insecure/index.js.map +1 -1
- package/dist/src/insecure/pb/proto.d.ts +3 -3
- package/dist/src/insecure/pb/proto.d.ts.map +1 -1
- package/dist/src/insecure/pb/proto.js +4 -5
- package/dist/src/insecure/pb/proto.js.map +1 -1
- package/dist/src/libp2p.d.ts +7 -3
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +35 -35
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/nat-manager.d.ts.map +1 -1
- package/dist/src/nat-manager.js +5 -3
- package/dist/src/nat-manager.js.map +1 -1
- package/dist/src/peer-record-updater.d.ts.map +1 -1
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +5 -5
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +2 -2
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/pnet/crypto.d.ts +1 -1
- package/dist/src/pnet/crypto.d.ts.map +1 -1
- package/dist/src/pnet/crypto.js +1 -1
- package/dist/src/pnet/crypto.js.map +1 -1
- package/dist/src/pnet/index.js +3 -3
- package/dist/src/pnet/index.js.map +1 -1
- package/dist/src/pnet/key-generator.d.ts.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.js +9 -9
- package/dist/src/pubsub/dummy-pubsub.js.map +1 -1
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +4 -4
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +8 -9
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +2 -1
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +38 -33
- package/dist/src/upgrader.js.map +1 -1
- package/dist/typedoc-urls.json +19 -6
- package/package.json +36 -33
- package/src/address-manager/index.ts +62 -35
- package/src/autonat/constants.ts +4 -0
- package/src/autonat/index.ts +567 -0
- package/src/autonat/pb/index.proto +35 -0
- package/src/autonat/pb/index.ts +320 -0
- package/src/circuit/constants.ts +48 -7
- package/src/circuit/index.ts +17 -129
- package/src/circuit/pb/index.proto +57 -32
- package/src/circuit/pb/index.ts +448 -138
- package/src/circuit/server/advert-service.ts +108 -0
- package/src/circuit/server/index.ts +421 -0
- package/src/circuit/server/reservation-store.ts +111 -0
- package/src/circuit/server/reservation-voucher.ts +51 -0
- package/src/circuit/transport/discovery.ts +127 -0
- package/src/circuit/transport/index.ts +325 -0
- package/src/circuit/transport/listener.ts +99 -0
- package/src/circuit/transport/reservation-store.ts +249 -0
- package/src/circuit/utils.ts +108 -0
- package/src/components.ts +22 -21
- package/src/config.ts +21 -25
- package/src/connection/index.ts +10 -10
- package/src/connection-manager/auto-dialler.ts +36 -40
- package/src/connection-manager/dialer/dial-request.ts +13 -6
- package/src/connection-manager/dialer/index.ts +34 -15
- package/src/connection-manager/index.ts +22 -23
- package/src/connection-manager/latency-monitor.ts +9 -9
- package/src/connection-manager/visibility-change-emitter.ts +4 -4
- package/src/content-routing/index.ts +18 -16
- package/src/content-routing/utils.ts +5 -5
- package/src/dht/dht-content-routing.ts +5 -4
- package/src/dht/dht-peer-routing.ts +5 -4
- package/src/dht/dummy-dht.ts +13 -13
- package/src/errors.ts +2 -1
- package/src/fetch/index.ts +13 -13
- package/src/fetch/pb/proto.ts +8 -7
- package/src/get-peer.ts +3 -3
- package/src/identify/index.ts +29 -24
- package/src/identify/pb/message.ts +3 -2
- package/src/index.ts +15 -8
- package/src/insecure/index.ts +2 -1
- package/src/insecure/pb/proto.ts +8 -9
- package/src/libp2p.ts +52 -46
- package/src/nat-manager.ts +11 -9
- package/src/peer-record-updater.ts +4 -4
- package/src/peer-routing.ts +9 -9
- package/src/ping/index.ts +6 -6
- package/src/pnet/crypto.ts +3 -3
- package/src/pnet/index.ts +3 -3
- package/src/pnet/key-generator.ts +1 -1
- package/src/pubsub/dummy-pubsub.ts +9 -9
- package/src/registrar.ts +12 -12
- package/src/transport-manager.ts +17 -18
- package/src/upgrader.ts +43 -42
- package/src/version.ts +1 -1
- package/dist/src/circuit/auto-relay.d.ts +0 -46
- package/dist/src/circuit/auto-relay.d.ts.map +0 -1
- package/dist/src/circuit/auto-relay.js +0 -220
- package/dist/src/circuit/auto-relay.js.map +0 -1
- package/dist/src/circuit/circuit/hop.d.ts +0 -42
- package/dist/src/circuit/circuit/hop.d.ts.map +0 -1
- package/dist/src/circuit/circuit/hop.js +0 -142
- package/dist/src/circuit/circuit/hop.js.map +0 -1
- package/dist/src/circuit/circuit/stop.d.ts +0 -24
- package/dist/src/circuit/circuit/stop.d.ts.map +0 -1
- package/dist/src/circuit/circuit/stop.js +0 -51
- package/dist/src/circuit/circuit/stop.js.map +0 -1
- package/dist/src/circuit/circuit/stream-handler.d.ts +0 -40
- package/dist/src/circuit/circuit/stream-handler.d.ts.map +0 -1
- package/dist/src/circuit/circuit/stream-handler.js +0 -59
- package/dist/src/circuit/circuit/stream-handler.js.map +0 -1
- package/dist/src/circuit/circuit/utils.d.ts +0 -7
- package/dist/src/circuit/circuit/utils.d.ts.map +0 -1
- package/dist/src/circuit/circuit/utils.js +0 -43
- package/dist/src/circuit/circuit/utils.js.map +0 -1
- package/dist/src/circuit/listener.d.ts +0 -9
- package/dist/src/circuit/listener.d.ts.map +0 -1
- package/dist/src/circuit/listener.js +0 -62
- package/dist/src/circuit/listener.js.map +0 -1
- package/dist/src/circuit/multicodec.d.ts +0 -2
- package/dist/src/circuit/multicodec.d.ts.map +0 -1
- package/dist/src/circuit/multicodec.js +0 -2
- package/dist/src/circuit/multicodec.js.map +0 -1
- package/dist/src/circuit/transport.d.ts +0 -51
- package/dist/src/circuit/transport.d.ts.map +0 -1
- package/dist/src/circuit/transport.js +0 -223
- package/dist/src/circuit/transport.js.map +0 -1
- package/dist/src/keychain/cms.d.ts +0 -33
- package/dist/src/keychain/cms.d.ts.map +0 -1
- package/dist/src/keychain/cms.js +0 -129
- package/dist/src/keychain/cms.js.map +0 -1
- package/dist/src/keychain/index.d.ts +0 -151
- package/dist/src/keychain/index.d.ts.map +0 -1
- package/dist/src/keychain/index.js +0 -519
- package/dist/src/keychain/index.js.map +0 -1
- package/dist/src/keychain/util.d.ts +0 -21
- package/dist/src/keychain/util.d.ts.map +0 -1
- package/dist/src/keychain/util.js +0 -79
- package/dist/src/keychain/util.js.map +0 -1
- package/src/circuit/IMPLEMENTATION_NOTES.md +0 -128
- package/src/circuit/README.md +0 -111
- package/src/circuit/auto-relay.ts +0 -292
- package/src/circuit/circuit/hop.ts +0 -220
- package/src/circuit/circuit/stop.ts +0 -83
- package/src/circuit/circuit/stream-handler.ts +0 -87
- package/src/circuit/circuit/utils.ts +0 -44
- package/src/circuit/listener.ts +0 -83
- package/src/circuit/multicodec.ts +0 -2
- package/src/circuit/transport.ts +0 -278
- package/src/keychain/README.md +0 -55
- package/src/keychain/cms.ts +0 -150
- package/src/keychain/doc/private-key.png +0 -0
- package/src/keychain/doc/private-key.xml +0 -1
- package/src/keychain/index.ts +0 -629
- package/src/keychain/util.ts +0 -82
package/src/identify/index.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { logger } from '@libp2p/logger'
|
|
2
|
-
import
|
|
2
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
3
3
|
import * as lp from 'it-length-prefixed'
|
|
4
4
|
import { pipe } from 'it-pipe'
|
|
5
5
|
import first from 'it-first'
|
|
6
6
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
7
|
-
import { multiaddr, protocols } from '@multiformats/multiaddr'
|
|
7
|
+
import { Multiaddr, multiaddr, protocols } from '@multiformats/multiaddr'
|
|
8
8
|
import { Identify } from './pb/message.js'
|
|
9
9
|
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
|
|
10
10
|
import {
|
|
@@ -65,6 +65,7 @@ export interface IdentifyServiceInit {
|
|
|
65
65
|
|
|
66
66
|
maxPushIncomingStreams: number
|
|
67
67
|
maxPushOutgoingStreams: number
|
|
68
|
+
maxObservedAddresses?: number
|
|
68
69
|
}
|
|
69
70
|
|
|
70
71
|
export interface IdentifyServiceComponents {
|
|
@@ -112,7 +113,7 @@ export class IdentifyService implements Startable {
|
|
|
112
113
|
const { peerId } = evt.detail
|
|
113
114
|
|
|
114
115
|
if (this.components.peerId.equals(peerId)) {
|
|
115
|
-
void this.pushToPeerStore().catch(err => log.error(err))
|
|
116
|
+
void this.pushToPeerStore().catch(err => { log.error(err) })
|
|
116
117
|
}
|
|
117
118
|
})
|
|
118
119
|
|
|
@@ -121,16 +122,16 @@ export class IdentifyService implements Startable {
|
|
|
121
122
|
const { peerId } = evt.detail
|
|
122
123
|
|
|
123
124
|
if (this.components.peerId.equals(peerId)) {
|
|
124
|
-
void this.pushToPeerStore().catch(err => log.error(err))
|
|
125
|
+
void this.pushToPeerStore().catch(err => { log.error(err) })
|
|
125
126
|
}
|
|
126
127
|
})
|
|
127
128
|
}
|
|
128
129
|
|
|
129
|
-
isStarted () {
|
|
130
|
+
isStarted (): boolean {
|
|
130
131
|
return this.started
|
|
131
132
|
}
|
|
132
133
|
|
|
133
|
-
async start () {
|
|
134
|
+
async start (): Promise<void> {
|
|
134
135
|
if (this.started) {
|
|
135
136
|
return
|
|
136
137
|
}
|
|
@@ -158,7 +159,7 @@ export class IdentifyService implements Startable {
|
|
|
158
159
|
this.started = true
|
|
159
160
|
}
|
|
160
161
|
|
|
161
|
-
async stop () {
|
|
162
|
+
async stop (): Promise<void> {
|
|
162
163
|
await this.components.registrar.unhandle(this.identifyProtocolStr)
|
|
163
164
|
await this.components.registrar.unhandle(this.identifyPushProtocolStr)
|
|
164
165
|
|
|
@@ -216,7 +217,7 @@ export class IdentifyService implements Startable {
|
|
|
216
217
|
/**
|
|
217
218
|
* Calls `push` on all peer connections
|
|
218
219
|
*/
|
|
219
|
-
async pushToPeerStore () {
|
|
220
|
+
async pushToPeerStore (): Promise<void> {
|
|
220
221
|
// Do not try to push if we are not running
|
|
221
222
|
if (!this.isStarted()) {
|
|
222
223
|
return
|
|
@@ -272,13 +273,13 @@ export class IdentifyService implements Startable {
|
|
|
272
273
|
)
|
|
273
274
|
|
|
274
275
|
if (data == null) {
|
|
275
|
-
throw
|
|
276
|
+
throw new CodeError('No data could be retrieved', codes.ERR_CONNECTION_ENDED)
|
|
276
277
|
}
|
|
277
278
|
|
|
278
279
|
try {
|
|
279
280
|
return Identify.decode(data)
|
|
280
281
|
} catch (err: any) {
|
|
281
|
-
throw
|
|
282
|
+
throw new CodeError(String(err), codes.ERR_INVALID_MESSAGE)
|
|
282
283
|
}
|
|
283
284
|
} finally {
|
|
284
285
|
if (timeoutController != null) {
|
|
@@ -310,17 +311,17 @@ export class IdentifyService implements Startable {
|
|
|
310
311
|
} = message
|
|
311
312
|
|
|
312
313
|
if (publicKey == null) {
|
|
313
|
-
throw
|
|
314
|
+
throw new CodeError('public key was missing from identify message', codes.ERR_MISSING_PUBLIC_KEY)
|
|
314
315
|
}
|
|
315
316
|
|
|
316
317
|
const id = await peerIdFromKeys(publicKey)
|
|
317
318
|
|
|
318
319
|
if (!connection.remotePeer.equals(id)) {
|
|
319
|
-
throw
|
|
320
|
+
throw new CodeError('identified peer does not match the expected peer', codes.ERR_INVALID_PEER)
|
|
320
321
|
}
|
|
321
322
|
|
|
322
323
|
if (this.components.peerId.equals(id)) {
|
|
323
|
-
throw
|
|
324
|
+
throw new CodeError('identified peer is our own peer id?', codes.ERR_INVALID_PEER)
|
|
324
325
|
}
|
|
325
326
|
|
|
326
327
|
// Get the observedAddr if there is one
|
|
@@ -333,7 +334,7 @@ export class IdentifyService implements Startable {
|
|
|
333
334
|
const envelope = await RecordEnvelope.openAndCertify(signedPeerRecord, PeerRecord.DOMAIN)
|
|
334
335
|
|
|
335
336
|
if (!envelope.peerId.equals(id)) {
|
|
336
|
-
throw
|
|
337
|
+
throw new CodeError('identified peer does not match the expected peer', codes.ERR_INVALID_PEER)
|
|
337
338
|
}
|
|
338
339
|
|
|
339
340
|
if (await this.components.peerStore.addressBook.consumePeerRecord(envelope)) {
|
|
@@ -378,17 +379,20 @@ export class IdentifyService implements Startable {
|
|
|
378
379
|
}
|
|
379
380
|
|
|
380
381
|
log('identify completed for peer %p and protocols %o', id, protocols)
|
|
382
|
+
log('our observed address is %s', cleanObservedAddr)
|
|
381
383
|
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
384
|
+
if (cleanObservedAddr != null &&
|
|
385
|
+
this.components.addressManager.getObservedAddrs().length < (this.init.maxObservedAddresses ?? Infinity)) {
|
|
386
|
+
log('storing our observed address %s', cleanObservedAddr?.toString())
|
|
387
|
+
this.components.addressManager.addObservedAddr(cleanObservedAddr)
|
|
388
|
+
}
|
|
385
389
|
}
|
|
386
390
|
|
|
387
391
|
/**
|
|
388
392
|
* Sends the `Identify` response with the Signed Peer Record
|
|
389
393
|
* to the requesting peer over the given `connection`
|
|
390
394
|
*/
|
|
391
|
-
async _handleIdentify (data: IncomingStreamData) {
|
|
395
|
+
async _handleIdentify (data: IncomingStreamData): Promise<void> {
|
|
392
396
|
const { connection, stream } = data
|
|
393
397
|
const timeoutController = new TimeoutController(this.init.timeout)
|
|
394
398
|
|
|
@@ -440,7 +444,7 @@ export class IdentifyService implements Startable {
|
|
|
440
444
|
/**
|
|
441
445
|
* Reads the Identify Push message from the given `connection`
|
|
442
446
|
*/
|
|
443
|
-
async _handlePush (data: IncomingStreamData) {
|
|
447
|
+
async _handlePush (data: IncomingStreamData): Promise<void> {
|
|
444
448
|
const { connection, stream } = data
|
|
445
449
|
const timeoutController = new TimeoutController(this.init.timeout)
|
|
446
450
|
|
|
@@ -467,14 +471,16 @@ export class IdentifyService implements Startable {
|
|
|
467
471
|
message = Identify.decode(data)
|
|
468
472
|
}
|
|
469
473
|
} catch (err: any) {
|
|
470
|
-
|
|
474
|
+
log.error('received invalid message', err)
|
|
475
|
+
return
|
|
471
476
|
} finally {
|
|
472
477
|
stream.close()
|
|
473
478
|
timeoutController.clear()
|
|
474
479
|
}
|
|
475
480
|
|
|
476
481
|
if (message == null) {
|
|
477
|
-
|
|
482
|
+
log.error('received invalid message')
|
|
483
|
+
return
|
|
478
484
|
}
|
|
479
485
|
|
|
480
486
|
const id = connection.remotePeer
|
|
@@ -509,8 +515,7 @@ export class IdentifyService implements Startable {
|
|
|
509
515
|
|
|
510
516
|
// LEGACY: Update peers data in PeerStore
|
|
511
517
|
try {
|
|
512
|
-
await this.components.peerStore.addressBook.set(id,
|
|
513
|
-
message.listenAddrs.map((addr) => multiaddr(addr)))
|
|
518
|
+
await this.components.peerStore.addressBook.set(id, message.listenAddrs.map((addr) => multiaddr(addr)))
|
|
514
519
|
} catch (err: any) {
|
|
515
520
|
log.error('received invalid addrs', err)
|
|
516
521
|
}
|
|
@@ -528,7 +533,7 @@ export class IdentifyService implements Startable {
|
|
|
528
533
|
/**
|
|
529
534
|
* Takes the `addr` and converts it to a Multiaddr if possible
|
|
530
535
|
*/
|
|
531
|
-
static getCleanMultiaddr (addr: Uint8Array | string | null | undefined) {
|
|
536
|
+
static getCleanMultiaddr (addr: Uint8Array | string | null | undefined): Multiaddr | undefined {
|
|
532
537
|
if (addr != null && addr.length > 0) {
|
|
533
538
|
try {
|
|
534
539
|
return multiaddr(addr)
|
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
/* eslint-disable complexity */
|
|
3
3
|
/* eslint-disable @typescript-eslint/no-namespace */
|
|
4
4
|
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-empty-interface */
|
|
5
6
|
|
|
6
7
|
import { encodeMessage, decodeMessage, message } from 'protons-runtime'
|
|
7
|
-
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
8
8
|
import type { Codec } from 'protons-runtime'
|
|
9
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
9
10
|
|
|
10
11
|
export interface Identify {
|
|
11
12
|
protocolVersion?: string
|
|
@@ -115,7 +116,7 @@ export namespace Identify {
|
|
|
115
116
|
return _codec
|
|
116
117
|
}
|
|
117
118
|
|
|
118
|
-
export const encode = (obj: Identify): Uint8Array => {
|
|
119
|
+
export const encode = (obj: Partial<Identify>): Uint8Array => {
|
|
119
120
|
return encodeMessage(obj, Identify.codec())
|
|
120
121
|
}
|
|
121
122
|
|
package/src/index.ts
CHANGED
|
@@ -22,9 +22,9 @@ import type { DualDHT } from '@libp2p/interface-dht'
|
|
|
22
22
|
import type { Datastore } from 'interface-datastore'
|
|
23
23
|
import type { PeerStoreInit } from '@libp2p/interface-peer-store'
|
|
24
24
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
25
|
-
import type { RelayConfig } from './circuit/index.js'
|
|
26
25
|
import type { PeerDiscovery } from '@libp2p/interface-peer-discovery'
|
|
27
|
-
import type {
|
|
26
|
+
import type { ConnectionProtector } from '@libp2p/interface-connection'
|
|
27
|
+
import type { ConnectionGater } from '@libp2p/interface-connection-gater'
|
|
28
28
|
import type { Transport } from '@libp2p/interface-transport'
|
|
29
29
|
import type { StreamMuxerFactory } from '@libp2p/interface-stream-muxer'
|
|
30
30
|
import type { ConnectionEncrypter } from '@libp2p/interface-connection-encrypter'
|
|
@@ -35,13 +35,15 @@ import type { Metrics } from '@libp2p/interface-metrics'
|
|
|
35
35
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
36
36
|
import type { PingServiceInit } from './ping/index.js'
|
|
37
37
|
import type { FetchServiceInit } from './fetch/index.js'
|
|
38
|
+
import type { AutonatServiceInit } from './autonat/index.js'
|
|
38
39
|
import type { Components } from './components.js'
|
|
39
40
|
import type { Libp2p } from '@libp2p/interface-libp2p'
|
|
40
|
-
import type { KeyChainInit } from '
|
|
41
|
+
import type { KeyChainInit } from '@libp2p/keychain'
|
|
41
42
|
import type { NatManagerInit } from './nat-manager.js'
|
|
42
43
|
import type { AddressManagerInit } from './address-manager/index.js'
|
|
43
44
|
import type { PeerRoutingInit } from './peer-routing.js'
|
|
44
45
|
import type { ConnectionManagerInit } from './connection-manager/index.js'
|
|
46
|
+
import type { CircuitRelayService } from './circuit/index.js'
|
|
45
47
|
|
|
46
48
|
/**
|
|
47
49
|
* For Libp2p configurations and modules details read the [Configuration Document](./CONFIGURATION.md).
|
|
@@ -65,7 +67,7 @@ export interface Libp2pInit {
|
|
|
65
67
|
/**
|
|
66
68
|
* A connection gater can deny new connections based on user criteria
|
|
67
69
|
*/
|
|
68
|
-
connectionGater:
|
|
70
|
+
connectionGater: ConnectionGater
|
|
69
71
|
|
|
70
72
|
/**
|
|
71
73
|
* libp2p transport manager configuration
|
|
@@ -103,23 +105,28 @@ export interface Libp2pInit {
|
|
|
103
105
|
* If configured as a relay this node will relay certain
|
|
104
106
|
* types of traffic for other peers
|
|
105
107
|
*/
|
|
106
|
-
relay:
|
|
108
|
+
relay: (components: Components) => CircuitRelayService
|
|
107
109
|
|
|
108
110
|
/**
|
|
109
|
-
*
|
|
111
|
+
* identify protocol options
|
|
110
112
|
*/
|
|
111
113
|
identify: IdentifyServiceInit
|
|
112
114
|
|
|
113
115
|
/**
|
|
114
|
-
*
|
|
116
|
+
* ping protocol options
|
|
115
117
|
*/
|
|
116
118
|
ping: PingServiceInit
|
|
117
119
|
|
|
118
120
|
/**
|
|
119
|
-
*
|
|
121
|
+
* fetch protocol options
|
|
120
122
|
*/
|
|
121
123
|
fetch: FetchServiceInit
|
|
122
124
|
|
|
125
|
+
/**
|
|
126
|
+
* autonat protocol options
|
|
127
|
+
*/
|
|
128
|
+
autonat: AutonatServiceInit
|
|
129
|
+
|
|
123
130
|
/**
|
|
124
131
|
* An array that must include at least 1 compliant transport
|
|
125
132
|
*/
|
package/src/insecure/index.ts
CHANGED
|
@@ -32,11 +32,12 @@ import { peerIdFromBytes, peerIdFromKeys } from '@libp2p/peer-id'
|
|
|
32
32
|
import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface-connection-encrypter'
|
|
33
33
|
import type { Duplex } from 'it-stream-types'
|
|
34
34
|
import map from 'it-map'
|
|
35
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
35
36
|
|
|
36
37
|
const log = logger('libp2p:plaintext')
|
|
37
38
|
const PROTOCOL = '/plaintext/2.0.0'
|
|
38
39
|
|
|
39
|
-
function lpEncodeExchange (exchange: Exchange) {
|
|
40
|
+
function lpEncodeExchange (exchange: Exchange): Uint8ArrayList {
|
|
40
41
|
const pb = Exchange.encode(exchange)
|
|
41
42
|
|
|
42
43
|
return lp.encode.single(pb)
|
package/src/insecure/pb/proto.ts
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
/* eslint-disable complexity */
|
|
3
3
|
/* eslint-disable @typescript-eslint/no-namespace */
|
|
4
4
|
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-empty-interface */
|
|
5
6
|
|
|
6
7
|
import { encodeMessage, decodeMessage, message, enumeration } from 'protons-runtime'
|
|
7
|
-
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
8
8
|
import type { Codec } from 'protons-runtime'
|
|
9
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
9
10
|
|
|
10
11
|
export interface Exchange {
|
|
11
12
|
id?: Uint8Array
|
|
@@ -29,9 +30,7 @@ export namespace Exchange {
|
|
|
29
30
|
|
|
30
31
|
if (obj.pubkey != null) {
|
|
31
32
|
w.uint32(18)
|
|
32
|
-
PublicKey.codec().encode(obj.pubkey, w
|
|
33
|
-
writeDefaults: false
|
|
34
|
-
})
|
|
33
|
+
PublicKey.codec().encode(obj.pubkey, w)
|
|
35
34
|
}
|
|
36
35
|
|
|
37
36
|
if (opts.lengthDelimited !== false) {
|
|
@@ -65,7 +64,7 @@ export namespace Exchange {
|
|
|
65
64
|
return _codec
|
|
66
65
|
}
|
|
67
66
|
|
|
68
|
-
export const encode = (obj: Exchange): Uint8Array => {
|
|
67
|
+
export const encode = (obj: Partial<Exchange>): Uint8Array => {
|
|
69
68
|
return encodeMessage(obj, Exchange.codec())
|
|
70
69
|
}
|
|
71
70
|
|
|
@@ -89,7 +88,7 @@ enum __KeyTypeValues {
|
|
|
89
88
|
}
|
|
90
89
|
|
|
91
90
|
export namespace KeyType {
|
|
92
|
-
export const codec = () => {
|
|
91
|
+
export const codec = (): Codec<KeyType> => {
|
|
93
92
|
return enumeration<KeyType>(__KeyTypeValues)
|
|
94
93
|
}
|
|
95
94
|
}
|
|
@@ -108,12 +107,12 @@ export namespace PublicKey {
|
|
|
108
107
|
w.fork()
|
|
109
108
|
}
|
|
110
109
|
|
|
111
|
-
if (
|
|
110
|
+
if (obj.Type != null && __KeyTypeValues[obj.Type] !== 0) {
|
|
112
111
|
w.uint32(8)
|
|
113
112
|
KeyType.codec().encode(obj.Type, w)
|
|
114
113
|
}
|
|
115
114
|
|
|
116
|
-
if (
|
|
115
|
+
if ((obj.Data != null && obj.Data.byteLength > 0)) {
|
|
117
116
|
w.uint32(18)
|
|
118
117
|
w.bytes(obj.Data)
|
|
119
118
|
}
|
|
@@ -152,7 +151,7 @@ export namespace PublicKey {
|
|
|
152
151
|
return _codec
|
|
153
152
|
}
|
|
154
153
|
|
|
155
|
-
export const encode = (obj: PublicKey): Uint8Array => {
|
|
154
|
+
export const encode = (obj: Partial<PublicKey>): Uint8Array => {
|
|
156
155
|
return encodeMessage(obj, PublicKey.codec())
|
|
157
156
|
}
|
|
158
157
|
|
package/src/libp2p.ts
CHANGED
|
@@ -10,9 +10,7 @@ import { codes } from './errors.js'
|
|
|
10
10
|
import { DefaultAddressManager } from './address-manager/index.js'
|
|
11
11
|
import { DefaultConnectionManager } from './connection-manager/index.js'
|
|
12
12
|
import { AutoDialler } from './connection-manager/auto-dialler.js'
|
|
13
|
-
import {
|
|
14
|
-
import { Relay } from './circuit/index.js'
|
|
15
|
-
import { KeyChain } from './keychain/index.js'
|
|
13
|
+
import { DefaultKeyChain } from '@libp2p/keychain'
|
|
16
14
|
import { DefaultTransportManager } from './transport-manager.js'
|
|
17
15
|
import { DefaultUpgrader } from './upgrader.js'
|
|
18
16
|
import { DefaultRegistrar } from './registrar.js'
|
|
@@ -24,10 +22,11 @@ import { PeerRecordUpdater } from './peer-record-updater.js'
|
|
|
24
22
|
import { DHTPeerRouting } from './dht/dht-peer-routing.js'
|
|
25
23
|
import { PersistentPeerStore } from '@libp2p/peer-store'
|
|
26
24
|
import { DHTContentRouting } from './dht/dht-content-routing.js'
|
|
25
|
+
import { AutonatService } from './autonat/index.js'
|
|
27
26
|
import { DefaultComponents } from './components.js'
|
|
28
27
|
import type { Components } from './components.js'
|
|
29
28
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
30
|
-
import type { Connection } from '@libp2p/interface-connection'
|
|
29
|
+
import type { Connection, Stream } from '@libp2p/interface-connection'
|
|
31
30
|
import type { PeerRouting } from '@libp2p/interface-peer-routing'
|
|
32
31
|
import type { ContentRouting } from '@libp2p/interface-content-routing'
|
|
33
32
|
import type { PubSub } from '@libp2p/interface-pubsub'
|
|
@@ -41,7 +40,7 @@ import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
|
41
40
|
import type { DualDHT } from '@libp2p/interface-dht'
|
|
42
41
|
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
|
|
43
42
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
44
|
-
import
|
|
43
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
45
44
|
import { unmarshalPublicKey } from '@libp2p/crypto/keys'
|
|
46
45
|
import type { Metrics } from '@libp2p/interface-metrics'
|
|
47
46
|
import { DummyDHT } from './dht/dummy-dht.js'
|
|
@@ -50,6 +49,9 @@ import { PeerSet } from '@libp2p/peer-collections'
|
|
|
50
49
|
import { DefaultDialer } from './connection-manager/dialer/index.js'
|
|
51
50
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
52
51
|
import type { Datastore } from 'interface-datastore'
|
|
52
|
+
import type { KeyChain } from '@libp2p/interface-keychain'
|
|
53
|
+
import mergeOptions from 'merge-options'
|
|
54
|
+
import type { CircuitRelayService } from './circuit/index.js'
|
|
53
55
|
|
|
54
56
|
const log = logger('libp2p')
|
|
55
57
|
|
|
@@ -58,8 +60,10 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
58
60
|
public dht: DualDHT
|
|
59
61
|
public pubsub: PubSub
|
|
60
62
|
public identifyService: IdentifyService
|
|
63
|
+
public circuitService?: CircuitRelayService
|
|
61
64
|
public fetchService: FetchService
|
|
62
65
|
public pingService: PingService
|
|
66
|
+
public autonatService: AutonatService
|
|
63
67
|
public components: Components
|
|
64
68
|
public peerStore: PeerStore
|
|
65
69
|
public contentRouting: ContentRouting
|
|
@@ -153,15 +157,9 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
153
157
|
// update our peer record when addresses change
|
|
154
158
|
this.configureComponent(new PeerRecordUpdater(this.components))
|
|
155
159
|
|
|
156
|
-
this.configureComponent(new AutoDialler(this.components, {
|
|
157
|
-
enabled: init.connectionManager.autoDial,
|
|
158
|
-
minConnections: init.connectionManager.minConnections,
|
|
159
|
-
autoDialInterval: init.connectionManager.autoDialInterval
|
|
160
|
-
}))
|
|
161
|
-
|
|
162
160
|
// Create keychain
|
|
163
|
-
const keychainOpts =
|
|
164
|
-
this.keychain = this.configureComponent(new
|
|
161
|
+
const keychainOpts = DefaultKeyChain.generateOptions()
|
|
162
|
+
this.keychain = this.configureComponent(new DefaultKeyChain(this.components, {
|
|
165
163
|
...keychainOpts,
|
|
166
164
|
...init.keychain
|
|
167
165
|
}))
|
|
@@ -169,10 +167,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
169
167
|
// Create the Nat Manager
|
|
170
168
|
this.services.push(new NatManager(this.components, init.nat))
|
|
171
169
|
|
|
172
|
-
init.transports.forEach((fn) => {
|
|
173
|
-
this.components.transportManager.add(this.configureComponent(fn(this.components)))
|
|
174
|
-
})
|
|
175
|
-
|
|
176
170
|
// Add the identify service
|
|
177
171
|
this.identifyService = new IdentifyService(this.components, {
|
|
178
172
|
...init.identify
|
|
@@ -224,15 +218,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
224
218
|
routers: contentRouters
|
|
225
219
|
}))
|
|
226
220
|
|
|
227
|
-
if (init.relay.enabled) {
|
|
228
|
-
this.components.transportManager.add(this.configureComponent(new Circuit(this.components, init.relay)))
|
|
229
|
-
|
|
230
|
-
this.configureComponent(new Relay(this.components, {
|
|
231
|
-
addressSorter: init.connectionManager.addressSorter,
|
|
232
|
-
...init.relay
|
|
233
|
-
}))
|
|
234
|
-
}
|
|
235
|
-
|
|
236
221
|
this.fetchService = this.configureComponent(new FetchService(this.components, {
|
|
237
222
|
...init.fetch
|
|
238
223
|
}))
|
|
@@ -241,6 +226,19 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
241
226
|
...init.ping
|
|
242
227
|
}))
|
|
243
228
|
|
|
229
|
+
this.autonatService = this.configureComponent(new AutonatService(this.components, {
|
|
230
|
+
...init.autonat
|
|
231
|
+
}))
|
|
232
|
+
|
|
233
|
+
this.configureComponent(new AutoDialler(this.components, {
|
|
234
|
+
minConnections: init.connectionManager.minConnections,
|
|
235
|
+
autoDialInterval: init.connectionManager.autoDialInterval
|
|
236
|
+
}))
|
|
237
|
+
|
|
238
|
+
if (init.relay != null) {
|
|
239
|
+
this.circuitService = this.configureComponent(init.relay(this.components))
|
|
240
|
+
}
|
|
241
|
+
|
|
244
242
|
// Discovery modules
|
|
245
243
|
for (const fn of init.peerDiscovery ?? []) {
|
|
246
244
|
const service = this.configureComponent(fn(this.components))
|
|
@@ -249,6 +247,11 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
249
247
|
this.onDiscoveryPeer(evt)
|
|
250
248
|
})
|
|
251
249
|
}
|
|
250
|
+
|
|
251
|
+
// Transport modules
|
|
252
|
+
init.transports.forEach((fn) => {
|
|
253
|
+
this.components.transportManager.add(this.configureComponent(fn(this.components)))
|
|
254
|
+
})
|
|
252
255
|
}
|
|
253
256
|
|
|
254
257
|
private configureComponent <T> (component: T): T {
|
|
@@ -262,7 +265,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
262
265
|
/**
|
|
263
266
|
* Starts the libp2p node and all its subsystems
|
|
264
267
|
*/
|
|
265
|
-
async start () {
|
|
268
|
+
async start (): Promise<void> {
|
|
266
269
|
if (this.started) {
|
|
267
270
|
return
|
|
268
271
|
}
|
|
@@ -271,6 +274,13 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
271
274
|
|
|
272
275
|
log('libp2p is starting')
|
|
273
276
|
|
|
277
|
+
const keys = await this.keychain.listKeys()
|
|
278
|
+
|
|
279
|
+
if (keys.find(key => key.name === 'self') == null) {
|
|
280
|
+
log('importing self key into keychain')
|
|
281
|
+
await this.keychain.importPeer('self', this.components.peerId)
|
|
282
|
+
}
|
|
283
|
+
|
|
274
284
|
try {
|
|
275
285
|
await Promise.all(
|
|
276
286
|
this.services.map(async service => {
|
|
@@ -282,7 +292,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
282
292
|
|
|
283
293
|
// start any startables
|
|
284
294
|
await Promise.all(
|
|
285
|
-
this.services.map(service => service.start())
|
|
295
|
+
this.services.map(async service => { await service.start() })
|
|
286
296
|
)
|
|
287
297
|
|
|
288
298
|
await Promise.all(
|
|
@@ -304,7 +314,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
304
314
|
/**
|
|
305
315
|
* Stop the libp2p node by closing its listeners and open connections
|
|
306
316
|
*/
|
|
307
|
-
async stop () {
|
|
317
|
+
async stop (): Promise<void> {
|
|
308
318
|
if (!this.started) {
|
|
309
319
|
return
|
|
310
320
|
}
|
|
@@ -322,7 +332,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
322
332
|
)
|
|
323
333
|
|
|
324
334
|
await Promise.all(
|
|
325
|
-
this.services.map(service => service.stop())
|
|
335
|
+
this.services.map(async service => { await service.stop() })
|
|
326
336
|
)
|
|
327
337
|
|
|
328
338
|
await Promise.all(
|
|
@@ -336,7 +346,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
336
346
|
log('libp2p has stopped')
|
|
337
347
|
}
|
|
338
348
|
|
|
339
|
-
isStarted () {
|
|
349
|
+
isStarted (): boolean {
|
|
340
350
|
return this.started
|
|
341
351
|
}
|
|
342
352
|
|
|
@@ -358,15 +368,15 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
358
368
|
return await this.components.connectionManager.openConnection(peer, options)
|
|
359
369
|
}
|
|
360
370
|
|
|
361
|
-
async dialProtocol (peer: PeerId | Multiaddr, protocols: string | string[], options: AbortOptions = {}) {
|
|
371
|
+
async dialProtocol (peer: PeerId | Multiaddr, protocols: string | string[], options: AbortOptions = {}): Promise<Stream> {
|
|
362
372
|
if (protocols == null) {
|
|
363
|
-
throw
|
|
373
|
+
throw new CodeError('no protocols were provided to open a stream', codes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
|
|
364
374
|
}
|
|
365
375
|
|
|
366
376
|
protocols = Array.isArray(protocols) ? protocols : [protocols]
|
|
367
377
|
|
|
368
378
|
if (protocols.length === 0) {
|
|
369
|
-
throw
|
|
379
|
+
throw new CodeError('no protocols were provided to open a stream', codes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
|
|
370
380
|
}
|
|
371
381
|
|
|
372
382
|
const connection = await this.dial(peer, options)
|
|
@@ -407,7 +417,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
407
417
|
}
|
|
408
418
|
|
|
409
419
|
if (this.dht == null) {
|
|
410
|
-
throw
|
|
420
|
+
throw new CodeError('Public key was not in the peer store and the DHT is not enabled', codes.ERR_NO_ROUTERS_AVAILABLE)
|
|
411
421
|
}
|
|
412
422
|
|
|
413
423
|
const peerKey = uint8ArrayConcat([
|
|
@@ -426,7 +436,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
426
436
|
}
|
|
427
437
|
}
|
|
428
438
|
|
|
429
|
-
throw
|
|
439
|
+
throw new CodeError(`Node not responding with its public key: ${peer.toString()}`, codes.ERR_INVALID_RECORD)
|
|
430
440
|
}
|
|
431
441
|
|
|
432
442
|
async fetch (peer: PeerId | Multiaddr, key: string, options: AbortOptions = {}): Promise<Uint8Array | null> {
|
|
@@ -477,7 +487,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
477
487
|
return await this.registrar.register(protocol, topology)
|
|
478
488
|
}
|
|
479
489
|
|
|
480
|
-
unregister (id: string) {
|
|
490
|
+
unregister (id: string): void {
|
|
481
491
|
this.registrar.unregister(id)
|
|
482
492
|
}
|
|
483
493
|
|
|
@@ -485,7 +495,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
485
495
|
* Called whenever peer discovery services emit `peer` events.
|
|
486
496
|
* Known peers may be emitted.
|
|
487
497
|
*/
|
|
488
|
-
onDiscoveryPeer (evt: CustomEvent<PeerInfo>) {
|
|
498
|
+
onDiscoveryPeer (evt: CustomEvent<PeerInfo>): void {
|
|
489
499
|
const { detail: peer } = evt
|
|
490
500
|
|
|
491
501
|
if (peer.id.toString() === this.peerId.toString()) {
|
|
@@ -494,11 +504,11 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
494
504
|
}
|
|
495
505
|
|
|
496
506
|
if (peer.multiaddrs.length > 0) {
|
|
497
|
-
void this.components.peerStore.addressBook.add(peer.id, peer.multiaddrs).catch(err => log.error(err))
|
|
507
|
+
void this.components.peerStore.addressBook.add(peer.id, peer.multiaddrs).catch(err => { log.error(err) })
|
|
498
508
|
}
|
|
499
509
|
|
|
500
510
|
if (peer.protocols.length > 0) {
|
|
501
|
-
void this.components.peerStore.protoBook.set(peer.id, peer.protocols).catch(err => log.error(err))
|
|
511
|
+
void this.components.peerStore.protoBook.set(peer.id, peer.protocols).catch(err => { log.error(err) })
|
|
502
512
|
}
|
|
503
513
|
|
|
504
514
|
this.dispatchEvent(new CustomEvent<PeerInfo>('peer:discovery', { detail: peer }))
|
|
@@ -516,13 +526,9 @@ export async function createLibp2pNode (options: Libp2pOptions): Promise<Libp2pN
|
|
|
516
526
|
if (datastore != null) {
|
|
517
527
|
try {
|
|
518
528
|
// try load the peer id from the keychain
|
|
519
|
-
|
|
520
|
-
const keyChain = new KeyChain({
|
|
529
|
+
const keyChain = new DefaultKeyChain({
|
|
521
530
|
datastore
|
|
522
|
-
},
|
|
523
|
-
...KeyChain.generateOptions(),
|
|
524
|
-
...(options.keychain ?? {})
|
|
525
|
-
})
|
|
531
|
+
}, mergeOptions(DefaultKeyChain.generateOptions(), options.keychain))
|
|
526
532
|
|
|
527
533
|
options.peerId = await keyChain.exportPeerId('self')
|
|
528
534
|
} catch (err: any) {
|