libp2p 0.42.1-c6718b2e → 0.42.2
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/index.min.js +22 -22
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +4 -0
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/circuit/index.d.ts +2 -1
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/index.js.map +1 -1
- package/dist/src/circuit/transport.js +1 -1
- package/dist/src/circuit/transport.js.map +1 -1
- package/dist/src/connection-manager/dialer/index.d.ts +4 -1
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
- package/dist/src/connection-manager/dialer/index.js +27 -20
- package/dist/src/connection-manager/dialer/index.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +13 -18
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/get-peer.d.ts +5 -3
- package/dist/src/get-peer.d.ts.map +1 -1
- package/dist/src/get-peer.js +10 -25
- package/dist/src/get-peer.js.map +1 -1
- package/dist/src/keychain/index.d.ts +6 -1
- package/dist/src/keychain/index.d.ts.map +1 -1
- package/dist/src/keychain/index.js +10 -0
- package/dist/src/keychain/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +1 -1
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +39 -14
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/upgrader.d.ts +1 -1
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +9 -6
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +1 -2
- package/src/address-manager/index.ts +5 -0
- package/src/circuit/index.ts +2 -1
- package/src/circuit/transport.ts +1 -1
- package/src/connection-manager/dialer/index.ts +29 -22
- package/src/connection-manager/index.ts +14 -16
- package/src/get-peer.ts +13 -35
- package/src/keychain/index.ts +14 -1
- package/src/libp2p.ts +42 -19
- package/src/upgrader.ts +12 -8
- package/src/version.ts +1 -1
|
@@ -12,14 +12,14 @@ import type { Connection, MultiaddrConnection } from '@libp2p/interface-connecti
|
|
|
12
12
|
import type { ConnectionManager, ConnectionManagerEvents, Dialer } from '@libp2p/interface-connection-manager'
|
|
13
13
|
import * as STATUS from '@libp2p/interface-connection/status'
|
|
14
14
|
import type { AddressSorter, PeerStore } from '@libp2p/interface-peer-store'
|
|
15
|
-
import {
|
|
15
|
+
import { multiaddr, Multiaddr, Resolver } from '@multiformats/multiaddr'
|
|
16
16
|
import { PeerMap } from '@libp2p/peer-collections'
|
|
17
17
|
import { TimeoutController } from 'timeout-abort-controller'
|
|
18
18
|
import { KEEP_ALIVE } from '@libp2p/interface-peer-store/tags'
|
|
19
19
|
import { RateLimiterMemory } from 'rate-limiter-flexible'
|
|
20
20
|
import type { Metrics } from '@libp2p/interface-metrics'
|
|
21
21
|
import type { Upgrader } from '@libp2p/interface-transport'
|
|
22
|
-
import {
|
|
22
|
+
import { getPeerAddress } from '../get-peer.js'
|
|
23
23
|
|
|
24
24
|
const log = logger('libp2p:connection-manager')
|
|
25
25
|
|
|
@@ -471,24 +471,22 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
471
471
|
}
|
|
472
472
|
|
|
473
473
|
async openConnection (peerIdOrMultiaddr: PeerId | Multiaddr, options: AbortOptions = {}): Promise<Connection> {
|
|
474
|
-
|
|
474
|
+
const { peerId, multiaddr } = getPeerAddress(peerIdOrMultiaddr)
|
|
475
475
|
|
|
476
|
-
if (
|
|
477
|
-
peerId = peerIdOrMultiaddr
|
|
478
|
-
} else if (isMultiaddr(peerIdOrMultiaddr)) {
|
|
479
|
-
const info = getPeer(peerIdOrMultiaddr)
|
|
480
|
-
peerId = info.id
|
|
481
|
-
} else {
|
|
476
|
+
if (peerId == null && multiaddr == null) {
|
|
482
477
|
throw errCode(new TypeError('Can only open connections to PeerIds or Multiaddrs'), codes.ERR_INVALID_PARAMETERS)
|
|
483
478
|
}
|
|
484
479
|
|
|
485
|
-
|
|
486
|
-
|
|
480
|
+
if (peerId != null) {
|
|
481
|
+
log('dial to', peerId)
|
|
482
|
+
|
|
483
|
+
const existingConnections = this.getConnections(peerId)
|
|
487
484
|
|
|
488
|
-
|
|
489
|
-
|
|
485
|
+
if (existingConnections.length > 0) {
|
|
486
|
+
log('had an existing connection to %p', peerId)
|
|
490
487
|
|
|
491
|
-
|
|
488
|
+
return existingConnections[0]
|
|
489
|
+
}
|
|
492
490
|
}
|
|
493
491
|
|
|
494
492
|
let timeoutController: TimeoutController | undefined
|
|
@@ -505,11 +503,11 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
505
503
|
|
|
506
504
|
try {
|
|
507
505
|
const connection = await this.components.dialer.dial(peerIdOrMultiaddr, options)
|
|
508
|
-
let peerConnections = this.connections.get(
|
|
506
|
+
let peerConnections = this.connections.get(connection.remotePeer.toString())
|
|
509
507
|
|
|
510
508
|
if (peerConnections == null) {
|
|
511
509
|
peerConnections = []
|
|
512
|
-
this.connections.set(
|
|
510
|
+
this.connections.set(connection.remotePeer.toString(), peerConnections)
|
|
513
511
|
}
|
|
514
512
|
|
|
515
513
|
// we get notified of connections via the Upgrader emitting "connection"
|
package/src/get-peer.ts
CHANGED
|
@@ -5,50 +5,28 @@ import errCode from 'err-code'
|
|
|
5
5
|
import { codes } from './errors.js'
|
|
6
6
|
import { isPeerId } from '@libp2p/interface-peer-id'
|
|
7
7
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
8
|
-
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
9
|
-
|
|
10
|
-
function peerIdFromMultiaddr (ma: Multiaddr) {
|
|
11
|
-
const idStr = ma.getPeerId()
|
|
12
|
-
|
|
13
|
-
if (idStr == null) {
|
|
14
|
-
throw errCode(
|
|
15
|
-
new Error(`${ma.toString()} does not have a valid peer type`),
|
|
16
|
-
codes.ERR_INVALID_MULTIADDR
|
|
17
|
-
)
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
try {
|
|
21
|
-
return peerIdFromString(idStr)
|
|
22
|
-
} catch (err: any) {
|
|
23
|
-
throw errCode(
|
|
24
|
-
new Error(`${ma.toString()} is not a valid peer type`),
|
|
25
|
-
codes.ERR_INVALID_MULTIADDR
|
|
26
|
-
)
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
8
|
|
|
30
9
|
/**
|
|
31
|
-
*
|
|
10
|
+
* Extracts a PeerId and/or multiaddr from the passed PeerId or Multiaddr
|
|
32
11
|
*/
|
|
33
|
-
export function
|
|
12
|
+
export function getPeerAddress (peer: PeerId | Multiaddr): { peerId?: PeerId, multiaddr?: Multiaddr } {
|
|
34
13
|
if (isPeerId(peer)) {
|
|
35
14
|
return {
|
|
36
|
-
|
|
37
|
-
multiaddrs: [],
|
|
38
|
-
protocols: []
|
|
15
|
+
peerId: peer
|
|
39
16
|
}
|
|
40
17
|
}
|
|
41
18
|
|
|
42
|
-
let addr
|
|
43
|
-
|
|
44
19
|
if (isMultiaddr(peer)) {
|
|
45
|
-
|
|
46
|
-
peer = peerIdFromMultiaddr(peer)
|
|
47
|
-
}
|
|
20
|
+
const peerId = peer.getPeerId()
|
|
48
21
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
22
|
+
return {
|
|
23
|
+
multiaddr: peer,
|
|
24
|
+
peerId: peerId == null ? undefined : peerIdFromString(peerId)
|
|
25
|
+
}
|
|
53
26
|
}
|
|
27
|
+
|
|
28
|
+
throw errCode(
|
|
29
|
+
new Error(`${peer} is not a PeerId or a Multiaddr`), // eslint-disable-line @typescript-eslint/restrict-template-expressions
|
|
30
|
+
codes.ERR_INVALID_MULTIADDR
|
|
31
|
+
)
|
|
54
32
|
}
|
package/src/keychain/index.ts
CHANGED
|
@@ -14,6 +14,8 @@ import type { PeerId } from '@libp2p/interface-peer-id'
|
|
|
14
14
|
import { pbkdf2, randomBytes } from '@libp2p/crypto'
|
|
15
15
|
import type { Startable } from '@libp2p/interfaces/dist/src/startable'
|
|
16
16
|
import type { Datastore } from 'interface-datastore'
|
|
17
|
+
import { peerIdFromKeys } from '@libp2p/peer-id'
|
|
18
|
+
import type { KeyTypes } from '@libp2p/crypto/keys'
|
|
17
19
|
|
|
18
20
|
const log = logger('libp2p:keychain')
|
|
19
21
|
|
|
@@ -222,7 +224,7 @@ export class KeyChain implements Startable {
|
|
|
222
224
|
* @param {string} type - One of the key types; 'rsa'.
|
|
223
225
|
* @param {number} [size = 2048] - The key size in bits. Used for rsa keys only
|
|
224
226
|
*/
|
|
225
|
-
async createKey (name: string, type:
|
|
227
|
+
async createKey (name: string, type: KeyTypes, size = 2048): Promise<KeyInfo> {
|
|
226
228
|
if (!validateKeyName(name) || name === 'self') {
|
|
227
229
|
await randomDelay()
|
|
228
230
|
throw errCode(new Error('Invalid key name'), codes.ERR_INVALID_KEY_NAME)
|
|
@@ -432,6 +434,17 @@ export class KeyChain implements Startable {
|
|
|
432
434
|
}
|
|
433
435
|
}
|
|
434
436
|
|
|
437
|
+
/**
|
|
438
|
+
* Export an existing key as a PeerId
|
|
439
|
+
*/
|
|
440
|
+
async exportPeerId (name: string) {
|
|
441
|
+
const password = 'temporary-password'
|
|
442
|
+
const pem = await this.exportKey(name, password)
|
|
443
|
+
const privateKey = await importKey(pem, password)
|
|
444
|
+
|
|
445
|
+
return await peerIdFromKeys(privateKey.public.bytes, privateKey.bytes)
|
|
446
|
+
}
|
|
447
|
+
|
|
435
448
|
/**
|
|
436
449
|
* Import a new key from a PEM encoded PKCS #8 string
|
|
437
450
|
*
|
package/src/libp2p.ts
CHANGED
|
@@ -2,11 +2,10 @@ import { logger } from '@libp2p/logger'
|
|
|
2
2
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
3
3
|
import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events'
|
|
4
4
|
import { Startable, isStartable } from '@libp2p/interfaces/startable'
|
|
5
|
-
import
|
|
5
|
+
import { isMultiaddr, Multiaddr } from '@multiformats/multiaddr'
|
|
6
6
|
import { MemoryDatastore } from 'datastore-core/memory'
|
|
7
7
|
import { DefaultPeerRouting } from './peer-routing.js'
|
|
8
8
|
import { CompoundContentRouting } from './content-routing/index.js'
|
|
9
|
-
import { getPeer } from './get-peer.js'
|
|
10
9
|
import { codes } from './errors.js'
|
|
11
10
|
import { DefaultAddressManager } from './address-manager/index.js'
|
|
12
11
|
import { DefaultConnectionManager } from './connection-manager/index.js'
|
|
@@ -49,6 +48,8 @@ import { DummyDHT } from './dht/dummy-dht.js'
|
|
|
49
48
|
import { DummyPubSub } from './pubsub/dummy-pubsub.js'
|
|
50
49
|
import { PeerSet } from '@libp2p/peer-collections'
|
|
51
50
|
import { DefaultDialer } from './connection-manager/dialer/index.js'
|
|
51
|
+
import { peerIdFromString } from '@libp2p/peer-id'
|
|
52
|
+
import type { Datastore } from 'interface-datastore'
|
|
52
53
|
|
|
53
54
|
const log = logger('libp2p')
|
|
54
55
|
|
|
@@ -354,11 +355,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
354
355
|
}
|
|
355
356
|
|
|
356
357
|
async dial (peer: PeerId | Multiaddr, options: AbortOptions = {}): Promise<Connection> {
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
await this.components.peerStore.addressBook.add(id, multiaddrs)
|
|
360
|
-
|
|
361
|
-
return await this.components.connectionManager.openConnection(id, options)
|
|
358
|
+
return await this.components.connectionManager.openConnection(peer, options)
|
|
362
359
|
}
|
|
363
360
|
|
|
364
361
|
async dialProtocol (peer: PeerId | Multiaddr, protocols: string | string[], options: AbortOptions = {}) {
|
|
@@ -386,9 +383,11 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
386
383
|
}
|
|
387
384
|
|
|
388
385
|
async hangUp (peer: PeerId | Multiaddr): Promise<void> {
|
|
389
|
-
|
|
386
|
+
if (isMultiaddr(peer)) {
|
|
387
|
+
peer = peerIdFromString(peer.getPeerId() ?? '')
|
|
388
|
+
}
|
|
390
389
|
|
|
391
|
-
await this.components.connectionManager.closeConnections(
|
|
390
|
+
await this.components.connectionManager.closeConnections(peer)
|
|
392
391
|
}
|
|
393
392
|
|
|
394
393
|
/**
|
|
@@ -431,23 +430,23 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
431
430
|
}
|
|
432
431
|
|
|
433
432
|
async fetch (peer: PeerId | Multiaddr, key: string, options: AbortOptions = {}): Promise<Uint8Array | null> {
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
433
|
+
if (isMultiaddr(peer)) {
|
|
434
|
+
const peerId = peerIdFromString(peer.getPeerId() ?? '')
|
|
435
|
+
await this.components.peerStore.addressBook.add(peerId, [peer])
|
|
436
|
+
peer = peerId
|
|
438
437
|
}
|
|
439
438
|
|
|
440
|
-
return await this.fetchService.fetch(
|
|
439
|
+
return await this.fetchService.fetch(peer, key, options)
|
|
441
440
|
}
|
|
442
441
|
|
|
443
442
|
async ping (peer: PeerId | Multiaddr, options: AbortOptions = {}): Promise<number> {
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
443
|
+
if (isMultiaddr(peer)) {
|
|
444
|
+
const peerId = peerIdFromString(peer.getPeerId() ?? '')
|
|
445
|
+
await this.components.peerStore.addressBook.add(peerId, [peer])
|
|
446
|
+
peer = peerId
|
|
448
447
|
}
|
|
449
448
|
|
|
450
|
-
return await this.pingService.ping(
|
|
449
|
+
return await this.pingService.ping(peer, options)
|
|
451
450
|
}
|
|
452
451
|
|
|
453
452
|
async handle (protocols: string | string[], handler: StreamHandler, options?: StreamHandlerOptions): Promise<void> {
|
|
@@ -512,6 +511,30 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
512
511
|
*/
|
|
513
512
|
export async function createLibp2pNode (options: Libp2pOptions): Promise<Libp2pNode> {
|
|
514
513
|
if (options.peerId == null) {
|
|
514
|
+
const datastore = options.datastore as Datastore | undefined
|
|
515
|
+
|
|
516
|
+
if (datastore != null) {
|
|
517
|
+
try {
|
|
518
|
+
// try load the peer id from the keychain
|
|
519
|
+
// @ts-expect-error missing the peer id property
|
|
520
|
+
const keyChain = new KeyChain({
|
|
521
|
+
datastore
|
|
522
|
+
}, {
|
|
523
|
+
...KeyChain.generateOptions(),
|
|
524
|
+
...(options.keychain ?? {})
|
|
525
|
+
})
|
|
526
|
+
|
|
527
|
+
options.peerId = await keyChain.exportPeerId('self')
|
|
528
|
+
} catch (err: any) {
|
|
529
|
+
if (err.code !== 'ERR_NOT_FOUND') {
|
|
530
|
+
throw err
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
if (options.peerId == null) {
|
|
537
|
+
// no peer id in the keychain, create a new peer id
|
|
515
538
|
options.peerId = await createEd25519PeerId()
|
|
516
539
|
}
|
|
517
540
|
|
package/src/upgrader.ts
CHANGED
|
@@ -250,18 +250,18 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
250
250
|
*/
|
|
251
251
|
async upgradeOutbound (maConn: MultiaddrConnection, opts?: UpgraderOptions): Promise<Connection> {
|
|
252
252
|
const idStr = maConn.remoteAddr.getPeerId()
|
|
253
|
-
|
|
254
|
-
throw errCode(new Error('outbound connection must have a peer id'), codes.ERR_INVALID_MULTIADDR)
|
|
255
|
-
}
|
|
253
|
+
let remotePeerId: PeerId | undefined
|
|
256
254
|
|
|
257
|
-
|
|
255
|
+
if (idStr != null) {
|
|
256
|
+
remotePeerId = peerIdFromString(idStr)
|
|
258
257
|
|
|
259
|
-
|
|
260
|
-
|
|
258
|
+
if (await this.components.connectionGater.denyOutboundConnection(remotePeerId, maConn)) {
|
|
259
|
+
throw errCode(new Error('The multiaddr connection is blocked by connectionGater.denyOutboundConnection'), codes.ERR_CONNECTION_INTERCEPTED)
|
|
260
|
+
}
|
|
261
261
|
}
|
|
262
262
|
|
|
263
263
|
let encryptedConn
|
|
264
|
-
let remotePeer
|
|
264
|
+
let remotePeer: PeerId
|
|
265
265
|
let upgradedConn
|
|
266
266
|
let cryptoProtocol
|
|
267
267
|
let muxerFactory
|
|
@@ -300,6 +300,10 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
300
300
|
throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
|
|
301
301
|
}
|
|
302
302
|
} else {
|
|
303
|
+
if (remotePeerId == null) {
|
|
304
|
+
throw errCode(new Error('Encryption was skipped but no peer id was passed'), codes.ERR_INVALID_PEER)
|
|
305
|
+
}
|
|
306
|
+
|
|
303
307
|
cryptoProtocol = 'native'
|
|
304
308
|
remotePeer = remotePeerId
|
|
305
309
|
}
|
|
@@ -593,7 +597,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
593
597
|
* Attempts to encrypt the given `connection` with the provided connection encrypters.
|
|
594
598
|
* The first `ConnectionEncrypter` module to succeed will be used
|
|
595
599
|
*/
|
|
596
|
-
async _encryptOutbound (connection: MultiaddrConnection, remotePeerId
|
|
600
|
+
async _encryptOutbound (connection: MultiaddrConnection, remotePeerId?: PeerId): Promise<CryptoResult> {
|
|
597
601
|
const protocols = Array.from(this.connectionEncryption.keys())
|
|
598
602
|
log('selecting outbound crypto protocol', protocols)
|
|
599
603
|
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const version = '0.42.
|
|
1
|
+
export const version = '0.42.2'
|
|
2
2
|
export const name = 'libp2p'
|