libp2p 0.36.2-cc60cfd → 0.36.2-fab4f13

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.
Files changed (98) hide show
  1. package/dist/src/circuit/circuit/hop.d.ts +3 -3
  2. package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
  3. package/dist/src/circuit/circuit/hop.js +1 -1
  4. package/dist/src/circuit/circuit/hop.js.map +1 -1
  5. package/dist/src/circuit/circuit/stop.d.ts +3 -3
  6. package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
  7. package/dist/src/circuit/circuit/stop.js.map +1 -1
  8. package/dist/src/circuit/circuit/stream-handler.d.ts +4 -4
  9. package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
  10. package/dist/src/circuit/circuit/stream-handler.js +2 -3
  11. package/dist/src/circuit/circuit/stream-handler.js.map +1 -1
  12. package/dist/src/circuit/circuit/utils.d.ts +2 -2
  13. package/dist/src/circuit/circuit/utils.d.ts.map +1 -1
  14. package/dist/src/circuit/circuit/utils.js.map +1 -1
  15. package/dist/src/circuit/pb/index.d.ts +42 -164
  16. package/dist/src/circuit/pb/index.d.ts.map +1 -0
  17. package/dist/src/circuit/pb/index.js +67 -523
  18. package/dist/src/circuit/pb/index.js.map +1 -0
  19. package/dist/src/dht/dummy-dht.d.ts +17 -0
  20. package/dist/src/dht/dummy-dht.d.ts.map +1 -0
  21. package/dist/src/dht/dummy-dht.js +39 -0
  22. package/dist/src/dht/dummy-dht.js.map +1 -0
  23. package/dist/src/dialer/index.d.ts.map +1 -1
  24. package/dist/src/dialer/index.js +0 -1
  25. package/dist/src/dialer/index.js.map +1 -1
  26. package/dist/src/errors.d.ts +2 -0
  27. package/dist/src/errors.d.ts.map +1 -1
  28. package/dist/src/errors.js +2 -0
  29. package/dist/src/errors.js.map +1 -1
  30. package/dist/src/fetch/index.d.ts.map +1 -1
  31. package/dist/src/fetch/index.js +5 -6
  32. package/dist/src/fetch/index.js.map +1 -1
  33. package/dist/src/fetch/pb/proto.d.ts +20 -128
  34. package/dist/src/fetch/pb/proto.d.ts.map +1 -0
  35. package/dist/src/fetch/pb/proto.js +44 -331
  36. package/dist/src/fetch/pb/proto.js.map +1 -0
  37. package/dist/src/identify/index.d.ts +4 -2
  38. package/dist/src/identify/index.d.ts.map +1 -1
  39. package/dist/src/identify/index.js +8 -8
  40. package/dist/src/identify/index.js.map +1 -1
  41. package/dist/src/identify/pb/message.d.ts +13 -108
  42. package/dist/src/identify/pb/message.d.ts.map +1 -0
  43. package/dist/src/identify/pb/message.js +21 -366
  44. package/dist/src/identify/pb/message.js.map +1 -0
  45. package/dist/src/index.d.ts +11 -4
  46. package/dist/src/index.d.ts.map +1 -1
  47. package/dist/src/index.js.map +1 -1
  48. package/dist/src/insecure/index.d.ts.map +1 -1
  49. package/dist/src/insecure/index.js +5 -2
  50. package/dist/src/insecure/index.js.map +1 -1
  51. package/dist/src/insecure/pb/proto.d.ts +24 -130
  52. package/dist/src/insecure/pb/proto.d.ts.map +1 -0
  53. package/dist/src/insecure/pb/proto.js +46 -388
  54. package/dist/src/insecure/pb/proto.js.map +1 -0
  55. package/dist/src/keychain/index.d.ts +3 -3
  56. package/dist/src/keychain/index.js +2 -2
  57. package/dist/src/keychain/util.d.ts +1 -1
  58. package/dist/src/keychain/util.d.ts.map +1 -1
  59. package/dist/src/libp2p.d.ts +7 -4
  60. package/dist/src/libp2p.d.ts.map +1 -1
  61. package/dist/src/libp2p.js +20 -5
  62. package/dist/src/libp2p.js.map +1 -1
  63. package/dist/src/metrics/stats.d.ts +1 -5
  64. package/dist/src/metrics/stats.d.ts.map +1 -1
  65. package/dist/src/metrics/stats.js.map +1 -1
  66. package/dist/src/pubsub/dummy-pubsub.d.ts +17 -0
  67. package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -0
  68. package/dist/src/pubsub/dummy-pubsub.js +37 -0
  69. package/dist/src/pubsub/dummy-pubsub.js.map +1 -0
  70. package/package.json +45 -44
  71. package/src/circuit/circuit/hop.ts +4 -4
  72. package/src/circuit/circuit/stop.ts +3 -3
  73. package/src/circuit/circuit/stream-handler.ts +5 -6
  74. package/src/circuit/circuit/utils.ts +2 -2
  75. package/src/circuit/pb/index.proto +1 -1
  76. package/src/circuit/pb/index.ts +88 -0
  77. package/src/dht/dummy-dht.ts +51 -0
  78. package/src/dialer/index.ts +0 -3
  79. package/src/errors.ts +2 -0
  80. package/src/fetch/index.ts +6 -7
  81. package/src/fetch/pb/proto.ts +58 -0
  82. package/src/identify/index.ts +10 -10
  83. package/src/identify/pb/message.ts +36 -0
  84. package/src/index.ts +12 -5
  85. package/src/insecure/index.ts +8 -4
  86. package/src/insecure/pb/proto.ts +61 -0
  87. package/src/keychain/index.ts +2 -2
  88. package/src/libp2p.ts +27 -9
  89. package/src/metrics/stats.ts +1 -6
  90. package/src/pubsub/dummy-pubsub.ts +51 -0
  91. package/src/circuit/pb/index.d.ts +0 -173
  92. package/src/circuit/pb/index.js +0 -528
  93. package/src/fetch/pb/proto.d.ts +0 -134
  94. package/src/fetch/pb/proto.js +0 -331
  95. package/src/identify/pb/message.d.ts +0 -110
  96. package/src/identify/pb/message.js +0 -369
  97. package/src/insecure/pb/proto.d.ts +0 -134
  98. package/src/insecure/pb/proto.js +0 -388
