libp2p 0.41.0 → 0.42.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.
Files changed (75) hide show
  1. package/README.md +5 -4
  2. package/dist/index.min.js +58 -0
  3. package/dist/src/address-manager/index.d.ts +8 -0
  4. package/dist/src/address-manager/index.d.ts.map +1 -1
  5. package/dist/src/address-manager/index.js.map +1 -1
  6. package/dist/src/circuit/index.d.ts +11 -1
  7. package/dist/src/circuit/index.d.ts.map +1 -1
  8. package/dist/src/circuit/index.js.map +1 -1
  9. package/dist/src/circuit/transport.d.ts +1 -1
  10. package/dist/src/circuit/transport.d.ts.map +1 -1
  11. package/dist/src/config.js +1 -1
  12. package/dist/src/config.js.map +1 -1
  13. package/dist/src/connection-manager/index.d.ts +9 -28
  14. package/dist/src/connection-manager/index.d.ts.map +1 -1
  15. package/dist/src/connection-manager/index.js +23 -12
  16. package/dist/src/connection-manager/index.js.map +1 -1
  17. package/dist/src/fetch/index.d.ts +14 -0
  18. package/dist/src/fetch/index.d.ts.map +1 -1
  19. package/dist/src/fetch/index.js +14 -0
  20. package/dist/src/fetch/index.js.map +1 -1
  21. package/dist/src/identify/index.d.ts +4 -1
  22. package/dist/src/identify/index.d.ts.map +1 -1
  23. package/dist/src/index.d.ts +123 -137
  24. package/dist/src/index.d.ts.map +1 -1
  25. package/dist/src/index.js +34 -0
  26. package/dist/src/index.js.map +1 -1
  27. package/dist/src/insecure/index.d.ts +23 -0
  28. package/dist/src/insecure/index.d.ts.map +1 -1
  29. package/dist/src/insecure/index.js +23 -0
  30. package/dist/src/insecure/index.js.map +1 -1
  31. package/dist/src/keychain/index.d.ts +2 -2
  32. package/dist/src/keychain/index.d.ts.map +1 -1
  33. package/dist/src/libp2p.d.ts +5 -2
  34. package/dist/src/libp2p.d.ts.map +1 -1
  35. package/dist/src/libp2p.js +16 -0
  36. package/dist/src/libp2p.js.map +1 -1
  37. package/dist/src/nat-manager.d.ts.map +1 -1
  38. package/dist/src/nat-manager.js +5 -1
  39. package/dist/src/nat-manager.js.map +1 -1
  40. package/dist/src/peer-routing.d.ts +1 -1
  41. package/dist/src/peer-routing.d.ts.map +1 -1
  42. package/dist/src/peer-routing.js +1 -1
  43. package/dist/src/peer-routing.js.map +1 -1
  44. package/dist/src/pnet/index.d.ts +23 -0
  45. package/dist/src/pnet/index.d.ts.map +1 -1
  46. package/dist/src/pnet/index.js +23 -0
  47. package/dist/src/pnet/index.js.map +1 -1
  48. package/dist/src/pnet/key-generator.d.ts +3 -2
  49. package/dist/src/pnet/key-generator.d.ts.map +1 -1
  50. package/dist/src/pnet/key-generator.js +7 -10
  51. package/dist/src/pnet/key-generator.js.map +1 -1
  52. package/dist/src/transport-manager.d.ts +1 -13
  53. package/dist/src/transport-manager.d.ts.map +1 -1
  54. package/dist/src/transport-manager.js +1 -14
  55. package/dist/src/transport-manager.js.map +1 -1
  56. package/package.json +30 -32
  57. package/src/address-manager/index.ts +8 -0
  58. package/src/circuit/index.ts +13 -1
  59. package/src/circuit/transport.ts +1 -1
  60. package/src/config.ts +1 -1
  61. package/src/connection-manager/index.ts +49 -56
  62. package/src/fetch/README.md +7 -2
  63. package/src/fetch/index.ts +14 -0
  64. package/src/identify/index.ts +1 -1
  65. package/src/index.ts +130 -149
  66. package/src/insecure/index.ts +24 -0
  67. package/src/keychain/index.ts +2 -2
  68. package/src/libp2p.ts +22 -2
  69. package/src/nat-manager.ts +6 -1
  70. package/src/peer-routing.ts +2 -2
  71. package/src/pnet/README.md +31 -29
  72. package/src/pnet/index.ts +24 -0
  73. package/src/pnet/key-generator.ts +7 -12
  74. package/src/transport-manager.ts +1 -15
  75. package/src/version.ts +1 -1
