Skip to content

Commit 5178617

Browse files
authored
refactor!: move pnet into separate package (#2165)
- Move pnet module to a separate package - Related to #1913
1 parent c8b71d8 commit 5178617

19 files changed

+292
-101
lines changed

doc/migrations/v0.46-v1.0.0.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ A migration guide for refactoring your application code from libp2p `v0.46` to `
77

88
- [AutoNAT](#autonat)
99
- [KeyChain](#keychain)
10+
- [Pnet](#pnet)
1011
- [Metrics](#metrics)
1112

1213
## AutoNAT
@@ -54,6 +55,22 @@ const libp2p = await createLibp2p({
5455
const keychain: Keychain = libp2p.services.keychain
5556
```
5657

58+
## Pnet
59+
60+
The pnet module is now published in its own package.
61+
62+
**Before**
63+
64+
```ts
65+
import { preSharedKey, generateKey } from 'libp2p/pnet'
66+
```
67+
68+
**After**
69+
70+
```ts
71+
import { preSharedKey, generateKey } from '@libp2p/pnet'
72+
```
73+
5774
## Metrics
5875

5976
The following metrics were renamed:

packages/libp2p/package.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@
7272
"types": "./dist/src/ping/index.d.ts",
7373
"import": "./dist/src/ping/index.js"
7474
},
75-
"./pnet": {
76-
"types": "./dist/src/pnet/index.d.ts",
77-
"import": "./dist/src/pnet/index.js"
78-
},
7975
"./upnp-nat": {
8076
"types": "./dist/src/upnp-nat/index.d.ts",
8177
"import": "./dist/src/upnp-nat/index.js"
@@ -157,8 +153,7 @@
157153
"rate-limiter-flexible": "^3.0.0",
158154
"uint8arraylist": "^2.4.3",
159155
"uint8arrays": "^4.0.6",
160-
"wherearewe": "^2.0.1",
161-
"xsalsa20": "^1.1.0"
156+
"wherearewe": "^2.0.1"
162157
},
163158
"devDependencies": {
164159
"@chainsafe/libp2p-gossipsub": "^10.0.0",
@@ -175,7 +170,6 @@
175170
"@libp2p/mplex": "^9.0.9",
176171
"@libp2p/tcp": "^8.0.10",
177172
"@libp2p/websockets": "^7.0.10",
178-
"@types/xsalsa20": "^1.1.0",
179173
"aegir": "^41.0.2",
180174
"execa": "^8.0.1",
181175
"go-libp2p": "^1.1.1",

packages/libp2p/src/pnet/README.md

Lines changed: 0 additions & 68 deletions
This file was deleted.

packages/libp2p/test/connection-manager/direct.node.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,11 @@ import { DefaultConnectionManager } from '../../src/connection-manager/index.js'
3232
import { codes as ErrorCodes } from '../../src/errors.js'
3333
import { plaintext } from '../../src/insecure/index.js'
3434
import { createLibp2pNode, type Libp2pNode } from '../../src/libp2p.js'
35-
import { preSharedKey } from '../../src/pnet/index.js'
3635
import { DefaultTransportManager } from '../../src/transport-manager.js'
37-
import swarmKey from '../fixtures/swarm.key.js'
3836
import type { PeerId } from '@libp2p/interface/peer-id'
3937
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
4038
import type { Multiaddr } from '@multiformats/multiaddr'
4139

42-
const swarmKeyBuffer = uint8ArrayFromString(swarmKey)
4340
const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
4441
const unsupportedAddr = multiaddr('/ip4/127.0.0.1/tcp/9999/ws/p2p/QmckxVrJw1Yo8LqvmDJNUmdAsKtSbiKWmrXJFyKmUraBoN')
4542

@@ -496,9 +493,11 @@ describe('libp2p.dialer (direct, TCP)', () => {
496493
})
497494

498495
it('should use the protectors when provided for connecting', async () => {
499-
const protector: ConnectionProtector = preSharedKey({
500-
psk: swarmKeyBuffer
501-
})()
496+
const protector: ConnectionProtector = {
497+
async protect (connection) {
498+
return connection
499+
}
500+
}
502501

503502
libp2p = await createLibp2pNode({
504503
peerId,
@@ -517,8 +516,6 @@ describe('libp2p.dialer (direct, TCP)', () => {
517516

518517
const protectorProtectSpy = sinon.spy(protector, 'protect')
519518

520-
remoteLibp2p.components.connectionProtector = preSharedKey({ psk: swarmKeyBuffer })()
521-
522519
await libp2p.start()
523520

524521
const connection = await libp2p.dial(remoteAddr)

packages/libp2p/test/fixtures/swarm.key.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/libp2p/test/upgrading/upgrader.spec.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ import { type Components, defaultComponents } from '../../src/components.js'
2626
import { codes } from '../../src/errors.js'
2727
import { createLibp2p } from '../../src/index.js'
2828
import { plaintext } from '../../src/insecure/index.js'
29-
import { preSharedKey } from '../../src/pnet/index.js'
3029
import { DEFAULT_MAX_OUTBOUND_STREAMS } from '../../src/registrar.js'
3130
import { DefaultUpgrader } from '../../src/upgrader.js'
32-
import swarmKey from '../fixtures/swarm.key.js'
3331
import type { Libp2p } from '@libp2p/interface'
3432
import type { Connection, ConnectionProtector, Stream } from '@libp2p/interface/connection'
3533
import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface/connection-encrypter'
@@ -206,9 +204,12 @@ describe('Upgrader', () => {
206204
it('should use a private connection protector when provided', async () => {
207205
const { inbound, outbound } = mockMultiaddrConnPair({ addrs, remotePeer })
208206

209-
const protector = preSharedKey({
210-
psk: uint8ArrayFromString(swarmKey)
211-
})()
207+
const protector: ConnectionProtector = {
208+
async protect (connection) {
209+
return connection
210+
}
211+
}
212+
212213
const protectorProtectSpy = sinon.spy(protector, 'protect')
213214

214215
localComponents.connectionProtector = protector
@@ -615,6 +616,12 @@ describe('libp2p.upgrader', () => {
615616
it('should create an Upgrader', async () => {
616617
const deferred = pDefer<Components>()
617618

619+
const protector: ConnectionProtector = {
620+
async protect (connection) {
621+
return connection
622+
}
623+
}
624+
618625
libp2p = await createLibp2p({
619626
peerId: peers[0],
620627
transports: [
@@ -627,9 +634,7 @@ describe('libp2p.upgrader', () => {
627634
connectionEncryption: [
628635
plaintext()
629636
],
630-
connectionProtector: preSharedKey({
631-
psk: uint8ArrayFromString(swarmKey)
632-
}),
637+
connectionProtector: () => protector,
633638
services: {
634639
test: (components: any) => {
635640
deferred.resolve(components)

packages/libp2p/typedoc.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"./src/identify/index.ts",
77
"./src/insecure/index.ts",
88
"./src/ping/index.ts",
9-
"./src/pnet/index.ts",
109
"./src/upnp-nat/index.ts"
1110
]
1211
}

packages/pnet/LICENSE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This project is dual licensed under MIT and Apache-2.0.
2+
3+
MIT: https://www.opensource.org/licenses/mit
4+
Apache-2.0: https://www.apache.org/licenses/license-2.0

packages/pnet/LICENSE-APACHE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
2+
3+
http://www.apache.org/licenses/LICENSE-2.0
4+
5+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

packages/pnet/LICENSE-MIT

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The MIT License (MIT)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

packages/pnet/README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
> Connection protection management for libp2p leveraging PSK encryption via XSalsa20.
2+
3+
[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
4+
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
5+
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p)
6+
[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p/main.yml?branch=master\&style=flat-square)](https://github.com/libp2p/js-libp2p/actions/workflows/main.yml?query=branch%3Amaster)
7+
8+
> Implementation of Connection protection management via a shared secret
9+
10+
# About
11+
12+
Connection protection management for libp2p leveraging PSK encryption via XSalsa20.
13+
14+
## Example
15+
16+
```typescript
17+
import { createLibp2p } from 'libp2p'
18+
import { preSharedKey, generateKey } from '@libp2p/pnet'
19+
20+
// Create a Uint8Array and write the swarm key to it
21+
const swarmKey = new Uint8Array(95)
22+
generateKey(swarmKey)
23+
24+
const node = await createLibp2p({
25+
// ...other options
26+
connectionProtector: preSharedKey({
27+
psk: swarmKey
28+
})
29+
})
30+
```
31+
32+
## Private Shared Keys
33+
34+
Private Shared Keys are expected to be in the following format:
35+
36+
```
37+
/key/swarm/psk/1.0.0/
38+
/base16/
39+
dffb7e3135399a8b1612b2aaca1c36a3a8ac2cd0cca51ceeb2ced87d308cac6d
40+
```
41+
42+
## PSK Generation
43+
44+
A utility method has been created to generate a key for your private network. You can use one of the methods below to generate your key.
45+
46+
### From a module using libp2p
47+
48+
If you have a module locally that depends on libp2p, you can run the following from that project, assuming the node\_modules are installed.
49+
50+
```console
51+
node -e "import('@libp2p/pnet').then(({ generateKey }) => generateKey(process.stdout))" > swarm.key
52+
```
53+
54+
### Programmatically
55+
56+
```js
57+
import fs from 'fs'
58+
import { generateKey } from '@libp2p/pnet'
59+
60+
const swarmKey = new Uint8Array(95)
61+
generateKey(swarmKey)
62+
63+
fs.writeFileSync('swarm.key', swarmKey)
64+
```
65+
66+
# Install
67+
68+
```console
69+
$ npm i @libp2p/pnet
70+
```
71+
72+
## Browser `<script>` tag
73+
74+
Loading this module through a script tag will make it's exports available as `Libp2pPnet` in the global namespace.
75+
76+
```html
77+
<script src="https://unpkg.com/@libp2p/pnet/dist/index.min.js"></script>
78+
```
79+
80+
# License
81+
82+
Licensed under either of
83+
84+
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
85+
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
86+
87+
# Contribution
88+
89+
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

0 commit comments

Comments
 (0)