libp2p 0.36.2-64bfcee → 0.36.2-a15254f
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/circuit/auto-relay.d.ts.map +1 -1
- package/dist/src/circuit/auto-relay.js +7 -6
- package/dist/src/circuit/auto-relay.js.map +1 -1
- package/dist/src/circuit/circuit/hop.d.ts +4 -4
- package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/hop.js +5 -5
- package/dist/src/circuit/circuit/hop.js.map +1 -1
- package/dist/src/circuit/circuit/stop.d.ts +3 -3
- package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/stop.js.map +1 -1
- package/dist/src/circuit/circuit/stream-handler.d.ts +4 -4
- package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
- package/dist/src/circuit/circuit/stream-handler.js +2 -3
- package/dist/src/circuit/circuit/stream-handler.js.map +1 -1
- package/dist/src/circuit/circuit/utils.d.ts +2 -2
- package/dist/src/circuit/circuit/utils.d.ts.map +1 -1
- package/dist/src/circuit/circuit/utils.js.map +1 -1
- package/dist/src/circuit/listener.d.ts +3 -3
- package/dist/src/circuit/listener.d.ts.map +1 -1
- package/dist/src/circuit/listener.js +9 -1
- package/dist/src/circuit/listener.js.map +1 -1
- package/dist/src/circuit/pb/index.d.ts +42 -164
- package/dist/src/circuit/pb/index.d.ts.map +1 -0
- package/dist/src/circuit/pb/index.js +67 -523
- package/dist/src/circuit/pb/index.js.map +1 -0
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +6 -4
- package/dist/src/circuit/transport.js.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +5 -7
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/auto-dialler.js +4 -4
- package/dist/src/connection-manager/auto-dialler.js.map +1 -1
- package/dist/src/{dialer → connection-manager/dialer}/auto-dialer.d.ts +2 -0
- package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +1 -0
- package/dist/src/connection-manager/dialer/auto-dialer.js +37 -0
- package/dist/src/connection-manager/dialer/auto-dialer.js.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/dial-request.d.ts +2 -2
- package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/dial-request.js +1 -1
- package/dist/src/connection-manager/dialer/dial-request.js.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/index.d.ts +35 -7
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/index.js +10 -24
- package/dist/src/connection-manager/dialer/index.js.map +1 -0
- package/dist/src/connection-manager/index.d.ts +55 -26
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +78 -40
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/dht/dummy-dht.d.ts +17 -0
- package/dist/src/dht/dummy-dht.d.ts.map +1 -0
- package/dist/src/dht/dummy-dht.js +39 -0
- package/dist/src/dht/dummy-dht.js.map +1 -0
- package/dist/src/errors.d.ts +2 -0
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +2 -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 -7
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/fetch/pb/proto.d.ts +20 -128
- package/dist/src/fetch/pb/proto.d.ts.map +1 -0
- package/dist/src/fetch/pb/proto.js +44 -331
- package/dist/src/fetch/pb/proto.js.map +1 -0
- package/dist/src/identify/index.d.ts +4 -2
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +12 -12
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/identify/pb/message.d.ts +13 -108
- package/dist/src/identify/pb/message.d.ts.map +1 -0
- package/dist/src/identify/pb/message.js +21 -366
- package/dist/src/identify/pb/message.js.map +1 -0
- package/dist/src/index.d.ts +11 -25
- 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 +5 -2
- package/dist/src/insecure/index.js.map +1 -1
- package/dist/src/insecure/pb/proto.d.ts +24 -130
- package/dist/src/insecure/pb/proto.d.ts.map +1 -0
- package/dist/src/insecure/pb/proto.js +46 -388
- package/dist/src/insecure/pb/proto.js.map +1 -0
- package/dist/src/keychain/index.d.ts +3 -3
- package/dist/src/keychain/index.js +2 -2
- package/dist/src/keychain/util.d.ts +1 -1
- package/dist/src/keychain/util.d.ts.map +1 -1
- package/dist/src/libp2p.d.ts +5 -4
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +69 -45
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/metrics/stats.d.ts +1 -5
- package/dist/src/metrics/stats.d.ts.map +1 -1
- package/dist/src/metrics/stats.js.map +1 -1
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +2 -1
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.d.ts +17 -0
- package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -0
- package/dist/src/pubsub/dummy-pubsub.js +37 -0
- package/dist/src/pubsub/dummy-pubsub.js.map +1 -0
- package/dist/src/registrar.js +1 -1
- package/dist/src/registrar.js.map +1 -1
- package/package.json +46 -44
- package/src/circuit/auto-relay.ts +8 -6
- package/src/circuit/circuit/hop.ts +9 -9
- package/src/circuit/circuit/stop.ts +3 -3
- package/src/circuit/circuit/stream-handler.ts +5 -6
- package/src/circuit/circuit/utils.ts +2 -2
- package/src/circuit/listener.ts +17 -4
- package/src/circuit/pb/index.proto +1 -1
- package/src/circuit/pb/index.ts +88 -0
- package/src/circuit/transport.ts +7 -4
- package/src/config.ts +5 -7
- package/src/connection-manager/auto-dialler.ts +4 -4
- package/src/{dialer → connection-manager/dialer}/auto-dialer.ts +23 -4
- package/src/{dialer → connection-manager/dialer}/dial-request.ts +4 -4
- package/src/{dialer → connection-manager/dialer}/index.ts +46 -39
- package/src/connection-manager/index.ts +143 -59
- package/src/dht/dummy-dht.ts +51 -0
- package/src/errors.ts +2 -0
- package/src/fetch/index.ts +7 -8
- package/src/fetch/pb/proto.ts +58 -0
- package/src/identify/index.ts +14 -14
- package/src/identify/pb/message.ts +36 -0
- package/src/index.ts +12 -30
- package/src/insecure/index.ts +8 -4
- package/src/insecure/pb/proto.ts +61 -0
- package/src/keychain/index.ts +2 -2
- package/src/libp2p.ts +81 -54
- package/src/metrics/stats.ts +1 -6
- package/src/ping/index.ts +2 -1
- package/src/pubsub/dummy-pubsub.ts +51 -0
- package/src/registrar.ts +1 -1
- package/dist/src/dialer/auto-dialer.d.ts.map +0 -1
- package/dist/src/dialer/auto-dialer.js +0 -24
- package/dist/src/dialer/auto-dialer.js.map +0 -1
- package/dist/src/dialer/dial-request.d.ts.map +0 -1
- package/dist/src/dialer/dial-request.js.map +0 -1
- package/dist/src/dialer/index.d.ts.map +0 -1
- package/dist/src/dialer/index.js.map +0 -1
- package/src/circuit/pb/index.d.ts +0 -173
- package/src/circuit/pb/index.js +0 -528
- package/src/fetch/pb/proto.d.ts +0 -134
- package/src/fetch/pb/proto.js +0 -331
- package/src/identify/pb/message.d.ts +0 -110
- package/src/identify/pb/message.js +0 -369
- package/src/insecure/pb/proto.d.ts +0 -134
- package/src/insecure/pb/proto.js +0 -388
package/src/identify/index.ts
CHANGED
|
@@ -8,7 +8,7 @@ import drain from 'it-drain'
|
|
|
8
8
|
import first from 'it-first'
|
|
9
9
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
10
10
|
import { Multiaddr, protocols } from '@multiformats/multiaddr'
|
|
11
|
-
import
|
|
11
|
+
import { Identify } from './pb/message.js'
|
|
12
12
|
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
|
|
13
13
|
import {
|
|
14
14
|
MULTICODEC_IDENTIFY,
|
|
@@ -23,7 +23,7 @@ import { codes } from '../errors.js'
|
|
|
23
23
|
import type { IncomingStreamData } from '@libp2p/interfaces/registrar'
|
|
24
24
|
import type { Connection } from '@libp2p/interfaces/connection'
|
|
25
25
|
import type { Startable } from '@libp2p/interfaces'
|
|
26
|
-
import { peerIdFromKeys
|
|
26
|
+
import { peerIdFromKeys } from '@libp2p/peer-id'
|
|
27
27
|
import type { Components } from '@libp2p/interfaces/components'
|
|
28
28
|
|
|
29
29
|
const log = logger('libp2p:identify')
|
|
@@ -132,11 +132,11 @@ export class IdentifyService implements Startable {
|
|
|
132
132
|
const { stream } = await connection.newStream([this.identifyPushProtocolStr])
|
|
133
133
|
|
|
134
134
|
await pipe(
|
|
135
|
-
[
|
|
135
|
+
[Identify.encode({
|
|
136
136
|
listenAddrs,
|
|
137
137
|
signedPeerRecord,
|
|
138
138
|
protocols
|
|
139
|
-
})
|
|
139
|
+
})],
|
|
140
140
|
lp.encode(),
|
|
141
141
|
stream,
|
|
142
142
|
drain
|
|
@@ -161,15 +161,15 @@ export class IdentifyService implements Startable {
|
|
|
161
161
|
|
|
162
162
|
const connections: Connection[] = []
|
|
163
163
|
|
|
164
|
-
for (const
|
|
165
|
-
const peerId =
|
|
164
|
+
for (const conn of this.components.getConnectionManager().getConnections()) {
|
|
165
|
+
const peerId = conn.remotePeer
|
|
166
166
|
const peer = await this.components.getPeerStore().get(peerId)
|
|
167
167
|
|
|
168
168
|
if (!peer.protocols.includes(this.identifyPushProtocolStr)) {
|
|
169
169
|
continue
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
-
connections.push(
|
|
172
|
+
connections.push(conn)
|
|
173
173
|
}
|
|
174
174
|
|
|
175
175
|
await this.push(connections)
|
|
@@ -194,9 +194,9 @@ export class IdentifyService implements Startable {
|
|
|
194
194
|
throw errCode(new Error('No data could be retrieved'), codes.ERR_CONNECTION_ENDED)
|
|
195
195
|
}
|
|
196
196
|
|
|
197
|
-
let message
|
|
197
|
+
let message: Identify
|
|
198
198
|
try {
|
|
199
|
-
message =
|
|
199
|
+
message = Identify.decode(data)
|
|
200
200
|
} catch (err: any) {
|
|
201
201
|
throw errCode(err, codes.ERR_INVALID_MESSAGE)
|
|
202
202
|
}
|
|
@@ -325,7 +325,7 @@ export class IdentifyService implements Startable {
|
|
|
325
325
|
signedPeerRecord = envelope.marshal()
|
|
326
326
|
}
|
|
327
327
|
|
|
328
|
-
const message =
|
|
328
|
+
const message = Identify.encode({
|
|
329
329
|
protocolVersion: this.host.protocolVersion,
|
|
330
330
|
agentVersion: this.host.agentVersion,
|
|
331
331
|
publicKey,
|
|
@@ -333,7 +333,7 @@ export class IdentifyService implements Startable {
|
|
|
333
333
|
signedPeerRecord,
|
|
334
334
|
observedAddr: connection.remoteAddr.bytes,
|
|
335
335
|
protocols: peerData.protocols
|
|
336
|
-
})
|
|
336
|
+
})
|
|
337
337
|
|
|
338
338
|
await pipe(
|
|
339
339
|
[message],
|
|
@@ -352,7 +352,7 @@ export class IdentifyService implements Startable {
|
|
|
352
352
|
async _handlePush (data: IncomingStreamData) {
|
|
353
353
|
const { connection, stream } = data
|
|
354
354
|
|
|
355
|
-
let message
|
|
355
|
+
let message: Identify | undefined
|
|
356
356
|
try {
|
|
357
357
|
const data = await pipe(
|
|
358
358
|
[],
|
|
@@ -362,7 +362,7 @@ export class IdentifyService implements Startable {
|
|
|
362
362
|
)
|
|
363
363
|
|
|
364
364
|
if (data != null) {
|
|
365
|
-
message =
|
|
365
|
+
message = Identify.decode(data)
|
|
366
366
|
}
|
|
367
367
|
} catch (err: any) {
|
|
368
368
|
return log.error('received invalid message', err)
|
|
@@ -442,4 +442,4 @@ export const multicodecs = {
|
|
|
442
442
|
IDENTIFY_PUSH: MULTICODEC_IDENTIFY_PUSH
|
|
443
443
|
}
|
|
444
444
|
|
|
445
|
-
export
|
|
445
|
+
export const Message = { Identify }
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/* eslint-disable import/export */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
|
3
|
+
|
|
4
|
+
import { encodeMessage, decodeMessage, message, string, bytes } from 'protons-runtime'
|
|
5
|
+
|
|
6
|
+
export interface Identify {
|
|
7
|
+
protocolVersion?: string
|
|
8
|
+
agentVersion?: string
|
|
9
|
+
publicKey?: Uint8Array
|
|
10
|
+
listenAddrs: Uint8Array[]
|
|
11
|
+
observedAddr?: Uint8Array
|
|
12
|
+
protocols: string[]
|
|
13
|
+
signedPeerRecord?: Uint8Array
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export namespace Identify {
|
|
17
|
+
export const codec = () => {
|
|
18
|
+
return message<Identify>({
|
|
19
|
+
5: { name: 'protocolVersion', codec: string, optional: true },
|
|
20
|
+
6: { name: 'agentVersion', codec: string, optional: true },
|
|
21
|
+
1: { name: 'publicKey', codec: bytes, optional: true },
|
|
22
|
+
2: { name: 'listenAddrs', codec: bytes, repeats: true },
|
|
23
|
+
4: { name: 'observedAddr', codec: bytes, optional: true },
|
|
24
|
+
3: { name: 'protocols', codec: string, repeats: true },
|
|
25
|
+
8: { name: 'signedPeerRecord', codec: bytes, optional: true }
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const encode = (obj: Identify): Uint8Array => {
|
|
30
|
+
return encodeMessage(obj, Identify.codec())
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const decode = (buf: Uint8Array): Identify => {
|
|
34
|
+
return decodeMessage(buf, Identify.codec())
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -16,11 +16,12 @@ import type { ConnectionEncrypter } from '@libp2p/interfaces/connection-encrypte
|
|
|
16
16
|
import type { PeerRouting } from '@libp2p/interfaces/peer-routing'
|
|
17
17
|
import type { ContentRouting } from '@libp2p/interfaces/content-routing'
|
|
18
18
|
import type { PubSub } from '@libp2p/interfaces/pubsub'
|
|
19
|
-
import type {
|
|
19
|
+
import type { Registrar, StreamHandler } from '@libp2p/interfaces/registrar'
|
|
20
|
+
import type { ConnectionManager } from '@libp2p/interfaces/connection-manager'
|
|
20
21
|
import type { Metrics, MetricsInit } from '@libp2p/interfaces/metrics'
|
|
21
22
|
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
|
|
22
|
-
import type { DialerInit } from '@libp2p/interfaces/dialer'
|
|
23
23
|
import type { KeyChain } from './keychain/index.js'
|
|
24
|
+
import type { ConnectionManagerInit } from './connection-manager/index.js'
|
|
24
25
|
|
|
25
26
|
export interface PersistentPeerStoreOptions {
|
|
26
27
|
threshold?: number
|
|
@@ -71,29 +72,6 @@ export interface AddressesConfig {
|
|
|
71
72
|
announceFilter: (multiaddrs: Multiaddr[]) => Multiaddr[]
|
|
72
73
|
}
|
|
73
74
|
|
|
74
|
-
export interface ConnectionManagerConfig {
|
|
75
|
-
/**
|
|
76
|
-
* If true, try to connect to all discovered peers up to the connection manager limit
|
|
77
|
-
*/
|
|
78
|
-
autoDial?: boolean
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* The maximum number of connections to keep open
|
|
82
|
-
*/
|
|
83
|
-
maxConnections: number
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* The minimum number of connections to keep open
|
|
87
|
-
*/
|
|
88
|
-
minConnections: number
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* How long to wait between attempting to keep our number of concurrent connections
|
|
92
|
-
* above minConnections
|
|
93
|
-
*/
|
|
94
|
-
autoDialInterval: number
|
|
95
|
-
}
|
|
96
|
-
|
|
97
75
|
export interface TransportManagerConfig {
|
|
98
76
|
faultTolerance?: FaultTolerance
|
|
99
77
|
}
|
|
@@ -117,11 +95,10 @@ export interface Libp2pInit {
|
|
|
117
95
|
peerId: PeerId
|
|
118
96
|
host: HostProperties
|
|
119
97
|
addresses: AddressesConfig
|
|
120
|
-
connectionManager:
|
|
98
|
+
connectionManager: ConnectionManagerInit
|
|
121
99
|
connectionGater: Partial<ConnectionGater>
|
|
122
100
|
transportManager: TransportManagerConfig
|
|
123
101
|
datastore: Datastore
|
|
124
|
-
dialer: DialerInit
|
|
125
102
|
metrics: MetricsInit
|
|
126
103
|
peerStore: PeerStoreInit
|
|
127
104
|
peerRouting: PeerRoutingConfig
|
|
@@ -154,9 +131,8 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
|
|
|
154
131
|
connectionManager: ConnectionManager
|
|
155
132
|
registrar: Registrar
|
|
156
133
|
metrics?: Metrics
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
dht?: DualDHT
|
|
134
|
+
pubsub: PubSub
|
|
135
|
+
dht: DualDHT
|
|
160
136
|
|
|
161
137
|
/**
|
|
162
138
|
* Load keychain keys from the datastore.
|
|
@@ -223,6 +199,12 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
|
|
|
223
199
|
* Sends a request to fetch the value associated with the given key from the given peer.
|
|
224
200
|
*/
|
|
225
201
|
fetch: (peer: PeerId | Multiaddr | string, key: string) => Promise<Uint8Array | null>
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Returns the public key for the passed PeerId. If the PeerId is of the 'RSA' type
|
|
205
|
+
* this may mean searching the DHT if the key is not present in the KeyStore.
|
|
206
|
+
*/
|
|
207
|
+
getPublicKey: (peer: PeerId, options?: AbortOptions) => Promise<Uint8Array>
|
|
226
208
|
}
|
|
227
209
|
|
|
228
210
|
export type Libp2pOptions = RecursivePartial<Libp2pInit>
|
package/src/insecure/index.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { logger } from '@libp2p/logger'
|
|
|
2
2
|
import { handshake } from 'it-handshake'
|
|
3
3
|
import * as lp from 'it-length-prefixed'
|
|
4
4
|
import { UnexpectedPeerError, InvalidCryptoExchangeError } from '@libp2p/interfaces/connection-encrypter/errors'
|
|
5
|
-
import { Exchange,
|
|
5
|
+
import { Exchange, KeyType } from './pb/proto.js'
|
|
6
6
|
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
7
7
|
import { peerIdFromBytes, peerIdFromKeys } from '@libp2p/peer-id'
|
|
8
8
|
import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interfaces/connection-encrypter'
|
|
@@ -11,8 +11,8 @@ import type { Duplex } from 'it-stream-types'
|
|
|
11
11
|
const log = logger('libp2p:plaintext')
|
|
12
12
|
const PROTOCOL = '/plaintext/2.0.0'
|
|
13
13
|
|
|
14
|
-
function lpEncodeExchange (exchange:
|
|
15
|
-
const pb = Exchange.encode(exchange)
|
|
14
|
+
function lpEncodeExchange (exchange: Exchange) {
|
|
15
|
+
const pb = Exchange.encode(exchange)
|
|
16
16
|
|
|
17
17
|
return lp.encode.single(pb)
|
|
18
18
|
}
|
|
@@ -37,7 +37,7 @@ async function encrypt (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: Pe
|
|
|
37
37
|
id: localId.toBytes(),
|
|
38
38
|
pubkey: {
|
|
39
39
|
Type: type,
|
|
40
|
-
Data: localId.publicKey
|
|
40
|
+
Data: localId.publicKey ?? new Uint8Array(0)
|
|
41
41
|
}
|
|
42
42
|
}).slice()
|
|
43
43
|
)
|
|
@@ -52,6 +52,10 @@ async function encrypt (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: Pe
|
|
|
52
52
|
|
|
53
53
|
let peerId
|
|
54
54
|
try {
|
|
55
|
+
if (id.pubkey == null) {
|
|
56
|
+
throw new Error('Public key missing')
|
|
57
|
+
}
|
|
58
|
+
|
|
55
59
|
if (id.pubkey.Data.length === 0) {
|
|
56
60
|
throw new Error('Public key data too short')
|
|
57
61
|
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/* eslint-disable import/export */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
|
3
|
+
|
|
4
|
+
import { encodeMessage, decodeMessage, message, bytes, enumeration } from 'protons-runtime'
|
|
5
|
+
|
|
6
|
+
export interface Exchange {
|
|
7
|
+
id?: Uint8Array
|
|
8
|
+
pubkey?: PublicKey
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export namespace Exchange {
|
|
12
|
+
export const codec = () => {
|
|
13
|
+
return message<Exchange>({
|
|
14
|
+
1: { name: 'id', codec: bytes, optional: true },
|
|
15
|
+
2: { name: 'pubkey', codec: PublicKey.codec(), optional: true }
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const encode = (obj: Exchange): Uint8Array => {
|
|
20
|
+
return encodeMessage(obj, Exchange.codec())
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const decode = (buf: Uint8Array): Exchange => {
|
|
24
|
+
return decodeMessage(buf, Exchange.codec())
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export enum KeyType {
|
|
29
|
+
RSA = 'RSA',
|
|
30
|
+
Ed25519 = 'Ed25519',
|
|
31
|
+
Secp256k1 = 'Secp256k1',
|
|
32
|
+
ECDSA = 'ECDSA'
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export namespace KeyType {
|
|
36
|
+
export const codec = () => {
|
|
37
|
+
return enumeration<typeof KeyType>(KeyType)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface PublicKey {
|
|
42
|
+
Type: KeyType
|
|
43
|
+
Data: Uint8Array
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export namespace PublicKey {
|
|
47
|
+
export const codec = () => {
|
|
48
|
+
return message<PublicKey>({
|
|
49
|
+
1: { name: 'Type', codec: KeyType.codec() },
|
|
50
|
+
2: { name: 'Data', codec: bytes }
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const encode = (obj: PublicKey): Uint8Array => {
|
|
55
|
+
return encodeMessage(obj, PublicKey.codec())
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const decode = (buf: Uint8Array): PublicKey => {
|
|
59
|
+
return decodeMessage(buf, PublicKey.codec())
|
|
60
|
+
}
|
|
61
|
+
}
|
package/src/keychain/index.ts
CHANGED
|
@@ -171,7 +171,7 @@ export class KeyChain {
|
|
|
171
171
|
/**
|
|
172
172
|
* Generates the options for a keychain. A random salt is produced.
|
|
173
173
|
*
|
|
174
|
-
* @returns {
|
|
174
|
+
* @returns {object}
|
|
175
175
|
*/
|
|
176
176
|
static generateOptions (): KeyChainInit {
|
|
177
177
|
const options = Object.assign({}, defaultOptions)
|
|
@@ -184,7 +184,7 @@ export class KeyChain {
|
|
|
184
184
|
* Gets an object that can encrypt/decrypt protected data.
|
|
185
185
|
* The default options for a keychain.
|
|
186
186
|
*
|
|
187
|
-
* @returns {
|
|
187
|
+
* @returns {object}
|
|
188
188
|
*/
|
|
189
189
|
static get options () {
|
|
190
190
|
return defaultOptions
|
package/src/libp2p.ts
CHANGED
|
@@ -11,7 +11,6 @@ import { DefaultConnectionManager } from './connection-manager/index.js'
|
|
|
11
11
|
import { AutoDialler } from './connection-manager/auto-dialler.js'
|
|
12
12
|
import { Circuit } from './circuit/transport.js'
|
|
13
13
|
import { Relay } from './circuit/index.js'
|
|
14
|
-
import { DefaultDialer } from './dialer/index.js'
|
|
15
14
|
import { KeyChain } from './keychain/index.js'
|
|
16
15
|
import { DefaultMetrics } from './metrics/index.js'
|
|
17
16
|
import { DefaultTransportManager } from './transport-manager.js'
|
|
@@ -25,14 +24,15 @@ import { PeerRecordUpdater } from './peer-record-updater.js'
|
|
|
25
24
|
import { DHTPeerRouting } from './dht/dht-peer-routing.js'
|
|
26
25
|
import { PersistentPeerStore } from '@libp2p/peer-store'
|
|
27
26
|
import { DHTContentRouting } from './dht/dht-content-routing.js'
|
|
28
|
-
import { AutoDialer } from './dialer/auto-dialer.js'
|
|
27
|
+
import { AutoDialer } from './connection-manager/dialer/auto-dialer.js'
|
|
29
28
|
import { Initializable, Components, isInitializable } from '@libp2p/interfaces/components'
|
|
30
29
|
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
31
30
|
import type { Connection } from '@libp2p/interfaces/connection'
|
|
32
31
|
import type { PeerRouting } from '@libp2p/interfaces/peer-routing'
|
|
33
32
|
import type { ContentRouting } from '@libp2p/interfaces/content-routing'
|
|
34
33
|
import type { PubSub } from '@libp2p/interfaces/pubsub'
|
|
35
|
-
import type {
|
|
34
|
+
import type { Registrar, StreamHandler } from '@libp2p/interfaces/registrar'
|
|
35
|
+
import type { ConnectionManager } from '@libp2p/interfaces/connection-manager'
|
|
36
36
|
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
|
|
37
37
|
import type { Libp2p, Libp2pEvents, Libp2pInit, Libp2pOptions } from './index.js'
|
|
38
38
|
import { validateConfig } from './config.js'
|
|
@@ -44,13 +44,16 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
|
44
44
|
import errCode from 'err-code'
|
|
45
45
|
import { unmarshalPublicKey } from '@libp2p/crypto/keys'
|
|
46
46
|
import type { Metrics } from '@libp2p/interfaces/metrics'
|
|
47
|
+
import { DummyDHT } from './dht/dummy-dht.js'
|
|
48
|
+
import { DummyPubSub } from './pubsub/dummy-pubsub.js'
|
|
49
|
+
import { PeerSet } from '@libp2p/peer-collections'
|
|
47
50
|
|
|
48
51
|
const log = logger('libp2p')
|
|
49
52
|
|
|
50
53
|
export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
51
54
|
public peerId: PeerId
|
|
52
|
-
public dht
|
|
53
|
-
public pubsub
|
|
55
|
+
public dht: DualDHT
|
|
56
|
+
public pubsub: PubSub
|
|
54
57
|
public identifyService?: IdentifyService
|
|
55
58
|
public fetchService: FetchService
|
|
56
59
|
public pingService: PingService
|
|
@@ -70,34 +73,40 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
70
73
|
constructor (init: Libp2pInit) {
|
|
71
74
|
super()
|
|
72
75
|
|
|
73
|
-
this.services = []
|
|
74
76
|
this.initializables = []
|
|
75
77
|
this.started = false
|
|
76
78
|
this.peerId = init.peerId
|
|
77
79
|
this.components = new Components({
|
|
78
80
|
peerId: init.peerId,
|
|
79
|
-
datastore: init.datastore ?? new MemoryDatastore()
|
|
81
|
+
datastore: init.datastore ?? new MemoryDatastore(),
|
|
82
|
+
connectionGater: {
|
|
83
|
+
denyDialPeer: async () => await Promise.resolve(false),
|
|
84
|
+
denyDialMultiaddr: async () => await Promise.resolve(false),
|
|
85
|
+
denyInboundConnection: async () => await Promise.resolve(false),
|
|
86
|
+
denyOutboundConnection: async () => await Promise.resolve(false),
|
|
87
|
+
denyInboundEncryptedConnection: async () => await Promise.resolve(false),
|
|
88
|
+
denyOutboundEncryptedConnection: async () => await Promise.resolve(false),
|
|
89
|
+
denyInboundUpgradedConnection: async () => await Promise.resolve(false),
|
|
90
|
+
denyOutboundUpgradedConnection: async () => await Promise.resolve(false),
|
|
91
|
+
filterMultiaddrForPeer: async () => await Promise.resolve(true),
|
|
92
|
+
...init.connectionGater
|
|
93
|
+
}
|
|
80
94
|
})
|
|
95
|
+
this.components.setPeerStore(new PersistentPeerStore({
|
|
96
|
+
addressFilter: this.components.getConnectionGater().filterMultiaddrForPeer,
|
|
97
|
+
...init.peerStore
|
|
98
|
+
}))
|
|
99
|
+
|
|
100
|
+
this.services = [
|
|
101
|
+
this.components
|
|
102
|
+
]
|
|
81
103
|
|
|
82
104
|
// Create Metrics
|
|
83
105
|
if (init.metrics.enabled) {
|
|
84
|
-
this.metrics = this.components.setMetrics(
|
|
106
|
+
this.metrics = this.components.setMetrics(new DefaultMetrics(init.metrics))
|
|
85
107
|
}
|
|
86
108
|
|
|
87
|
-
this.components.
|
|
88
|
-
denyDialPeer: async () => await Promise.resolve(false),
|
|
89
|
-
denyDialMultiaddr: async () => await Promise.resolve(false),
|
|
90
|
-
denyInboundConnection: async () => await Promise.resolve(false),
|
|
91
|
-
denyOutboundConnection: async () => await Promise.resolve(false),
|
|
92
|
-
denyInboundEncryptedConnection: async () => await Promise.resolve(false),
|
|
93
|
-
denyOutboundEncryptedConnection: async () => await Promise.resolve(false),
|
|
94
|
-
denyInboundUpgradedConnection: async () => await Promise.resolve(false),
|
|
95
|
-
denyOutboundUpgradedConnection: async () => await Promise.resolve(false),
|
|
96
|
-
filterMultiaddrForPeer: async () => await Promise.resolve(true),
|
|
97
|
-
...init.connectionGater
|
|
98
|
-
}))
|
|
99
|
-
|
|
100
|
-
this.peerStore = this.components.setPeerStore(this.configureComponent(new PersistentPeerStore(this.components, init.peerStore)))
|
|
109
|
+
this.peerStore = this.components.getPeerStore()
|
|
101
110
|
|
|
102
111
|
this.peerStore.addEventListener('peer', evt => {
|
|
103
112
|
const { detail: peerData } = evt
|
|
@@ -107,32 +116,30 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
107
116
|
|
|
108
117
|
// Set up connection protector if configured
|
|
109
118
|
if (init.connectionProtector != null) {
|
|
110
|
-
this.components.setConnectionProtector(
|
|
119
|
+
this.components.setConnectionProtector(init.connectionProtector)
|
|
111
120
|
}
|
|
112
121
|
|
|
113
122
|
// Set up the Upgrader
|
|
114
|
-
this.components.setUpgrader(
|
|
123
|
+
this.components.setUpgrader(new DefaultUpgrader(this.components, {
|
|
115
124
|
connectionEncryption: (init.connectionEncryption ?? []).map(component => this.configureComponent(component)),
|
|
116
125
|
muxers: (init.streamMuxers ?? []).map(component => this.configureComponent(component))
|
|
117
|
-
}))
|
|
126
|
+
}))
|
|
118
127
|
|
|
119
128
|
// Create the Connection Manager
|
|
120
|
-
this.connectionManager = this.components.setConnectionManager(
|
|
129
|
+
this.connectionManager = this.components.setConnectionManager(new DefaultConnectionManager(init.connectionManager))
|
|
121
130
|
|
|
122
131
|
// Create the Registrar
|
|
123
|
-
this.registrar = this.components.setRegistrar(
|
|
132
|
+
this.registrar = this.components.setRegistrar(new DefaultRegistrar(this.components))
|
|
124
133
|
|
|
125
134
|
// Setup the transport manager
|
|
126
|
-
this.components.setTransportManager(
|
|
135
|
+
this.components.setTransportManager(new DefaultTransportManager(this.components, init.transportManager))
|
|
127
136
|
|
|
128
137
|
// Addresses {listen, announce, noAnnounce}
|
|
129
|
-
this.components.setAddressManager(
|
|
138
|
+
this.components.setAddressManager(new DefaultAddressManager(this.components, init.addresses))
|
|
130
139
|
|
|
131
140
|
// update our peer record when addresses change
|
|
132
141
|
this.configureComponent(new PeerRecordUpdater(this.components))
|
|
133
142
|
|
|
134
|
-
this.components.setDialer(this.configureComponent(new DefaultDialer(this.components, init.dialer)))
|
|
135
|
-
|
|
136
143
|
this.configureComponent(new AutoDialler(this.components, {
|
|
137
144
|
enabled: init.connectionManager.autoDial,
|
|
138
145
|
minConnections: init.connectionManager.minConnections,
|
|
@@ -167,12 +174,16 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
167
174
|
|
|
168
175
|
// dht provided components (peerRouting, contentRouting, dht)
|
|
169
176
|
if (init.dht != null) {
|
|
170
|
-
this.dht = this.components.setDHT(
|
|
177
|
+
this.dht = this.components.setDHT(init.dht)
|
|
178
|
+
} else {
|
|
179
|
+
this.dht = new DummyDHT()
|
|
171
180
|
}
|
|
172
181
|
|
|
173
182
|
// Create pubsub if provided
|
|
174
183
|
if (init.pubsub != null) {
|
|
175
|
-
this.pubsub = this.components.setPubSub(
|
|
184
|
+
this.pubsub = this.components.setPubSub(init.pubsub)
|
|
185
|
+
} else {
|
|
186
|
+
this.pubsub = new DummyPubSub()
|
|
176
187
|
}
|
|
177
188
|
|
|
178
189
|
// Attach remaining APIs
|
|
@@ -180,7 +191,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
180
191
|
|
|
181
192
|
const peerRouters: PeerRouting[] = (init.peerRouters ?? []).map(component => this.configureComponent(component))
|
|
182
193
|
|
|
183
|
-
if (
|
|
194
|
+
if (init.dht != null) {
|
|
184
195
|
// add dht to routers
|
|
185
196
|
peerRouters.push(this.configureComponent(new DHTPeerRouting(this.dht)))
|
|
186
197
|
|
|
@@ -197,7 +208,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
197
208
|
|
|
198
209
|
const contentRouters: ContentRouting[] = (init.contentRouters ?? []).map(component => this.configureComponent(component))
|
|
199
210
|
|
|
200
|
-
if (
|
|
211
|
+
if (init.dht != null) {
|
|
201
212
|
// add dht to routers
|
|
202
213
|
contentRouters.push(this.configureComponent(new DHTContentRouting(this.dht)))
|
|
203
214
|
}
|
|
@@ -210,7 +221,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
210
221
|
this.components.getTransportManager().add(this.configureComponent(new Circuit()))
|
|
211
222
|
|
|
212
223
|
this.configureComponent(new Relay(this.components, {
|
|
213
|
-
addressSorter: init.
|
|
224
|
+
addressSorter: init.connectionManager.addressSorter,
|
|
214
225
|
...init.relay
|
|
215
226
|
}))
|
|
216
227
|
}
|
|
@@ -225,7 +236,8 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
225
236
|
|
|
226
237
|
const autoDialer = this.configureComponent(new AutoDialer(this.components, {
|
|
227
238
|
enabled: init.connectionManager.autoDial !== false,
|
|
228
|
-
minConnections: init.connectionManager.minConnections
|
|
239
|
+
minConnections: init.connectionManager.minConnections,
|
|
240
|
+
dialTimeout: init.connectionManager.dialTimeout ?? 30000
|
|
229
241
|
}))
|
|
230
242
|
|
|
231
243
|
this.addEventListener('peer:discovery', evt => {
|
|
@@ -373,24 +385,41 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
373
385
|
}
|
|
374
386
|
|
|
375
387
|
getConnections (peerId?: PeerId): Connection[] {
|
|
376
|
-
if (peerId == null) {
|
|
377
|
-
return this.components.getConnectionManager().getConnectionList()
|
|
378
|
-
}
|
|
379
|
-
|
|
380
388
|
return this.components.getConnectionManager().getConnections(peerId)
|
|
381
389
|
}
|
|
382
390
|
|
|
383
391
|
getPeers (): PeerId[] {
|
|
384
|
-
|
|
385
|
-
|
|
392
|
+
const peerSet = new PeerSet()
|
|
393
|
+
|
|
394
|
+
for (const conn of this.components.getConnectionManager().getConnections()) {
|
|
395
|
+
peerSet.add(conn.remotePeer)
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return Array.from(peerSet)
|
|
386
399
|
}
|
|
387
400
|
|
|
388
401
|
async dial (peer: PeerId | Multiaddr, options: AbortOptions = {}): Promise<Connection> {
|
|
389
|
-
|
|
402
|
+
const { id, multiaddrs } = getPeer(peer)
|
|
403
|
+
|
|
404
|
+
await this.components.getPeerStore().addressBook.add(id, multiaddrs)
|
|
405
|
+
|
|
406
|
+
return await this.components.getConnectionManager().openConnection(id, options)
|
|
390
407
|
}
|
|
391
408
|
|
|
392
409
|
async dialProtocol (peer: PeerId | Multiaddr, protocols: string | string[], options: AbortOptions = {}) {
|
|
393
|
-
|
|
410
|
+
if (protocols == null) {
|
|
411
|
+
throw errCode(new Error('no protocols were provided to open a stream'), codes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
protocols = Array.isArray(protocols) ? protocols : [protocols]
|
|
415
|
+
|
|
416
|
+
if (protocols.length === 0) {
|
|
417
|
+
throw errCode(new Error('no protocols were provided to open a stream'), codes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const connection = await this.dial(peer)
|
|
421
|
+
|
|
422
|
+
return await connection.newStream(protocols)
|
|
394
423
|
}
|
|
395
424
|
|
|
396
425
|
getMultiaddrs (): Multiaddr[] {
|
|
@@ -400,21 +429,19 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
400
429
|
async hangUp (peer: PeerId | Multiaddr | string): Promise<void> {
|
|
401
430
|
const { id } = getPeer(peer)
|
|
402
431
|
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
await Promise.all(
|
|
406
|
-
connections.map(async connection => {
|
|
407
|
-
return await connection.close()
|
|
408
|
-
})
|
|
409
|
-
)
|
|
432
|
+
await this.components.getConnectionManager().closeConnections(id)
|
|
410
433
|
}
|
|
411
434
|
|
|
412
435
|
/**
|
|
413
436
|
* Get the public key for the given peer id
|
|
414
437
|
*/
|
|
415
|
-
async getPublicKey (peer: PeerId, options: AbortOptions = {}) {
|
|
438
|
+
async getPublicKey (peer: PeerId, options: AbortOptions = {}): Promise<Uint8Array> {
|
|
416
439
|
log('getPublicKey %p', peer)
|
|
417
440
|
|
|
441
|
+
if (peer.publicKey != null) {
|
|
442
|
+
return peer.publicKey
|
|
443
|
+
}
|
|
444
|
+
|
|
418
445
|
const peerInfo = await this.peerStore.get(peer)
|
|
419
446
|
|
|
420
447
|
if (peerInfo.pubKey != null) {
|
|
@@ -437,7 +464,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
437
464
|
|
|
438
465
|
await this.peerStore.keyBook.set(peer, event.value)
|
|
439
466
|
|
|
440
|
-
return key
|
|
467
|
+
return key.bytes
|
|
441
468
|
}
|
|
442
469
|
}
|
|
443
470
|
|
package/src/metrics/stats.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { CustomEvent, EventEmitter } from '@libp2p/interfaces'
|
|
|
2
2
|
import { createMovingAverage } from './moving-average.js'
|
|
3
3
|
// @ts-expect-error no types
|
|
4
4
|
import retimer from 'retimer'
|
|
5
|
-
import type { MovingAverages, Stats } from '@libp2p/interfaces/metrics'
|
|
5
|
+
import type { MovingAverages, Stats, TransferStats } from '@libp2p/interfaces/metrics'
|
|
6
6
|
|
|
7
7
|
export interface StatsEvents {
|
|
8
8
|
'update': CustomEvent<TransferStats>
|
|
@@ -16,11 +16,6 @@ export interface StatsInit {
|
|
|
16
16
|
computeThrottleTimeout: number
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export interface TransferStats {
|
|
20
|
-
dataReceived: BigInt
|
|
21
|
-
dataSent: BigInt
|
|
22
|
-
}
|
|
23
|
-
|
|
24
19
|
export class DefaultStats extends EventEmitter<StatsEvents> implements Stats {
|
|
25
20
|
private readonly enabled: boolean
|
|
26
21
|
public queue: Array<[string, number, number]>
|
package/src/ping/index.ts
CHANGED
|
@@ -63,7 +63,8 @@ export class PingService implements Startable {
|
|
|
63
63
|
async ping (peer: PeerId): Promise<number> {
|
|
64
64
|
log('dialing %s to %p', this.protocol, peer)
|
|
65
65
|
|
|
66
|
-
const
|
|
66
|
+
const connection = await this.components.getConnectionManager().openConnection(peer)
|
|
67
|
+
const { stream } = await connection.newStream([this.protocol])
|
|
67
68
|
const start = Date.now()
|
|
68
69
|
const data = randomBytes(PING_LENGTH)
|
|
69
70
|
|