package/src/libp2p.ts CHANGED
@@ -32,7 +32,7 @@ import type { Connection } from '@libp2p/interface-connection'
32
32
  import type { PeerRouting } from '@libp2p/interface-peer-routing'
33
33
  import type { ContentRouting } from '@libp2p/interface-content-routing'
34
34
  import type { PubSub } from '@libp2p/interface-pubsub'
35
- import type { Registrar, StreamHandler, StreamHandlerOptions } from '@libp2p/interface-registrar'
35
+ import type { Registrar, StreamHandler, StreamHandlerOptions, Topology } from '@libp2p/interface-registrar'
36
36
  import type { ConnectionManager } from '@libp2p/interface-connection-manager'
37
37
  import type { PeerInfo } from '@libp2p/interface-peer-info'
38
38
  import type { Libp2p, Libp2pEvents, Libp2pInit, Libp2pOptions } from './index.js'
@@ -56,7 +56,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
56
56
  public peerId: PeerId
57
57
  public dht: DualDHT
58
58
  public pubsub: PubSub
59
- public identifyService?: IdentifyService
59
+ public identifyService: IdentifyService
60
60
  public fetchService: FetchService
61
61
  public pingService: PingService
62
62
  public components: Components
@@ -132,6 +132,14 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
132
132
  // Create the Connection Manager
133
133
  this.connectionManager = this.components.connectionManager = new DefaultConnectionManager(this.components, init.connectionManager)
134
134
 
135
+ // forward connection manager events
136
+ this.components.connectionManager.addEventListener('peer:disconnect', (event) => {
137
+ this.dispatchEvent(new CustomEvent<Connection>('peer:disconnect', { detail: event.detail }))
138
+ })
139
+ this.components.connectionManager.addEventListener('peer:connect', (event) => {
140
+ this.dispatchEvent(new CustomEvent<Connection>('peer:connect', { detail: event.detail }))
141
+ })
142
+
135
143
  // Create the Registrar
136
144
  this.registrar = this.components.registrar = new DefaultRegistrar(this.components)
137
145
 
@@ -373,6 +381,10 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
373
381
  return this.components.addressManager.getAddresses()
374
382
  }
375
383
 
384
+ getProtocols (): string[] {
385
+ return this.components.registrar.getProtocols()
386
+ }
387
+
376
388
  async hangUp (peer: PeerId | Multiaddr): Promise<void> {
377
389
  const { id } = getPeer(peer)
378
390
 
@@ -462,6 +474,14 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
462
474
  )
463
475
  }
464
476
 