@@ -75,8 +75,7 @@ export class FetchService implements Startable {
75
75
  const shake = handshake(stream)
76
76
 
77
77
  // send message
78
- const request = new FetchRequest({ identifier: key })
79
- shake.write(lp.encode.single(FetchRequest.encode(request).finish()).slice())
78
+ shake.write(lp.encode.single(FetchRequest.encode({ identifier: key })).slice())
80
79
 
81
80
  // read response
82
81
  // @ts-expect-error fromReader returns a Source which has no .next method
@@ -109,21 +108,21 @@ export class FetchService implements Startable {
109
108
  // @ts-expect-error fromReader returns a Source which has no .next method
110
109
  const request = FetchRequest.decode((await lp.decode.fromReader(shake.reader).next()).value.slice())
111
110
 
112
- let response
111
+ let response: FetchResponse
113
112
  const lookup = this._getLookupFunction(request.identifier)
114
113
  if (lookup != null) {
115
114
  const data = await lookup(request.identifier)
116
115
  if (data != null) {
117
- response = new FetchResponse({ status: FetchResponse.StatusCode.OK, data })
116
+ response = { status: FetchResponse.StatusCode.OK, data }
118
117
  } else {
119
- response = new FetchResponse({ status: FetchResponse.StatusCode.NOT_FOUND })
118
+ response = { status: FetchResponse.StatusCode.NOT_FOUND, data: new Uint8Array(0) }
120
119
  }
121
120
  } else {
122
121
  const errmsg = (new TextEncoder()).encode('No lookup function registered for key: ' + request.identifier)
123
- response = new FetchResponse({ status: FetchResponse.StatusCode.ERROR, data: errmsg })
122
+ response = { status: FetchResponse.StatusCode.ERROR, data: errmsg }
124
123
  }
125
124
 
126
- shake.write(lp.encode.single(FetchResponse.encode(response).finish()).slice())
125
+ shake.write(lp.encode.single(FetchResponse.encode(response)).slice())
127
126
  }
128
127
 
129
128
  /**
@@ -0,0 +1,58 @@
1
+ /* eslint-disable import/export */
2
+ /* eslint-disable @typescript-eslint/no-namespace */
3
+
4
+ import { encodeMessage, decodeMessage, message, string, enumeration, bytes } from 'protons-runtime'
5
+
6
+ export interface FetchRequest {
7
+ identifier: string
8
+ }
9
+
10
+ export namespace FetchRequest {
11
+ export const codec = () => {
12
+ return message<FetchRequest>({
13
+ 1: { name: 'identifier', codec: string }
14
+ })
15
+ }
16
+
17
+ export const encode = (obj: FetchRequest): Uint8Array => {
18
+ return encodeMessage(obj, FetchRequest.codec())
19
+ }
20
+
21
+ export const decode = (buf: Uint8Array): FetchRequest => {
22
+ return decodeMessage(buf, FetchRequest.codec())
23
+ }
24
+ }
25
+
26
+ export interface FetchResponse {
27
+ status: FetchResponse.StatusCode
28
+ data: Uint8Array
29
+ }
30
+
31
+ export namespace FetchResponse {
32
+ export enum StatusCode {
33
+ OK = 'OK',
34
+ NOT_FOUND = 'NOT_FOUND',
35
+ ERROR = 'ERROR'
36
+ }
37
+
38
+ export namespace StatusCode {
39
+ export const codec = () => {
40
+ return enumeration<typeof StatusCode>(StatusCode)
41
+ }
42
+ }
43
+
44
+ export const codec = () => {
45
+ return message<FetchResponse>({
46
+ 1: { name: 'status', codec: FetchResponse.StatusCode.codec() },
47
+ 2: { name: 'data', codec: bytes }
48
+ })
49
+ }
50
+
51
+ export const encode = (obj: FetchResponse): Uint8Array => {
52
+ return encodeMessage(obj, FetchResponse.codec())
53
+ }
54
+
55
+ export const decode = (buf: Uint8Array): FetchResponse => {
56
+ return decodeMessage(buf, FetchResponse.codec())
57
+ }
58
+ }
@@ -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 Message from './pb/message.js'
11
+ import { Identify } from './pb/message.js'
12
12
  import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
13
13
  import {
14
14
  MULTICODEC_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
- [Message.Identify.encode({
135
+ [Identify.encode({
136
136
  listenAddrs,
137
137
  signedPeerRecord,
138
138
  protocols
139
- }).finish()],
139
+ })],
140
140
  lp.encode(),
141
141
  stream,
142
142
  drain
@@ -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 = Message.Identify.decode(data)
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 = Message.Identify.encode({
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
- }).finish()
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 = Message.Identify.decode(data)
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 { Message }
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,8 +16,8 @@ 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 { ConnectionManager, StreamHandler } from '@libp2p/interfaces/registrar'
20
- import type { MetricsInit } from '@libp2p/interfaces/metrics'
19
+ import type { ConnectionManager, Registrar, StreamHandler } from '@libp2p/interfaces/registrar'
20
+ import type { Metrics, MetricsInit } from '@libp2p/interfaces/metrics'
21
21
  import type { PeerInfo } from '@libp2p/interfaces/peer-info'
22
22
  import type { DialerInit } from '@libp2p/interfaces/dialer'
23
23
  import type { KeyChain } from './keychain/index.js'
@@ -152,9 +152,10 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
152
152
  contentRouting: ContentRouting
153
153
  keychain: KeyChain
154
154
  connectionManager: ConnectionManager
155
-
156
- pubsub?: PubSub
157
- dht?: DualDHT
155
+ registrar: Registrar
156
+ metrics?: Metrics
157
+ pubsub: PubSub
158
+ dht: DualDHT
158
159
 
159
160
  /**
160
161
  * Load keychain keys from the datastore.
@@ -221,6 +222,12 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
221
222
  * Sends a request to fetch the value associated with the given key from the given peer.
222
223
  */
223
224
  fetch: (peer: PeerId | Multiaddr | string, key: string) => Promise<Uint8Array | null>
225
+
226
+ /**
227
+ * Returns the public key for the passed PeerId. If the PeerId is of the 'RSA' type
228
+ * this may mean searching the DHT if the key is not present in the KeyStore.
229
+ */
230
+ getPublicKey: (peer: PeerId, options?: AbortOptions) => Promise<Uint8Array>
224
231
  }
225
232
 
226
233
  export type Libp2pOptions = RecursivePartial<Libp2pInit>
@@ -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, IExchange, KeyType } from './pb/proto.js'
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: IExchange) {
15
- const pb = Exchange.encode(exchange).finish()
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
+ }
@@ -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 {Object}
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 {Object}
187
+ * @returns {object}
188
188
  */
