Skip to content

Commit 54dfca9

Browse files
committed
Merge branch 'master' into docs/update-peer-discovery
2 parents 995d61b + 4084163 commit 54dfca9

File tree

8 files changed

+190
-5
lines changed

8 files changed

+190
-5
lines changed

.github/dependabot.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ updates:
66
interval: daily
77
time: "10:00"
88
open-pull-requests-limit: 10
9+
commit-message:
10+
prefix: "deps"
11+
prefix-development: "deps(dev)"

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@
1010

1111

1212

13+
### [0.42.2](https://www.github.com/libp2p/js-libp2p/compare/v0.42.1...v0.42.2) (2023-01-17)
14+
15+
16+
### Bug Fixes
17+
18+
* allow configuring circuit stream limits ([#1542](https://www.github.com/libp2p/js-libp2p/issues/1542)) ([f82e6b8](https://www.github.com/libp2p/js-libp2p/commit/f82e6b86e375b86e71cd339660a348ecba4bf68d))
19+
* allow dialing multiaddrs without peer ids ([#1548](https://www.github.com/libp2p/js-libp2p/issues/1548)) ([398e231](https://www.github.com/libp2p/js-libp2p/commit/398e231337c3db1ccd5b4254fb18ab1903aa68b2))
20+
* allow exporting PeerIds from the keychain ([#1546](https://www.github.com/libp2p/js-libp2p/issues/1546)) ([141e072](https://www.github.com/libp2p/js-libp2p/commit/141e0722ee2cd92b2b928767710de7443b5a4c56))
21+
* allow reading PeerId from keychain ([#1552](https://www.github.com/libp2p/js-libp2p/issues/1552)) ([0831cd9](https://www.github.com/libp2p/js-libp2p/commit/0831cd960d423545ee60b457d66a6a996888804b))
22+
* do not append peer id to path addresses ([#1547](https://www.github.com/libp2p/js-libp2p/issues/1547)) ([bd2bdf7](https://www.github.com/libp2p/js-libp2p/commit/bd2bdf7ca0d87ab63b2e9acf7edf7a5752e0559c))
23+
* improve pubsub example ([#1549](https://www.github.com/libp2p/js-libp2p/issues/1549)) ([ba8527c](https://www.github.com/libp2p/js-libp2p/commit/ba8527c317b9f1f31f5066b6204fda35d393058f))
24+
1325
### [0.42.1](https://www.github.com/libp2p/js-libp2p/compare/v0.42.0...v0.42.1) (2023-01-11)
1426

1527

doc/CONFIGURATION.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ const node = await createLibp2p({
543543
544544
#### Configuring Connection Manager
545545
546-
The Connection Manager prunes Connections in libp2p whenever certain limits are exceeded. If Metrics are enabled, you can also configure the Connection Manager to monitor the bandwidth of libp2p and prune connections as needed. You can read more about what Connection Manager does at [./CONNECTION_MANAGER.md](https://libp2p.github.io/js-libp2p-interfaces/modules/_libp2p_interface_connection_manager.html). The configuration values below show the defaults for Connection Manager.
546+
The Connection Manager prunes Connections in libp2p whenever certain limits are exceeded. If Metrics are enabled, you can also configure the Connection Manager to monitor the bandwidth of libp2p and prune connections as needed. You can read more about what Connection Manager does at [./CONNECTION_MANAGER.md](./CONNECTION_MANAGER.md). The configuration values below show the defaults for Connection Manager. See [./CONNECTION_MANAGER.md](./CONNECTION_MANAGER.md#options) for a full description of the parameters.
547547
548548
```js
549549
import { createLibp2p } from 'libp2p'

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "libp2p",
3-
"version": "0.42.1",
3+
"version": "0.42.2",
44
"description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
55
"license": "Apache-2.0 OR MIT",
66
"homepage": "https://github.com/libp2p/js-libp2p#readme",
@@ -200,7 +200,6 @@
200200
"p-times": "^4.0.0",
201201
"p-wait-for": "^5.0.0",
202202
"protons": "^6.0.0",
203-
"rimraf": "^3.0.2",
204203
"sinon": "^15.0.1",
205204
"sinon-ts": "^1.0.0"
206205
},

src/circuit/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ import type { ContentRouting } from '@libp2p/interface-content-routing'
1616
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
1717
import type { TransportManager } from '@libp2p/interface-transport'
1818
import type { PeerId } from '@libp2p/interface-peer-id'
19+
import type { StreamHandlerOptions } from '@libp2p/interface-registrar'
1920

20-
export interface RelayConfig {
21+
export interface RelayConfig extends StreamHandlerOptions {
2122
enabled: boolean
2223
advertise: RelayAdvertiseConfig
2324
hop: HopConfig

src/circuit/transport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class Circuit implements Transport, Startable {
6767
void this._onProtocol(data).catch(err => {
6868
log.error(err)
6969
})
70-
})
70+
}, { ...this._init })
7171
.catch(err => {
7272
log.error(err)
7373
})

src/libp2p.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import { DummyPubSub } from './pubsub/dummy-pubsub.js'
4949
import { PeerSet } from '@libp2p/peer-collections'
5050
import { DefaultDialer } from './connection-manager/dialer/index.js'
5151
import { peerIdFromString } from '@libp2p/peer-id'
52+
import type { Datastore } from 'interface-datastore'
5253

5354
const log = logger('libp2p')
5455

@@ -510,6 +511,30 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
510511
*/
511512
export async function createLibp2pNode (options: Libp2pOptions): Promise<Libp2pNode> {
512513
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
513538
options.peerId = await createEd25519PeerId()
514539
}
515540

test/core/peer-id.spec.ts

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/* eslint-env mocha */
2+
3+
import { expect } from 'aegir/chai'
4+
import { webSockets } from '@libp2p/websockets'
5+
import { plaintext } from '../../src/insecure/index.js'
6+
import { createLibp2p, Libp2p } from '../../src/index.js'
7+
import { MemoryDatastore } from 'datastore-core'
8+
9+
describe('peer-id', () => {
10+
let libp2p: Libp2p
11+
12+
afterEach(async () => {
13+
if (libp2p != null) {
14+
await libp2p.stop()
15+
}
16+
})
17+
18+
it('should create a PeerId if none is passed', async () => {
19+
libp2p = await createLibp2p({
20+
transports: [
21+
webSockets()
22+
],
23+
connectionEncryption: [
24+
plaintext()
25+
]
26+
})
27+
28+
expect(libp2p.peerId).to.be.ok()
29+
})
30+
31+
it('should retrieve the PeerId from the datastore', async () => {
32+
const datastore = new MemoryDatastore()
33+
34+
libp2p = await createLibp2p({
35+
datastore,
36+
transports: [
37+
webSockets()
38+
],
39+
connectionEncryption: [
40+
plaintext()
41+
]
42+
})
43+
44+
// this PeerId was created by default
45+
const peerId = libp2p.peerId
46+
47+
await libp2p.stop()
48+
49+
// create a new node from the same datastore
50+
libp2p = await createLibp2p({
51+
datastore,
52+
transports: [
53+
webSockets()
54+
],
55+
connectionEncryption: [
56+
plaintext()
57+
]
58+
})
59+
60+
// the new node should have read the PeerId from the datastore
61+
// instead of creating a new one
62+
expect(libp2p.peerId.toString()).to.equal(peerId.toString())
63+
})
64+
65+
it('should retrieve the PeerId from the datastore with a keychain password', async () => {
66+
const datastore = new MemoryDatastore()
67+
const keychain = {
68+
pass: 'very-long-password-must-be-over-twenty-characters-long',
69+
dek: {
70+
salt: 'CpjNIxMqAZ+aJg+ezLfuzG4a'
71+
}
72+
}
73+
74+
libp2p = await createLibp2p({
75+
datastore,
76+
keychain,
77+
transports: [
78+
webSockets()
79+
],
80+
connectionEncryption: [
81+
plaintext()
82+
]
83+
})
84+
85+
// this PeerId was created by default
86+
const peerId = libp2p.peerId
87+
88+
await libp2p.stop()
89+
90+
// create a new node from the same datastore
91+
libp2p = await createLibp2p({
92+
datastore,
93+
keychain,
94+
transports: [
95+
webSockets()
96+
],
97+
connectionEncryption: [
98+
plaintext()
99+
]
100+
})
101+
102+
// the new node should have read the PeerId from the datastore
103+
// instead of creating a new one
104+
expect(libp2p.peerId.toString()).to.equal(peerId.toString())
105+
})
106+
107+
it('should fail to start if retrieving the PeerId from the datastore fails', async () => {
108+
const datastore = new MemoryDatastore()
109+
const keychain = {
110+
pass: 'very-long-password-must-be-over-twenty-characters-long',
111+
dek: {
112+
salt: 'CpjNIxMqAZ+aJg+ezLfuzG4a'
113+
}
114+
}
115+
116+
libp2p = await createLibp2p({
117+
datastore,
118+
keychain,
119+
transports: [
120+
webSockets()
121+
],
122+
connectionEncryption: [
123+
plaintext()
124+
]
125+
})
126+
await libp2p.stop()
127+
128+
// creating a new node from the same datastore but with the wrong keychain config should fail
129+
await expect(createLibp2p({
130+
datastore,
131+
keychain: {
132+
pass: 'different-very-long-password-must-be-over-twenty-characters-long',
133+
dek: {
134+
salt: 'different-CpjNIxMqAZ+aJg+ezLfuzG4a'
135+
}
136+
},
137+
transports: [
138+
webSockets()
139+
],
140+
connectionEncryption: [
141+
plaintext()
142+
]
143+
})).to.eventually.rejectedWith('Invalid PEM formatted message')
144+
})
145+
})

0 commit comments

Comments
 (0)