477
+ async register (protocol: string, topology: Topology): Promise<string> {
478
+ return await this.registrar.register(protocol, topology)
479
+ }
480
+
481
+ unregister (id: string) {
482
+ this.registrar.unregister(id)
483
+ }
484
+
465
485
  /**
466
486
  * Called whenever peer discovery services emit `peer` events.
467
487
  * Known peers may be emitted.
@@ -149,11 +149,16 @@ export class NatManager implements Startable {
149
149
 
150
150
  const client = await this._getClient()
151
151
  const publicIp = this.externalAddress ?? await client.externalIp()
152
+ const isPrivate = isPrivateIp(publicIp)
152
153
 
153
- if (isPrivateIp(publicIp)) {
154
+ if (isPrivate === true) {
154
155
  throw new Error(`${publicIp} is private - please set config.nat.externalIp to an externally routable IP or ensure you are not behind a double NAT`)
155
156
  }
156
157
 
158
+ if (isPrivate == null) {
159
+ throw new Error(`${publicIp} is not an IP address`)
160
+ }
161
+
157
162
  const publicPort = highPort()
158
163
 
159
164
  log(`opening uPnP connection from ${publicIp}:${publicPort} to ${host}:${port}`)
@@ -50,7 +50,7 @@ export interface RefreshManagerInit {
50
50
  }
51
51
 
52
52
  export interface PeerRoutingInit {
53
- routers: PeerRouting[]
53
+ routers?: PeerRouting[]
54
54
  refreshManager?: RefreshManagerInit
55
55
  }
56
56
 
@@ -69,7 +69,7 @@ export class DefaultPeerRouting implements PeerRouting, Startable {
69
69
 
70
70
  constructor (components: DefaultPeerRoutingComponents, init: PeerRoutingInit) {
71
71
  this.components = components
72
- this.routers = init.routers
72
+ this.routers = init.routers ?? []
73
73
  this.refreshManagerInit = init.refreshManager ?? {}
74
74
  this.started = false
75
75
 
@@ -1,34 +1,41 @@
1
- js-libp2p-pnet
1
+ js-libp2p-pnet <!-- omit in toc -->
2
2
  ==================
3
3
 
4
4
  > Connection protection management for libp2p leveraging PSK encryption via XSalsa20.
5
5
 
6
6
  **Note**: git history prior to merging into js-libp2p can be found in the original repository, https://github.com/libp2p/js-libp2p-pnet.
7
7
 
8
- ## Table of Contents
8
+ ## Table of Contents <!-- omit in toc -->
9
9
 
10
- - [js-libp2p-pnet](#js-libp2p-pnet)
11
- - [Table of Contents](#table-of-contents)
12
- - [Usage](#usage)
13
- - [Examples](#examples)
14
- - [Private Shared Keys](#private-shared-keys)
15
- - [PSK Generation](#psk-generation)
16
- - [From libp2p-pnet](#from-libp2p-pnet)
17
- - [From a module using libp2p](#from-a-module-using-libp2p)
18
- - [Programmatically](#programmatically)
10
+ - [Usage](#usage)
11
+ - [Examples](#examples)
12
+ - [Private Shared Keys](#private-shared-keys)
13
+ - [PSK Generation](#psk-generation)
14
+ - [From a module using libp2p](#from-a-module-using-libp2p)
15
+ - [Programmatically](#programmatically)
19
16
 
20
17
  ## Usage
21
18
 
22
19
  ```js
23
- const Protector from 'libp2p-pnet')
24
- const protector = new Protector(swarmKeyBuffer)
25
- const privateConnection = protector.protect(myPublicConnection, (err) => { })
20
+ import { createLibp2p } from 'libp2p'
21
+ import { preSharedKey, generateKey } from 'libp2p/pnet'
22
+
23
+ // Create a Uint8Array and write the swarm key to it
24
+ const swarmKey = new Uint8Array(95)
25
+ generateKey(swarmKey)
26
+
27
+ const node = await createLibp2p({
28
+ // ...other options
29
+ connectionProtector: preSharedKey({
30
+ psk: swarmKey
31
+ })
32
+ })
26
33
  ```
27
34
 
28
- ### Examples
35
+ ## Examples
29
36
  [Private Networks with IPFS](../../examples/pnet-ipfs)
30
37
 
31
- ### Private Shared Keys
38
+ ## Private Shared Keys
32
39
 
33
40
  Private Shared Keys are expected to be in the following format:
34
41
 
@@ -38,33 +45,28 @@ Private Shared Keys are expected to be in the following format:
38
45
  dffb7e3135399a8b1612b2aaca1c36a3a8ac2cd0cca51ceeb2ced87d308cac6d
39
46
  ```
40
47
 
41
- ### PSK Generation
48
+ ## PSK Generation
42
49
 
43
50
  A utility method has been created to generate a key for your private network. You can
44
51
  use one of the methods below to generate your key.
45
52
 
46
- #### From libp2p-pnet
47
-
48
- If you have libp2p locally, you can run the following from the projects root.
49
-
50
- ```sh
51
- node ./src/pnet/key-generator.js > swarm.key
52
- ```
53
-
54
53
  #### From a module using libp2p
55
54
 
56
55
  If you have a module locally that depends on libp2p, you can run the following from
57
56
  that project, assuming the node_modules are installed.
58
57
 
59
- ```sh
60
- node -e "require('libp2p/src/pnet').generate(process.stdout)" > swarm.key
58
+ ```console
59
+ node -e "import('libp2p/pnet').then(({ generateKey }) => generateKey(process.stdout))" > swarm.key
61
60
  ```
62
61
 
63
62
  #### Programmatically
64
63
 
65
64
  ```js
66
- const writeKey from 'libp2p/src/pnet').generate
65
+ import fs from 'fs'
66
+ import { generateKey } from 'libp2p/pnet'
67
+
67
68
  const swarmKey = new Uint8Array(95)
68
- writeKey(swarmKey)
69
+ generateKey(swarmKey)
70
+
69
71
  fs.writeFileSync('swarm.key', swarmKey)
70
72
  ```
package/src/pnet/index.ts CHANGED
@@ -1,3 +1,27 @@
1
+ /**
2
+ * @packageDocumentation
3
+ *
4
+ * Connection protection management for libp2p leveraging PSK encryption via XSalsa20.
5
+ *
6
+ * @example
7
+ *
8
+ * ```typescript
9
+ * import { createLibp2p } from 'libp2p'
10
+ * import { preSharedKey, generateKey } from 'libp2p/pnet'
11
+ *
12
+ * // Create a Uint8Array and write the swarm key to it
13
+ * const swarmKey = new Uint8Array(95)
14
+ * generateKey(swarmKey)
15
+ *
16
+ * const node = await createLibp2p({
17
+ * // ...other options
18
+ * connectionProtector: preSharedKey({
19
+ * psk: swarmKey
20
+ * })
21
+ * })
22
+ * ```
23
+ */
24
+
1
25
  import { logger } from '@libp2p/logger'