189
189
  static get options () {
190
190
  return defaultOptions
package/src/libp2p.ts CHANGED
@@ -32,7 +32,7 @@ import type { Connection } from '@libp2p/interfaces/connection'
32
32
  import type { PeerRouting } from '@libp2p/interfaces/peer-routing'
33
33
  import type { ContentRouting } from '@libp2p/interfaces/content-routing'
34
34
  import type { PubSub } from '@libp2p/interfaces/pubsub'
35
- import type { ConnectionManager, StreamHandler } from '@libp2p/interfaces/registrar'
35
+ import type { ConnectionManager, Registrar, StreamHandler } from '@libp2p/interfaces/registrar'
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'
@@ -43,13 +43,16 @@ import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
43
43
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
44
44
  import errCode from 'err-code'
45
45
  import { unmarshalPublicKey } from '@libp2p/crypto/keys'
46
+ import type { Metrics } from '@libp2p/interfaces/metrics'
47
+ import { DummyDHT } from './dht/dummy-dht.js'
48
+ import { DummyPubSub } from './pubsub/dummy-pubsub.js'
46
49
 
47
50
  const log = logger('libp2p')
48
51
 
49
52
  export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
50
53
  public peerId: PeerId
51
- public dht?: DualDHT
52
- public pubsub?: PubSub
54
+ public dht: DualDHT
55
+ public pubsub: PubSub
53
56
  public identifyService?: IdentifyService
54
57
  public fetchService: FetchService
55
58
  public pingService: PingService
@@ -59,6 +62,8 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
59
62
  public peerRouting: PeerRouting
60
63
  public keychain: KeyChain
61
64
  public connectionManager: ConnectionManager
65
+ public registrar: Registrar
66
+ public metrics?: Metrics
62
67
 
63
68
  private started: boolean
64
69
  private readonly services: Startable[]
@@ -78,7 +83,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
78
83
 
79
84
  // Create Metrics
80
85
  if (init.metrics.enabled) {
81
- this.components.setMetrics(this.configureComponent(new DefaultMetrics(init.metrics)))
86
+ this.metrics = this.components.setMetrics(this.configureComponent(new DefaultMetrics(init.metrics)))
82
87
  }
83
88
 
84
89
  this.components.setConnectionGater(this.configureComponent({
@@ -117,7 +122,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
117
122
  this.connectionManager = this.components.setConnectionManager(this.configureComponent(new DefaultConnectionManager(this.components, init.connectionManager)))
118
123
 
119
124
  // Create the Registrar
120
- this.components.setRegistrar(this.configureComponent(new DefaultRegistrar(this.components)))
125
+ this.registrar = this.components.setRegistrar(this.configureComponent(new DefaultRegistrar(this.components)))
121
126
 
122
127
  // Setup the transport manager
123
128
  this.components.setTransportManager(this.configureComponent(new DefaultTransportManager(this.components, init.transportManager)))
@@ -165,11 +170,15 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
165
170
  // dht provided components (peerRouting, contentRouting, dht)
166
171
  if (init.dht != null) {
167
172
  this.dht = this.components.setDHT(this.configureComponent(init.dht))
173
+ } else {
174
+ this.dht = new DummyDHT()
168
175
  }
169
176
 
170
177
  // Create pubsub if provided
171
178
  if (init.pubsub != null) {
172
179
  this.pubsub = this.components.setPubSub(this.configureComponent(init.pubsub))
180
+ } else {
181
+ this.pubsub = new DummyPubSub()
173
182
  }
174
183
 
175
184
  // Attach remaining APIs
@@ -177,9 +186,14 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
177
186
 
178
187
  const peerRouters: PeerRouting[] = (init.peerRouters ?? []).map(component => this.configureComponent(component))
179
188
 
180
- if (this.dht != null) {
189
+ if (init.dht != null) {
181
190
  // add dht to routers
182
191
  peerRouters.push(this.configureComponent(new DHTPeerRouting(this.dht)))
192
+
193
+ // use dht for peer discovery
194
+ this.dht.addEventListener('peer', (evt) => {
195
+ this.onDiscoveryPeer(evt)
196
+ })
183
197
  }
184
198
 
185
199
  this.peerRouting = this.components.setPeerRouting(this.configureComponent(new DefaultPeerRouting(this.components, {
@@ -189,7 +203,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
189
203
 
190
204
  const contentRouters: ContentRouting[] = (init.contentRouters ?? []).map(component => this.configureComponent(component))
191
205
 
192
- if (this.dht != null) {
206
+ if (init.dht != null) {
193
207
  // add dht to routers
194
208
  contentRouters.push(this.configureComponent(new DHTContentRouting(this.dht)))
195
209
  }
@@ -404,9 +418,13 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
404
418
  /**
405
419
  * Get the public key for the given peer id
406
420
  */
407
- async getPublicKey (peer: PeerId, options: AbortOptions = {}) {
421
+ async getPublicKey (peer: PeerId, options: AbortOptions = {}): Promise<Uint8Array> {
408
422
  log('getPublicKey %p', peer)
409
423
 
424
+ if (peer.publicKey != null) {
425
+ return peer.publicKey
426
+ }
427
+
410
428
  const peerInfo = await this.peerStore.get(peer)
411
429
 
412
430
  if (peerInfo.pubKey != null) {
@@ -429,7 +447,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
429
447
 
430
448
  await this.peerStore.keyBook.set(peer, event.value)
431
449
 
432
- return key
450
+ return key.bytes
433
451
  }
434
452
  }
435
453
 
@@ -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]>
@@ -0,0 +1,51 @@
1
+ import { EventEmitter } from '@libp2p/interfaces'
2
+ import type { PeerId } from '@libp2p/interfaces/peer-id'
3
+ import type { PublishResult, PubSub, PubSubEvents, StrictNoSign, StrictSign } from '@libp2p/interfaces/pubsub'
4
+ import errCode from 'err-code'
5
+ import { messages, codes } from '../errors.js'
6
+
7
+ export class DummyPubSub extends EventEmitter<PubSubEvents> implements PubSub {
8
+ isStarted (): boolean {
9
+ return false
10
+ }
11
+
12
+ start (): void | Promise<void> {
13
+
14
+ }
15
+
16
+ stop (): void | Promise<void> {
17
+
18
+ }
19
+
20
+ get globalSignaturePolicy (): typeof StrictSign | typeof StrictNoSign {
21
+ throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
22
+ }
23
+
24
+ get multicodecs (): string[] {
25
+ throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
26
+ }
27
+
28
+ getPeers (): PeerId[] {
29
+ throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
30
+ }
31
+
32
+ getTopics (): string[] {
33
+ throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
34
+ }
35
+
36
+ subscribe (): void {
37
+ throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
38
+ }
39
+
40
+ unsubscribe (): void {
41
+ throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
42
+ }
43
+
44
+ getSubscribers (): PeerId[] {
45
+ throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
46
+ }
47
+
48
+ async publish (): Promise<PublishResult> {
49
+ throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
50
+ }
51
+ }