2
26
  import { pipe } from 'it-pipe'
3
27
  import errCode from 'err-code'
@@ -5,24 +5,19 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
5
5
  /**
6
6
  * Generates a PSK that can be used in a libp2p-pnet private network
7
7
  *
8
- * @param {Uint8Array} bytes - An object to write the psk into
8
+ * @param {Uint8Array | NodeJS.WriteStream} bytes - An object to write the psk into
9
9
  * @returns {void}
10
10
  */
11
- export function generateKey (bytes: Uint8Array) {
11
+ export function generateKey (bytes: Uint8Array | NodeJS.WriteStream) {
12
12
  const psk = uint8ArrayToString(randomBytes(KEY_LENGTH), 'base16')
13
13
  const key = uint8ArrayFromString('/key/swarm/psk/1.0.0/\n/base16/\n' + psk)
14
14
 
15
- bytes.set(key)
15
+ if (bytes instanceof Uint8Array) {
16
+ bytes.set(key)
17
+ } else {
18
+ bytes.write(key)
19
+ }
16
20
  }
17
21
 
18
22
  export const NONCE_LENGTH = 24
19
23
  export const KEY_LENGTH = 32
20
-
21
- try {
22
- if (require.main === module) {
23
- // @ts-expect-error
24
- generate(process.stdout)
25
- }
26
- } catch (error: any) {
27
-
28
- }
@@ -2,6 +2,7 @@ import { logger } from '@libp2p/logger'
2
2
  import pSettle from 'p-settle'
3
3
  import { codes } from './errors.js'
4
4
  import errCode from 'err-code'
5
+ import { FaultTolerance } from '@libp2p/interface-transport'
5
6
  import type { Listener, Transport, TransportManager, TransportManagerEvents, Upgrader } from '@libp2p/interface-transport'
6
7
  import type { Multiaddr } from '@multiformats/multiaddr'
7
8
  import type { Connection } from '@libp2p/interface-connection'
@@ -270,18 +271,3 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
270
271
  await Promise.all(tasks)
271
272
  }
272
273
  }
273
-
274
- /**
275
- * Enum Transport Manager Fault Tolerance values
276
- */
277
- export enum FaultTolerance {
278
- /**
279
- * should be used for failing in any listen circumstance
280
- */
281
- FATAL_ALL = 0,
282
-
283
- /**
284
- * should be used for not failing when not listening
285
- */
286
- NO_FATAL
287
- }
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '0.41.0'
1
+ export const version = '0.42.0'
2
2
  export const name = 'libp2p'